1 /*
2 * Copyright (c) 2013, 2015, 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
880 public static LongStream iterate(final long seed, final LongUnaryOperator f) {
881 Objects.requireNonNull(f);
882 final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
883 long t = seed;
884
885 @Override
886 public boolean hasNext() {
887 return true;
888 }
889
890 @Override
891 public long nextLong() {
892 long v = t;
893 t = f.applyAsLong(t);
894 return v;
895 }
896 };
897 return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
898 iterator,
899 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
900 }
901
902 /**
903 * Returns an infinite sequential unordered stream where each element is
904 * generated by the provided {@code LongSupplier}. This is suitable for
905 * generating constant streams, streams of random elements, etc.
906 *
907 * @param s the {@code LongSupplier} for generated elements
908 * @return a new infinite sequential unordered {@code LongStream}
909 */
910 public static LongStream generate(LongSupplier s) {
911 Objects.requireNonNull(s);
912 return StreamSupport.longStream(
913 new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false);
914 }
915
916 /**
917 * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
918 * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
919 * {@code 1}.
|
1 /*
2 * Copyright (c) 2013, 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
880 public static LongStream iterate(final long seed, final LongUnaryOperator f) {
881 Objects.requireNonNull(f);
882 final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
883 long t = seed;
884
885 @Override
886 public boolean hasNext() {
887 return true;
888 }
889
890 @Override
891 public long nextLong() {
892 long v = t;
893 t = f.applyAsLong(t);
894 return v;
895 }
896 };
897 return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
898 iterator,
899 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
900 }
901
902 /**
903 * Returns a sequential ordered {@code LongStream} produced by iterative
904 * application of a function {@code f} to an initial element {@code seed},
905 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
906 * {@code f(f(seed))}, etc. The stream terminates when {@code predicate}
907 * returns false.
908 *
909 * <p>The first element (position {@code 0}) in the {@code LongStream} will be
910 * the provided {@code seed}. For {@code n > 0}, the element at position
911 * {@code n}, will be the result of applying the function {@code f} to the
912 * element at position {@code n - 1}.
913 *
914 * @param seed the initial element
915 * @param predicate a predicate to apply to elements to determine when the
916 * stream must terminate.
917 * @param f a function to be applied to the previous element to produce
918 * a new element
919 * @return a new sequential {@code LongStream}
920 * @since 9
921 */
922 public static LongStream iterate(long seed, LongPredicate predicate, LongUnaryOperator f) {
923 Objects.requireNonNull(f);
924 Objects.requireNonNull(predicate);
925 Spliterator.OfLong spliterator = new Spliterators.AbstractLongSpliterator(Long.MAX_VALUE,
926 Spliterator.ORDERED | Spliterator.IMMUTABLE) {
927 long prev;
928 boolean started, finished;
929
930 @Override
931 public boolean tryAdvance(LongConsumer action) {
932 Objects.requireNonNull(action);
933 if (finished)
934 return false;
935 long t;
936 if (started)
937 t = f.applyAsLong(prev);
938 else {
939 t = seed;
940 started = true;
941 }
942 if (!predicate.test(t)) {
943 finished = true;
944 return false;
945 }
946 action.accept(prev = t);
947 return true;
948 }
949
950 @Override
951 public void forEachRemaining(LongConsumer action) {
952 Objects.requireNonNull(action);
953 if (finished)
954 return;
955 long t = started ? f.applyAsLong(prev) : seed;
956 finished = true;
957 while (predicate.test(t)) {
958 action.accept(t);
959 t = f.applyAsLong(t);
960 }
961 }
962 };
963 return StreamSupport.longStream(spliterator, false);
964 }
965
966 /**
967 * Returns an infinite sequential unordered stream where each element is
968 * generated by the provided {@code LongSupplier}. This is suitable for
969 * generating constant streams, streams of random elements, etc.
970 *
971 * @param s the {@code LongSupplier} for generated elements
972 * @return a new infinite sequential unordered {@code LongStream}
973 */
974 public static LongStream generate(LongSupplier s) {
975 Objects.requireNonNull(s);
976 return StreamSupport.longStream(
977 new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false);
978 }
979
980 /**
981 * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
982 * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
983 * {@code 1}.
|