< prev index next >
src/java.base/share/classes/java/util/stream/IntStream.java
Print this page
@@ -1,7 +1,7 @@
/*
- * Copyright (c) 2012, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -883,11 +883,11 @@
* element at position {@code n - 1}.
*
* @param seed the initial element
* @param f a function to be applied to the previous element to produce
* a new element
- * @return A new sequential {@code IntStream}
+ * @return a new sequential {@code IntStream}
*/
public static IntStream iterate(final int seed, final IntUnaryOperator f) {
Objects.requireNonNull(f);
final PrimitiveIterator.OfInt iterator = new PrimitiveIterator.OfInt() {
int t = seed;
@@ -908,10 +908,74 @@
iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
}
/**
+ * Returns a sequential ordered {@code IntStream} produced by iterative
+ * application of a function {@code f} to an initial element {@code seed},
+ * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
+ * {@code f(f(seed))}, etc. The stream terminates when {@code predicate}
+ * returns false.
+ *
+ * <p>The first element (position {@code 0}) in the {@code IntStream} will be
+ * the provided {@code seed}. For {@code n > 0}, the element at position
+ * {@code n}, will be the result of applying the function {@code f} to the
+ * element at position {@code n - 1}.
+ *
+ * @param seed the initial element
+ * @param predicate a predicate to apply to elements to determine when the
+ * stream must terminate.
+ * @param f a function to be applied to the previous element to produce
+ * a new element
+ * @return a new sequential {@code IntStream}
+ * @since 9
+ */
+ public static IntStream iterate(int seed, IntPredicate predicate, IntUnaryOperator f) {
+ Objects.requireNonNull(f);
+ Objects.requireNonNull(predicate);
+ Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
+ Spliterator.ORDERED | Spliterator.IMMUTABLE) {
+ int prev;
+ boolean started, finished;
+
+ @Override
+ public boolean tryAdvance(IntConsumer action) {
+ Objects.requireNonNull(action);
+ if (finished)
+ return false;
+ int t;
+ if (started)
+ t = f.applyAsInt(prev);
+ else {
+ t = seed;
+ started = true;
+ }
+ if (!predicate.test(t)) {
+ finished = true;
+ return false;
+ }
+ action.accept(prev = t);
+ return true;
+ }
+
+ @Override
+ public void forEachRemaining(IntConsumer action) {
+ Objects.requireNonNull(action);
+ if (finished)
+ return;
+ int t = started ? f.applyAsInt(prev) : seed;
+ finished = true;
+ while (predicate.test(t)) {
+ action.accept(t);
+ t = f.applyAsInt(t);
+ }
+ }
+ };
+ return StreamSupport.intStream(spliterator, false);
+ }
+
+ /**
* Returns an infinite sequential unordered stream where each element is
* generated by the provided {@code IntSupplier}. This is suitable for
* generating constant streams, streams of random elements, etc.
*
* @param s the {@code IntSupplier} for generated elements
< prev index next >