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.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  
58  
59      /**
60       * Creates an AssertionNode using an arbitrary candidate assertion with a
61       * descriptions used for filter AST walker dumps.
62       * 
63       * @param assertion the arbitrary selection logic.
64       * @param desc the printout representation for filter prints.
65       */
66      public AssertionNode( Assertion assertion, String desc )
67      {
68          super( AssertionType.ASSERTION );
69          this.desc = desc;
70          this.assertion = assertion;
71  
72          /*
73           * We never want this node to ever make it to the point of becoming a
74           * candidate for use in an enumeration so we set the scan count to the
75           * maximum value.
76           */
77          set( "count", Long.MAX_VALUE );
78      }
79  
80  
81      /**
82       * Gets the Assertion used by this assertion node.
83       * 
84       * @return the assertion used by this node
85       */
86      public Assertion getAssertion()
87      {
88          return assertion;
89      }
90  
91  
92      // ------------------------------------------------------------------------
93      // 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
94      // ------------------------------------------------------------------------
95  
96      /**
97       * Always returns true since an AssertionNode has no children.
98       * 
99       * @see ExprNode#isLeaf()
100      * @return true if the node is a leaf,false otherwise
101      */
102     @Override
103     public boolean isLeaf()
104     {
105         return true;
106     }
107 
108 
109     /**
110      * @see ExprNode#printRefinementToBuffer(StringBuilder) 
111      */
112     @Override
113     public StringBuilder printRefinementToBuffer( StringBuilder buf )
114     {
115         throw new UnsupportedOperationException( I18n.err( I18n.ERR_13304_ASSERTIONNODE_IN_REFINEMENT ) );
116     }
117 
118 
119     /**
120      * Tells if this Node is Schema aware.
121      * 
122      * @return true if the Node is SchemaAware
123      */
124     @Override
125     public boolean isSchemaAware()
126     {
127         return true;
128     }
129 
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public boolean equals( Object obj )
136     {
137         if ( obj == this )
138         {
139             return true;
140         }
141 
142         if ( !( obj instanceof AssertionNode ) )
143         {
144             return false;
145         }
146         AssertionNode that = ( AssertionNode ) obj;
147         if ( assertion == null )
148         {
149             if ( that.assertion != null )
150             {
151                 return false;
152             }
153         }
154         else
155         {
156             if ( !assertion.equals( that.assertion ) )
157             {
158                 return false;
159             }
160         }
161         if ( desc == null )
162         {
163             if ( that.desc != null )
164             {
165                 return false;
166             }
167         }
168         else
169         {
170             if ( !desc.equals( that.desc ) )
171             {
172                 return false;
173             }
174         }
175         return super.equals( obj );
176     }
177 
178 
179     /**
180      * @see Object#hashCode()
181      * @return the instance's hash code 
182      */
183     @Override
184     public int hashCode()
185     {
186         int h = 37;
187 
188         h = h * 17 + super.hashCode();
189         h = h * 17 + ( assertion != null ? assertion.hashCode() : 0 );
190         h = h * 17 + ( desc != null ? desc.hashCode() : 0 );
191 
192         return h;
193     }
194 
195 
196     /**
197      * @see ExprNode#accept(
198      *FilterVisitor)
199      */
200     @Override
201     public Object accept( FilterVisitor visitor )
202     {
203         return visitor.visit( this );
204     }
205 
206 
207     /**
208      * @see Object#toString
209      * @return A string representing the AndNode
210      */
211     @Override
212     public String toString()
213     {
214         StringBuilder buf = new StringBuilder();
215 
216         buf.append( "(@" );
217         buf.append( desc );
218         buf.append( super.toString() );
219         buf.append( ')' );
220 
221         return buf.toString();
222     }
223 }