< prev index next >

src/java.base/share/classes/java/util/stream/DoubleStream.java

Print this page
rev 47861 : 8181175: Stream.concat behaves like terminal operation
Reviewed-by: smarks, briangoetz, dfuchs


1072      * generated by the provided {@code DoubleSupplier}.  This is suitable for
1073      * generating constant streams, streams of random elements, etc.
1074      *
1075      * @param s the {@code DoubleSupplier} for generated elements
1076      * @return a new infinite sequential unordered {@code DoubleStream}
1077      */
1078     public static DoubleStream generate(DoubleSupplier s) {
1079         Objects.requireNonNull(s);
1080         return StreamSupport.doubleStream(
1081                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfDouble(Long.MAX_VALUE, s), false);
1082     }
1083 
1084     /**
1085      * Creates a lazily concatenated stream whose elements are all the
1086      * elements of the first stream followed by all the elements of the
1087      * second stream.  The resulting stream is ordered if both
1088      * of the input streams are ordered, and parallel if either of the input
1089      * streams is parallel.  When the resulting stream is closed, the close
1090      * handlers for both input streams are invoked.
1091      *




1092      * @implNote
1093      * Use caution when constructing streams from repeated concatenation.
1094      * Accessing an element of a deeply concatenated stream can result in deep
1095      * call chains, or even {@code StackOverflowError}.
1096      *












1097      * @param a the first stream
1098      * @param b the second stream
1099      * @return the concatenation of the two input streams
1100      */
1101     public static DoubleStream concat(DoubleStream a, DoubleStream b) {
1102         Objects.requireNonNull(a);
1103         Objects.requireNonNull(b);
1104 
1105         Spliterator.OfDouble split = new Streams.ConcatSpliterator.OfDouble(
1106                 a.spliterator(), b.spliterator());
1107         DoubleStream stream = StreamSupport.doubleStream(split, a.isParallel() || b.isParallel());
1108         return stream.onClose(Streams.composedClose(a, b));
1109     }
1110 
1111     /**
1112      * A mutable builder for a {@code DoubleStream}.
1113      *
1114      * <p>A stream builder has a lifecycle, which starts in a building
1115      * phase, during which elements can be added, and then transitions to a built
1116      * phase, after which elements may not be added.  The built phase




1072      * generated by the provided {@code DoubleSupplier}.  This is suitable for
1073      * generating constant streams, streams of random elements, etc.
1074      *
1075      * @param s the {@code DoubleSupplier} for generated elements
1076      * @return a new infinite sequential unordered {@code DoubleStream}
1077      */
1078     public static DoubleStream generate(DoubleSupplier s) {
1079         Objects.requireNonNull(s);
1080         return StreamSupport.doubleStream(
1081                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfDouble(Long.MAX_VALUE, s), false);
1082     }
1083 
1084     /**
1085      * Creates a lazily concatenated stream whose elements are all the
1086      * elements of the first stream followed by all the elements of the
1087      * second stream.  The resulting stream is ordered if both
1088      * of the input streams are ordered, and parallel if either of the input
1089      * streams is parallel.  When the resulting stream is closed, the close
1090      * handlers for both input streams are invoked.
1091      *
1092      * <p>This method operates on the two input streams and binds each stream
1093      * to its source.  As a result subsequent modifications to an input stream
1094      * source may not be reflected in the concatenated stream result.
1095      *
1096      * @implNote
1097      * Use caution when constructing streams from repeated concatenation.
1098      * Accessing an element of a deeply concatenated stream can result in deep
1099      * call chains, or even {@code StackOverflowError}.
1100      *
1101      * @apiNote
1102      * To preserve optimization opportunities this method binds each stream to
1103      * its source and accepts only two streams as parameters.  For example, the
1104      * exact size of the concatenated stream source can be computed if the exact
1105      * size of each input stream source is known.
1106      * To concatenate more streams without binding, or without nested calls to
1107      * this method, try creating a stream of streams and flat-mapping with the
1108      * identity function, for example:
1109      * <pre>{@code
1110      *     DoubleStream concat = Stream.of(s1, s2, s3, s4).flatMapToDouble(s -> s);
1111      * }</pre>
1112      *
1113      * @param a the first stream
1114      * @param b the second stream
1115      * @return the concatenation of the two input streams
1116      */
1117     public static DoubleStream concat(DoubleStream a, DoubleStream b) {
1118         Objects.requireNonNull(a);
1119         Objects.requireNonNull(b);
1120 
1121         Spliterator.OfDouble split = new Streams.ConcatSpliterator.OfDouble(
1122                 a.spliterator(), b.spliterator());
1123         DoubleStream stream = StreamSupport.doubleStream(split, a.isParallel() || b.isParallel());
1124         return stream.onClose(Streams.composedClose(a, b));
1125     }
1126 
1127     /**
1128      * A mutable builder for a {@code DoubleStream}.
1129      *
1130      * <p>A stream builder has a lifecycle, which starts in a building
1131      * phase, during which elements can be added, and then transitions to a built
1132      * phase, after which elements may not be added.  The built phase


< prev index next >