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.i18n.I18n;
24  
25  
26  /**
27   * Node used for the application of arbitrary predicates on return candidates.
28   * Applies dynamic and programatic criteria for the selection of candidates for
29   * return. Nodes of this type may be introduced into the filter expression to
30   * provided the opportunity to constrain the search further without altering the
31   * search algorithm.
32   * 
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
34   */
35  public abstract class AssertionNode extends AbstractExprNode
36  {
37      /** The assertion or predicate to apply */
38      private final Assertion assertion;
39  
40      /** Description of assertion for polish printouts */
41      private final String desc;
42  
43  
44      // ------------------------------------------------------------------------
45      // C O N S T R U C T O R S
46      // ------------------------------------------------------------------------
47  
48      /**
49       * Creates an AssertionNode using an arbitrary candidate assertion.
50       * 
51       * @param assertion the arbitrary selection logic.
52       */
53      public AssertionNode( Assertion assertion )
54      {
55          this( assertion, "ASSERTION" );
56  
57          isSchemaAware = true;
58      }
59  
60  
61      /**
62       * Creates an AssertionNode using an arbitrary candidate assertion with a
63       * descriptions used for filter AST walker dumps.
64       * 
65       * @param assertion the arbitrary selection logic.
66       * @param desc the printout representation for filter prints.
67       */
68      public AssertionNode( Assertion assertion, String desc )
69      {
70          super( AssertionType.ASSERTION );
71          this.desc = desc;
72          this.assertion = assertion;
73  
74          /*
75           * We never want this node to ever make it to the point of becoming a
76           * candidate for use in an enumeration so we set the scan count to the
77           * maximum value.
78           */
79          set( "count", Long.MAX_VALUE );
80      }
81  
82  
83      /**
84       * Gets the Assertion used by this assertion node.
85       * 
86       * @return the assertion used by this node
87       */
88      public Assertion getAssertion()
89      {
90          return assertion;
91      }
92  
93  
94      // ------------------------------------------------------------------------
95      // A B S T R A C T M E T H O D I M P L E M E N T A T I O N S
96      // ------------------------------------------------------------------------
97  
98      /**
99       * Always returns true since an AssertionNode has no children.
100      * 
101      * @see ExprNode#isLeaf()
102      * @return true if the node is a leaf,false otherwise
103      */
104     @Override
105     public boolean isLeaf()
106     {
107         return true;
108     }
109 
110 
111     /**
112      * @see ExprNode#printRefinementToBuffer(StringBuilder) 
113      */
114     @Override
115     public StringBuilder printRefinementToBuffer( StringBuilder buf )
116     {
117         throw new UnsupportedOperationException( I18n.err( I18n.ERR_04145 ) );
118     }
119 
120 
121     /**
122      * {@inheritDoc}
123      */
124     @Override
125     public boolean equals( Object obj )
126     {
127         if ( obj == this )
128         {
129             return true;
130         }
131 
132         if ( ( obj == null ) || !( obj instanceof AssertionNode ) )
133         {
134             return false;
135         }
136         AssertionNode that = ( AssertionNode ) obj;
137         if ( assertion == null )
138         {
139             if ( that.assertion != null )
140             {
141                 return false;
142             }
143         }
144         else
145         {
146             if ( !assertion.equals( that.assertion ) )
147             {
148                 return false;
149             }
150         }
151         if ( desc == null )
152         {
153             if ( that.desc != null )
154             {
155                 return false;
156             }
157         }
158         else
159         {
160             if ( !desc.equals( that.desc ) )
161             {
162                 return false;
163             }
164         }
165         return super.equals( obj );
166     }
167 
168 
169     /**
170      * @see Object#hashCode()
171      * @return the instance's hash code 
172      */
173     @Override
174     public int hashCode()
175     {
176         int h = 37;
177 
178         h = h * 17 + super.hashCode();
179         h = h * 17 + ( assertion != null ? assertion.hashCode() : 0 );
180         h = h * 17 + ( desc != null ? desc.hashCode() : 0 );
181 
182         return h;
183     }
184 
185 
186     /**
187      * @see ExprNode#accept(
188      *FilterVisitor)
189      */
190     @Override
191     public Object accept( FilterVisitor visitor )
192     {
193         return visitor.visit( this );
194     }
195 
196 
197     /**
198      * @see Object#toString
199      * @return A string representing the AndNode
200      */
201     @Override
202     public String toString()
203     {
204         StringBuilder buf = new StringBuilder();
205 
206         buf.append( "(@" );
207         buf.append( desc );
208         buf.append( super.toString() );
209         buf.append( ')' );
210 
211         return buf.toString();
212     }
213 }