-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterIterator.java
71 lines (61 loc) · 1.77 KB
/
FilterIterator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
public class FilterIterator<T> implements Iterator<T> {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, null, null, null, 11, 12);
FilterIterator<Integer> fi = new FilterIterator<>(list.iterator(), el -> el == null || el % 2 == 0);
System.out.print("Result: [ ");
while (fi.hasNext()) {
Integer n = fi.next();
System.out.print("" + n + ", ");
}
System.out.println("]");
}
private final Iterator<T> it;
private final Predicate<T> filter;
private T next = null;
enum State {
// next is not calculated
NOT_READY,
// next is calculated, but not returned
READY,
// next is calculated and return
DONE
}
private State state = State.NOT_READY;
FilterIterator(Iterator<T> i, Predicate<T> p) {
it = i;
filter = p;
}
@Override
public boolean hasNext() {
if (state == State.DONE) return false;
boolean result = false;
if (state == State.READY) result = true;
else {
while (it.hasNext()) {
T elem = it.next();
if (filter.test(elem)) {
next = elem;
result = true;
state = State.READY;
break;
}
}
}
return result;
}
@Override
public T next() {
if (hasNext()) {
T result = next;
next = null;
state = State.NOT_READY;
return result;
}
state = State.DONE;
return null;
}
}