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

Print this page
rev 7962 : 8017513: Support for closeable streams
8022237: j.u.s.BaseStream.onClose() has an issue in implementation or requires spec clarification
8022572: Same exception instances thrown from j.u.stream.Stream.onClose() handlers are not listed as suppressed
Summary: BaseStream implements AutoCloseable; Remove CloseableStream and DelegatingStream
Reviewed-by:
Contributed-by: brian.goetz@oracle.com


 874     /**
 875      * Returns a sequential {@code Stream} where each element is
 876      * generated by a {@code Supplier}.  This is suitable for generating
 877      * constant streams, streams of random elements, etc.
 878      *
 879      * @param <T> the type of stream elements
 880      * @param s the {@code Supplier} of generated elements
 881      * @return a new sequential {@code Stream}
 882      */
 883     public static<T> Stream<T> generate(Supplier<T> s) {
 884         Objects.requireNonNull(s);
 885         return StreamSupport.stream(
 886                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
 887     }
 888 
 889     /**
 890      * Creates a lazy concatenated {@code Stream} whose elements are all the
 891      * elements of a first {@code Stream} succeeded by all the elements of the
 892      * second {@code Stream}. The resulting stream is ordered if both
 893      * of the input streams are ordered, and parallel if either of the input
 894      * streams is parallel.

 895      *
 896      * @param <T> The type of stream elements
 897      * @param a the first stream
 898      * @param b the second stream to concatenate on to end of the first
 899      *        stream
 900      * @return the concatenation of the two input streams
 901      */
 902     public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
 903         Objects.requireNonNull(a);
 904         Objects.requireNonNull(b);
 905 
 906         @SuppressWarnings("unchecked")
 907         Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
 908                 (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
 909         return StreamSupport.stream(split, a.isParallel() || b.isParallel());

 910     }
 911 
 912     /**
 913      * A mutable builder for a {@code Stream}.  This allows the creation of a
 914      * {@code Stream} by generating elements individually and adding them to the
 915      * {@code Builder} (without the copying overhead that comes from using
 916      * an {@code ArrayList} as a temporary buffer.)
 917      *
 918      * <p>A {@code Stream.Builder} has a lifecycle, where it starts in a building
 919      * phase, during which elements can be added, and then transitions to a built
 920      * phase, after which elements may not be added.  The built phase begins
 921      * when the {@link #build()} method is called, which creates an ordered
 922      * {@code Stream} whose elements are the elements that were added to the stream
 923      * builder, in the order they were added.
 924      *
 925      * @param <T> the type of stream elements
 926      * @see Stream#builder()
 927      * @since 1.8
 928      */
 929     public interface Builder<T> extends Consumer<T> {




 874     /**
 875      * Returns a sequential {@code Stream} where each element is
 876      * generated by a {@code Supplier}.  This is suitable for generating
 877      * constant streams, streams of random elements, etc.
 878      *
 879      * @param <T> the type of stream elements
 880      * @param s the {@code Supplier} of generated elements
 881      * @return a new sequential {@code Stream}
 882      */
 883     public static<T> Stream<T> generate(Supplier<T> s) {
 884         Objects.requireNonNull(s);
 885         return StreamSupport.stream(
 886                 new StreamSpliterators.InfiniteSupplyingSpliterator.OfRef<>(Long.MAX_VALUE, s), false);
 887     }
 888 
 889     /**
 890      * Creates a lazy concatenated {@code Stream} whose elements are all the
 891      * elements of a first {@code Stream} succeeded by all the elements of the
 892      * second {@code Stream}. The resulting stream is ordered if both
 893      * of the input streams are ordered, and parallel if either of the input
 894      * streams is parallel.  When the resulting stream is closed, the close
 895      * handlers for both input streams is invoked.
 896      *
 897      * @param <T> The type of stream elements
 898      * @param a the first stream
 899      * @param b the second stream to concatenate on to end of the first
 900      *        stream
 901      * @return the concatenation of the two input streams
 902      */
 903     public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b) {
 904         Objects.requireNonNull(a);
 905         Objects.requireNonNull(b);
 906 
 907         @SuppressWarnings("unchecked")
 908         Spliterator<T> split = new Streams.ConcatSpliterator.OfRef<>(
 909                 (Spliterator<T>) a.spliterator(), (Spliterator<T>) b.spliterator());
 910         Stream<T> stream = StreamSupport.stream(split, a.isParallel() || b.isParallel());
 911         return stream.onClose(Streams.composedClose(a, b));
 912     }
 913 
 914     /**
 915      * A mutable builder for a {@code Stream}.  This allows the creation of a
 916      * {@code Stream} by generating elements individually and adding them to the
 917      * {@code Builder} (without the copying overhead that comes from using
 918      * an {@code ArrayList} as a temporary buffer.)
 919      *
 920      * <p>A {@code Stream.Builder} has a lifecycle, where it starts in a building
 921      * phase, during which elements can be added, and then transitions to a built
 922      * phase, after which elements may not be added.  The built phase begins
 923      * when the {@link #build()} method is called, which creates an ordered
 924      * {@code Stream} whose elements are the elements that were added to the stream
 925      * builder, in the order they were added.
 926      *
 927      * @param <T> the type of stream elements
 928      * @see Stream#builder()
 929      * @since 1.8
 930      */
 931     public interface Builder<T> extends Consumer<T> {