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

The following examples show how to use java.util.Spliterator#getExactSizeIfKnown() . 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: SpliteratorCollisions.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 testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;

    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }
    assertContents(fromSplit, exp, isOrdered);
}
 
Example 2
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 testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;
    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }
    assertContents(fromSplit, exp, isOrdered);
}
 
Example 3
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 testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;
    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }
    assertContents(fromSplit, exp, isOrdered);
}
 
Example 4
Source File: SpliteratorTraversingAndSplittingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;
    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }
    assertContents(fromSplit, exp, isOrdered);
}
 
Example 5
Source File: SpliteratorTestHelper.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter,
        ContentAsserter<T> asserter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;
    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }

    asserter.assertContents(fromSplit, exp, isOrdered);
}
 
Example 6
Source File: SpliteratorTestHelper.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter,
        ContentAsserter<T> asserter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;
    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }

    asserter.assertContents(fromSplit, exp, isOrdered);
}
 
Example 7
Source File: SpliteratorTestHelper.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitOnce(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter,
        ContentAsserter<T> asserter) {
    S spliterator = supplier.get();
    long sizeIfKnown = spliterator.getExactSizeIfKnown();
    boolean isOrdered = spliterator.hasCharacteristics(Spliterator.ORDERED);

    ArrayList<T> fromSplit = new ArrayList<>();
    Spliterator<T> s1 = supplier.get();
    Spliterator<T> s2 = s1.trySplit();
    long s1Size = s1.getExactSizeIfKnown();
    long s2Size = (s2 != null) ? s2.getExactSizeIfKnown() : 0;
    Consumer<T> addToFromSplit = boxingAdapter.apply(fromSplit::add);
    if (s2 != null)
        s2.forEachRemaining(addToFromSplit);
    s1.forEachRemaining(addToFromSplit);

    if (sizeIfKnown >= 0) {
        assertEquals(sizeIfKnown, fromSplit.size());
        if (s1Size >= 0 && s2Size >= 0)
            assertEquals(sizeIfKnown, s1Size + s2Size);
    }

    asserter.assertContents(fromSplit, exp, isOrdered);
}
 
Example 8
Source File: AbstractPipeline.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
    return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
}
 
Example 9
Source File: SpliteratorCollisions.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0 && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 10
Source File: AbstractPipeline.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
    return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
}
 
Example 11
Source File: AbstractPipeline.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
@Override
final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
    return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
}
 
Example 12
Source File: SpliteratorTraversingAndSplittingTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0 && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 13
Source File: SpliteratorTraversingAndSplittingTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0 && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 14
Source File: SpliteratorCollisions.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0 && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 15
Source File: AbstractPipeline.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
@Override
final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
    return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
}
 
Example 16
Source File: SpliteratorTraversingAndSplittingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0 && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 17
Source File: SpliteratorCollisions.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0 && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d", leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 18
Source File: SpliteratorTestHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0
            && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 19
Source File: SpliteratorTestHelper.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private static <T> void testSplitUntilNull(SplitNode<T> e) {
    // Use an explicit stack to avoid a StackOverflowException when testing a Spliterator
    // that when repeatedly split produces a right-balanced (and maybe degenerate) tree, or
    // for a spliterator that is badly behaved.
    Deque<SplitNode<T>> stack = new ArrayDeque<>();
    stack.push(e);

    int iteration = 0;
    while (!stack.isEmpty()) {
        assertTrue(iteration++ < MAXIMUM_STACK_CAPACITY, "Exceeded maximum stack modification count of 1 << 18");

        e = stack.pop();
        Spliterator<T> parentAndRightSplit = e.s;

        long parentEstimateSize = parentAndRightSplit.estimateSize();
        assertTrue(parentEstimateSize >= 0,
                   String.format("Split size estimate %d < 0", parentEstimateSize));

        long parentSize = parentAndRightSplit.getExactSizeIfKnown();
        Spliterator<T> leftSplit = parentAndRightSplit.trySplit();
        if (leftSplit == null) {
            parentAndRightSplit.forEachRemaining(e.c);
            continue;
        }

        assertSpliterator(leftSplit, e.rootCharacteristics);
        assertSpliterator(parentAndRightSplit, e.rootCharacteristics);

        if (parentEstimateSize != Long.MAX_VALUE && leftSplit.estimateSize() > 0
            && parentAndRightSplit.estimateSize() > 0) {
            assertTrue(leftSplit.estimateSize() < parentEstimateSize,
                       String.format("Left split size estimate %d >= parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() < parentEstimateSize,
                       String.format("Right split size estimate %d >= parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
        }
        else {
            assertTrue(leftSplit.estimateSize() <= parentEstimateSize,
                       String.format("Left split size estimate %d > parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
            assertTrue(parentAndRightSplit.estimateSize() <= parentEstimateSize,
                       String.format("Right split size estimate %d > parent split size estimate %d",
                                     leftSplit.estimateSize(), parentEstimateSize));
        }

        long leftSize = leftSplit.getExactSizeIfKnown();
        long rightSize = parentAndRightSplit.getExactSizeIfKnown();
        if (parentSize >= 0 && leftSize >= 0 && rightSize >= 0)
            assertEquals(parentSize, leftSize + rightSize,
                         String.format("exact left split size %d + exact right split size %d != parent exact split size %d",
                                       leftSize, rightSize, parentSize));

        // Add right side to stack first so left side is popped off first
        stack.push(e.fromSplit(parentAndRightSplit));
        stack.push(e.fromSplit(leftSplit));
    }
}
 
Example 20
Source File: AbstractPipeline.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
@Override
final <P_IN> long exactOutputSizeIfKnown(Spliterator<P_IN> spliterator) {
    return StreamOpFlag.SIZED.isKnown(getStreamAndOpFlags()) ? spliterator.getExactSizeIfKnown() : -1;
}