-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathZipWithIndexDoubleSpliterator.java
executable file
·60 lines (49 loc) · 1.8 KB
/
ZipWithIndexDoubleSpliterator.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
package spliterators.part2.exercise;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Consumer;
public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator<IndexedDoublePair> {
private final OfDouble inner;
private int currentIndex;
public ZipWithIndexDoubleSpliterator(OfDouble inner) {
this(0, inner);
}
private ZipWithIndexDoubleSpliterator(int firstIndex, OfDouble inner) {
super(inner.estimateSize(), inner.characteristics());
currentIndex = firstIndex;
this.inner = inner;
}
@Override
public int characteristics() {
return inner.characteristics();
}
@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
return inner.tryAdvance((Double d) -> {
action.accept(new IndexedDoublePair(currentIndex, d));
currentIndex++;
});
}
@Override
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) {
inner.forEachRemaining((Double d) -> {
action.accept(new IndexedDoublePair(currentIndex, d));
currentIndex++;
});
}
@Override
public Spliterator<IndexedDoublePair> trySplit() {
if (inner.hasCharacteristics(Spliterator.SUBSIZED)) {
final OfDouble ofDouble = this.inner.trySplit();
if (ofDouble == null) return null;
final ZipWithIndexDoubleSpliterator result = new ZipWithIndexDoubleSpliterator(currentIndex, ofDouble);
currentIndex += ofDouble.estimateSize();
return result;
}
return super.trySplit();
}
@Override
public long estimateSize() {
return inner.estimateSize();
}
}