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 scope the search scope
55       */
56      public ScopeNode( AliasDerefMode aliasDerefAliases, Dn baseDn, String baseId, SearchScope scope )
57      {
58          super( AssertionType.SCOPE );
59          this.scope = scope;
60          this.baseDn = baseDn;
61          this.aliasDerefAliases = aliasDerefAliases;
62          this.baseId = baseId;
63          isSchemaAware = true;
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      public boolean isLeaf()
74      {
75          return true;
76      }
77  
78  
79      /**
80       * Gets the search scope.
81       * 
82       * @return the search scope 
83       */
84      public SearchScope getScope()
85      {
86          return scope;
87      }
88  
89  
90      /**
91       * Gets the base dn.
92       * 
93       * @return the base dn
94       */
95      public Dn getBaseDn()
96      {
97          return baseDn;
98      }
99  
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 }