748 * @apiNote
749 * <p>An equivalent sequence of increasing values can be produced
750 * sequentially using a {@code for} loop as follows:
751 * <pre>{@code
752 * for (long i = startInclusive; i < endExclusive ; i++) { ... }
753 * }</pre>
754 *
755 * @param startInclusive the (inclusive) initial value
756 * @param endExclusive the exclusive upper bound
757 * @return a sequential {@code LongStream} for the range of {@code long}
758 * elements
759 */
760 public static LongStream range(long startInclusive, final long endExclusive) {
761 if (startInclusive >= endExclusive) {
762 return empty();
763 } else if (endExclusive - startInclusive < 0) {
764 // Size of range > Long.MAX_VALUE
765 // Split the range in two and concatenate
766 // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then
767 // the lower range, [Long.MIN_VALUE, 0) will be further split in two
768 // long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1;
769 // return Streams.concat(range(startInclusive, m), range(m, endExclusive));
770 // This is temporary until Streams.concat is supported
771 throw new UnsupportedOperationException();
772 } else {
773 return StreamSupport.longStream(
774 new Streams.RangeLongSpliterator(startInclusive, endExclusive, false));
775 }
776 }
777
778 /**
779 * Returns a sequential {@code LongStream} from {@code startInclusive}
780 * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of
781 * {@code 1}.
782 *
783 * @apiNote
784 * <p>An equivalent sequence of increasing values can be produced
785 * sequentially using a {@code for} loop as follows:
786 * <pre>{@code
787 * for (long i = startInclusive; i <= endInclusive ; i++) { ... }
788 * }</pre>
789 *
790 * @param startInclusive the (inclusive) initial value
791 * @param endInclusive the inclusive upper bound
792 * @return a sequential {@code LongStream} for the range of {@code long}
793 * elements
794 */
795 public static LongStream rangeClosed(long startInclusive, final long endInclusive) {
796 if (startInclusive > endInclusive) {
797 return empty();
798 } else if (endInclusive - startInclusive + 1 <= 0) {
799 // Size of range > Long.MAX_VALUE
800 // Split the range in two and concatenate
801 // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then
802 // the lower range, [Long.MIN_VALUE, 0), and upper range,
803 // [0, Long.MAX_VALUE], will both be further split in two
804 // long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
805 // return Streams.concat(range(startInclusive, m), rangeClosed(m, endInclusive));
806 // This is temporary until Streams.concat is supported
807 throw new UnsupportedOperationException();
808 } else {
809 return StreamSupport.longStream(
810 new Streams.RangeLongSpliterator(startInclusive, endInclusive, true));
811 }
812 }
813 }
|
748 * @apiNote
749 * <p>An equivalent sequence of increasing values can be produced
750 * sequentially using a {@code for} loop as follows:
751 * <pre>{@code
752 * for (long i = startInclusive; i < endExclusive ; i++) { ... }
753 * }</pre>
754 *
755 * @param startInclusive the (inclusive) initial value
756 * @param endExclusive the exclusive upper bound
757 * @return a sequential {@code LongStream} for the range of {@code long}
758 * elements
759 */
760 public static LongStream range(long startInclusive, final long endExclusive) {
761 if (startInclusive >= endExclusive) {
762 return empty();
763 } else if (endExclusive - startInclusive < 0) {
764 // Size of range > Long.MAX_VALUE
765 // Split the range in two and concatenate
766 // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then
767 // the lower range, [Long.MIN_VALUE, 0) will be further split in two
768 long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1;
769 return concat(range(startInclusive, m), range(m, endExclusive));
770 } else {
771 return StreamSupport.longStream(
772 new Streams.RangeLongSpliterator(startInclusive, endExclusive, false));
773 }
774 }
775
776 /**
777 * Returns a sequential {@code LongStream} from {@code startInclusive}
778 * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of
779 * {@code 1}.
780 *
781 * @apiNote
782 * <p>An equivalent sequence of increasing values can be produced
783 * sequentially using a {@code for} loop as follows:
784 * <pre>{@code
785 * for (long i = startInclusive; i <= endInclusive ; i++) { ... }
786 * }</pre>
787 *
788 * @param startInclusive the (inclusive) initial value
789 * @param endInclusive the inclusive upper bound
790 * @return a sequential {@code LongStream} for the range of {@code long}
791 * elements
792 */
793 public static LongStream rangeClosed(long startInclusive, final long endInclusive) {
794 if (startInclusive > endInclusive) {
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));
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 (a.isParallel() || b.isParallel())
828 ? StreamSupport.longParallelStream(split)
829 : StreamSupport.longStream(split);
830 }
831 }
|