jdk/src/java.base/share/classes/java/util/stream/IntStream.java
changeset 40806 46132d430504
parent 39060 d39deb27f118
equal deleted inserted replaced
40805:c58794ecca6c 40806:46132d430504
   560      * can be parallelized without requiring additional synchronization.
   560      * can be parallelized without requiring additional synchronization.
   561      *
   561      *
   562      * <p>This is a <a href="package-summary.html#StreamOps">terminal
   562      * <p>This is a <a href="package-summary.html#StreamOps">terminal
   563      * operation</a>.
   563      * operation</a>.
   564      *
   564      *
   565      * @param <R> type of the result
   565      * @param <R> the type of the mutable result container
   566      * @param supplier a function that creates a new result container. For a
   566      * @param supplier a function that creates a new mutable result container.
   567      *                 parallel execution, this function may be called
   567      *                 For a parallel execution, this function may be called
   568      *                 multiple times and must return a fresh value each time.
   568      *                 multiple times and must return a fresh value each time.
   569      * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
   569      * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
   570      *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
   570      *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
   571      *                    <a href="package-summary.html#Statelessness">stateless</a>
   571      *                    <a href="package-summary.html#Statelessness">stateless</a>
   572      *                    function for incorporating an additional element into a result
   572      *                    function that must fold an element into a result
       
   573      *                    container.
   573      * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
   574      * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
   574      *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
   575      *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
   575      *                    <a href="package-summary.html#Statelessness">stateless</a>
   576      *                    <a href="package-summary.html#Statelessness">stateless</a>
   576      *                    function for combining two values, which must be
   577      *                    function that accepts two partial result containers
   577      *                    compatible with the accumulator function
   578      *                    and merges them, which must be compatible with the
       
   579      *                    accumulator function.  The combiner function must fold
       
   580      *                    the elements from the second result container into the
       
   581      *                    first result container.
   578      * @return the result of the reduction
   582      * @return the result of the reduction
   579      * @see Stream#collect(Supplier, BiConsumer, BiConsumer)
   583      * @see Stream#collect(Supplier, BiConsumer, BiConsumer)
   580      */
   584      */
   581     <R> R collect(Supplier<R> supplier,
   585     <R> R collect(Supplier<R> supplier,
   582                   ObjIntConsumer<R> accumulator,
   586                   ObjIntConsumer<R> accumulator,
   884      *
   888      *
   885      * <p>The first element (position {@code 0}) in the {@code IntStream} will be
   889      * <p>The first element (position {@code 0}) in the {@code IntStream} will be
   886      * the provided {@code seed}.  For {@code n > 0}, the element at position
   890      * the provided {@code seed}.  For {@code n > 0}, the element at position
   887      * {@code n}, will be the result of applying the function {@code f} to the
   891      * {@code n}, will be the result of applying the function {@code f} to the
   888      * element at position {@code n - 1}.
   892      * element at position {@code n - 1}.
       
   893      *
       
   894      * <p>The action of applying {@code f} for one element
       
   895      * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
       
   896      * the action of applying {@code f} for subsequent elements.  For any given
       
   897      * element the action may be performed in whatever thread the library
       
   898      * chooses.
   889      *
   899      *
   890      * @param seed the initial element
   900      * @param seed the initial element
   891      * @param f a function to be applied to the previous element to produce
   901      * @param f a function to be applied to the previous element to produce
   892      *          a new element
   902      *          a new element
   893      * @return a new sequential {@code IntStream}
   903      * @return a new sequential {@code IntStream}
   916         return StreamSupport.intStream(spliterator, false);
   926         return StreamSupport.intStream(spliterator, false);
   917     }
   927     }
   918 
   928 
   919     /**
   929     /**
   920      * Returns a sequential ordered {@code IntStream} produced by iterative
   930      * Returns a sequential ordered {@code IntStream} produced by iterative
   921      * application of a function to an initial element, conditioned on
   931      * application of the given {@code next} function to an initial element,
   922      * satisfying the supplied predicate.  The stream terminates as soon as
   932      * conditioned on satisfying the given {@code hasNext} predicate.  The
   923      * the predicate returns false.
   933      * stream terminates as soon as the {@code hasNext} predicate returns false.
   924      *
   934      *
   925      * <p>
   935      * <p>{@code IntStream.iterate} should produce the same sequence of elements as
   926      * {@code IntStream.iterate} should produce the same sequence of elements
   936      * produced by the corresponding for-loop:
   927      * as produced by the corresponding for-loop:
   937      * <pre>{@code
   928      * <pre>{@code
   938      *     for (int index=seed; hasNext.test(index); index = next.applyAsInt(index)) {
   929      *     for (int index=seed; predicate.test(index); index = f.applyAsInt(index)) {
       
   930      *         ...
   939      *         ...
   931      *     }
   940      *     }
   932      * }</pre>
   941      * }</pre>
   933      *
   942      *
   934      * <p>
   943      * <p>The resulting sequence may be empty if the {@code hasNext} predicate
   935      * The resulting sequence may be empty if the predicate does not hold on
   944      * does not hold on the seed value.  Otherwise the first element will be the
   936      * the seed value.  Otherwise the first element will be the supplied seed
   945      * supplied {@code seed} value, the next element (if present) will be the
   937      * value, the next element (if present) will be the result of applying the
   946      * result of applying the {@code next} function to the {@code seed} value,
   938      * function f to the seed value, and so on iteratively until the predicate
   947      * and so on iteratively until the {@code hasNext} predicate indicates that
   939      * indicates that the stream should terminate.
   948      * the stream should terminate.
       
   949      *
       
   950      * <p>The action of applying the {@code hasNext} predicate to an element
       
   951      * <a href="../concurrent/package-summary.html#MemoryVisibility"><i>happens-before</i></a>
       
   952      * the action of applying the {@code next} function to that element.  The
       
   953      * action of applying the {@code next} function for one element
       
   954      * <i>happens-before</i> the action of applying the {@code hasNext}
       
   955      * predicate for subsequent elements.  For any given element an action may
       
   956      * be performed in whatever thread the library chooses.
   940      *
   957      *
   941      * @param seed the initial element
   958      * @param seed the initial element
   942      * @param predicate a predicate to apply to elements to determine when the
   959      * @param hasNext a predicate to apply to elements to determine when the
   943      *          stream must terminate.
   960      *                stream must terminate.
   944      * @param f a function to be applied to the previous element to produce
   961      * @param next a function to be applied to the previous element to produce
   945      *          a new element
   962      *             a new element
   946      * @return a new sequential {@code IntStream}
   963      * @return a new sequential {@code IntStream}
   947      * @since 9
   964      * @since 9
   948      */
   965      */
   949     public static IntStream iterate(int seed, IntPredicate predicate, IntUnaryOperator f) {
   966     public static IntStream iterate(int seed, IntPredicate hasNext, IntUnaryOperator next) {
   950         Objects.requireNonNull(f);
   967         Objects.requireNonNull(next);
   951         Objects.requireNonNull(predicate);
   968         Objects.requireNonNull(hasNext);
   952         Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
   969         Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
   953                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
   970                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
   954             int prev;
   971             int prev;
   955             boolean started, finished;
   972             boolean started, finished;
   956 
   973 
   959                 Objects.requireNonNull(action);
   976                 Objects.requireNonNull(action);
   960                 if (finished)
   977                 if (finished)
   961                     return false;
   978                     return false;
   962                 int t;
   979                 int t;
   963                 if (started)
   980                 if (started)
   964                     t = f.applyAsInt(prev);
   981                     t = next.applyAsInt(prev);
   965                 else {
   982                 else {
   966                     t = seed;
   983                     t = seed;
   967                     started = true;
   984                     started = true;
   968                 }
   985                 }
   969                 if (!predicate.test(t)) {
   986                 if (!hasNext.test(t)) {
   970                     finished = true;
   987                     finished = true;
   971                     return false;
   988                     return false;
   972                 }
   989                 }
   973                 action.accept(prev = t);
   990                 action.accept(prev = t);
   974                 return true;
   991                 return true;
   978             public void forEachRemaining(IntConsumer action) {
   995             public void forEachRemaining(IntConsumer action) {
   979                 Objects.requireNonNull(action);
   996                 Objects.requireNonNull(action);
   980                 if (finished)
   997                 if (finished)
   981                     return;
   998                     return;
   982                 finished = true;
   999                 finished = true;
   983                 int t = started ? f.applyAsInt(prev) : seed;
  1000                 int t = started ? next.applyAsInt(prev) : seed;
   984                 while (predicate.test(t)) {
  1001                 while (hasNext.test(t)) {
   985                     action.accept(t);
  1002                     action.accept(t);
   986                     t = f.applyAsInt(t);
  1003                     t = next.applyAsInt(t);
   987                 }
  1004                 }
   988             }
  1005             }
   989         };
  1006         };
   990         return StreamSupport.intStream(spliterator, false);
  1007         return StreamSupport.intStream(spliterator, false);
   991     }
  1008     }