< prev index next >

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

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


1064      * @return a sequential {@code IntStream} for the range of {@code int}
1065      *         elements
1066      */
1067     public static IntStream rangeClosed(int startInclusive, int endInclusive) {
1068         if (startInclusive > endInclusive) {
1069             return empty();
1070         } else {
1071             return StreamSupport.intStream(
1072                     new Streams.RangeIntSpliterator(startInclusive, endInclusive, true), false);
1073         }
1074     }
1075 
1076     /**
1077      * Creates a lazily concatenated stream whose elements are all the
1078      * elements of the first stream followed by all the elements of the
1079      * second stream.  The resulting stream is ordered if both
1080      * of the input streams are ordered, and parallel if either of the input
1081      * streams is parallel.  When the resulting stream is closed, the close
1082      * handlers for both input streams are invoked.
1083      *




1084      * @implNote
1085      * Use caution when constructing streams from repeated concatenation.
1086      * Accessing an element of a deeply concatenated stream can result in deep
1087      * call chains, or even {@code StackOverflowError}.
1088      *












1089      * @param a the first stream
1090      * @param b the second stream
1091      * @return the concatenation of the two input streams
1092      */
1093     public static IntStream concat(IntStream a, IntStream b) {
1094         Objects.requireNonNull(a);
1095         Objects.requireNonNull(b);
1096 
1097         Spliterator.OfInt split = new Streams.ConcatSpliterator.OfInt(
1098                 a.spliterator(), b.spliterator());
1099         IntStream stream = StreamSupport.intStream(split, a.isParallel() || b.isParallel());
1100         return stream.onClose(Streams.composedClose(a, b));
1101     }
1102 
1103     /**
1104      * A mutable builder for an {@code IntStream}.
1105      *
1106      * <p>A stream builder has a lifecycle, which starts in a building
1107      * phase, during which elements can be added, and then transitions to a built
1108      * phase, after which elements may not be added.  The built phase




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


< prev index next >