src/share/classes/java/util/stream/LongStream.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


 795             return empty();
 796         } else if (endInclusive - startInclusive + 1 <= 0) {
 797             // Size of range > Long.MAX_VALUE
 798             // Split the range in two and concatenate
 799             // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then
 800             // the lower range, [Long.MIN_VALUE, 0), and upper range,
 801             // [0, Long.MAX_VALUE], will both be further split in two
 802             long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
 803             return concat(range(startInclusive, m), rangeClosed(m, endInclusive));
 804         } else {
 805             return StreamSupport.longStream(
 806                     new Streams.RangeLongSpliterator(startInclusive, endInclusive, true), false);
 807         }
 808     }
 809 
 810     /**
 811      * Creates a lazy concatenated {@code LongStream} whose elements are all the
 812      * elements of a first {@code LongStream} succeeded by all the elements of the
 813      * second {@code LongStream}. The resulting stream is ordered if both
 814      * of the input streams are ordered, and parallel if either of the input
 815      * streams is parallel.

 816      *
 817      * @param a the first stream
 818      * @param b the second stream to concatenate on to end of the first stream
 819      * @return the concatenation of the two streams
 820      */
 821     public static LongStream concat(LongStream a, LongStream b) {
 822         Objects.requireNonNull(a);
 823         Objects.requireNonNull(b);
 824 
 825         Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong(
 826                 a.spliterator(), b.spliterator());
 827         return StreamSupport.longStream(split, a.isParallel() || b.isParallel());

 828     }
 829 
 830     /**
 831      * A mutable builder for a {@code LongStream}.
 832      *
 833      * <p>A stream builder has a lifecycle, where it starts in a building
 834      * phase, during which elements can be added, and then transitions to a
 835      * built phase, after which elements may not be added.  The built phase
 836      * begins when the {@link #build()} method is called, which creates an
 837      * ordered stream whose elements are the elements that were added to the
 838      * stream builder, in the order they were added.
 839      *
 840      * @see LongStream#builder()
 841      * @since 1.8
 842      */
 843     public interface Builder extends LongConsumer {
 844 
 845         /**
 846          * Adds an element to the stream being built.
 847          *




 795             return empty();
 796         } else if (endInclusive - startInclusive + 1 <= 0) {
 797             // Size of range > Long.MAX_VALUE
 798             // Split the range in two and concatenate
 799             // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then
 800             // the lower range, [Long.MIN_VALUE, 0), and upper range,
 801             // [0, Long.MAX_VALUE], will both be further split in two
 802             long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
 803             return concat(range(startInclusive, m), rangeClosed(m, endInclusive));
 804         } else {
 805             return StreamSupport.longStream(
 806                     new Streams.RangeLongSpliterator(startInclusive, endInclusive, true), false);
 807         }
 808     }
 809 
 810     /**
 811      * Creates a lazy concatenated {@code LongStream} whose elements are all the
 812      * elements of a first {@code LongStream} succeeded by all the elements of the
 813      * second {@code LongStream}. The resulting stream is ordered if both
 814      * of the input streams are ordered, and parallel if either of the input
 815      * streams is parallel.  When the resulting stream is closed, the close
 816      * handlers for both input streams is invoked.
 817      *
 818      * @param a the first stream
 819      * @param b the second stream to concatenate on to end of the first stream
 820      * @return the concatenation of the two streams
 821      */
 822     public static LongStream concat(LongStream a, LongStream b) {
 823         Objects.requireNonNull(a);
 824         Objects.requireNonNull(b);
 825 
 826         Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong(
 827                 a.spliterator(), b.spliterator());
 828         LongStream stream = StreamSupport.longStream(split, a.isParallel() || b.isParallel());
 829         return stream.onClose(Streams.composedClose(a, b));
 830     }
 831 
 832     /**
 833      * A mutable builder for a {@code LongStream}.
 834      *
 835      * <p>A stream builder has a lifecycle, where it starts in a building
 836      * phase, during which elements can be added, and then transitions to a
 837      * built phase, after which elements may not be added.  The built phase
 838      * begins when the {@link #build()} method is called, which creates an
 839      * ordered stream whose elements are the elements that were added to the
 840      * stream builder, in the order they were added.
 841      *
 842      * @see LongStream#builder()
 843      * @since 1.8
 844      */
 845     public interface Builder extends LongConsumer {
 846 
 847         /**
 848          * Adds an element to the stream being built.
 849          *