1 /*
2 * Copyright (c) 2012, 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
868 * @return the new stream
869 */
870 public static IntStream of(int... values) {
871 return Arrays.stream(values);
872 }
873
874 /**
875 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
876 * application of a function {@code f} to an initial element {@code seed},
877 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
878 * {@code f(f(seed))}, etc.
879 *
880 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
881 * the provided {@code seed}. For {@code n > 0}, the element at position
882 * {@code n}, will be the result of applying the function {@code f} to the
883 * element at position {@code n - 1}.
884 *
885 * @param seed the initial element
886 * @param f a function to be applied to the previous element to produce
887 * a new element
888 * @return A new sequential {@code IntStream}
889 */
890 public static IntStream iterate(final int seed, final IntUnaryOperator f) {
891 Objects.requireNonNull(f);
892 final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
893 int t = seed;
894
895 @Override
896 public boolean hasNext() {
897 return true;
898 }
899
900 @Override
901 public int nextInt() {
902 int v = t;
903 t = f.applyAsInt(t);
904 return v;
905 }
906 };
907 return StreamSupport.intStream(Spliterators.spliteratorUnknownSize(
908 iterator,
909 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
910 }
911
912 /**
913 * Returns an infinite sequential unordered stream where each element is
914 * generated by the provided {@code IntSupplier}. This is suitable for
915 * generating constant streams, streams of random elements, etc.
916 *
917 * @param s the {@code IntSupplier} for generated elements
918 * @return a new infinite sequential unordered {@code IntStream}
919 */
920 public static IntStream generate(IntSupplier s) {
921 Objects.requireNonNull(s);
922 return StreamSupport.intStream(
923 new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false);
924 }
925
926 /**
927 * Returns a sequential ordered {@code IntStream} from {@code startInclusive}
928 * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
929 * {@code 1}.
|
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
868 * @return the new stream
869 */
870 public static IntStream of(int... values) {
871 return Arrays.stream(values);
872 }
873
874 /**
875 * Returns an infinite sequential ordered {@code IntStream} produced by iterative
876 * application of a function {@code f} to an initial element {@code seed},
877 * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
878 * {@code f(f(seed))}, etc.
879 *
880 * <p>The first element (position {@code 0}) in the {@code IntStream} will be
881 * the provided {@code seed}. For {@code n > 0}, the element at position
882 * {@code n}, will be the result of applying the function {@code f} to the
883 * element at position {@code n - 1}.
884 *
885 * @param seed the initial element
886 * @param f a function to be applied to the previous element to produce
887 * a new element
888 * @return a new sequential {@code IntStream}
889 */
890 public static IntStream iterate(final int seed, final IntUnaryOperator f) {
891 Objects.requireNonNull(f);
892 Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
893 Spliterator.ORDERED | Spliterator.IMMUTABLE) {
894 int prev;
895 boolean started;
896
897 @Override
898 public boolean tryAdvance(IntConsumer action) {
899 Objects.requireNonNull(action);
900 int t;
901 if (started)
902 t = f.applyAsInt(prev);
903 else {
904 t = seed;
905 started = true;
906 }
907 action.accept(prev = t);
908 return true;
909 }
910 };
911 return StreamSupport.intStream(spliterator, false);
912 }
913
914 /**
915 * Returns a sequential ordered {@code IntStream} produced by iterative
916 * application of a function to an initial element, conditioned on
917 * satisfying the supplied predicate. The stream terminates as soon as
918 * the predicate function returns false.
919 *
920 * <p>
921 * {@code IntStream.iterate} should produce the same sequence of elements
922 * as produced by the corresponding for-loop:
923 * <pre>{@code
924 * for (int index=seed; predicate.test(index); index = f.apply(index)) {
925 * ...
926 * }
927 * }</pre>
928 *
929 * <p>
930 * The resulting sequence may be empty if the predicate does not hold on
931 * the seed value. Otherwise the first element will be the supplied seed
932 * value, the next element (if present) will be the result of applying the
933 * function f to the seed value, and so on iteratively until the predicate
934 * indicates that the stream should terminate.
935 *
936 * @param seed the initial element
937 * @param predicate a predicate to apply to elements to determine when the
938 * stream must terminate.
939 * @param f a function to be applied to the previous element to produce
940 * a new element
941 * @return a new sequential {@code IntStream}
942 * @since 9
943 */
944 public static IntStream iterate(int seed, IntPredicate predicate, IntUnaryOperator f) {
945 Objects.requireNonNull(f);
946 Objects.requireNonNull(predicate);
947 Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
948 Spliterator.ORDERED | Spliterator.IMMUTABLE) {
949 int prev;
950 boolean started, finished;
951
952 @Override
953 public boolean tryAdvance(IntConsumer action) {
954 Objects.requireNonNull(action);
955 if (finished)
956 return false;
957 int t;
958 if (started)
959 t = f.applyAsInt(prev);
960 else {
961 t = seed;
962 started = true;
963 }
964 if (!predicate.test(t)) {
965 finished = true;
966 return false;
967 }
968 action.accept(prev = t);
969 return true;
970 }
971
972 @Override
973 public void forEachRemaining(IntConsumer action) {
974 Objects.requireNonNull(action);
975 if (finished)
976 return;
977 int t = started ? f.applyAsInt(prev) : seed;
978 finished = true;
979 while (predicate.test(t)) {
980 action.accept(t);
981 t = f.applyAsInt(t);
982 }
983 }
984 };
985 return StreamSupport.intStream(spliterator, false);
986 }
987
988 /**
989 * Returns an infinite sequential unordered stream where each element is
990 * generated by the provided {@code IntSupplier}. This is suitable for
991 * generating constant streams, streams of random elements, etc.
992 *
993 * @param s the {@code IntSupplier} for generated elements
994 * @return a new infinite sequential unordered {@code IntStream}
995 */
996 public static IntStream generate(IntSupplier s) {
997 Objects.requireNonNull(s);
998 return StreamSupport.intStream(
999 new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false);
1000 }
1001
1002 /**
1003 * Returns a sequential ordered {@code IntStream} from {@code startInclusive}
1004 * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
1005 * {@code 1}.
|