-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathZipWithIndexDoubleSpliterator.java
executable file
·72 lines (60 loc) · 2.18 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
61
62
63
64
65
66
67
68
69
70
71
72
package spliterators.part2.exercise;
import java.util.Comparator;
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 long currentIndex;
public ZipWithIndexDoubleSpliterator(OfDouble inner) {
this(0, inner);
}
private ZipWithIndexDoubleSpliterator(long firstIndex, OfDouble inner) {
super(inner.estimateSize(), inner.characteristics());
currentIndex = firstIndex;
this.inner = inner;
}
@Override
public int characteristics() {
return inner.characteristics() | NONNULL;
}
@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
if (inner.tryAdvance((double s) -> action.accept(new IndexedDoublePair(currentIndex, s)))) {
currentIndex++;
return true;
} else {
return false;
}
}
@Override
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) {
inner.forEachRemaining((double s) -> {
action.accept(new IndexedDoublePair(currentIndex, s));
currentIndex++;
});
}
@Override
public Spliterator<IndexedDoublePair> trySplit() {
if (inner.hasCharacteristics(Spliterator.SUBSIZED)) {
Spliterator<IndexedDoublePair> spliterator = new ZipWithIndexDoubleSpliterator(currentIndex, inner.trySplit());
currentIndex += spliterator.estimateSize();
return spliterator;
} else{
return super.trySplit();
}
}
@Override
public long estimateSize() {
return inner.estimateSize();
}
@Override
public Comparator<IndexedDoublePair> getComparator() {
if (inner.hasCharacteristics(SORTED)) {
Comparator<? super Double> comparator = inner.getComparator();
return (o1, o2) -> comparator.compare(o1.getValue(), o2.getValue());
} else {
throw new IllegalStateException();
}
}
}