< prev index next >

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

Print this page
rev 47861 : 8181175: Stream.concat behaves like terminal operation
Reviewed-by: smarks, briangoetz, dfuchs
   1 /*
   2  * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1324      * generating constant streams, streams of random elements, etc.
1325      *
1326      * @param <T> the type of stream elements
1327      * @param s the {@code Supplier} of generated elements
1328      * @return a new infinite sequential unordered {@code Stream}
1329      */
1330     public static<T> Stream<T> generate(Supplier<? extends T> s) {
1331         Objects.requireNonNull(s);
1332         return StreamSupport.stream(
1333                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
1334     }
1335 
1336     /**
1337      * Creates a lazily concatenated stream whose elements are all the
1338      * elements of the first stream followed by all the elements of the
1339      * second stream.  The resulting stream is ordered if both
1340      * of the input streams are ordered, and parallel if either of the input
1341      * streams is parallel.  When the resulting stream is closed, the close
1342      * handlers for both input streams are invoked.
1343      *




1344      * @implNote
1345      * Use caution when constructing streams from repeated concatenation.
1346      * Accessing an element of a deeply concatenated stream can result in deep
1347      * call chains, or even {@code StackOverflowError}.
1348      *
1349      * <p>Subsequent changes to the sequential/parallel execution mode of the
1350      * returned stream are not guaranteed to be propagated to the input streams.
1351      *












1352      * @param <T> The type of stream elements
1353      * @param a the first stream
1354      * @param b the second stream
1355      * @return the concatenation of the two input streams
1356      */
1357     public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
1358         Objects.requireNonNull(a);
1359         Objects.requireNonNull(b);
1360 
1361         @SuppressWarnings("unchecked")
1362         Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
1363                 (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
1364         Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel());
1365         return stream.onClose(Streams.composedClose(a, b));
1366     }
1367 
1368     /**
1369      * A mutable builder for a {@code Stream}.  This allows the creation of a
1370      * {@code Stream} by generating elements individually and adding them to the
1371      * {@code Builder} (without the copying overhead that comes from using


   1 /*
   2  * Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


1324      * generating constant streams, streams of random elements, etc.
1325      *
1326      * @param <T> the type of stream elements
1327      * @param s the {@code Supplier} of generated elements
1328      * @return a new infinite sequential unordered {@code Stream}
1329      */
1330     public static<T> Stream<T> generate(Supplier<? extends T> s) {
1331         Objects.requireNonNull(s);
1332         return StreamSupport.stream(
1333                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
1334     }
1335 
1336     /**
1337      * Creates a lazily concatenated stream whose elements are all the
1338      * elements of the first stream followed by all the elements of the
1339      * second stream.  The resulting stream is ordered if both
1340      * of the input streams are ordered, and parallel if either of the input
1341      * streams is parallel.  When the resulting stream is closed, the close
1342      * handlers for both input streams are invoked.
1343      *
1344      * <p>This method operates on the two input streams and binds each stream
1345      * to its source.  As a result subsequent modifications to an input stream
1346      * source may not be reflected in the concatenated stream result.
1347      *
1348      * @implNote
1349      * Use caution when constructing streams from repeated concatenation.
1350      * Accessing an element of a deeply concatenated stream can result in deep
1351      * call chains, or even {@code StackOverflowError}.
1352      *
1353      * <p>Subsequent changes to the sequential/parallel execution mode of the
1354      * returned stream are not guaranteed to be propagated to the input streams.
1355      *
1356      * @apiNote
1357      * To preserve optimization opportunities this method binds each stream to
1358      * its source and accepts only two streams as parameters.  For example, the
1359      * exact size of the concatenated stream source can be computed if the exact
1360      * size of each input stream source is known.
1361      * To concatenate more streams without binding, or without nested calls to
1362      * this method, try creating a stream of streams and flat-mapping with the
1363      * identity function, for example:
1364      * <pre>{@code
1365      *     Stream<T> concat = Stream.of(s1, s2, s3, s4).flatMap(s -> s);
1366      * }</pre>
1367      *
1368      * @param <T> The type of stream elements
1369      * @param a the first stream
1370      * @param b the second stream
1371      * @return the concatenation of the two input streams
1372      */
1373     public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
1374         Objects.requireNonNull(a);
1375         Objects.requireNonNull(b);
1376 
1377         @SuppressWarnings("unchecked")
1378         Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
1379                 (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
1380         Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel());
1381         return stream.onClose(Streams.composedClose(a, b));
1382     }
1383 
1384     /**
1385      * A mutable builder for a {@code Stream}.  This allows the creation of a
1386      * {@code Stream} by generating elements individually and adding them to the
1387      * {@code Builder} (without the copying overhead that comes from using


< prev index next >