-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilteredIterator.java
More file actions
43 lines (34 loc) · 881 Bytes
/
Copy pathFilteredIterator.java
File metadata and controls
43 lines (34 loc) · 881 Bytes
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
package utils;
import java.util.Iterator;
import java.util.function.BiPredicate;
public class FilteredIterator<T,E> implements IndexedIterator<T> {
private final Iterator<T> iterator;
private final E value;
private final BiPredicate<T, E> check;
public FilteredIterator(final Iterator<T> iterator,final BiPredicate<T,E> check,final E value) {
this.iterator=iterator;
this.check=check;
this.value=value;
}
private T stash=null;
private int nextIndex=-1;
private T findNext() {
while(iterator.hasNext()) {
var nextOne=iterator.next();
nextIndex++;
if(check.test(nextOne, value)) return nextOne;
}
return null;
}
public final int nextIndex() {
return nextIndex;
}
@Override
public boolean hasNext() {
return (stash=findNext())!=null;
}
@Override
public T next() {
return stash;
}
}