jdk/src/java.base/share/classes/java/util/stream/IntStream.java
author psandoz
Fri, 20 May 2016 11:47:39 +0200
changeset 38442 387a4457880c
parent 36223 de2976b3614c
child 38444 a5cdecb7d181
permissions -rw-r--r--
8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline Reviewed-by: briangoetz, redestad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     1
/*
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
     2
 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     4
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    10
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    16
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    20
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    23
 * questions.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    24
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    25
package java.util.stream;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    26
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    27
import java.util.Arrays;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    28
import java.util.IntSummaryStatistics;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    29
import java.util.Objects;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    30
import java.util.OptionalDouble;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    31
import java.util.OptionalInt;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    32
import java.util.PrimitiveIterator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    33
import java.util.Spliterator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    34
import java.util.Spliterators;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    35
import java.util.function.BiConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    36
import java.util.function.Function;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    37
import java.util.function.IntBinaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    38
import java.util.function.IntConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    39
import java.util.function.IntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
import java.util.function.IntPredicate;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    41
import java.util.function.IntSupplier;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    42
import java.util.function.IntToDoubleFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    43
import java.util.function.IntToLongFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    44
import java.util.function.IntUnaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    45
import java.util.function.ObjIntConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    46
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    47
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    48
/**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    49
 * A sequence of primitive int-valued elements supporting sequential and parallel
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    50
 * aggregate operations.  This is the {@code int} primitive specialization of
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    51
 * {@link Stream}.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    52
 *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    53
 * <p>The following example illustrates an aggregate operation using
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    54
 * {@link Stream} and {@link IntStream}, computing the sum of the weights of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    55
 * red widgets:
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    56
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    57
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    58
 *     int sum = widgets.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    59
 *                      .filter(w -> w.getColor() == RED)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    60
 *                      .mapToInt(w -> w.getWeight())
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    61
 *                      .sum();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    62
 * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    63
 *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    64
 * See the class documentation for {@link Stream} and the package documentation
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    65
 * for <a href="package-summary.html">java.util.stream</a> for additional
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    66
 * specification of streams, stream operations, stream pipelines, and
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    67
 * parallelism.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    68
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    69
 * @since 1.8
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    70
 * @see Stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    71
 * @see <a href="package-summary.html">java.util.stream</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    72
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    73
public interface IntStream extends BaseStream<Integer, IntStream> {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    74
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    75
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    76
     * Returns a stream consisting of the elements of this stream that match
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    77
     * the given predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    78
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    79
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    80
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    81
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    82
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    83
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    84
     *                  predicate to apply to each element to determine if it
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    85
     *                  should be included
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    86
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    87
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    88
    IntStream filter(IntPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    89
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    90
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    91
     * Returns a stream consisting of the results of applying the given
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    92
     * function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    93
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    94
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    95
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    96
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    97
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    98
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
    99
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   100
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   101
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   102
    IntStream map(IntUnaryOperator mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   103
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   104
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   105
     * Returns an object-valued {@code Stream} consisting of the results of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   106
     * applying the given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   107
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   108
     * <p>This is an <a href="package-summary.html#StreamOps">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   109
     *     intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   110
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   111
     * @param <U> the element type of the new stream
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   112
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   113
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   114
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   115
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   116
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   117
    <U> Stream<U> mapToObj(IntFunction<? extends U> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   118
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   119
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   120
     * Returns a {@code LongStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   121
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   122
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   123
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   124
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   125
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   126
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   127
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   128
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   129
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   130
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   131
    LongStream mapToLong(IntToLongFunction mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   132
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   133
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   134
     * Returns a {@code DoubleStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   135
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   136
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   137
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   138
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   139
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   140
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   141
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   142
     *               function to apply to each element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   143
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   144
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   145
    DoubleStream mapToDouble(IntToDoubleFunction mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   146
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   147
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   148
     * Returns a stream consisting of the results of replacing each element of
22352
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   149
     * this stream with the contents of a mapped stream produced by applying
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   150
     * the provided mapping function to each element.  Each mapped stream is
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   151
     * {@link java.util.stream.BaseStream#close() closed} after its contents
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   152
     * have been placed into this stream.  (If a mapped stream is {@code null}
ecbf37860ffa 8032190: It's unclear that flatMap will ensure each stream will be closed.
psandoz
parents: 21846
diff changeset
   153
     * an empty stream is used, instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   154
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   155
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   156
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   157
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   158
     * @param mapper a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   159
     *               <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   160
     *               function to apply to each element which produces an
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   161
     *               {@code IntStream} of new values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   162
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   163
     * @see Stream#flatMap(Function)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   164
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   165
    IntStream flatMap(IntFunction<? extends IntStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   166
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
     * Returns a stream consisting of the distinct elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   169
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   171
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   172
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   173
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   174
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   175
    IntStream distinct();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   176
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   177
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   178
     * Returns a stream consisting of the elements of this stream in sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   179
     * order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   180
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   181
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   183
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   184
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   185
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   186
    IntStream sorted();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   187
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   188
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   189
     * Returns a stream consisting of the elements of this stream, additionally
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   190
     * performing the provided action on each element as elements are consumed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
     * from the resulting stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   192
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   193
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   194
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   195
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   196
     * <p>For parallel stream pipelines, the action may be called at
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   197
     * whatever time and in whatever thread the element is made available by the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   198
     * upstream operation.  If the action modifies shared state,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   199
     * it is responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   201
     * @apiNote This method exists mainly to support debugging, where you want
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   202
     * to see the elements as they flow past a certain point in a pipeline:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   203
     * <pre>{@code
21846
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   204
     *     IntStream.of(1, 2, 3, 4)
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   205
     *         .filter(e -> e > 2)
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   206
     *         .peek(e -> System.out.println("Filtered value: " + e))
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   207
     *         .map(e -> e * e)
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   208
     *         .peek(e -> System.out.println("Mapped value: " + e))
c10feb34bc0b 8028516: Java doc error in Int/Long/Double/Stream.peek
psandoz
parents: 21339
diff changeset
   209
     *         .sum();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   210
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   211
     *
38442
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   212
     * <p>In cases where stream implementation is able to optimize away the
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   213
     * production of some or all the elements (such as with short-circuiting
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   214
     * operations like {@code findFirst}, or in the example described in
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   215
     * {@link #count}), the action will not be invoked for those elements.
387a4457880c 8130023: API java.util.stream: explicitly specify guaranteed execution of the pipeline
psandoz
parents: 36223
diff changeset
   216
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   217
     * @param action a <a href="package-summary.html#NonInterference">
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   218
     *               non-interfering</a> action to perform on the elements as
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   219
     *               they are consumed from the stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   220
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   221
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   222
    IntStream peek(IntConsumer action);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   223
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   224
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   225
     * Returns a stream consisting of the elements of this stream, truncated
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   226
     * to be no longer than {@code maxSize} in length.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   227
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   228
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   229
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   230
     *
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   231
     * @apiNote
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   232
     * While {@code limit()} is generally a cheap operation on sequential
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   233
     * stream pipelines, it can be quite expensive on ordered parallel pipelines,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   234
     * especially for large values of {@code maxSize}, since {@code limit(n)}
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   235
     * is constrained to return not just any <em>n</em> elements, but the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   236
     * <em>first n</em> elements in the encounter order.  Using an unordered
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   237
     * stream source (such as {@link #generate(IntSupplier)}) or removing the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   238
     * ordering constraint with {@link #unordered()} may result in significant
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   239
     * speedups of {@code limit()} in parallel pipelines, if the semantics of
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   240
     * your situation permit.  If consistency with encounter order is required,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   241
     * and you are experiencing poor performance or memory utilization with
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   242
     * {@code limit()} in parallel pipelines, switching to sequential execution
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   243
     * with {@link #sequential()} may improve performance.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   244
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   245
     * @param maxSize the number of elements the stream should be limited to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   246
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   247
     * @throws IllegalArgumentException if {@code maxSize} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   248
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   249
    IntStream limit(long maxSize);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   250
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   251
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   252
     * Returns a stream consisting of the remaining elements of this stream
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   253
     * after discarding the first {@code n} elements of the stream.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   254
     * If this stream contains fewer than {@code n} elements then an
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   255
     * empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   256
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   257
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   258
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   259
     *
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   260
     * @apiNote
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   261
     * While {@code skip()} is generally a cheap operation on sequential
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   262
     * stream pipelines, it can be quite expensive on ordered parallel pipelines,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   263
     * especially for large values of {@code n}, since {@code skip(n)}
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   264
     * is constrained to skip not just any <em>n</em> elements, but the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   265
     * <em>first n</em> elements in the encounter order.  Using an unordered
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   266
     * stream source (such as {@link #generate(IntSupplier)}) or removing the
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   267
     * ordering constraint with {@link #unordered()} may result in significant
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   268
     * speedups of {@code skip()} in parallel pipelines, if the semantics of
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   269
     * your situation permit.  If consistency with encounter order is required,
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   270
     * and you are experiencing poor performance or memory utilization with
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   271
     * {@code skip()} in parallel pipelines, switching to sequential execution
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   272
     * with {@link #sequential()} may improve performance.
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   273
     *
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   274
     * @param n the number of leading elements to skip
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   275
     * @return the new stream
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   276
     * @throws IllegalArgumentException if {@code n} is negative
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   277
     */
20866
36155ee613ef 8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents: 19850
diff changeset
   278
    IntStream skip(long n);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   279
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   280
    /**
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   281
     * Returns, if this stream is ordered, a stream consisting of the longest
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   282
     * prefix of elements taken from this stream that match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   283
     * Otherwise returns, if this stream is unordered, a stream consisting of a
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   284
     * subset of elements taken from this stream that match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   285
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   286
     * <p>If this stream is ordered then the longest prefix is a contiguous
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   287
     * sequence of elements of this stream that match the given predicate.  The
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   288
     * first element of the sequence is the first element of this stream, and
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   289
     * the element immediately following the last element of the sequence does
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   290
     * not match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   291
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   292
     * <p>If this stream is unordered, and some (but not all) elements of this
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   293
     * stream match the given predicate, then the behavior of this operation is
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   294
     * nondeterministic; it is free to take any subset of matching elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   295
     * (which includes the empty set).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   296
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   297
     * <p>Independent of whether this stream is ordered or unordered if all
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   298
     * elements of this stream match the given predicate then this operation
32002
e378c0dc767e 8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents: 31644
diff changeset
   299
     * takes all elements (the result is the same as the input), or if no
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   300
     * elements of the stream match the given predicate then no elements are
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   301
     * taken (the result is an empty stream).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   302
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   303
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   304
     * stateful intermediate operation</a>.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   305
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   306
     * @implSpec
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   307
     * The default implementation obtains the {@link #spliterator() spliterator}
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   308
     * of this stream, wraps that spliterator so as to support the semantics
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   309
     * of this operation on traversal, and returns a new stream associated with
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   310
     * the wrapped spliterator.  The returned stream preserves the execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   311
     * characteristics of this stream (namely parallel or sequential execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   312
     * as per {@link #isParallel()}) but the wrapped spliterator may choose to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   313
     * not support splitting.  When the returned stream is closed, the close
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   314
     * handlers for both the returned and this stream are invoked.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   315
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   316
     * @apiNote
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   317
     * While {@code takeWhile()} is generally a cheap operation on sequential
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   318
     * stream pipelines, it can be quite expensive on ordered parallel
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   319
     * pipelines, since the operation is constrained to return not just any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   320
     * valid prefix, but the longest prefix of elements in the encounter order.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   321
     * Using an unordered stream source (such as {@link #generate(IntSupplier)})
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   322
     * or removing the ordering constraint with {@link #unordered()} may result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   323
     * in significant speedups of {@code takeWhile()} in parallel pipelines, if
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   324
     * the semantics of your situation permit.  If consistency with encounter
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   325
     * order is required, and you are experiencing poor performance or memory
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   326
     * utilization with {@code takeWhile()} in parallel pipelines, switching to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   327
     * sequential execution with {@link #sequential()} may improve performance.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   328
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   329
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   330
     *                  <a href="package-summary.html#Statelessness">stateless</a>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   331
     *                  predicate to apply to elements to determine the longest
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   332
     *                  prefix of elements.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   333
     * @return the new stream
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32002
diff changeset
   334
     * @since 9
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   335
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   336
    default IntStream takeWhile(IntPredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   337
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   338
        // Reuses the unordered spliterator, which, when encounter is present,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   339
        // is safe to use as long as it configured not to split
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   340
        return StreamSupport.intStream(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   341
                new WhileOps.UnorderedWhileSpliterator.OfInt.Taking(spliterator(), true, predicate),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   342
                isParallel()).onClose(this::close);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   343
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   344
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   345
    /**
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   346
     * Returns, if this stream is ordered, a stream consisting of the remaining
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   347
     * elements of this stream after dropping the longest prefix of elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   348
     * that match the given predicate.  Otherwise returns, if this stream is
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   349
     * unordered, a stream consisting of the remaining elements of this stream
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   350
     * after dropping a subset of elements that match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   351
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   352
     * <p>If this stream is ordered then the longest prefix is a contiguous
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   353
     * sequence of elements of this stream that match the given predicate.  The
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   354
     * first element of the sequence is the first element of this stream, and
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   355
     * the element immediately following the last element of the sequence does
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   356
     * not match the given predicate.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   357
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   358
     * <p>If this stream is unordered, and some (but not all) elements of this
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   359
     * stream match the given predicate, then the behavior of this operation is
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   360
     * nondeterministic; it is free to drop any subset of matching elements
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   361
     * (which includes the empty set).
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   362
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   363
     * <p>Independent of whether this stream is ordered or unordered if all
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   364
     * elements of this stream match the given predicate then this operation
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   365
     * drops all elements (the result is an empty stream), or if no elements of
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   366
     * the stream match the given predicate then no elements are dropped (the
32002
e378c0dc767e 8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents: 31644
diff changeset
   367
     * result is the same as the input).
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   368
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   369
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   370
     * intermediate operation</a>.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   371
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   372
     * @implSpec
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   373
     * The default implementation obtains the {@link #spliterator() spliterator}
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   374
     * of this stream, wraps that spliterator so as to support the semantics
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   375
     * of this operation on traversal, and returns a new stream associated with
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   376
     * the wrapped spliterator.  The returned stream preserves the execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   377
     * characteristics of this stream (namely parallel or sequential execution
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   378
     * as per {@link #isParallel()}) but the wrapped spliterator may choose to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   379
     * not support splitting.  When the returned stream is closed, the close
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   380
     * handlers for both the returned and this stream are invoked.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   381
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   382
     * @apiNote
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   383
     * While {@code dropWhile()} is generally a cheap operation on sequential
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   384
     * stream pipelines, it can be quite expensive on ordered parallel
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   385
     * pipelines, since the operation is constrained to return not just any
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   386
     * valid prefix, but the longest prefix of elements in the encounter order.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   387
     * Using an unordered stream source (such as {@link #generate(IntSupplier)})
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   388
     * or removing the ordering constraint with {@link #unordered()} may result
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   389
     * in significant speedups of {@code dropWhile()} in parallel pipelines, if
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   390
     * the semantics of your situation permit.  If consistency with encounter
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   391
     * order is required, and you are experiencing poor performance or memory
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   392
     * utilization with {@code dropWhile()} in parallel pipelines, switching to
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   393
     * sequential execution with {@link #sequential()} may improve performance.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   394
     *
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   395
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   396
     *                  <a href="package-summary.html#Statelessness">stateless</a>
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   397
     *                  predicate to apply to elements to determine the longest
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   398
     *                  prefix of elements.
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   399
     * @return the new stream
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 32002
diff changeset
   400
     * @since 9
31644
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   401
     */
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   402
    default IntStream dropWhile(IntPredicate predicate) {
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   403
        Objects.requireNonNull(predicate);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   404
        // Reuses the unordered spliterator, which, when encounter is present,
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   405
        // is safe to use as long as it configured not to split
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   406
        return StreamSupport.intStream(
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   407
                new WhileOps.UnorderedWhileSpliterator.OfInt.Dropping(spliterator(), true, predicate),
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   408
                isParallel()).onClose(this::close);
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   409
    }
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   410
dbca10d053fd 8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents: 29489
diff changeset
   411
    /**
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   412
     * Performs an action for each element of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   413
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   414
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   415
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   416
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   417
     * <p>For parallel stream pipelines, this operation does <em>not</em>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   418
     * guarantee to respect the encounter order of the stream, as doing so
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   419
     * would sacrifice the benefit of parallelism.  For any given element, the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   420
     * action may be performed at whatever time and in whatever thread the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   421
     * library chooses.  If the action accesses shared state, it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   422
     * responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   423
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   424
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   425
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   426
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   427
    void forEach(IntConsumer action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   428
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   429
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   430
     * Performs an action for each element of this stream, guaranteeing that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   431
     * each element is processed in encounter order for streams that have a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   432
     * defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   433
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   434
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   435
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   436
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   437
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   438
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   439
     * @see #forEach(IntConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   440
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   441
    void forEachOrdered(IntConsumer action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   442
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   443
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   444
     * Returns an array containing the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   445
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   446
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   447
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   448
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   449
     * @return an array containing the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   450
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   451
    int[] toArray();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   452
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   453
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   454
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   455
     * elements of this stream, using the provided identity value and an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   456
     * <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   457
     * accumulation function, and returns the reduced value.  This is equivalent
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   458
     * to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   459
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   460
     *     int result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   461
     *     for (int element : this stream)
22996
0ce70f4ef909 8029944: Primitive Stream reduce method documentation pseudo code misidentifies apply method
michaelm
parents: 22352
diff changeset
   462
     *         result = accumulator.applyAsInt(result, element)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   463
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   464
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   465
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   466
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   467
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   468
     * <p>The {@code identity} value must be an identity for the accumulator
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   469
     * function. This means that for all {@code x},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   470
     * {@code accumulator.apply(identity, x)} is equal to {@code x}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   471
     * The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   472
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   473
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   474
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   475
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   476
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   477
     * @apiNote Sum, min, max, and average are all special cases of reduction.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   478
     * Summing a stream of numbers can be expressed as:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   479
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   480
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   481
     *     int sum = integers.reduce(0, (a, b) -> a+b);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   482
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   483
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   484
     * or more compactly:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   485
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   486
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   487
     *     int sum = integers.reduce(0, Integer::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   488
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   489
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   490
     * <p>While this may seem a more roundabout way to perform an aggregation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   491
     * compared to simply mutating a running total in a loop, reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   492
     * operations parallelize more gracefully, without needing additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   493
     * synchronization and with greatly reduced risk of data races.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   494
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   495
     * @param identity the identity value for the accumulating function
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   496
     * @param op an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   497
     *           <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   498
     *           <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   499
     *           function for combining two values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   500
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   501
     * @see #sum()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   502
     * @see #min()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   503
     * @see #max()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   504
     * @see #average()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   505
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   506
    int reduce(int identity, IntBinaryOperator op);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   507
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   508
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   509
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   510
     * elements of this stream, using an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   511
     * <a href="package-summary.html#Associativity">associative</a> accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   512
     * function, and returns an {@code OptionalInt} describing the reduced value,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   513
     * if any. This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   514
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   515
     *     boolean foundAny = false;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   516
     *     int result = null;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   517
     *     for (int element : this stream) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   518
     *         if (!foundAny) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   519
     *             foundAny = true;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   520
     *             result = element;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   521
     *         }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   522
     *         else
22996
0ce70f4ef909 8029944: Primitive Stream reduce method documentation pseudo code misidentifies apply method
michaelm
parents: 22352
diff changeset
   523
     *             result = accumulator.applyAsInt(result, element);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   524
     *     }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   525
     *     return foundAny ? OptionalInt.of(result) : OptionalInt.empty();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   526
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   527
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   528
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   529
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   530
     * <p>The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   531
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   532
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   533
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   534
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   535
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   536
     * @param op an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   537
     *           <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   538
     *           <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   539
     *           function for combining two values
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   540
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   541
     * @see #reduce(int, IntBinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   542
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   543
    OptionalInt reduce(IntBinaryOperator op);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   544
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   545
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   546
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   547
     * reduction</a> operation on the elements of this stream.  A mutable
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   548
     * reduction is one in which the reduced value is a mutable result container,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   549
     * such as an {@code ArrayList}, and elements are incorporated by updating
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   550
     * the state of the result rather than by replacing the result.  This
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   551
     * produces a result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   552
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   553
     *     R result = supplier.get();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   554
     *     for (int element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   555
     *         accumulator.accept(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   556
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   557
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   558
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   559
     * <p>Like {@link #reduce(int, IntBinaryOperator)}, {@code collect} operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   560
     * can be parallelized without requiring additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   561
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   562
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   563
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   564
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   565
     * @param <R> type of the result
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   566
     * @param supplier a function that creates a new result container. For a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   567
     *                 parallel execution, this function may be called
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   568
     *                 multiple times and must return a fresh value each time.
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   569
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   570
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   571
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   572
     *                    function for incorporating an additional element into a result
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   573
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   574
     *                    <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   575
     *                    <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   576
     *                    function for combining two values, which must be
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   577
     *                    compatible with the accumulator function
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   578
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   579
     * @see Stream#collect(Supplier, BiConsumer, BiConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   580
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   581
    <R> R collect(Supplier<R> supplier,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   582
                  ObjIntConsumer<R> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   583
                  BiConsumer<R, R> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   584
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   585
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   586
     * Returns the sum of elements in this stream.  This is a special case
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   587
     * of a <a href="package-summary.html#Reduction">reduction</a>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   588
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   589
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   590
     *     return reduce(0, Integer::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   591
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   592
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   593
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   594
     * operation</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   595
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   596
     * @return the sum of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   597
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   598
    int sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   599
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   600
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   601
     * Returns an {@code OptionalInt} describing the minimum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   602
     * stream, or an empty optional if this stream is empty.  This is a special
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   603
     * case of a <a href="package-summary.html#Reduction">reduction</a>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   604
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   605
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   606
     *     return reduce(Integer::min);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   607
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   608
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   609
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   610
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   611
     * @return an {@code OptionalInt} containing the minimum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   612
     * stream, or an empty {@code OptionalInt} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   613
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   614
    OptionalInt min();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   615
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   616
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   617
     * Returns an {@code OptionalInt} describing the maximum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   618
     * stream, or an empty optional if this stream is empty.  This is a special
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   619
     * case of a <a href="package-summary.html#Reduction">reduction</a>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   620
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   621
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   622
     *     return reduce(Integer::max);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   623
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   624
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   625
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   626
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   627
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   628
     * @return an {@code OptionalInt} containing the maximum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   629
     * stream, or an empty {@code OptionalInt} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   630
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   631
    OptionalInt max();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   632
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   633
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   634
     * Returns the count of elements in this stream.  This is a special case of
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   635
     * a <a href="package-summary.html#Reduction">reduction</a> and is
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   636
     * equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   637
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   638
     *     return mapToLong(e -> 1L).sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   639
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   640
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   641
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   642
     *
29489
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   643
     * @apiNote
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   644
     * An implementation may choose to not execute the stream pipeline (either
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   645
     * sequentially or in parallel) if it is capable of computing the count
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   646
     * directly from the stream source.  In such cases no source elements will
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   647
     * be traversed and no intermediate operations will be evaluated.
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   648
     * Behavioral parameters with side-effects, which are strongly discouraged
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   649
     * except for harmless cases such as debugging, may be affected.  For
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   650
     * example, consider the following stream:
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   651
     * <pre>{@code
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   652
     *     IntStream s = IntStream.of(1, 2, 3, 4);
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   653
     *     long count = s.peek(System.out::println).count();
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   654
     * }</pre>
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   655
     * The number of elements covered by the stream source is known and the
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   656
     * intermediate operation, {@code peek}, does not inject into or remove
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   657
     * elements from the stream (as may be the case for {@code flatMap} or
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   658
     * {@code filter} operations).  Thus the count is 4 and there is no need to
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   659
     * execute the pipeline and, as a side-effect, print out the elements.
fe7624d92790 8067969: Optimize Stream.count for SIZED Streams
psandoz
parents: 29103
diff changeset
   660
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   661
     * @return the count of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   662
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   663
    long count();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   664
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   665
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18825
diff changeset
   666
     * Returns an {@code OptionalDouble} describing the arithmetic mean of elements of
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   667
     * this stream, or an empty optional if this stream is empty.  This is a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   668
     * special case of a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   669
     * <a href="package-summary.html#Reduction">reduction</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   670
     *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   671
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   672
     * operation</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   673
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   674
     * @return an {@code OptionalDouble} containing the average element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   675
     * stream, or an empty optional if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   676
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   677
    OptionalDouble average();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   678
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   679
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   680
     * Returns an {@code IntSummaryStatistics} describing various
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   681
     * summary data about the elements of this stream.  This is a special
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   682
     * case of a <a href="package-summary.html#Reduction">reduction</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   683
     *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   684
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   685
     * operation</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   686
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   687
     * @return an {@code IntSummaryStatistics} describing various summary data
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   688
     * about the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   689
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   690
    IntSummaryStatistics summaryStatistics();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   691
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   692
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   693
     * Returns whether any elements of this stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   694
     * predicate.  May not evaluate the predicate on all elements if not
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   695
     * necessary for determining the result.  If the stream is empty then
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   696
     * {@code false} is returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   697
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   698
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   699
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   700
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   701
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   702
     * This method evaluates the <em>existential quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   703
     * predicate over the elements of the stream (for some x P(x)).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   704
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   705
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   706
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   707
     *                  predicate to apply to elements of this stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   708
     * @return {@code true} if any elements of the stream match the provided
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   709
     * predicate, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   710
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   711
    boolean anyMatch(IntPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   712
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   713
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   714
     * Returns whether all elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   715
     * May not evaluate the predicate on all elements if not necessary for
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   716
     * determining the result.  If the stream is empty then {@code true} is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   717
     * returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   718
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   719
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   720
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   721
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   722
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   723
     * This method evaluates the <em>universal quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   724
     * predicate over the elements of the stream (for all x P(x)).  If the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   725
     * stream is empty, the quantification is said to be <em>vacuously
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   726
     * satisfied</em> and is always {@code true} (regardless of P(x)).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   727
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   728
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   729
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   730
     *                  predicate to apply to elements of this stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   731
     * @return {@code true} if either all elements of the stream match the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   732
     * provided predicate or the stream is empty, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   733
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   734
    boolean allMatch(IntPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   735
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   736
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   737
     * Returns whether no elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   738
     * May not evaluate the predicate on all elements if not necessary for
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   739
     * determining the result.  If the stream is empty then {@code true} is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   740
     * returned and the predicate is not evaluated.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   741
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   742
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   743
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   744
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   745
     * @apiNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   746
     * This method evaluates the <em>universal quantification</em> of the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   747
     * negated predicate over the elements of the stream (for all x ~P(x)).  If
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   748
     * the stream is empty, the quantification is said to be vacuously satisfied
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   749
     * and is always {@code true}, regardless of P(x).
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   750
     *
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   751
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   752
     *                  <a href="package-summary.html#Statelessness">stateless</a>
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   753
     *                  predicate to apply to elements of this stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   754
     * @return {@code true} if either no elements of the stream match the
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   755
     * provided predicate or the stream is empty, otherwise {@code false}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   756
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   757
    boolean noneMatch(IntPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   758
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   759
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   760
     * Returns an {@link OptionalInt} describing the first element of this
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   761
     * stream, or an empty {@code OptionalInt} if the stream is empty.  If the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   762
     * stream has no encounter order, then any element may be returned.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   763
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   764
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   765
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   766
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   767
     * @return an {@code OptionalInt} describing the first element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   768
     * or an empty {@code OptionalInt} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   769
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   770
    OptionalInt findFirst();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   771
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   772
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   773
     * Returns an {@link OptionalInt} describing some element of the stream, or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   774
     * an empty {@code OptionalInt} if the stream is empty.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   775
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   776
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   777
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   778
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   779
     * <p>The behavior of this operation is explicitly nondeterministic; it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   780
     * free to select any element in the stream.  This is to allow for maximal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   781
     * performance in parallel operations; the cost is that multiple invocations
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   782
     * on the same source may not return the same result.  (If a stable result
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   783
     * is desired, use {@link #findFirst()} instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   784
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   785
     * @return an {@code OptionalInt} describing some element of this stream, or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   786
     * an empty {@code OptionalInt} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   787
     * @see #findFirst()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   788
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   789
    OptionalInt findAny();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   790
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   791
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   792
     * Returns a {@code LongStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   793
     * converted to {@code long}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   794
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   795
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   796
     * operation</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   797
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   798
     * @return a {@code LongStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   799
     * converted to {@code long}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   800
     */
18154
5ede18269905 8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents: 17914
diff changeset
   801
    LongStream asLongStream();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   802
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   803
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   804
     * Returns a {@code DoubleStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   805
     * converted to {@code double}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   806
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   807
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   808
     * operation</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   809
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   810
     * @return a {@code DoubleStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   811
     * converted to {@code double}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   812
     */
18154
5ede18269905 8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents: 17914
diff changeset
   813
    DoubleStream asDoubleStream();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   814
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   815
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   816
     * Returns a {@code Stream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   817
     * each boxed to an {@code Integer}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   818
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   819
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   820
     * operation</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   821
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   822
     * @return a {@code Stream} consistent of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   823
     * each boxed to an {@code Integer}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   824
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   825
    Stream<Integer> boxed();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   826
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   827
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   828
    IntStream sequential();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   829
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   830
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   831
    IntStream parallel();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   832
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   833
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   834
    PrimitiveIterator.OfInt iterator();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   835
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   836
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   837
    Spliterator.OfInt spliterator();
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   838
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   839
    // Static factories
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   840
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   841
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   842
     * Returns a builder for an {@code IntStream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   843
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   844
     * @return a stream builder
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   845
     */
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   846
    public static Builder builder() {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   847
        return new Streams.IntStreamBuilderImpl();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   848
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   849
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   850
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   851
     * Returns an empty sequential {@code IntStream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   852
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   853
     * @return an empty sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   854
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   855
    public static IntStream empty() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   856
        return StreamSupport.intStream(Spliterators.emptyIntSpliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   857
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   858
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   859
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   860
     * Returns a sequential {@code IntStream} containing a single element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   861
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   862
     * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   863
     * @return a singleton sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   864
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   865
    public static IntStream of(int t) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   866
        return StreamSupport.intStream(new Streams.IntStreamBuilderImpl(t), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   867
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   868
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   869
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   870
     * Returns a sequential ordered stream whose elements are the specified values.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   871
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   872
     * @param values the elements of the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   873
     * @return the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   874
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   875
    public static IntStream of(int... values) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   876
        return Arrays.stream(values);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   877
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   878
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   879
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   880
     * Returns an infinite sequential ordered {@code IntStream} produced by iterative
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   881
     * application of a function {@code f} to an initial element {@code seed},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   882
     * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   883
     * {@code f(f(seed))}, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   884
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   885
     * <p>The first element (position {@code 0}) in the {@code IntStream} will be
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   886
     * the provided {@code seed}.  For {@code n > 0}, the element at position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   887
     * {@code n}, will be the result of applying the function {@code f} to the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   888
     * element at position {@code n - 1}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   889
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   890
     * @param seed the initial element
25526
d3cbdae6e9f9 8044047: Missing null pointer checks for streams
psandoz
parents: 22996
diff changeset
   891
     * @param f a function to be applied to the previous element to produce
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   892
     *          a new element
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   893
     * @return a new sequential {@code IntStream}
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   894
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   895
    public static IntStream iterate(final int seed, final IntUnaryOperator f) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   896
        Objects.requireNonNull(f);
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   897
        Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   898
               Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   899
            int prev;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   900
            boolean started;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   901
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   902
            @Override
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   903
            public boolean tryAdvance(IntConsumer action) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   904
                Objects.requireNonNull(action);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   905
                int t;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   906
                if (started)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   907
                    t = f.applyAsInt(prev);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   908
                else {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   909
                    t = seed;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   910
                    started = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   911
                }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   912
                action.accept(prev = t);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   913
                return true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   914
            }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   915
        };
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   916
        return StreamSupport.intStream(spliterator, false);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   917
    }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   918
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   919
    /**
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   920
     * Returns a sequential ordered {@code IntStream} produced by iterative
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   921
     * application of a function to an initial element, conditioned on
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   922
     * satisfying the supplied predicate.  The stream terminates as soon as
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   923
     * the predicate returns false.
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   924
     *
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   925
     * <p>
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   926
     * {@code IntStream.iterate} should produce the same sequence of elements
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   927
     * as produced by the corresponding for-loop:
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   928
     * <pre>{@code
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   929
     *     for (int index=seed; predicate.test(index); index = f.apply(index)) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   930
     *         ...
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   931
     *     }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   932
     * }</pre>
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   933
     *
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   934
     * <p>
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   935
     * The resulting sequence may be empty if the predicate does not hold on
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   936
     * the seed value.  Otherwise the first element will be the supplied seed
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   937
     * value, the next element (if present) will be the result of applying the
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   938
     * function f to the seed value, and so on iteratively until the predicate
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   939
     * indicates that the stream should terminate.
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   940
     *
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   941
     * @param seed the initial element
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   942
     * @param predicate a predicate to apply to elements to determine when the
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   943
     *          stream must terminate.
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   944
     * @param f a function to be applied to the previous element to produce
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   945
     *          a new element
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   946
     * @return a new sequential {@code IntStream}
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   947
     * @since 9
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   948
     */
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   949
    public static IntStream iterate(int seed, IntPredicate predicate, IntUnaryOperator f) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   950
        Objects.requireNonNull(f);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   951
        Objects.requireNonNull(predicate);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   952
        Spliterator.OfInt spliterator = new Spliterators.AbstractIntSpliterator(Long.MAX_VALUE,
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   953
               Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   954
            int prev;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   955
            boolean started, finished;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   956
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   957
            @Override
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   958
            public boolean tryAdvance(IntConsumer action) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   959
                Objects.requireNonNull(action);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   960
                if (finished)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   961
                    return false;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   962
                int t;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   963
                if (started)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   964
                    t = f.applyAsInt(prev);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   965
                else {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   966
                    t = seed;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   967
                    started = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   968
                }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   969
                if (!predicate.test(t)) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   970
                    finished = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   971
                    return false;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   972
                }
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   973
                action.accept(prev = t);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   974
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   975
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   976
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   977
            @Override
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   978
            public void forEachRemaining(IntConsumer action) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   979
                Objects.requireNonNull(action);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   980
                if (finished)
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   981
                    return;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   982
                finished = true;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   983
                int t = started ? f.applyAsInt(prev) : seed;
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   984
                while (predicate.test(t)) {
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   985
                    action.accept(t);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   986
                    t = f.applyAsInt(t);
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   987
                }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   988
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   989
        };
36223
de2976b3614c 8072727: add variation of Stream.iterate() that's finite
tvaleev
parents: 35302
diff changeset
   990
        return StreamSupport.intStream(spliterator, false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   991
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   992
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   993
    /**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   994
     * Returns an infinite sequential unordered stream where each element is
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   995
     * generated by the provided {@code IntSupplier}.  This is suitable for
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   996
     * generating constant streams, streams of random elements, etc.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   997
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   998
     * @param s the {@code IntSupplier} for generated elements
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
   999
     * @return a new infinite sequential unordered {@code IntStream}
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1000
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1001
    public static IntStream generate(IntSupplier s) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1002
        Objects.requireNonNull(s);
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 18158
diff changeset
  1003
        return StreamSupport.intStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1004
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfInt(Long.MAX_VALUE, s), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1005
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1006
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1007
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1008
     * Returns a sequential ordered {@code IntStream} from {@code startInclusive}
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1009
     * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1010
     * {@code 1}.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1011
     *
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1012
     * @apiNote
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1013
     * <p>An equivalent sequence of increasing values can be produced
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1014
     * sequentially using a {@code for} loop as follows:
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1015
     * <pre>{@code
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1016
     *     for (int i = startInclusive; i < endExclusive ; i++) { ... }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1017
     * }</pre>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1018
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1019
     * @param startInclusive the (inclusive) initial value
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1020
     * @param endExclusive the exclusive upper bound
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1021
     * @return a sequential {@code IntStream} for the range of {@code int}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1022
     *         elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1023
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1024
    public static IntStream range(int startInclusive, int endExclusive) {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1025
        if (startInclusive >= endExclusive) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1026
            return empty();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1027
        } else {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1028
            return StreamSupport.intStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1029
                    new Streams.RangeIntSpliterator(startInclusive, endExclusive, false), false);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1030
        }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1031
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1032
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1033
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1034
     * Returns a sequential ordered {@code IntStream} from {@code startInclusive}
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1035
     * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1036
     * {@code 1}.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1037
     *
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1038
     * @apiNote
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1039
     * <p>An equivalent sequence of increasing values can be produced
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1040
     * sequentially using a {@code for} loop as follows:
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1041
     * <pre>{@code
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1042
     *     for (int i = startInclusive; i <= endInclusive ; i++) { ... }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1043
     * }</pre>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1044
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1045
     * @param startInclusive the (inclusive) initial value
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1046
     * @param endInclusive the inclusive upper bound
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1047
     * @return a sequential {@code IntStream} for the range of {@code int}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1048
     *         elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1049
     */
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1050
    public static IntStream rangeClosed(int startInclusive, int endInclusive) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1051
        if (startInclusive > endInclusive) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1052
            return empty();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1053
        } else {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
  1054
            return StreamSupport.intStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
  1055
                    new Streams.RangeIntSpliterator(startInclusive, endInclusive, true), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1056
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
  1057
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1058
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1059
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1060
     * Creates a lazily concatenated stream whose elements are all the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1061
     * elements of the first stream followed by all the elements of the
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1062
     * second stream.  The resulting stream is ordered if both
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1063
     * of the input streams are ordered, and parallel if either of the input
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19214
diff changeset
  1064
     * streams is parallel.  When the resulting stream is closed, the close
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19214
diff changeset
  1065
     * handlers for both input streams are invoked.
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1066
     *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1067
     * @implNote
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1068
     * Use caution when constructing streams from repeated concatenation.
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1069
     * Accessing an element of a deeply concatenated stream can result in deep
29103
1dbde34ad64c 8073779: StackOverflowError called StackOverflowException in javadoc
igerasim
parents: 25859
diff changeset
  1070
     * call chains, or even {@code StackOverflowError}.
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 20866
diff changeset
  1071
     *
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1072
     * @param a the first stream
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1073
     * @param b the second stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1074
     * @return the concatenation of the two input streams
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1075
     */
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1076
    public static IntStream concat(IntStream a, IntStream b) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1077
        Objects.requireNonNull(a);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1078
        Objects.requireNonNull(b);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1079
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1080
        Spliterator.OfInt split = new Streams.ConcatSpliterator.OfInt(
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1081
                a.spliterator(), b.spliterator());
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19214
diff changeset
  1082
        IntStream stream = StreamSupport.intStream(split, a.isParallel() || b.isParallel());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19214
diff changeset
  1083
        return stream.onClose(Streams.composedClose(a, b));
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
  1084
    }
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1085
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1086
    /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1087
     * A mutable builder for an {@code IntStream}.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1088
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1089
     * <p>A stream builder has a lifecycle, which starts in a building
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1090
     * phase, during which elements can be added, and then transitions to a built
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
  1091
     * phase, after which elements may not be added.  The built phase
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1092
     * begins when the {@link #build()} method is called, which creates an
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1093
     * ordered stream whose elements are the elements that were added to the
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1094
     * stream builder, in the order they were added.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1095
     *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1096
     * @see IntStream#builder()
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1097
     * @since 1.8
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1098
     */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1099
    public interface Builder extends IntConsumer {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1100
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1101
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1102
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1103
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1104
         * @throws IllegalStateException if the builder has already transitioned
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1105
         * to the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1106
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1107
        @Override
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1108
        void accept(int t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1109
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1110
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1111
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1112
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1113
         * @implSpec
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1114
         * The default implementation behaves as if:
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1115
         * <pre>{@code
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1116
         *     accept(t)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1117
         *     return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1118
         * }</pre>
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1119
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1120
         * @param t the element to add
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1121
         * @return {@code this} builder
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1122
         * @throws IllegalStateException if the builder has already transitioned
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1123
         * to the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1124
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1125
        default Builder add(int t) {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1126
            accept(t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1127
            return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1128
        }
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1129
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1130
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1131
         * Builds the stream, transitioning this builder to the built state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1132
         * An {@code IllegalStateException} is thrown if there are further
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1133
         * attempts to operate on the builder after it has entered the built
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1134
         * state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1135
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1136
         * @return the built stream
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1137
         * @throws IllegalStateException if the builder has already transitioned to
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1138
         * the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1139
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1140
        IntStream build();
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
  1141
    }
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
  1142
}