001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020
021 package org.apache.directory.shared.ldap.filter;
022
023
024 import java.util.List;
025
026 /**
027 * Node representing an AND connector in a filter operation
028 *
029 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
030 * @version $Rev: 517453 $
031 */
032 public class AndNode extends BranchNode
033 {
034 /**
035 * Creates a AndNode using a logical operator and a list of children.
036 *
037 * @param childList the child nodes under this branch node.
038 */
039 public AndNode( List<ExprNode> childList )
040 {
041 super( AssertionType.AND, childList );
042 }
043
044 /**
045 * Creates a AndNode using a logical operator and a list of children.
046 *
047 * @param childList the child nodes under this branch node.
048 */
049 public AndNode( ExprNode... childList )
050 {
051 super( AssertionType.AND, childList );
052 }
053
054
055 /**
056 * Clone the AndNode
057 */
058 @Override public ExprNode clone()
059 {
060 return super.clone();
061 }
062
063
064 /**
065 * Creates an empty AndNode
066 */
067 public AndNode()
068 {
069 super( AssertionType.AND );
070 }
071
072
073 /**
074 * Gets the operator for this branch node.
075 *
076 * @return the operator constant.
077 */
078 public AssertionType getOperator()
079 {
080 return AssertionType.AND;
081 }
082
083
084 /**
085 * Tests whether or not this node is a disjunction (a OR'ed branch).
086 *
087 * @return true if the operation is a OR, false otherwise.
088 */
089 public boolean isDisjunction()
090 {
091 return false;
092 }
093
094
095 /**
096 * Tests whether or not this node is a conjunction (a AND'ed branch).
097 *
098 * @return true if the operation is a AND, false otherwise.
099 */
100 public boolean isConjunction()
101 {
102 return true;
103 }
104
105
106 /**
107 * Tests whether or not this node is a negation (a NOT'ed branch).
108 *
109 * @return true if the operation is a NOT, false otherwise.
110 */
111 public boolean isNegation()
112 {
113 return false;
114 }
115
116
117 /**
118 * @see ExprNode#printRefinementToBuffer(StringBuffer)
119 *
120 * @param buf the buffer to append to.
121 * @return The buffer in which the refinement has been appended
122 * @throws UnsupportedOperationException if this node isn't a part of a refinement.
123 */
124 public StringBuilder printRefinementToBuffer( StringBuilder buf )
125 {
126 buf.append( "and: {" );
127 boolean isFirst = true;
128
129 for ( ExprNode node:children )
130 {
131 if ( isFirst )
132 {
133 isFirst = false;
134 }
135 else
136 {
137 buf.append( ", " );
138 }
139
140 node.printRefinementToBuffer( buf );
141 }
142
143 buf.append( '}' );
144
145 return buf;
146 }
147
148 /**
149 * Gets the recursive prefix string represent of the filter from this node
150 * down.
151 *
152 * @see java.lang.Object#toString()
153 * @return A string representing the AndNode
154 */
155 public String toString()
156 {
157 StringBuffer buf = new StringBuffer();
158 buf.append( "(&" );
159
160 buf.append( super.toString() );
161
162 for ( ExprNode child:getChildren() )
163 {
164 buf.append( child );
165 }
166
167 buf.append( ')' );
168
169 return buf.toString();
170 }
171
172
173 /**
174 * @see Object#hashCode()
175 * @return the instance's hash code
176 */
177 public int hashCode()
178 {
179 int hash = 37;
180 hash = hash*17 + AssertionType.AND.hashCode();
181 hash = hash*17 + ( annotations == null ? 0 : annotations.hashCode() );
182 return hash;
183 }
184
185
186 /*
187 * (non-Javadoc)
188 *
189 * @see java.lang.Object#equals(java.lang.Object)
190 */
191 public boolean equals( Object other )
192 {
193 if ( this == other )
194 {
195 return true;
196 }
197
198 if ( !( other instanceof AndNode ) )
199 {
200 return false;
201 }
202
203 AndNode otherExprNode = ( AndNode ) other;
204
205 List<ExprNode> otherChildren = otherExprNode.getChildren();
206
207 if ( otherChildren == children )
208 {
209 return true;
210 }
211
212 if ( children.size() != otherChildren.size() )
213 {
214 return false;
215 }
216
217 for ( int i = 0; i < children.size(); i++ )
218 {
219 ExprNode child = children.get( i );
220 ExprNode otherChild = otherChildren.get( i );
221
222 if ( !child.equals( otherChild ) )
223 {
224 return false;
225 }
226 }
227
228 return true;
229 }
230 }