1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.api.ldap.model.filter;
21
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.commons.collections.CollectionUtils;
27
28
29
30
31
32
33
34
35 public class BranchNode extends AbstractExprNode
36 {
37
38 protected List<ExprNode> children = null;
39
40
41
42
43
44
45
46 protected BranchNode( AssertionType assertionType, List<ExprNode> childList )
47 {
48 super( assertionType );
49
50 if ( null == childList )
51 {
52 this.children = new ArrayList<>( 2 );
53 }
54 else
55 {
56 this.children = childList;
57 }
58
59 isSchemaAware = true;
60 }
61
62
63
64
65
66
67
68
69 protected BranchNode( AssertionType assertionType, ExprNode... childList )
70 {
71 super( assertionType );
72
73 if ( null == children )
74 {
75 this.children = new ArrayList<>( childList.length );
76 }
77
78 CollectionUtils.addAll( children, childList );
79 }
80
81
82
83
84
85
86
87 protected BranchNode( AssertionType assertionType )
88 {
89 super( assertionType );
90
91 this.children = new ArrayList<>( 2 );
92 isSchemaAware = true;
93 }
94
95
96
97
98
99
100 @Override
101 public final boolean isLeaf()
102 {
103 return false;
104 }
105
106
107
108
109
110
111
112 @Override
113 public ExprNode clone()
114 {
115 ExprNode clone = super.clone();
116
117
118 if ( children != null )
119 {
120 ( ( BranchNode ) clone ).children = new ArrayList<>();
121
122 for ( ExprNode child : children )
123 {
124 ( ( BranchNode ) clone ).children.add( ( ExprNode ) child.clone() );
125 }
126 }
127
128 return clone;
129 }
130
131
132
133
134
135
136
137 public void addNode( ExprNode node )
138 {
139 children.add( node );
140 }
141
142
143
144
145
146
147
148 public void addNodeToHead( ExprNode node )
149 {
150 children.add( 0, node );
151 }
152
153
154
155
156
157
158
159
160
161
162 public List<ExprNode> getChildren()
163 {
164 return children;
165 }
166
167
168
169
170
171
172
173 public void setChildren( List<ExprNode> list )
174 {
175 children = list;
176 }
177
178
179
180
181
182
183
184
185
186 public ExprNode getFirstChild()
187 {
188 if ( !children.isEmpty() )
189 {
190 return children.get( 0 );
191 }
192
193 return null;
194 }
195
196
197
198
199
200
201
202
203 @Override
204 public final Object accept( FilterVisitor visitor )
205 {
206 if ( visitor.isPrefix() )
207 {
208 List<ExprNode> childrenList = visitor.getOrder( this, this.children );
209 ExprNode result = null;
210
211 if ( visitor.canVisit( this ) )
212 {
213 result = ( ExprNode ) visitor.visit( this );
214 }
215
216 for ( ExprNode node : childrenList )
217 {
218 node.accept( visitor );
219 }
220
221 return result;
222 }
223 else
224 {
225 if ( visitor.canVisit( this ) )
226 {
227 return visitor.visit( this );
228 }
229 else
230 {
231 return null;
232 }
233 }
234 }
235
236
237
238
239
240
241 @Override
242 public int hashCode()
243 {
244 int h = 37;
245
246 h = h * 17 + super.hashCode();
247
248 if ( children != null )
249 {
250 for ( ExprNode child : children )
251 {
252 h = h * 17 + child.hashCode();
253 }
254 }
255
256 return h;
257 }
258
259
260
261
262
263 @Override
264 public boolean equals( Object other )
265 {
266 if ( this == other )
267 {
268 return true;
269 }
270
271 if ( !( other instanceof BranchNode ) )
272 {
273 return false;
274 }
275
276 if ( other.getClass() != this.getClass() )
277 {
278 return false;
279 }
280
281 BranchNode otherExprNode = ( BranchNode ) other;
282
283 List<ExprNode> otherChildren = otherExprNode.getChildren();
284
285 if ( otherChildren == children )
286 {
287 return true;
288 }
289
290 if ( children.size() != otherChildren.size() )
291 {
292 return false;
293 }
294
295 for ( int i = 0; i < children.size(); i++ )
296 {
297 ExprNode child = children.get( i );
298 ExprNode otherChild = children.get( i );
299
300 if ( !child.equals( otherChild ) )
301 {
302 return false;
303 }
304 }
305
306 return true;
307 }
308 }