Search in sources :

Example 16 with Spliterator

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;
}
Also used : Consumer(java.util.function.Consumer) Stream(java.util.stream.Stream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Spliterator(java.util.Spliterator) Function(java.util.function.Function) BooleanSupplier(java.util.function.BooleanSupplier) LongConsumer(java.util.function.LongConsumer) LongConsumer(java.util.function.LongConsumer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BooleanSupplier(java.util.function.BooleanSupplier) Spliterator(java.util.Spliterator)

Example 17 with Spliterator

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;
}
Also used : Spliterator(java.util.Spliterator)

Example 18 with Spliterator

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);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) InstanceOfAssertFactories(org.assertj.core.api.InstanceOfAssertFactories) Scannable(reactor.core.Scannable) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Tuples(reactor.util.function.Tuples) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) CoreSubscriber(reactor.core.CoreSubscriber) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Schedulers(reactor.core.scheduler.Schedulers) MockUtils(reactor.test.MockUtils) Subscriber(org.reactivestreams.Subscriber) Iterator(java.util.Iterator) Context(reactor.util.context.Context) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) List(java.util.List) Fuseable(reactor.core.Fuseable) NonNull(reactor.util.annotation.NonNull) Subscription(org.reactivestreams.Subscription) AssertSubscriber(reactor.test.subscriber.AssertSubscriber) Collections(java.util.Collections) Spliterator(java.util.Spliterator) Timeout(org.junit.jupiter.api.Timeout) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(java.util.Iterator) Test(org.junit.jupiter.api.Test)

Example 19 with Spliterator

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);
}
Also used : HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) OfInt(java.util.Spliterator.OfInt) IntConsumer(java.util.function.IntConsumer) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Set(java.util.Set) VersionCheck(org.openj9.test.util.VersionCheck) Test(org.testng.annotations.Test) Spliterator(java.util.Spliterator) ArrayList(java.util.ArrayList) OfInt(java.util.Spliterator.OfInt) ArrayList(java.util.ArrayList) Spliterator(java.util.Spliterator) Test(org.testng.annotations.Test)

Example 20 with Spliterator

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);
    }
}
Also used : HashSet(java.util.HashSet) Objects(java.util.Objects) List(java.util.List) OfInt(java.util.Spliterator.OfInt) IntConsumer(java.util.function.IntConsumer) Assert.assertEquals(org.testng.Assert.assertEquals) Assert.assertTrue(org.testng.Assert.assertTrue) Set(java.util.Set) VersionCheck(org.openj9.test.util.VersionCheck) Test(org.testng.annotations.Test) Spliterator(java.util.Spliterator) ArrayList(java.util.ArrayList) OfInt(java.util.Spliterator.OfInt) ArrayList(java.util.ArrayList) Spliterator(java.util.Spliterator) Test(org.testng.annotations.Test)

Aggregations

Spliterator (java.util.Spliterator)101 List (java.util.List)42 ArrayList (java.util.ArrayList)34 HashSet (java.util.HashSet)30 IntConsumer (java.util.function.IntConsumer)30 Set (java.util.Set)25 Function (java.util.function.Function)23 Spliterators (java.util.Spliterators)21 Collectors (java.util.stream.Collectors)21 Stream (java.util.stream.Stream)21 Objects (java.util.Objects)20 Map (java.util.Map)19 Comparator (java.util.Comparator)18 Iterator (java.util.Iterator)18 Consumer (java.util.function.Consumer)18 Arrays (java.util.Arrays)17 LongConsumer (java.util.function.LongConsumer)17 Supplier (java.util.function.Supplier)16 StreamSupport (java.util.stream.StreamSupport)16 Collections (java.util.Collections)15