1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.functor.core.collection; |
18 | |
|
19 | |
import java.util.Collections; |
20 | |
import java.util.Iterator; |
21 | |
|
22 | |
import org.apache.commons.functor.UnaryFunction; |
23 | |
import org.apache.commons.functor.UnaryPredicate; |
24 | |
import org.apache.commons.functor.core.IsInstance; |
25 | |
import org.apache.commons.functor.core.composite.UnaryAnd; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | 2 | public class FilteredIterable<T> implements Iterable<T> { |
35 | |
@SuppressWarnings({ "rawtypes", "unchecked" }) |
36 | |
|
37 | 2 | private static final FilteredIterable EMPTY = new FilteredIterable(Collections.EMPTY_LIST) { |
38 | |
@Override |
39 | |
public FilteredIterable retain(Class type) { |
40 | 0 | return this; |
41 | |
} |
42 | |
|
43 | |
@Override |
44 | |
public synchronized FilteredIterable retain(UnaryPredicate predicate) { |
45 | 0 | return this; |
46 | |
} |
47 | |
|
48 | |
@Override |
49 | |
public FilteredIterable retain(Class... ofType) { |
50 | 0 | return this; |
51 | |
} |
52 | |
}; |
53 | |
|
54 | |
private final Iterable<? extends T> iterable; |
55 | |
private UnaryAnd<T> predicate; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private FilteredIterable(Iterable<? extends T> iterable) { |
62 | 60 | super(); |
63 | 60 | this.iterable = iterable; |
64 | 60 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
public Iterator<T> iterator() { |
70 | 48 | return FilteredIterator.filter(iterable.iterator(), predicate); |
71 | |
} |
72 | |
|
73 | |
@Override |
74 | |
public String toString() { |
75 | 6 | return "FilteredIterable<" + iterable + ">"; |
76 | |
} |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
public FilteredIterable<T> retain(UnaryPredicate<? super T> predicate) { |
84 | 20 | if (predicate == null) { |
85 | 2 | throw new NullPointerException("filtering predicate was null"); |
86 | |
} |
87 | 18 | synchronized (this) { |
88 | 18 | if (this.predicate == null) { |
89 | 18 | this.predicate = new UnaryAnd<T>(); |
90 | |
} |
91 | 18 | this.predicate.and(predicate); |
92 | 18 | } |
93 | 18 | return this; |
94 | |
} |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
public <U> FilteredIterable<U> retain(final Class<U> type) { |
103 | 12 | if (type == null) { |
104 | 2 | throw new NullPointerException("filtered type was null"); |
105 | |
} |
106 | 10 | return new FilteredIterable<U>(new Iterable<U>() { |
107 | |
|
108 | |
public Iterator<U> iterator() { |
109 | 10 | return TransformedIterator.transform( |
110 | |
FilteredIterator.filter(FilteredIterable.this.iterator(), IsInstance.of(type)), |
111 | 10 | new UnaryFunction<T, U>() { |
112 | |
|
113 | |
@SuppressWarnings("unchecked") |
114 | |
|
115 | |
public U evaluate(T obj) { |
116 | 22 | return (U) obj; |
117 | |
} |
118 | |
}); |
119 | |
} |
120 | |
|
121 | |
}); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public FilteredIterable<T> retain(final Class<?>... ofType) { |
130 | 4 | if (ofType == null) { |
131 | 2 | throw new NullPointerException("array of filtered types was null"); |
132 | |
} |
133 | 2 | return retain(new UnaryPredicate<T>() { |
134 | |
|
135 | |
public boolean test(T obj) { |
136 | 24 | for (Class<?> type : ofType) { |
137 | 18 | if (type.isInstance(obj)) { |
138 | 4 | return true; |
139 | |
} |
140 | |
} |
141 | 6 | return false; |
142 | |
} |
143 | |
}); |
144 | |
} |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public static <T> FilteredIterable<T> of(Iterable<T> iterable) { |
155 | 50 | if (iterable == null) { |
156 | 2 | return null; |
157 | |
} |
158 | 48 | if (iterable instanceof FilteredIterable<?>) { |
159 | 0 | return (FilteredIterable<T>) iterable; |
160 | |
} |
161 | 48 | return new FilteredIterable<T>(iterable); |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
@SuppressWarnings("unchecked") |
170 | |
public static <T> FilteredIterable<T> empty() { |
171 | 4 | return EMPTY; |
172 | |
} |
173 | |
} |