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
|
package tools.refinery.store.map;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;
public class CursorAsIterator<K,V,D> implements Iterator<D> {
private final Cursor<K, V> internal;
private final BiFunction<K, V, D> entryTransformation;
private final BiPredicate<K,V> filtering;
D lastValidElement;
public CursorAsIterator(Cursor<K, V> internal, BiFunction<K, V, D> entryTransformation, BiPredicate<K,V> filtering) {
this.internal = internal;
this.entryTransformation = entryTransformation;
this.filtering = filtering;
moveToNext();
}
public CursorAsIterator(Cursor<K, V> internal, BiFunction<K, V, D> entryTransformation) {
this.internal = internal;
this.entryTransformation = entryTransformation;
this.filtering = ((k,v)->true);
moveToNext();
}
private void moveToNext() {
internal.move();
while(!internal.isTerminated() && !filtering.test(internal.getKey(), internal.getValue())) {
internal.move();
}
if(!internal.isTerminated()) {
lastValidElement = entryTransformation.apply(internal.getKey(), internal.getValue());
}
}
@Override
public boolean hasNext() {
return !internal.isTerminated();
}
@Override
public D next() {
if(hasNext()) {
D last = lastValidElement;
moveToNext();
return last;
} else {
throw new NoSuchElementException();
}
}
}
|