1
2
3
4
5 package org.rcfaces.core.internal.util;
6
7 import java.lang.reflect.Array;
8 import java.util.ArrayList;
9 import java.util.Collections;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.NoSuchElementException;
13
14 import javax.faces.component.UIComponent;
15
16 import org.rcfaces.core.component.iterator.IComponentIterator;
17 import org.rcfaces.core.internal.Constants;
18 import org.rcfaces.core.internal.manager.IContainerManager;
19
20
21
22
23
24 public class ComponentIterators {
25 private static final String REVISION = "$Revision: 1.18 $";
26
27 public static final IComponentIterator EMPTY_COMPONENT_ITERATOR = new EmptyComponentIterator();
28
29 public static final UIComponent[] EMPTY_COMPONENT_ARRAY = new UIComponent[0];
30
31 public static int indexOf(IContainerManager parent, UIComponent child,
32 Class childClass) {
33
34 if (Constants.CACHED_COMPONENT_ITERATOR
35 && Constants.STATED_COMPONENT_CHILDREN_LIST) {
36 UIComponent aos[] = CachedChildrenList.getArray(parent, childClass);
37 for (int i = 0; i < aos.length; i++) {
38 if (child == aos[i] || child.equals(aos[i])) {
39 return i;
40 }
41 }
42 return -1;
43 }
44
45 int idx = 0;
46 for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
47 UIComponent obj = (UIComponent) it.next();
48 if (obj == null) {
49 continue;
50 }
51
52 if (childClass.isInstance(obj) == false) {
53 continue;
54 }
55
56 if (child == obj || obj.equals(child)) {
57 return idx;
58 }
59
60 idx++;
61 }
62
63 return -1;
64 }
65
66 public static UIComponent componentAt(IContainerManager parent,
67 Class childClass, int position) {
68 if (Constants.CACHED_COMPONENT_ITERATOR
69 && Constants.STATED_COMPONENT_CHILDREN_LIST) {
70 UIComponent elements[] = CachedChildrenList.getArray(parent,
71 childClass);
72 if (position < 0 || position >= elements.length) {
73 throw new IndexOutOfBoundsException("Out of range (0 <= "
74 + position + " <= " + (elements.length - 1) + ")");
75 }
76
77 return elements[position];
78 }
79
80 int idx = 0;
81 for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
82 UIComponent obj = (UIComponent) it.next();
83 if (obj == null) {
84 continue;
85 }
86
87 if (childClass.isInstance(obj) == false) {
88 continue;
89 }
90
91 if (idx == position) {
92 return obj;
93 }
94
95 idx++;
96 }
97
98 throw new IndexOutOfBoundsException("Out of range (0 <= " + position
99 + " <= " + (idx - 1) + ")");
100 }
101
102 public static int count(IContainerManager parent, Class childClass) {
103 if (Constants.CACHED_COMPONENT_ITERATOR
104 && Constants.STATED_COMPONENT_CHILDREN_LIST) {
105 return CachedChildrenList.getCount(parent, childClass);
106 }
107
108 int cnt = 0;
109 for (Iterator it = parent.getChildren().iterator(); it.hasNext();) {
110 UIComponent obj = (UIComponent) it.next();
111 if (obj == null) {
112 continue;
113 }
114
115 if (childClass.isInstance(obj) == false) {
116 continue;
117 }
118
119 cnt++;
120 }
121
122 return cnt;
123 }
124
125 public static boolean removeAll(IContainerManager parent, Class childClass) {
126 int count = parent.getChildCount();
127 if (count < 1) {
128 return false;
129 }
130
131 List rev = list((UIComponent) parent, childClass);
132
133 if (rev == null || rev.isEmpty()) {
134 return false;
135 }
136
137 return parent.getChildren().removeAll(rev);
138 }
139
140 public static List list(UIComponent parent, Class childClass) {
141
142 if (Constants.CACHED_COMPONENT_ITERATOR
143 && Constants.STATED_COMPONENT_CHILDREN_LIST) {
144 if (parent instanceof IContainerManager) {
145 return CachedChildrenList.getList((IContainerManager) parent,
146 childClass);
147 }
148 }
149
150 List components = parent.getChildren();
151 int total = components.size();
152 if (total == 0) {
153 return Collections.EMPTY_LIST;
154 }
155
156 List rev = null;
157 for (Iterator it = components.iterator(); it.hasNext(); total--) {
158 UIComponent component = (UIComponent) it.next();
159
160 if (childClass.isInstance(component) == false) {
161 continue;
162 }
163
164 if (rev == null) {
165 rev = new ArrayList(total);
166 }
167
168 rev.add(component);
169 }
170
171 if (rev == null) {
172 return Collections.EMPTY_LIST;
173 }
174
175 return rev;
176 }
177
178
179
180
181
182
183 private static class EmptyComponentIterator implements IComponentIterator {
184 private static final String REVISION = "$Revision: 1.18 $";
185
186
187
188
189
190
191 public final int count() {
192 return 0;
193 }
194
195
196
197
198
199
200 public final boolean hasNext() {
201 return false;
202 }
203
204
205
206
207
208
209 public final UIComponent nextComponent() {
210 throw new NoSuchElementException("Empty iterator");
211 }
212
213 public UIComponent[] toArray(UIComponent[] array) {
214 return EMPTY_COMPONENT_ARRAY;
215 }
216
217 }
218
219
220
221
222
223
224 public static class ComponentListIterator implements IComponentIterator {
225 private static final String REVISION = "$Revision: 1.18 $";
226
227 private final Iterator iterator;
228
229 private int count;
230
231 protected ComponentListIterator(List list) {
232 this.iterator = list.iterator();
233 this.count = list.size();
234 }
235
236
237
238
239
240
241 public final int count() {
242 return count;
243 }
244
245
246
247
248
249
250 public final boolean hasNext() {
251 return count > 0;
252 }
253
254
255
256
257
258
259 public final UIComponent nextComponent() {
260 Object object = iterator.next();
261
262 count--;
263
264 return (UIComponent) object;
265 }
266
267 public UIComponent[] toArray(UIComponent[] array) {
268 if (count < 1) {
269
270
271 if (array == null) {
272 return EMPTY_COMPONENT_ARRAY;
273 }
274
275 if (array.length == 0) {
276 return array;
277 }
278
279 return (UIComponent[]) Array.newInstance(array.getClass()
280 .getComponentType(), 0);
281 }
282
283 if (array == null) {
284 array = new UIComponent[count];
285
286 } else if (array.length != count) {
287 array = (UIComponent[]) Array.newInstance(array.getClass()
288 .getComponentType(), count);
289 }
290
291 for (int i = 0; count > 0; i++) {
292 array[i] = (UIComponent) iterator.next();
293
294 count--;
295 }
296
297 return array;
298 }
299 }
300
301
302
303
304
305
306 public static class ComponentArrayIterator implements IComponentIterator {
307 private static final String REVISION = "$Revision: 1.18 $";
308
309 private final Object array[];
310
311 private int count;
312
313 protected ComponentArrayIterator(UIComponent object) {
314 this.array = new Object[] { object };
315 this.count = 1;
316 }
317
318 protected ComponentArrayIterator(Object array[]) {
319 this.array = array;
320 this.count = array.length;
321 }
322
323
324
325
326
327
328 public final int count() {
329 return count;
330 }
331
332
333
334
335
336
337 public final boolean hasNext() {
338 return count > 0;
339 }
340
341
342
343
344
345
346 public final UIComponent nextComponent() {
347 if (count < 1) {
348 throw new NoSuchElementException(
349 "No more components ! (position="
350 + (array.length - count) + ", arraySize="
351 + array.length + ")");
352 }
353
354 UIComponent component = (UIComponent) array[array.length - count];
355 count--;
356
357 return component;
358 }
359
360 public UIComponent[] toArray(UIComponent[] array) {
361 if (count < 1) {
362 throw new NoSuchElementException(
363 "No more components ! (position="
364 + (array.length - count) + ", arraySize="
365 + array.length + ")");
366 }
367
368 if (array == null) {
369 array = new UIComponent[count];
370
371 } else if (array.length < count) {
372 array = (UIComponent[]) Array.newInstance(array.getClass()
373 .getComponentType(), count);
374 }
375
376 System.arraycopy(this.array, this.array.length - count, array, 0,
377 count);
378
379 count = 0;
380
381 return array;
382 }
383
384 }
385 }