001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *  
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *  
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License. 
018 *  
019 */
020package org.apache.directory.shared.ldap.model.filter;
021
022
023import org.apache.directory.shared.ldap.model.message.AliasDerefMode;
024import org.apache.directory.shared.ldap.model.message.SearchScope;
025import org.apache.directory.shared.ldap.model.name.Dn;
026
027
028/**
029 * Node used not to represent a published assertion but an assertion on the
030 * scope of the search.
031 * 
032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033 */
034public class ScopeNode extends AbstractExprNode
035{
036    /** the scope of this node */
037    private final SearchScope scope;
038
039    /** the search base */
040    private final Dn baseDn;
041
042    /** the alias dereferencing mode */
043    private final AliasDerefMode aliasDerefAliases;
044
045
046    /**
047     * Creates a new ScopeNode object.
048     * 
049     * @param aliasDerefAliases the alias dereferencing mode
050     * @param baseDn the search base
051     * @param scope the search scope
052     */
053    public ScopeNode( AliasDerefMode aliasDerefAliases, Dn baseDn, SearchScope scope )
054    {
055        super( AssertionType.SCOPE );
056        this.scope = scope;
057        this.baseDn = baseDn;
058        this.aliasDerefAliases = aliasDerefAliases;
059        isSchemaAware = true;
060    }
061
062    /**
063     * Always returns true since a scope node has no children.
064     * 
065     * @see ExprNode#isLeaf()
066     * @return <code>true</code>
067     */
068    public boolean isLeaf()
069    {
070        return true;
071    }
072
073
074    /**
075     * Gets the search scope.
076     * 
077     * @return the search scope 
078     */
079    public SearchScope getScope()
080    {
081        return scope;
082    }
083
084
085    /**
086     * Gets the base dn.
087     * 
088     * @return the base dn
089     */
090    public Dn getBaseDn()
091    {
092        return baseDn;
093    }
094
095
096    /**
097     * Gets the alias dereferencing mode type safe enumeration.
098     * 
099     * @return the alias dereferencing enumeration constant.
100     */
101    public AliasDerefMode getDerefAliases()
102    {
103        return aliasDerefAliases;
104    }
105
106
107    /**
108     * @see ExprNode#accept(
109     *FilterVisitor)
110     * 
111     * @param visitor the filter expression tree structure visitor
112     * @return The modified element
113     */
114    public Object accept( FilterVisitor visitor )
115    {
116        if ( visitor.canVisit( this ) )
117        {
118            return visitor.visit( this );
119        }
120        else
121        {
122            return null;
123        }
124    }
125
126
127    /**
128     * {@inheritDoc}
129     */
130    @Override
131    public boolean equals( Object obj )
132    {
133        if ( obj == this )
134        {
135            return true;
136        }
137
138        if ( ( obj == null ) || !( obj instanceof ScopeNode ) )
139        {
140            return false;
141        }
142        ScopeNode that = ( ScopeNode ) obj;
143        if ( aliasDerefAliases == null )
144        {
145            if ( that.aliasDerefAliases != null )
146            {
147                return false;
148            }
149        }
150        else
151        {
152            if ( !aliasDerefAliases.equals( that.aliasDerefAliases ) )
153            {
154                return false;
155            }
156        }
157        if ( baseDn == null )
158        {
159            if ( that.baseDn != null )
160            {
161                return false;
162            }
163        }
164        else
165        {
166            if ( !baseDn.equals( that.baseDn ) )
167            {
168                return false;
169            }
170        }
171        if ( scope.getScope() != that.scope.getScope() )
172        {
173            return false;
174        }
175        return super.equals( obj );
176    }
177
178
179    /**
180     * @see Object#hashCode()
181     * @return the instance's hash code 
182     */
183    @Override
184    public int hashCode()
185    {
186        int h = 37;
187        
188        h = h*17 + super.hashCode();
189        h = h*17 + ( aliasDerefAliases != null ? aliasDerefAliases.hashCode() : 0 );
190        h = h*17 + ( baseDn != null ? baseDn.hashCode() : 0 );
191        h = h*17 + scope.getScope();
192        
193        return h;
194    }
195
196
197    /**
198     * @see Object#toString()
199     * @return A string representing the AndNode
200     */
201    public String toString()
202    {
203        StringBuilder buf = new StringBuilder();
204        
205        buf.append( "(#{" );
206
207        switch ( scope )
208        {
209            case OBJECT:
210                buf.append( "OBJECT_SCOPE" );
211
212                break;
213
214            case ONELEVEL:
215                buf.append( "ONE_LEVEL_SCOPE" );
216
217                break;
218
219            case SUBTREE:
220                buf.append( "SUBTREE_SCOPE (Estimated)" );
221
222                break;
223
224            default:
225                buf.append( "UNKNOWN" );
226                break;
227        }
228        
229        buf.append( ", '" );
230        buf.append( baseDn );
231        buf.append( "', " );
232        buf.append( aliasDerefAliases );
233        buf.append( "}" );
234        buf.append( super.toString() );
235        buf.append( ')' );
236        
237        return buf.toString();
238    }
239}