KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > incava > util > Collect


1 package org.incava.util;
2
3 import java.util.*;
4
5
6 /**
7  * Collects a collections into a collection.
8  */

9 public abstract class Collect extends ArrayList
10 {
11     /**
12      * Creates a new collection, where the condition passes the condition.
13      *
14      * @param c The collection from which to build the new collection.
15      */

16     public Collect(Collection c)
17     {
18         Iterator it = c.iterator();
19         while (it.hasNext()) {
20             Object JavaDoc obj = it.next();
21             if (where(obj)) {
22                 add(block(obj));
23             }
24         }
25     }
26
27     /**
28      * Ditto, but for real arrays.
29      */

30     public Collect(Object JavaDoc[] ary)
31     {
32         for (int i = 0; i < ary.length; ++i) {
33             Object JavaDoc obj = ary[i];
34             if (where(obj)) {
35                 add(block(obj));
36             }
37         }
38     }
39
40     /**
41      * Must be defined to return where the given object satisfies the condition.
42      *
43      * @param obj An object from the collection passed to the constructor.
44      */

45     public abstract boolean where(Object JavaDoc obj);
46     
47     /**
48      * Returns the object to add to the collection.
49      *
50      * @param obj An object from the collection passed to the constructor.
51      */

52     public Object JavaDoc block(Object JavaDoc obj)
53     {
54         return obj;
55     }
56 }
57
Popular Tags