|
Lines 23-29
Link Here
|
| 23 |
* to perform different processing on the objects passed to it. |
23 |
* to perform different processing on the objects passed to it. |
| 24 |
*/ |
24 |
*/ |
| 25 |
public class Collector { |
25 |
public class Collector { |
| 26 |
private ArrayList collected = null; |
26 |
private Set collected = null; |
| 27 |
|
27 |
|
| 28 |
/** |
28 |
/** |
| 29 |
* Creates a new collector. |
29 |
* Creates a new collector. |
|
Lines 45-61
Link Here
|
| 45 |
* or <code>false</code> to indicate the traversal should stop. |
45 |
* or <code>false</code> to indicate the traversal should stop. |
| 46 |
*/ |
46 |
*/ |
| 47 |
public boolean accept(Object object) { |
47 |
public boolean accept(Object object) { |
| 48 |
getList().add(object); |
48 |
getCollection().add(object); |
| 49 |
return true; |
49 |
return true; |
| 50 |
} |
50 |
} |
| 51 |
|
51 |
|
| 52 |
/** |
52 |
/** |
| 53 |
* Returns the list that is being used to collect results. |
53 |
* Returns the collection that is being used to collect results. Unlike {@toCollection}, |
| 54 |
* @return the list being used to collect results. |
54 |
* this returns the actual modifiable collection that is being used to store results. The |
|
|
55 |
* return value is only intended to be used within subclasses and should not be exposed |
| 56 |
* outside of a collection class. |
| 57 |
* |
| 58 |
* @return the collection being used to collect results. |
| 55 |
*/ |
59 |
*/ |
| 56 |
protected List getList() { |
60 |
protected Collection getCollection() { |
| 57 |
if (collected == null) |
61 |
if (collected == null) |
| 58 |
collected = new ArrayList(); |
62 |
collected = new HashSet(); |
| 59 |
return collected; |
63 |
return collected; |
| 60 |
} |
64 |
} |
| 61 |
|
65 |
|
|
Lines 100-106
Link Here
|
| 100 |
return result; |
104 |
return result; |
| 101 |
} |
105 |
} |
| 102 |
|
106 |
|
|
|
107 |
/** |
| 108 |
* Returns the collected objects as an immutable collection. |
| 109 |
* |
| 110 |
* @return An unmodifiable collection of the collected objects |
| 111 |
*/ |
| 103 |
public Collection toCollection() { |
112 |
public Collection toCollection() { |
| 104 |
return collected == null ? Collections.EMPTY_LIST : Collections.unmodifiableList(collected); |
113 |
return collected == null ? Collections.EMPTY_SET : Collections.unmodifiableSet(collected); |
| 105 |
} |
114 |
} |
| 106 |
} |
115 |
} |