View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    https://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  package org.apache.directory.api.ldap.model.filter;
21  
22  
23  import org.apache.directory.api.ldap.model.message.AliasDerefMode;
24  import org.apache.directory.api.ldap.model.message.SearchScope;
25  import org.apache.directory.api.ldap.model.name.Dn;
26  
27  
28  /**
29   * Node used not to represent a published assertion but an assertion on the
30   * scope of the search.
31   * 
32   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
33   */
34  public class ScopeNode extends AbstractExprNode
35  {
36      /** the scope of this node */
37      private final SearchScope scope;
38  
39      /** the search base */
40      private final Dn baseDn;
41  
42      /** the search ID */
43      private final String baseId;
44  
45      /** the alias dereferencing mode */
46      private final AliasDerefMode aliasDerefAliases;
47  
48  
49      /**
50       * Creates a new ScopeNode object.
51       * 
52       * @param aliasDerefAliases the alias dereferencing mode
53       * @param baseDn the search base
54       * @param baseId the search ID
55       * @param scope the search scope
56       */
57      public ScopeNode( AliasDerefMode aliasDerefAliases, Dn baseDn, String baseId, SearchScope scope )
58      {
59          super( AssertionType.SCOPE );
60          this.scope = scope;
61          this.baseDn = baseDn;
62          this.aliasDerefAliases = aliasDerefAliases;
63          this.baseId = baseId;
64      }
65  
66  
67      /**
68       * Always returns true since a scope node has no children.
69       * 
70       * @see ExprNode#isLeaf()
71       * @return <code>true</code>
72       */
73      @Override
74      public boolean isLeaf()
75      {
76          return true;
77      }
78  
79  
80      /**
81       * Gets the search scope.
82       * 
83       * @return the search scope 
84       */
85      public SearchScope getScope()
86      {
87          return scope;
88      }
89  
90  
91      /**
92       * Gets the base dn.
93       * 
94       * @return the base dn
95       */
96      public Dn getBaseDn()
97      {
98          return baseDn;
99      }
100 
101 
102     /**
103      * Gets the base ID.
104      * 
105      * @return the base ID
106      */
107     public String getBaseId()
108     {
109         return baseId;
110     }
111 
112 
113     /**
114      * Gets the alias dereferencing mode type safe enumeration.
115      * 
116      * @return the alias dereferencing enumeration constant.
117      */
118     public AliasDerefMode getDerefAliases()
119     {
120         return aliasDerefAliases;
121     }
122 
123 
124     /**
125      * @see ExprNode#accept(
126      *FilterVisitor)
127      * 
128      * @param visitor the filter expression tree structure visitor
129      * @return The modified element
130      */
131     @Override
132     public Object accept( FilterVisitor visitor )
133     {
134         if ( visitor.canVisit( this ) )
135         {
136             return visitor.visit( this );
137         }
138         else
139         {
140             return null;
141         }
142     }
143 
144 
145     /**
146      * Tells if this Node is Schema aware.
147      * 
148      * @return true if the Node is SchemaAware
149      */
150     @Override
151     public boolean isSchemaAware()
152     {
153         return true;
154     }
155 
156 
157     /**
158      * {@inheritDoc}
159      */
160     @Override
161     public boolean equals( Object obj )
162     {
163         if ( obj == this )
164         {
165             return true;
166         }
167 
168         if ( !( obj instanceof ScopeNode ) )
169         {
170             return false;
171         }
172         ScopeNode that = ( ScopeNode ) obj;
173         if ( aliasDerefAliases == null )
174         {
175             if ( that.aliasDerefAliases != null )
176             {
177                 return false;
178             }
179         }
180         else
181         {
182             if ( !aliasDerefAliases.equals( that.aliasDerefAliases ) )
183             {
184                 return false;
185             }
186         }
187         if ( baseDn == null )
188         {
189             if ( that.baseDn != null )
190             {
191                 return false;
192             }
193         }
194         else
195         {
196             if ( !baseDn.equals( that.baseDn ) )
197             {
198                 return false;
199             }
200         }
201         if ( scope.getScope() != that.scope.getScope() )
202         {
203             return false;
204         }
205         return super.equals( obj );
206     }
207 
208 
209     /**
210      * @see Object#hashCode()
211      * @return the instance's hash code 
212      */
213     @Override
214     public int hashCode()
215     {
216         int h = 37;
217 
218         h = h * 17 + super.hashCode();
219         h = h * 17 + ( aliasDerefAliases != null ? aliasDerefAliases.hashCode() : 0 );
220         h = h * 17 + ( baseDn != null ? baseDn.hashCode() : 0 );
221         h = h * 17 + scope.getScope();
222 
223         return h;
224     }
225 
226 
227     /**
228      * @see Object#toString()
229      * @return A string representing the AndNode
230      */
231     @Override
232     public String toString()
233     {
234         StringBuilder buf = new StringBuilder();
235 
236         buf.append( "(#{" );
237 
238         switch ( scope )
239         {
240             case OBJECT:
241                 buf.append( "OBJECT_SCOPE" );
242 
243                 break;
244 
245             case ONELEVEL:
246                 buf.append( "ONE_LEVEL_SCOPE" );
247 
248                 break;
249 
250             case SUBTREE:
251                 buf.append( "SUBTREE_SCOPE (Estimated)" );
252 
253                 break;
254 
255             default:
256                 buf.append( "UNKNOWN" );
257                 break;
258         }
259 
260         buf.append( ", '" );
261         buf.append( baseDn );
262         buf.append( "', " );
263         buf.append( aliasDerefAliases );
264         buf.append( "}" );
265         buf.append( super.toString() );
266         buf.append( ')' );
267 
268         return buf.toString();
269     }
270 }