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  
21  package org.apache.directory.api.ldap.model.filter;
22  
23  
24  import java.util.List;
25  
26  
27  /**
28   * Node representing an AND connector in a filter operation
29   * 
30   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
31   */
32  public class AndNode extends BranchNode
33  {
34      /**
35       * Creates a AndNode using a logical operator and a list of children.
36       * 
37       * @param childList the child nodes under this branch node.
38       */
39      public AndNode( List<ExprNode> childList )
40      {
41          super( AssertionType.AND, childList );
42      }
43  
44  
45      /**
46       * Creates a AndNode using a logical operator and a list of children.
47       * 
48       * @param childList the child nodes under this branch node.
49       */
50      public AndNode( ExprNode... childList )
51      {
52          super( AssertionType.AND, childList );
53      }
54  
55  
56      /**
57       * Creates an empty AndNode
58       */
59      public AndNode()
60      {
61          super( AssertionType.AND );
62      }
63  
64  
65      /**
66       * Gets the operator for this branch node.
67       * 
68       * @return the operator constant.
69       */
70      public AssertionType getOperator()
71      {
72          return AssertionType.AND;
73      }
74  
75  
76      /**
77       * Tests whether or not this node is a disjunction (a OR'ed branch).
78       * 
79       * @return true if the operation is a OR, false otherwise.
80       */
81      public boolean isDisjunction()
82      {
83          return false;
84      }
85  
86  
87      /**
88       * Tests whether or not this node is a conjunction (a AND'ed branch).
89       * 
90       * @return true if the operation is a AND, false otherwise.
91       */
92      public boolean isConjunction()
93      {
94          return true;
95      }
96  
97  
98      /**
99       * Tests whether or not this node is a negation (a NOT'ed branch).
100      * 
101      * @return true if the operation is a NOT, false otherwise.
102      */
103     public boolean isNegation()
104     {
105         return false;
106     }
107 
108 
109     /**
110      * @see ExprNode#printRefinementToBuffer(StringBuilder)
111      * 
112      * @param buf the buffer to append to.
113      * @return The buffer in which the refinement has been appended
114      */
115     @Override
116     public StringBuilder printRefinementToBuffer( StringBuilder buf )
117     {
118         buf.append( "and: {" );
119         boolean isFirst = true;
120 
121         for ( ExprNode node : children )
122         {
123             if ( isFirst )
124             {
125                 isFirst = false;
126                 buf.append( ' ' );
127             }
128             else
129             {
130                 buf.append( ", " );
131             }
132 
133             node.printRefinementToBuffer( buf );
134         }
135 
136         buf.append( " }" );
137 
138         return buf;
139     }
140 
141 
142     /**
143      * Gets the recursive prefix string represent of the filter from this node
144      * down.
145      * 
146      * @see java.lang.Object#toString()
147      * @return A string representing the AndNode
148      */
149     @Override
150     public String toString()
151     {
152         StringBuilder buf = new StringBuilder();
153         buf.append( "(&" );
154 
155         buf.append( super.toString() );
156 
157         for ( ExprNode child : getChildren() )
158         {
159             buf.append( child );
160         }
161 
162         buf.append( ')' );
163 
164         return buf.toString();
165     }
166 
167 
168     /**
169      * @see Object#hashCode()
170      * @return the instance's hash code 
171      */
172     @Override
173     public int hashCode()
174     {
175         int hash = 37;
176         hash = hash * 17 + AssertionType.AND.hashCode();
177         hash = hash * 17 + ( annotations == null ? 0 : annotations.hashCode() );
178         return hash;
179     }
180 
181 
182     /**
183      * @see java.lang.Object#equals(java.lang.Object)
184      */
185     @Override
186     public boolean equals( Object other )
187     {
188         if ( this == other )
189         {
190             return true;
191         }
192 
193         if ( !( other instanceof AndNode ) )
194         {
195             return false;
196         }
197 
198         AndNode otherExprNode = ( AndNode ) other;
199 
200         List<ExprNode> otherChildren = otherExprNode.getChildren();
201 
202         if ( otherChildren == children )
203         {
204             return true;
205         }
206 
207         if ( children.size() != otherChildren.size() )
208         {
209             return false;
210         }
211 
212         for ( int i = 0; i < children.size(); i++ )
213         {
214             ExprNode child = children.get( i );
215             ExprNode otherChild = otherChildren.get( i );
216 
217             if ( !child.equals( otherChild ) )
218             {
219                 return false;
220             }
221         }
222 
223         return true;
224     }
225 }