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.api.ldap.model.filter;
021
022
023import org.apache.directory.api.ldap.model.message.AliasDerefMode;
024import org.apache.directory.api.ldap.model.message.SearchScope;
025import org.apache.directory.api.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 search ID */
043    private final String baseId;
044
045    /** the alias dereferencing mode */
046    private final AliasDerefMode aliasDerefAliases;
047
048
049    /**
050     * Creates a new ScopeNode object.
051     * 
052     * @param aliasDerefAliases the alias dereferencing mode
053     * @param baseDn the search base
054     * @param scope the search scope
055     */
056    public ScopeNode( AliasDerefMode aliasDerefAliases, Dn baseDn, String baseId, SearchScope scope )
057    {
058        super( AssertionType.SCOPE );
059        this.scope = scope;
060        this.baseDn = baseDn;
061        this.aliasDerefAliases = aliasDerefAliases;
062        this.baseId = baseId;
063        isSchemaAware = true;
064    }
065
066
067    /**
068     * Always returns true since a scope node has no children.
069     * 
070     * @see ExprNode#isLeaf()
071     * @return <code>true</code>
072     */
073    public boolean isLeaf()
074    {
075        return true;
076    }
077
078
079    /**
080     * Gets the search scope.
081     * 
082     * @return the search scope 
083     */
084    public SearchScope getScope()
085    {
086        return scope;
087    }
088
089
090    /**
091     * Gets the base dn.
092     * 
093     * @return the base dn
094     */
095    public Dn getBaseDn()
096    {
097        return baseDn;
098    }
099
100
101    /**
102     * Gets the base ID.
103     * 
104     * @return the base ID
105     */
106    public String getBaseId()
107    {
108        return baseId;
109    }
110
111
112    /**
113     * Gets the alias dereferencing mode type safe enumeration.
114     * 
115     * @return the alias dereferencing enumeration constant.
116     */
117    public AliasDerefMode getDerefAliases()
118    {
119        return aliasDerefAliases;
120    }
121
122
123    /**
124     * @see ExprNode#accept(
125     *FilterVisitor)
126     * 
127     * @param visitor the filter expression tree structure visitor
128     * @return The modified element
129     */
130    public Object accept( FilterVisitor visitor )
131    {
132        if ( visitor.canVisit( this ) )
133        {
134            return visitor.visit( this );
135        }
136        else
137        {
138            return null;
139        }
140    }
141
142
143    /**
144     * {@inheritDoc}
145     */
146    @Override
147    public boolean equals( Object obj )
148    {
149        if ( obj == this )
150        {
151            return true;
152        }
153
154        if ( ( obj == null ) || !( obj instanceof ScopeNode ) )
155        {
156            return false;
157        }
158        ScopeNode that = ( ScopeNode ) obj;
159        if ( aliasDerefAliases == null )
160        {
161            if ( that.aliasDerefAliases != null )
162            {
163                return false;
164            }
165        }
166        else
167        {
168            if ( !aliasDerefAliases.equals( that.aliasDerefAliases ) )
169            {
170                return false;
171            }
172        }
173        if ( baseDn == null )
174        {
175            if ( that.baseDn != null )
176            {
177                return false;
178            }
179        }
180        else
181        {
182            if ( !baseDn.equals( that.baseDn ) )
183            {
184                return false;
185            }
186        }
187        if ( scope.getScope() != that.scope.getScope() )
188        {
189            return false;
190        }
191        return super.equals( obj );
192    }
193
194
195    /**
196     * @see Object#hashCode()
197     * @return the instance's hash code 
198     */
199    @Override
200    public int hashCode()
201    {
202        int h = 37;
203
204        h = h * 17 + super.hashCode();
205        h = h * 17 + ( aliasDerefAliases != null ? aliasDerefAliases.hashCode() : 0 );
206        h = h * 17 + ( baseDn != null ? baseDn.hashCode() : 0 );
207        h = h * 17 + scope.getScope();
208
209        return h;
210    }
211
212
213    /**
214     * @see Object#toString()
215     * @return A string representing the AndNode
216     */
217    public String toString()
218    {
219        StringBuilder buf = new StringBuilder();
220
221        buf.append( "(#{" );
222
223        switch ( scope )
224        {
225            case OBJECT:
226                buf.append( "OBJECT_SCOPE" );
227
228                break;
229
230            case ONELEVEL:
231                buf.append( "ONE_LEVEL_SCOPE" );
232
233                break;
234
235            case SUBTREE:
236                buf.append( "SUBTREE_SCOPE (Estimated)" );
237
238                break;
239
240            default:
241                buf.append( "UNKNOWN" );
242                break;
243        }
244
245        buf.append( ", '" );
246        buf.append( baseDn );
247        buf.append( "', " );
248        buf.append( aliasDerefAliases );
249        buf.append( "}" );
250        buf.append( super.toString() );
251        buf.append( ')' );
252
253        return buf.toString();
254    }
255}