01: package org.incava.util;
02:
03: import java.util.*;
04:
05: /**
06: * Collects a collections into a collection.
07: */
08: public abstract class Collect extends ArrayList {
09: /**
10: * Creates a new collection, where the condition passes the condition.
11: *
12: * @param c The collection from which to build the new collection.
13: */
14: public Collect(Collection c) {
15: Iterator it = c.iterator();
16: while (it.hasNext()) {
17: Object obj = it.next();
18: if (where(obj)) {
19: add(block(obj));
20: }
21: }
22: }
23:
24: /**
25: * Ditto, but for real arrays.
26: */
27: public Collect(Object[] ary) {
28: for (int i = 0; i < ary.length; ++i) {
29: Object obj = ary[i];
30: if (where(obj)) {
31: add(block(obj));
32: }
33: }
34: }
35:
36: /**
37: * Must be defined to return where the given object satisfies the condition.
38: *
39: * @param obj An object from the collection passed to the constructor.
40: */
41: public abstract boolean where(Object obj);
42:
43: /**
44: * Returns the object to add to the collection.
45: *
46: * @param obj An object from the collection passed to the constructor.
47: */
48: public Object block(Object obj) {
49: return obj;
50: }
51: }
|