Search in sources :

Example 66 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, false).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 67 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 68 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 69 with Spliterator

use of java.util.Spliterator in project openj9 by eclipse-openj9.

the class StringStreams method testStringCharsSpliteratorSplitting.

@Test
public void testStringCharsSpliteratorSplitting() {
    OfInt s = DIGITS.chars().spliterator();
    ArrayList<OfInt> sArray = new ArrayList<>();
    while (Objects.nonNull(s)) {
        sArray.add(s);
        s = s.trySplit();
    }
    final HashSet<Integer> actualIntegers = new HashSet<>();
    for (OfInt e : sArray) {
        e.forEachRemaining((IntConsumer) i -> actualIntegers.add(Integer.valueOf(i)));
    }
    checkDigits(actualIntegers);
}
Also used : OfInt(java.util.Spliterator.OfInt) 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) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 70 with Spliterator

use of java.util.Spliterator in project openj9 by eclipse-openj9.

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)

Aggregations

Spliterator (java.util.Spliterator)124 List (java.util.List)47 ArrayList (java.util.ArrayList)41 HashSet (java.util.HashSet)36 IntConsumer (java.util.function.IntConsumer)35 Set (java.util.Set)31 Objects (java.util.Objects)25 Collectors (java.util.stream.Collectors)25 Spliterators (java.util.Spliterators)24 Function (java.util.function.Function)24 Iterator (java.util.Iterator)23 Consumer (java.util.function.Consumer)23 Stream (java.util.stream.Stream)23 Map (java.util.Map)22 Arrays (java.util.Arrays)20 LongConsumer (java.util.function.LongConsumer)20 Collections (java.util.Collections)19 StreamSupport (java.util.stream.StreamSupport)19 Comparator (java.util.Comparator)18 Test (org.junit.Test)18