-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathZipWithIndexDoubleSpliterator.java
More file actions
executable file
·71 lines (60 loc) · 2.32 KB
/
ZipWithIndexDoubleSpliterator.java
File metadata and controls
executable file
·71 lines (60 loc) · 2.32 KB
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
package spliterators.part2.exercise;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
public class ZipWithIndexDoubleSpliterator extends Spliterators.AbstractSpliterator<IndexedDoublePair> {
private final OfDouble inner;
private AtomicLong currentIndex;
public ZipWithIndexDoubleSpliterator(OfDouble inner) {
this(0, inner);
}
private ZipWithIndexDoubleSpliterator(long firstIndex, OfDouble inner) {
super(inner.estimateSize(), inner.characteristics());
if (! inner.hasCharacteristics(SUBSIZED)) throw new IllegalStateException("Zip got not subsized Spliterator");
currentIndex = new AtomicLong(firstIndex);
this.inner = inner;
}
@Override
public int characteristics() {
int characteristics = inner.characteristics();
characteristics &= ~SORTED;
return characteristics;
}
@Override
public boolean tryAdvance(Consumer<? super IndexedDoublePair> action) {
// TODO
final boolean res =
inner.tryAdvance((Double v) ->
action.accept(new IndexedDoublePair((int)currentIndex.get(), v)));
if (res) currentIndex.incrementAndGet();
return res;
}
@Override
public void forEachRemaining(Consumer<? super IndexedDoublePair> action) {
// TODO
inner.forEachRemaining((Double v) -> {
action.accept(new IndexedDoublePair((int)currentIndex.get(), v));
currentIndex.incrementAndGet();
});
}
@Override
public Spliterator<IndexedDoublePair> trySplit() {
// TODO
if (inner.hasCharacteristics(SUBSIZED)) {
OfDouble newSplit = inner.trySplit();
if (newSplit == null)
return null;
Spliterator<IndexedDoublePair> zipped =
new ZipWithIndexDoubleSpliterator(currentIndex.get(), newSplit);
currentIndex.addAndGet(newSplit.estimateSize());
return zipped;
} else return super.trySplit();
}
@Override
public long estimateSize() {
// TODO
return inner.estimateSize();
}
}