-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathZipWithArraySpliterator.java
More file actions
executable file
·80 lines (69 loc) · 2.61 KB
/
ZipWithArraySpliterator.java
File metadata and controls
executable file
·80 lines (69 loc) · 2.61 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
72
73
74
75
76
77
78
79
80
package spliterators.part3.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 ZipWithArraySpliterator<A, B> extends Spliterators.AbstractSpliterator<Pair<A, B>> {
private final Spliterator<A> inner;
private final B[] array;
private final AtomicLong startInclusive;
private final long endExclusive;
public ZipWithArraySpliterator(Spliterator<A> inner, B[] array) {
super(array.length, inner.characteristics());
// TODO
this.inner = inner;
this.array = array;
this.endExclusive = array.length;
startInclusive = new AtomicLong();
}
private ZipWithArraySpliterator(Spliterator<A> inner, B[] array, long startInclusive, long endExclusive) {
super(array.length, inner.characteristics());
// TODO
this.inner = inner;
this.array = array;
this.startInclusive = new AtomicLong(startInclusive);
this.endExclusive = endExclusive;
}
@Override
public int characteristics() {
// TODO
int characteristics = inner.characteristics();
if (!inner.hasCharacteristics(SUBSIZED))
throw new IllegalStateException();
characteristics &= ~SORTED;
return characteristics;
}
@Override
public boolean tryAdvance(Consumer<? super Pair<A, B>> action) {
// TODO
if (startInclusive.get() >= endExclusive) return false;
inner.tryAdvance(v -> action.accept(
new Pair<>(v, array[(int) startInclusive.get()]))
);
startInclusive.incrementAndGet();
return true;
}
@Override
public void forEachRemaining(Consumer<? super Pair<A, B>> action) {
// TODO
inner.forEachRemaining(v -> {
if (startInclusive.get() < array.length)
action.accept(new Pair<>(v, array[(int) startInclusive.getAndIncrement()]));
});
}
@Override
public Spliterator<Pair<A, B>> trySplit() {
// TODO
final Spliterator<A> part = inner.trySplit();
if (part != null) {
final long prevStartInclusive = startInclusive.getAndAdd(part.estimateSize());
return new ZipWithArraySpliterator<>(part, array, prevStartInclusive, startInclusive.get());
} else return null;
}
@Override
public long estimateSize() {
// TODO
return endExclusive - startInclusive.get();
}
}