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   *    http://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          isSchemaAware = true;
65      }
66  
67  
68      /**
69       * Always returns true since a scope node has no children.
70       * 
71       * @see ExprNode#isLeaf()
72       * @return <code>true</code>
73       */
74      @Override
75      public boolean isLeaf()
76      {
77          return true;
78      }
79  
80  
81      /**
82       * Gets the search scope.
83       * 
84       * @return the search scope 
85       */
86      public SearchScope getScope()
87      {
88          return scope;
89      }
90  
91  
92      /**
93       * Gets the base dn.
94       * 
95       * @return the base dn
96       */
97      public Dn getBaseDn()
98      {
99          return baseDn;
100     }
101 
102 
103     /**
104      * Gets the base ID.
105      * 
106      * @return the base ID
107      */
108     public String getBaseId()
109     {
110         return baseId;
111     }
112 
113 
114     /**
115      * Gets the alias dereferencing mode type safe enumeration.
116      * 
117      * @return the alias dereferencing enumeration constant.
118      */
119     public AliasDerefMode getDerefAliases()
120     {
121         return aliasDerefAliases;
122     }
123 
124 
125     /**
126      * @see ExprNode#accept(
127      *FilterVisitor)
128      * 
129      * @param visitor the filter expression tree structure visitor
130      * @return The modified element
131      */
132     @Override
133     public Object accept( FilterVisitor visitor )
134     {
135         if ( visitor.canVisit( this ) )
136         {
137             return visitor.visit( this );
138         }
139         else
140         {
141             return null;
142         }
143     }
144 
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public boolean equals( Object obj )
151     {
152         if ( obj == this )
153         {
154             return true;
155         }
156 
157         if ( ( obj == null ) || !( obj instanceof ScopeNode ) )
158         {
159             return false;
160         }
161         ScopeNode that = ( ScopeNode ) obj;
162         if ( aliasDerefAliases == null )
163         {
164             if ( that.aliasDerefAliases != null )
165             {
166                 return false;
167             }
168         }
169         else
170         {
171             if ( !aliasDerefAliases.equals( that.aliasDerefAliases ) )
172             {
173                 return false;
174             }
175         }
176         if ( baseDn == null )
177         {
178             if ( that.baseDn != null )
179             {
180                 return false;
181             }
182         }
183         else
184         {
185             if ( !baseDn.equals( that.baseDn ) )
186             {
187                 return false;
188             }
189         }
190         if ( scope.getScope() != that.scope.getScope() )
191         {
192             return false;
193         }
194         return super.equals( obj );
195     }
196 
197 
198     /**
199      * @see Object#hashCode()
200      * @return the instance's hash code 
201      */
202     @Override
203     public int hashCode()
204     {
205         int h = 37;
206 
207         h = h * 17 + super.hashCode();
208         h = h * 17 + ( aliasDerefAliases != null ? aliasDerefAliases.hashCode() : 0 );
209         h = h * 17 + ( baseDn != null ? baseDn.hashCode() : 0 );
210         h = h * 17 + scope.getScope();
211 
212         return h;
213     }
214 
215 
216     /**
217      * @see Object#toString()
218      * @return A string representing the AndNode
219      */
220     @Override
221     public String toString()
222     {
223         StringBuilder buf = new StringBuilder();
224 
225         buf.append( "(#{" );
226 
227         switch ( scope )
228         {
229             case OBJECT:
230                 buf.append( "OBJECT_SCOPE" );
231 
232                 break;
233 
234             case ONELEVEL:
235                 buf.append( "ONE_LEVEL_SCOPE" );
236 
237                 break;
238 
239             case SUBTREE:
240                 buf.append( "SUBTREE_SCOPE (Estimated)" );
241 
242                 break;
243 
244             default:
245                 buf.append( "UNKNOWN" );
246                 break;
247         }
248 
249         buf.append( ", '" );
250         buf.append( baseDn );
251         buf.append( "', " );
252         buf.append( aliasDerefAliases );
253         buf.append( "}" );
254         buf.append( super.toString() );
255         buf.append( ')' );
256 
257         return buf.toString();
258     }
259 }