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

The following examples show how to use java.util.Spliterator#hasCharacteristics() . 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: 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 testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter,
        ContentAsserter<T> asserter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    asserter.assertContents(splits, exp, isOrdered);
}
 
Example 2
Source File: SpliteratorCollisions.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    assertContents(splits, exp, isOrdered);
}
 
Example 3
Source File: SpliteratorTestHelper.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static void assertSpliterator(Spliterator<?> s) {
    if (s.hasCharacteristics(Spliterator.SUBSIZED)) {
        assertTrue(s.hasCharacteristics(Spliterator.SIZED));
    }
    if (s.hasCharacteristics(Spliterator.SIZED)) {
        assertTrue(s.estimateSize() != Long.MAX_VALUE);
        assertTrue(s.getExactSizeIfKnown() >= 0);
    }
    try {
        s.getComparator();
        assertTrue(s.hasCharacteristics(Spliterator.SORTED));
    } catch (IllegalStateException e) {
        assertFalse(s.hasCharacteristics(Spliterator.SORTED));
    }
}
 
Example 4
Source File: SpliteratorTestHelper.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter,
        ContentAsserter<T> asserter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    asserter.assertContents(splits, exp, isOrdered);
}
 
Example 5
Source File: SpliteratorTraversingAndSplittingTest.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void assertSpliterator(Spliterator<?> s) {
    if (s.hasCharacteristics(Spliterator.SUBSIZED)) {
        assertTrue(s.hasCharacteristics(Spliterator.SIZED));
    }
    if (s.hasCharacteristics(Spliterator.SIZED)) {
        assertTrue(s.estimateSize() != Long.MAX_VALUE);
        assertTrue(s.getExactSizeIfKnown() >= 0);
    }
    try {
        s.getComparator();
        assertTrue(s.hasCharacteristics(Spliterator.SORTED));
    } catch (IllegalStateException e) {
        assertFalse(s.hasCharacteristics(Spliterator.SORTED));
    }
}
 
Example 6
Source File: SpliteratorTraversingAndSplittingTest.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 testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertRootSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    assertContents(splits, exp, isOrdered);
}
 
Example 7
Source File: SpliteratorCollisions.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private static void assertSpliterator(Spliterator<?> s) {
    if (s.hasCharacteristics(Spliterator.SUBSIZED)) {
        assertTrue(s.hasCharacteristics(Spliterator.SIZED));
    }
    if (s.hasCharacteristics(Spliterator.SIZED)) {
        assertTrue(s.estimateSize() != Long.MAX_VALUE);
        assertTrue(s.getExactSizeIfKnown() >= 0);
    }
    try {
        s.getComparator();
        assertTrue(s.hasCharacteristics(Spliterator.SORTED));
    } catch (IllegalStateException e) {
        assertFalse(s.hasCharacteristics(Spliterator.SORTED));
    }
}
 
Example 8
Source File: SpliteratorCollisions.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 testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    assertContents(splits, exp, isOrdered);
}
 
Example 9
Source File: SpliteratorTraversingAndSplittingTest.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static void assertSpliterator(Spliterator<?> s) {
    if (s.hasCharacteristics(Spliterator.SUBSIZED)) {
        assertTrue(s.hasCharacteristics(Spliterator.SIZED));
    }
    if (s.hasCharacteristics(Spliterator.SIZED)) {
        assertTrue(s.estimateSize() != Long.MAX_VALUE);
        assertTrue(s.getExactSizeIfKnown() >= 0);
    }
    try {
        s.getComparator();
        assertTrue(s.hasCharacteristics(Spliterator.SORTED));
    } catch (IllegalStateException e) {
        assertFalse(s.hasCharacteristics(Spliterator.SORTED));
    }
}
 
Example 10
Source File: SpliteratorCollisions.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    assertContents(splits, exp, isOrdered);
}
 
Example 11
Source File: SpliteratorCollisions.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    assertContents(splits, exp, isOrdered);
}
 
Example 12
Source File: SpliteratorCollisions.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 testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    assertContents(splits, exp, isOrdered);
}
 
Example 13
Source File: SpliteratorTestHelper.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static <T, S extends Spliterator<T>> void testSplitUntilNull(
        Collection<T> exp,
        Supplier<S> supplier,
        UnaryOperator<Consumer<T>> boxingAdapter,
        ContentAsserter<T> asserter) {
    Spliterator<T> s = supplier.get();
    boolean isOrdered = s.hasCharacteristics(Spliterator.ORDERED);
    assertSpliterator(s);

    List<T> splits = new ArrayList<>();
    Consumer<T> c = boxingAdapter.apply(splits::add);

    testSplitUntilNull(new SplitNode<T>(c, s));
    asserter.assertContents(splits, exp, isOrdered);
}
 
Example 14
Source File: AbstractPipeline.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the source spliterator for this pipeline stage.  For a sequential or
 * stateless parallel pipeline, this is the source spliterator.  For a
 * stateful parallel pipeline, this is a spliterator describing the results
 * of all computations up to and including the most recent stateful
 * operation.
 */
@SuppressWarnings("unchecked")
private Spliterator<?> sourceSpliterator(int terminalFlags) {
    // Get the source spliterator of the pipeline
    Spliterator<?> spliterator = null;
    if (sourceStage.sourceSpliterator != null) {
        spliterator = sourceStage.sourceSpliterator;
        sourceStage.sourceSpliterator = null;
    }
    else if (sourceStage.sourceSupplier != null) {
        spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
        sourceStage.sourceSupplier = null;
    }
    else {
        throw new IllegalStateException(MSG_CONSUMED);
    }

    if (isParallel() && sourceStage.sourceAnyStateful) {
        // Adapt the source spliterator, evaluating each stateful op
        // in the pipeline up to and including this pipeline stage.
        // The depth and flags of each pipeline stage are adjusted accordingly.
        int depth = 1;
        for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
             u != e;
             u = p, p = p.nextStage) {

            int thisOpFlags = p.sourceOrOpFlags;
            if (p.opIsStateful()) {
                depth = 0;

                if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
                    // Clear the short circuit flag for next pipeline stage
                    // This stage encapsulates short-circuiting, the next
                    // stage may not have any short-circuit operations, and
                    // if so spliterator.forEachRemaining should be used
                    // for traversal
                    thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
                }

                spliterator = p.opEvaluateParallelLazy(u, spliterator);

                // Inject or clear SIZED on the source pipeline stage
                // based on the stage's spliterator
                thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
                        ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
                        : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
            }
            p.depth = depth++;
            p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
        }
    }

    if (terminalFlags != 0)  {
        // Apply flags from the terminal operation to last pipeline stage
        combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
    }

    return spliterator;
}
 
Example 15
Source File: AbstractPipeline.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the source spliterator for this pipeline stage.  For a sequential or
 * stateless parallel pipeline, this is the source spliterator.  For a
 * stateful parallel pipeline, this is a spliterator describing the results
 * of all computations up to and including the most recent stateful
 * operation.
 */
@SuppressWarnings("unchecked")
private Spliterator<?> sourceSpliterator(int terminalFlags) {
    // Get the source spliterator of the pipeline
    Spliterator<?> spliterator = null;
    if (sourceStage.sourceSpliterator != null) {
        spliterator = sourceStage.sourceSpliterator;
        sourceStage.sourceSpliterator = null;
    }
    else if (sourceStage.sourceSupplier != null) {
        spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
        sourceStage.sourceSupplier = null;
    }
    else {
        throw new IllegalStateException(MSG_CONSUMED);
    }

    if (isParallel() && sourceStage.sourceAnyStateful) {
        // Adapt the source spliterator, evaluating each stateful op
        // in the pipeline up to and including this pipeline stage.
        // The depth and flags of each pipeline stage are adjusted accordingly.
        int depth = 1;
        for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
             u != e;
             u = p, p = p.nextStage) {

            int thisOpFlags = p.sourceOrOpFlags;
            if (p.opIsStateful()) {
                depth = 0;

                if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
                    // Clear the short circuit flag for next pipeline stage
                    // This stage encapsulates short-circuiting, the next
                    // stage may not have any short-circuit operations, and
                    // if so spliterator.forEachRemaining should be used
                    // for traversal
                    thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
                }

                spliterator = p.opEvaluateParallelLazy(u, spliterator);

                // Inject or clear SIZED on the source pipeline stage
                // based on the stage's spliterator
                thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
                        ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
                        : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
            }
            p.depth = depth++;
            p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
        }
    }

    if (terminalFlags != 0)  {
        // Apply flags from the terminal operation to last pipeline stage
        combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
    }

    return spliterator;
}
 
Example 16
Source File: AbstractPipeline.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the source spliterator for this pipeline stage.  For a sequential or
 * stateless parallel pipeline, this is the source spliterator.  For a
 * stateful parallel pipeline, this is a spliterator describing the results
 * of all computations up to and including the most recent stateful
 * operation.
 */
@SuppressWarnings("unchecked")
private Spliterator<?> sourceSpliterator(int terminalFlags) {
    // Get the source spliterator of the pipeline
    Spliterator<?> spliterator = null;
    if (sourceStage.sourceSpliterator != null) {
        spliterator = sourceStage.sourceSpliterator;
        sourceStage.sourceSpliterator = null;
    }
    else if (sourceStage.sourceSupplier != null) {
        spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
        sourceStage.sourceSupplier = null;
    }
    else {
        throw new IllegalStateException(MSG_CONSUMED);
    }

    if (isParallel() && sourceStage.sourceAnyStateful) {
        // Adapt the source spliterator, evaluating each stateful op
        // in the pipeline up to and including this pipeline stage.
        // The depth and flags of each pipeline stage are adjusted accordingly.
        int depth = 1;
        for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
             u != e;
             u = p, p = p.nextStage) {

            int thisOpFlags = p.sourceOrOpFlags;
            if (p.opIsStateful()) {
                depth = 0;

                if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
                    // Clear the short circuit flag for next pipeline stage
                    // This stage encapsulates short-circuiting, the next
                    // stage may not have any short-circuit operations, and
                    // if so spliterator.forEachRemaining should be used
                    // for traversal
                    thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
                }

                spliterator = p.opEvaluateParallelLazy(u, spliterator);

                // Inject or clear SIZED on the source pipeline stage
                // based on the stage's spliterator
                thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
                        ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
                        : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
            }
            p.depth = depth++;
            p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
        }
    }

    if (terminalFlags != 0)  {
        // Apply flags from the terminal operation to last pipeline stage
        combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
    }

    return spliterator;
}
 
Example 17
Source File: AbstractPipeline.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the source spliterator for this pipeline stage.  For a sequential or
 * stateless parallel pipeline, this is the source spliterator.  For a
 * stateful parallel pipeline, this is a spliterator describing the results
 * of all computations up to and including the most recent stateful
 * operation.
 */
@SuppressWarnings("unchecked")
private Spliterator<?> sourceSpliterator(int terminalFlags) {
    // Get the source spliterator of the pipeline
    Spliterator<?> spliterator = null;
    if (sourceStage.sourceSpliterator != null) {
        spliterator = sourceStage.sourceSpliterator;
        sourceStage.sourceSpliterator = null;
    }
    else if (sourceStage.sourceSupplier != null) {
        spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
        sourceStage.sourceSupplier = null;
    }
    else {
        throw new IllegalStateException(MSG_CONSUMED);
    }

    if (isParallel() && sourceStage.sourceAnyStateful) {
        // Adapt the source spliterator, evaluating each stateful op
        // in the pipeline up to and including this pipeline stage.
        // The depth and flags of each pipeline stage are adjusted accordingly.
        int depth = 1;
        for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
             u != e;
             u = p, p = p.nextStage) {

            int thisOpFlags = p.sourceOrOpFlags;
            if (p.opIsStateful()) {
                depth = 0;

                if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
                    // Clear the short circuit flag for next pipeline stage
                    // This stage encapsulates short-circuiting, the next
                    // stage may not have any short-circuit operations, and
                    // if so spliterator.forEachRemaining should be used
                    // for traversal
                    thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
                }

                spliterator = p.opEvaluateParallelLazy(u, spliterator);

                // Inject or clear SIZED on the source pipeline stage
                // based on the stage's spliterator
                thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
                        ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
                        : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
            }
            p.depth = depth++;
            p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
        }
    }

    if (terminalFlags != 0)  {
        // Apply flags from the terminal operation to last pipeline stage
        combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
    }

    return spliterator;
}
 
Example 18
Source File: AbstractPipeline.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the source spliterator for this pipeline stage.  For a sequential or
 * stateless parallel pipeline, this is the source spliterator.  For a
 * stateful parallel pipeline, this is a spliterator describing the results
 * of all computations up to and including the most recent stateful
 * operation.
 */
@SuppressWarnings("unchecked")
private Spliterator<?> sourceSpliterator(int terminalFlags) {
    // Get the source spliterator of the pipeline
    Spliterator<?> spliterator = null;
    if (sourceStage.sourceSpliterator != null) {
        spliterator = sourceStage.sourceSpliterator;
        sourceStage.sourceSpliterator = null;
    }
    else if (sourceStage.sourceSupplier != null) {
        spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
        sourceStage.sourceSupplier = null;
    }
    else {
        throw new IllegalStateException(MSG_CONSUMED);
    }

    if (isParallel() && sourceStage.sourceAnyStateful) {
        // Adapt the source spliterator, evaluating each stateful op
        // in the pipeline up to and including this pipeline stage.
        // The depth and flags of each pipeline stage are adjusted accordingly.
        int depth = 1;
        for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
             u != e;
             u = p, p = p.nextStage) {

            int thisOpFlags = p.sourceOrOpFlags;
            if (p.opIsStateful()) {
                depth = 0;

                if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
                    // Clear the short circuit flag for next pipeline stage
                    // This stage encapsulates short-circuiting, the next
                    // stage may not have any short-circuit operations, and
                    // if so spliterator.forEachRemaining should be used
                    // for traversal
                    thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
                }

                spliterator = p.opEvaluateParallelLazy(u, spliterator);

                // Inject or clear SIZED on the source pipeline stage
                // based on the stage's spliterator
                thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
                        ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
                        : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
            }
            p.depth = depth++;
            p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
        }
    }

    if (terminalFlags != 0)  {
        // Apply flags from the terminal operation to last pipeline stage
        combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
    }

    return spliterator;
}
 
Example 19
Source File: AbstractPipeline.java    From desugar_jdk_libs with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get the source spliterator for this pipeline stage.  For a sequential or
 * stateless parallel pipeline, this is the source spliterator.  For a
 * stateful parallel pipeline, this is a spliterator describing the results
 * of all computations up to and including the most recent stateful
 * operation.
 */
@SuppressWarnings("unchecked")
private Spliterator<?> sourceSpliterator(int terminalFlags) {
    // Get the source spliterator of the pipeline
    Spliterator<?> spliterator = null;
    if (sourceStage.sourceSpliterator != null) {
        spliterator = sourceStage.sourceSpliterator;
        sourceStage.sourceSpliterator = null;
    }
    else if (sourceStage.sourceSupplier != null) {
        spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
        sourceStage.sourceSupplier = null;
    }
    else {
        throw new IllegalStateException(MSG_CONSUMED);
    }

    if (isParallel() && sourceStage.sourceAnyStateful) {
        // Adapt the source spliterator, evaluating each stateful op
        // in the pipeline up to and including this pipeline stage.
        // The depth and flags of each pipeline stage are adjusted accordingly.
        int depth = 1;
        for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
             u != e;
             u = p, p = p.nextStage) {

            int thisOpFlags = p.sourceOrOpFlags;
            if (p.opIsStateful()) {
                depth = 0;

                if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
                    // Clear the short circuit flag for next pipeline stage
                    // This stage encapsulates short-circuiting, the next
                    // stage may not have any short-circuit operations, and
                    // if so spliterator.forEachRemaining should be used
                    // for traversal
                    thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
                }

                spliterator = p.opEvaluateParallelLazy(u, spliterator);

                // Inject or clear SIZED on the source pipeline stage
                // based on the stage's spliterator
                thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
                        ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
                        : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
            }
            p.depth = depth++;
            p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
        }
    }

    if (terminalFlags != 0)  {
        // Apply flags from the terminal operation to last pipeline stage
        combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
    }

    return spliterator;
}
 
Example 20
Source File: AbstractPipeline.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Get the source spliterator for this pipeline stage.  For a sequential or
 * stateless parallel pipeline, this is the source spliterator.  For a
 * stateful parallel pipeline, this is a spliterator describing the results
 * of all computations up to and including the most recent stateful
 * operation.
 */
@SuppressWarnings("unchecked")
private Spliterator<?> sourceSpliterator(int terminalFlags) {
    // Get the source spliterator of the pipeline
    Spliterator<?> spliterator = null;
    if (sourceStage.sourceSpliterator != null) {
        spliterator = sourceStage.sourceSpliterator;
        sourceStage.sourceSpliterator = null;
    }
    else if (sourceStage.sourceSupplier != null) {
        spliterator = (Spliterator<?>) sourceStage.sourceSupplier.get();
        sourceStage.sourceSupplier = null;
    }
    else {
        throw new IllegalStateException(MSG_CONSUMED);
    }

    if (isParallel() && sourceStage.sourceAnyStateful) {
        // Adapt the source spliterator, evaluating each stateful op
        // in the pipeline up to and including this pipeline stage.
        // The depth and flags of each pipeline stage are adjusted accordingly.
        int depth = 1;
        for (@SuppressWarnings("rawtypes") AbstractPipeline u = sourceStage, p = sourceStage.nextStage, e = this;
             u != e;
             u = p, p = p.nextStage) {

            int thisOpFlags = p.sourceOrOpFlags;
            if (p.opIsStateful()) {
                depth = 0;

                if (StreamOpFlag.SHORT_CIRCUIT.isKnown(thisOpFlags)) {
                    // Clear the short circuit flag for next pipeline stage
                    // This stage encapsulates short-circuiting, the next
                    // stage may not have any short-circuit operations, and
                    // if so spliterator.forEachRemaining should be used
                    // for traversal
                    thisOpFlags = thisOpFlags & ~StreamOpFlag.IS_SHORT_CIRCUIT;
                }

                spliterator = p.opEvaluateParallelLazy(u, spliterator);

                // Inject or clear SIZED on the source pipeline stage
                // based on the stage's spliterator
                thisOpFlags = spliterator.hasCharacteristics(Spliterator.SIZED)
                        ? (thisOpFlags & ~StreamOpFlag.NOT_SIZED) | StreamOpFlag.IS_SIZED
                        : (thisOpFlags & ~StreamOpFlag.IS_SIZED) | StreamOpFlag.NOT_SIZED;
            }
            p.depth = depth++;
            p.combinedFlags = StreamOpFlag.combineOpFlags(thisOpFlags, u.combinedFlags);
        }
    }

    if (terminalFlags != 0)  {
        // Apply flags from the terminal operation to last pipeline stage
        combinedFlags = StreamOpFlag.combineOpFlags(terminalFlags, combinedFlags);
    }

    return spliterator;
}