use of java.util.Spliterator in project cyclops by aol.
the class FlatMapOperator method subscribe.
@Override
public StreamSubscription subscribe(Consumer<? super R> onNext, Consumer<? super Throwable> onError, Runnable onComplete) {
StreamSubscription[] s = { null };
// 1st bit for completing, 2 bit for inner active, 100 for complete
AtomicInteger status = new AtomicInteger(0);
BooleanSupplier[] thunk = { () -> {
s[0].request(1);
return true;
} };
StreamSubscription res = new StreamSubscription() {
LongConsumer work = n -> {
thunk[0].getAsBoolean();
};
@Override
public void request(long n) {
if (n <= 0)
onError.accept(new IllegalArgumentException("3.9 While the Subscription is not cancelled, Subscription.request(long n) MUST throw a java.lang.IllegalArgumentException if the argument is <= 0."));
this.singleActiveRequest(n, work);
}
@Override
public void cancel() {
s[0].cancel();
super.cancel();
}
};
s[0] = source.subscribe(e -> {
try {
Spliterator<? extends R> split = mapper.apply(e).spliterator();
int statusLocal = -1;
do {
statusLocal = status.get();
} while (// set inner active
!status.compareAndSet(statusLocal, statusLocal | (1 << 1)));
AtomicInteger advancing = new AtomicInteger(0);
thunk[0] = () -> {
while (res.isActive()) {
// outer loop to capture missed demand
boolean canAdvance = false;
if (!advancing.compareAndSet(0, 1)) {
return false;
}
try {
while (res.isActive()) {
try {
canAdvance = split.tryAdvance(onNext);
} catch (Throwable t) {
onError.accept(t);
}
if (canAdvance) {
res.requested.decrementAndGet();
} else {
int thunkStatusLocal = -1;
do {
thunkStatusLocal = status.get();
} while (// unset inner active
!status.compareAndSet(thunkStatusLocal, thunkStatusLocal & ~(1 << 1)));
if (status.compareAndSet(1, 100)) {
onComplete.run();
return true;
}
break;
}
}
} finally {
advancing.set(0);
}
if (!canAdvance && res.isActive() && !(status.get() >= 100)) {
s[0].request(1);
return true;
} else if (!canAdvance) {
return true;
}
}
return true;
};
thunk[0].getAsBoolean();
} catch (Throwable t) {
onError.accept(t);
}
}, t -> {
onError.accept(t);
res.requested.decrementAndGet();
if (res.isActive()) {
s[0].request(1);
}
}, () -> {
int statusLocal = -1;
do {
statusLocal = status.get();
} while (!status.compareAndSet(statusLocal, statusLocal | (1 << 0)));
if (status.compareAndSet(1, 100)) {
onComplete.run();
}
});
return res;
}
use of java.util.Spliterator in project cyclops by aol.
the class CopyableSpliterator method copy.
public static <T> Spliterator[] copy(Spliterator[] spliterators) {
Spliterator[] copies = new Spliterator[spliterators.length];
int i = 0;
for (Spliterator next : spliterators) {
copies[i++] = copy(next);
}
return copies;
}
use of java.util.Spliterator in project reactor-core by reactor.
the class FluxIterableTest method infiniteGeneratorDoesntHangFusedDiscard.
@Test
void infiniteGeneratorDoesntHangFusedDiscard() {
class Generator implements Iterable<Integer> {
final int seed;
Generator(int seed) {
this.seed = seed;
}
@NonNull
@Override
public Iterator<Integer> iterator() {
return new Iterator<Integer>() {
int value = seed;
@Override
public boolean hasNext() {
return true;
}
@Override
public Integer next() {
return value++;
}
};
}
}
Generator one = new Generator(1);
// smoke test: this Iterable is indeed NOT SIZED
assertThat(one.spliterator().hasCharacteristics(Spliterator.SIZED)).as("spliterator not sized").isFalse();
AtomicInteger discardCount = new AtomicInteger();
Flux.fromIterable(one).publishOn(Schedulers.single()).take(10).doOnDiscard(Integer.class, i -> discardCount.incrementAndGet()).blockLast(Duration.ofSeconds(1));
assertThat(discardCount).as("discardCount").hasValue(0);
}
use of java.util.Spliterator in project openj9 by eclipse.
the class StringStreams method testStringCharsSpliteratorSingleChar.
@Test
public void testStringCharsSpliteratorSingleChar() {
Spliterator.OfInt s = SINGLECHAR.chars().spliterator();
final ArrayList<Integer> actualIntegers = new ArrayList<>();
s.forEachRemaining((IntConsumer) i -> actualIntegers.add(Integer.valueOf(i)));
checkDigitsOrdered(SINGLECHAR, actualIntegers);
}
use of java.util.Spliterator in project openj9 by eclipse.
the class StringStreams method testStringCharsSpliterator.
@Test
public void testStringCharsSpliterator() {
Spliterator.OfInt s = DIGITS.codePoints().spliterator();
final ArrayList<Integer> actualIntegers = new ArrayList<>();
s.forEachRemaining((IntConsumer) i -> actualIntegers.add(Integer.valueOf(i)));
for (int c = 0; c < DIGITS.length(); ++c) {
assertEquals(actualIntegers.get(c), Integer.valueOf(DIGITS.codePointAt(c)), // $NON-NLS-1$
"Wrong value at position " + c);
}
}
Aggregations