Java Code Examples for java.util.Spliterator#tryAdvance()

The following examples show how to use java.util.Spliterator#tryAdvance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Node.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Return a node describing a subsequence of the elements of this node,
 * starting at the given inclusive start offset and ending at the given
 * exclusive end offset.
 *
 * @param from The (inclusive) starting offset of elements to include, must
 *             be in range 0..count().
 * @param to The (exclusive) end offset of elements to include, must be
 *           in range 0..count().
 * @param generator A function to be used to create a new array, if needed,
 *                  for reference nodes.
 * @return the truncated node
 */
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
    if (from == 0 && to == count())
        return this;
    Spliterator<T> spliterator = spliterator();
    long size = to - from;
    Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
    if (to == count()) {
        spliterator.forEachRemaining(nodeBuilder);
    } else {
        for (int i = 0; i < size && spliterator.tryAdvance(nodeBuilder); i++) { }
    }
    nodeBuilder.end();
    return nodeBuilder.build();
}
 
Example 2
Source File: SpliteratorTestHelper.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
Example 3
Source File: SpliteratorTraversingAndSplittingTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
Example 4
Source File: SpliteratorCollisions.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
Example 5
Source File: SpliteratorTestHelper.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
static<U> void mixedTraverseAndSplit(Consumer<U> b, Spliterator<U> splTop) {
    Spliterator<U> spl1, spl2, spl3;
    splTop.tryAdvance(b);
    spl2 = splTop.trySplit();
    if (spl2 != null) {
        spl2.tryAdvance(b);
        spl1 = spl2.trySplit();
        if (spl1 != null) {
            spl1.tryAdvance(b);
            spl1.forEachRemaining(b);
        }
        spl2.tryAdvance(b);
        spl2.forEachRemaining(b);
    }
    splTop.tryAdvance(b);
    spl3 = splTop.trySplit();
    if (spl3 != null) {
        spl3.tryAdvance(b);
        spl3.forEachRemaining(b);
    }
    splTop.tryAdvance(b);
    splTop.forEachRemaining(b);
}
 
Example 6
Source File: SpliteratorTestHelper.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> { }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
Example 7
Source File: SpliteratorTestHelper.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
static<U> void mixedTraverseAndSplit(Consumer<U> b, Spliterator<U> splTop) {
    Spliterator<U> spl1, spl2, spl3;
    splTop.tryAdvance(b);
    spl2 = splTop.trySplit();
    if (spl2 != null) {
        spl2.tryAdvance(b);
        spl1 = spl2.trySplit();
        if (spl1 != null) {
            spl1.tryAdvance(b);
            spl1.forEachRemaining(b);
        }
        spl2.tryAdvance(b);
        spl2.forEachRemaining(b);
    }
    splTop.tryAdvance(b);
    spl3 = splTop.trySplit();
    if (spl3 != null) {
        spl3.tryAdvance(b);
        spl3.forEachRemaining(b);
    }
    splTop.tryAdvance(b);
    splTop.forEachRemaining(b);
}
 
Example 8
Source File: SpliteratorTraversingAndSplittingTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
Example 9
Source File: SpliteratorTraversingAndSplittingTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitAfterFullTraversal(
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    // Full traversal using tryAdvance
    Spliterator<T> spliterator = supplier.get();
    while (spliterator.tryAdvance(boxingAdapter.apply(e -> { }))) { }
    Spliterator<T> split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using forEach
    spliterator = supplier.get();
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);

    // Full traversal using tryAdvance then forEach
    spliterator = supplier.get();
    spliterator.tryAdvance(boxingAdapter.apply(e -> { }));
    spliterator.forEachRemaining(boxingAdapter.apply(e -> {
    }));
    split = spliterator.trySplit();
    assertNull(split);
}
 
Example 10
Source File: ListSpliteratorTest.java    From exonum-java-binding with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest
@ValueSource(longs = {1, 2, 3})
void estimateSizeAfterSingleSuccessfulAdvance(long listSize) {
  ListIndex<Integer> list = createListMock();
  when(list.size()).thenReturn(listSize);
  ModificationCounter counter = mock(ModificationCounter.class);
  Spliterator<Integer> spliterator = new ListSpliterator<>(list, counter, true);

  // Advance the iterator
  spliterator.tryAdvance(NULL_CONSUMER);

  assertThat(spliterator.estimateSize()).isEqualTo(listSize - 1);
}
 
Example 11
Source File: SpliteratorLateBindingTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "Source")
public <T> void testTryAdvance(String description, Supplier<Source<T>> ss) {
    Source<T> source = ss.get();
    Spliterator<T> s = source.spliterator();

    source.update();

    Set<T> a = new HashSet<>();
    while (s.tryAdvance(a::add)) {
    }

    Set<T> e = new HashSet<>();
    source.spliterator().forEachRemaining(e::add);
    assertEquals(a, e);
}
 
Example 12
Source File: HashSetIterationTest.java    From hellokoding-courses with MIT License 5 votes vote down vote up
@Test
public void iterateWithSplitIterator() {
    Set<Integer> set = new HashSet<>(Set.of(3, 1, 2));
    Spliterator<Integer> spliterator = set.spliterator();

    spliterator.tryAdvance(ele -> System.out.printf("%d ", ele));
}
 
Example 13
Source File: SpliteratorLateBindingFailFastTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "Source")
public <T> void lateBindingTestWithTryAdvance(String description, Supplier<Source<T>> ss) {
    Source<T> source = ss.get();
    Collection<T> c = source.asCollection();
    Spliterator<T> s = c.spliterator();

    source.update();

    Set<T> r = new HashSet<>();
    while (s.tryAdvance(r::add)) { }

    assertEquals(r, new HashSet<>(c));
}
 
Example 14
Source File: RandomCollectionsMerger.java    From exchange-core with Apache License 2.0 5 votes vote down vote up
public static <T> ArrayList<T> mergeCollections(final Collection<? extends Collection<T>> chunks, final long seed) {

        final JDKRandomGenerator jdkRandomGenerator = new JDKRandomGenerator(Long.hashCode(seed));

        final ArrayList<T> mergedResult = new ArrayList<>();

        // create initial weight pairs
        List<Pair<Spliterator<T>, Double>> weightPairs = chunks.stream()
                .map(chunk -> Pair.create(chunk.spliterator(), (double) chunk.size()))
                .collect(Collectors.toList());

        while (!weightPairs.isEmpty()) {

            final EnumeratedDistribution<Spliterator<T>> ed = new EnumeratedDistribution<>(jdkRandomGenerator, weightPairs);

            // take random elements until face too many misses
            int missCounter = 0;
            while (missCounter++ < 3) {
                final Spliterator<T> sample = ed.sample();
                if (sample.tryAdvance(mergedResult::add)) {
                    missCounter = 0;
                }
            }

            // as empty queues leading to misses - rebuild wight pairs without them
            weightPairs = weightPairs.stream()
                    .filter(p -> p.getFirst().estimateSize() > 0)
                    .map(p -> Pair.create(p.getFirst(), (double) p.getFirst().estimateSize()))
                    .collect(Collectors.toList());

//            log.debug("rebuild size {}", weightPairs.size());
        }

        return mergedResult;
    }
 
Example 15
Source File: MergedAnnotationsCollectionTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void spliteratorEstimatesSize() {
	MergedAnnotations annotations = getDirectAndSimple();
	Spliterator<MergedAnnotation<Annotation>> spliterator = annotations.spliterator();
	assertThat(spliterator.estimateSize()).isEqualTo(5);
	spliterator.tryAdvance(
			annotation -> assertThat(annotation.getType()).isEqualTo(Direct.class));
	assertThat(spliterator.estimateSize()).isEqualTo(4);
}
 
Example 16
Source File: ReferencePipeline.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
final void forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) {
    do { } while (!sink.cancellationRequested() && spliterator.tryAdvance(sink));
}
 
Example 17
Source File: ReferencePipeline.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
final boolean forEachWithCancel(Spliterator<P_OUT> spliterator, Sink<P_OUT> sink) {
    boolean cancelled;
    do { } while (!(cancelled = sink.cancellationRequested()) && spliterator.tryAdvance(sink));
    return cancelled;
}
 
Example 18
Source File: ReferencePipeline.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final <R> Stream<R> flatMap(Function<? super P_OUT, ? extends Stream<? extends R>> mapper) {
    Objects.requireNonNull(mapper);
    return new StatelessOp<P_OUT, R>(this, StreamShape.REFERENCE,
                                 StreamOpFlag.NOT_SORTED | StreamOpFlag.NOT_DISTINCT | StreamOpFlag.NOT_SIZED) {
        @Override
        Sink<P_OUT> opWrapSink(int flags, Sink<R> sink) {
            return new Sink.ChainedReference<P_OUT, R>(sink) {
                // true if cancellationRequested() has been called
                boolean cancellationRequestedCalled;

                @Override
                public void begin(long size) {
                    downstream.begin(-1);
                }

                @Override
                public void accept(P_OUT u) {
                    try (Stream<? extends R> result = mapper.apply(u)) {
                        if (result != null) {
                            if (!cancellationRequestedCalled) {
                                result.sequential().forEach(downstream);
                            }
                            else {
                                Spliterator<? extends R> s = result.sequential().spliterator();
                                do { } while (!downstream.cancellationRequested() && s.tryAdvance(downstream));
                            }
                        }
                    }
                }

                @Override
                public boolean cancellationRequested() {
                    // If this method is called then an operation within the stream
                    // pipeline is short-circuiting (see AbstractPipeline.copyInto).
                    // Note that we cannot differentiate between an upstream or
                    // downstream operation
                    cancellationRequestedCalled = true;
                    return downstream.cancellationRequested();
                }
            };
        }
    };
}
 
Example 19
Source File: Node.java    From desugar_jdk_libs with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a node describing a subsequence of the elements of this node,
 * starting at the given inclusive start offset and ending at the given
 * exclusive end offset.
 *
 * @param from The (inclusive) starting offset of elements to include, must
 *             be in range 0..count().
 * @param to The (exclusive) end offset of elements to include, must be
 *           in range 0..count().
 * @param generator A function to be used to create a new array, if needed,
 *                  for reference nodes.
 * @return the truncated node
 */
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
    if (from == 0 && to == count())
        return this;
    Spliterator<T> spliterator = spliterator();
    long size = to - from;
    Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
    for (int i = 0; (i < size) && spliterator.tryAdvance(nodeBuilder); i++) { }
    nodeBuilder.end();
    return nodeBuilder.build();
}
 
Example 20
Source File: Node.java    From jdk8u60 with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Return a node describing a subsequence of the elements of this node,
 * starting at the given inclusive start offset and ending at the given
 * exclusive end offset.
 *
 * @param from The (inclusive) starting offset of elements to include, must
 *             be in range 0..count().
 * @param to The (exclusive) end offset of elements to include, must be
 *           in range 0..count().
 * @param generator A function to be used to create a new array, if needed,
 *                  for reference nodes.
 * @return the truncated node
 */
default Node<T> truncate(long from, long to, IntFunction<T[]> generator) {
    if (from == 0 && to == count())
        return this;
    Spliterator<T> spliterator = spliterator();
    long size = to - from;
    Node.Builder<T> nodeBuilder = Nodes.builder(size, generator);
    nodeBuilder.begin(size);
    for (int i = 0; i < from && spliterator.tryAdvance(e -> { }); i++) { }
    for (int i = 0; (i < size) && spliterator.tryAdvance(nodeBuilder); i++) { }
    nodeBuilder.end();
    return nodeBuilder.build();
}