jdk/src/share/classes/java/util/stream/LongStream.java
author henryjen
Fri, 06 Sep 2013 22:20:01 -0700
changeset 19850 93b368e54c1c
parent 19800 6e1fef53ea55
child 20866 36155ee613ef
permissions -rw-r--r--
8011916: Spec update for java.util.stream 8024339: j.u.s.Stream.reduce(BinaryOperator) throws unexpected NPE Reviewed-by: mduigou Contributed-by: brian.goetz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     1
/*
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
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
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    27
import java.nio.charset.Charset;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    28
import java.nio.file.Files;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    29
import java.nio.file.Path;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    30
import java.util.Arrays;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    31
import java.util.Collection;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    32
import java.util.LongSummaryStatistics;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    33
import java.util.Objects;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    34
import java.util.OptionalDouble;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    35
import java.util.OptionalLong;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    36
import java.util.PrimitiveIterator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    37
import java.util.Spliterator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    38
import java.util.Spliterators;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    39
import java.util.concurrent.ConcurrentHashMap;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
import java.util.function.BiConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    41
import java.util.function.Function;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    42
import java.util.function.LongBinaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    43
import java.util.function.LongConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    44
import java.util.function.LongFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    45
import java.util.function.LongPredicate;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    46
import java.util.function.LongSupplier;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    47
import java.util.function.LongToDoubleFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    48
import java.util.function.LongToIntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    49
import java.util.function.LongUnaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    50
import java.util.function.ObjLongConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    51
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    52
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    53
/**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    54
 * A sequence of elements supporting sequential and parallel aggregate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    55
 * operations.  The following example illustrates an aggregate operation using
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    56
 * {@link Stream} and {@link LongStream}:
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    57
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    58
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    59
 *     long sum = widgets.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    60
 *                       .filter(w -> w.getColor() == RED)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    61
 *                       .mapToLong(w -> w.getWeight())
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    62
 *                       .sum();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    63
 * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    64
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    65
 * In this example, {@code widgets} is a {@code Collection<Widget>}.  We create
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    66
 * a stream of {@code Widget} objects via {@link Collection#stream Collection.stream()},
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    67
 * filter it to produce a stream containing only the red widgets, and then
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    68
 * transform it into a stream of {@code long} values representing the weight of
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    69
 * each red widget. Then this stream is summed to produce a total weight.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    70
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    71
 * <p>To perform a computation, stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    72
 * <a href="package-summary.html#StreamOps">operations</a> are composed into a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    73
 * <em>stream pipeline</em>.  A stream pipeline consists of a source (which
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    74
 * might be an array, a collection, a generator function, an IO channel,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    75
 * etc), zero or more <em>intermediate operations</em> (which transform a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    76
 * stream into another stream, such as {@link LongStream#filter(LongPredicate)}), and a
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    77
 * <em>terminal operation</em> (which produces a result or side-effect, such
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    78
 * as {@link LongStream#sum()} or {@link LongStream#forEach(LongConsumer)}).
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    79
 * Streams are lazy; computation on the source data is only performed when the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    80
 * terminal operation is initiated, and source elements are consumed only
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    81
 * as needed.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    82
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    83
 * <p>Collections and streams, while bearing some superficial similarities,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    84
 * have different goals.  Collections are primarily concerned with the efficient
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    85
 * management of, and access to, their elements.  By contrast, streams do not
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    86
 * provide a means to directly access or manipulate their elements, and are
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    87
 * instead concerned with declaratively describing their source and the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    88
 * computational operations which will be performed in aggregate on that source.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    89
 * However, if the provided stream operations do not offer the desired
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    90
 * functionality, the {@link #iterator()} and {@link #spliterator()} operations
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    91
 * can be used to perform a controlled traversal.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    92
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    93
 * <p>A stream pipeline, like the "widgets" example above, can be viewed as
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    94
 * a <em>query</em> on the stream source.  Unless the source was explicitly
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    95
 * designed for concurrent modification (such as a {@link ConcurrentHashMap}),
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    96
 * unpredictable or erroneous behavior may result from modifying the stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    97
 * source while it is being queried.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    98
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    99
 * <p>Most stream operations accept parameters that describe user-specified
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   100
 * behavior, such as the lambda expression {@code w -> w.getWeight()} passed to
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   101
 * {@code mapToLong} in the example above.  Such parameters are always instances
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   102
 * of a <a href="../function/package-summary.html">functional interface</a> such
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   103
 * as {@link java.util.function.Function}, and are often lambda expressions or
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   104
 * method references.  These parameters can never be null, should not modify the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   105
 * stream source, and should be
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   106
 * <a href="package-summary.html#NonInterference">effectively stateless</a>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   107
 * (their result should not depend on any state that might change during
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   108
 * execution of the stream pipeline.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   109
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   110
 * <p>A stream should be operated on (invoking an intermediate or terminal stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   111
 * operation) only once.  This rules out, for example, "forked" streams, where
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   112
 * the same source feeds two or more pipelines, or multiple traversals of the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   113
 * same stream.  A stream implementation may throw {@link IllegalStateException}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   114
 * if it detects that the stream is being reused. However, since some stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   115
 * operations may return their receiver rather than a new stream object, it may
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   116
 * not be possible to detect reuse in all cases.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   117
 *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   118
 * <p>Streams have a {@link #close()} method and implement {@link AutoCloseable},
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   119
 * but nearly all stream instances do not actually need to be closed after use.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   120
 * Generally, only streams whose source is an IO channel (such as those returned
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   121
 * by {@link Files#lines(Path, Charset)}) will require closing.  Most streams
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   122
 * are backed by collections, arrays, or generating functions, which require no
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   123
 * special resource management.  (If a stream does require closing, it can be
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   124
 * declared as a resource in a {@code try}-with-resources statement.)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   125
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   126
 * <p>Stream pipelines may execute either sequentially or in
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   127
 * <a href="package-summary.html#Parallelism">parallel</a>.  This
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   128
 * execution mode is a property of the stream.  Streams are created
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   129
 * with an initial choice of sequential or parallel execution.  (For example,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   130
 * {@link Collection#stream() Collection.stream()} creates a sequential stream,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   131
 * and {@link Collection#parallelStream() Collection.parallelStream()} creates
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   132
 * a parallel one.)  This choice of execution mode may be modified by the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   133
 * {@link #sequential()} or {@link #parallel()} methods, and may be queried with
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   134
 * the {@link #isParallel()} method.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   135
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   136
 * @since 1.8
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   137
 * @see <a href="package-summary.html">java.util.stream</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   138
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   139
public interface LongStream extends BaseStream<Long, LongStream> {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   140
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   141
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   142
     * Returns a stream consisting of the elements of this stream that match
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   143
     * the given predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   144
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   145
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   146
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   147
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   148
     * @param predicate a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   149
     *                  non-interfering, stateless</a> predicate to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   150
     *                  each element to determine if it should be included
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   151
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   152
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   153
    LongStream filter(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   154
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   155
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   156
     * Returns a stream consisting of the results of applying the given
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   157
     * function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   158
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   159
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   160
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   161
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   162
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   163
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   164
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   165
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   166
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
    LongStream map(LongUnaryOperator mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   169
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
     * Returns an object-valued {@code Stream} consisting of the results of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   171
     * applying the given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   172
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   173
     * <p>This is an <a href="package-summary.html#StreamOps">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   174
     *     intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   175
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   176
     * @param <U> the element type of the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   177
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   178
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   179
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   180
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   181
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
    <U> Stream<U> mapToObj(LongFunction<? extends U> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   183
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   184
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   185
     * Returns an {@code IntStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   186
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   187
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   188
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   189
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   190
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   192
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   193
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   194
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   195
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   196
    IntStream mapToInt(LongToIntFunction mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   197
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   198
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   199
     * Returns a {@code DoubleStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   201
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   202
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   203
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   204
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   205
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   206
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   207
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   208
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   209
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   210
    DoubleStream mapToDouble(LongToDoubleFunction mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   211
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   212
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   213
     * Returns a stream consisting of the results of replacing each element of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   214
     * this stream with the contents of the stream produced by applying the
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   215
     * provided mapping function to each element.  (If the result of the mapping
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   216
     * function is {@code null}, this is treated as if the result was an empty
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   217
     * stream.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   218
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   219
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   220
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   221
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   222
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   223
     *               non-interfering, stateless</a> function to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   224
     *               each element which produces an {@code LongStream} of new
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   225
     *               values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   226
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   227
     * @see Stream#flatMap(Function)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   228
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   229
    LongStream flatMap(LongFunction<? extends LongStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   230
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   231
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   232
     * Returns a stream consisting of the distinct elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   233
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   234
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   235
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   236
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   237
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   238
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   239
    LongStream distinct();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   240
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   241
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   242
     * Returns a stream consisting of the elements of this stream in sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   243
     * order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   244
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   245
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   246
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   247
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   248
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   249
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   250
    LongStream sorted();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   251
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   252
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   253
     * Returns a stream consisting of the elements of this stream, additionally
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   254
     * performing the provided action on each element as elements are consumed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   255
     * from the resulting stream.
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 an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   258
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   259
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   260
     * <p>For parallel stream pipelines, the action may be called at
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   261
     * whatever time and in whatever thread the element is made available by the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   262
     * upstream operation.  If the action modifies shared state,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   263
     * it is responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   264
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   265
     * @apiNote This method exists mainly to support debugging, where you want
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   266
     * to see the elements as they flow past a certain point in a pipeline:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   267
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   268
     *     list.stream()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   269
     *         .filter(filteringFunction)
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   270
     *         .peek(e -> System.out.println("Filtered value: " + e));
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   271
     *         .map(mappingFunction)
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   272
     *         .peek(e -> System.out.println("Mapped value: " + e));
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   273
     *         .collect(Collectors.toLongSummaryStastistics());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   274
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   275
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   276
     * @param action a <a href="package-summary.html#NonInterference">
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   277
     *               non-interfering</a> action to perform on the elements as
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   278
     *               they are consumed from the stream
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   279
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   280
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   281
    LongStream peek(LongConsumer action);
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   282
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   283
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   284
     * Returns a stream consisting of the elements of this stream, truncated
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   285
     * to be no longer than {@code maxSize} in length.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   286
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   287
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   288
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   289
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   290
     * @param maxSize the number of elements the stream should be limited to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   291
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   292
     * @throws IllegalArgumentException if {@code maxSize} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   293
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   294
    LongStream limit(long maxSize);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   295
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   296
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   297
     * Returns a stream consisting of the remaining elements of this stream
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   298
     * after discarding the first {@code startInclusive} elements of the stream.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   299
     * If this stream contains fewer than {@code startInclusive} elements then an
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   300
     * empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   301
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   302
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   303
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   304
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   305
     * @param startInclusive the number of leading elements to skip
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   306
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   307
     * @throws IllegalArgumentException if {@code startInclusive} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   308
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   309
    LongStream substream(long startInclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   310
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   311
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   312
     * Returns a stream consisting of the remaining elements of this stream
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   313
     * after discarding the first {@code startInclusive} elements and truncating
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   314
     * the result to be no longer than {@code endExclusive - startInclusive}
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   315
     * elements in length. If this stream contains fewer than
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   316
     * {@code startInclusive} elements then an empty stream will be returned.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   317
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   318
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   319
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   320
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   321
     * @param startInclusive the starting position of the substream, inclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   322
     * @param endExclusive the ending position of the substream, exclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   323
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   324
     * @throws IllegalArgumentException if {@code startInclusive} or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   325
     * {@code endExclusive} is negative or {@code startInclusive} is greater
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   326
     * than {@code endExclusive}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   327
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   328
    LongStream substream(long startInclusive, long endExclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   329
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   330
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   331
     * Performs an action for each element of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   332
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   333
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   334
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   335
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   336
     * <p>For parallel stream pipelines, this operation does <em>not</em>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   337
     * guarantee to respect the encounter order of the stream, as doing so
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   338
     * would sacrifice the benefit of parallelism.  For any given element, the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   339
     * action may be performed at whatever time and in whatever thread the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   340
     * library chooses.  If the action accesses shared state, it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   341
     * responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   342
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   343
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   344
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   345
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   346
    void forEach(LongConsumer action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   347
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   348
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   349
     * Performs an action for each element of this stream, guaranteeing that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   350
     * each element is processed in encounter order for streams that have a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   351
     * defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   352
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   353
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   354
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   355
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   356
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   357
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   358
     * @see #forEach(LongConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   359
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   360
    void forEachOrdered(LongConsumer action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   361
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   362
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   363
     * Returns an array containing the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   364
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   365
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   366
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   367
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   368
     * @return an array containing the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   369
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   370
    long[] toArray();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   371
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   372
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   373
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   374
     * elements of this stream, using the provided identity value and an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   375
     * <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   376
     * accumulation function, and returns the reduced value.  This is equivalent
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   377
     * to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   378
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   379
     *     long result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   380
     *     for (long element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   381
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   382
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   383
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   384
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   385
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   386
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   387
     * <p>The {@code identity} value must be an identity for the accumulator
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   388
     * function. This means that for all {@code x},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   389
     * {@code accumulator.apply(identity, x)} is equal to {@code x}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   390
     * The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   391
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   392
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   393
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   394
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   395
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   396
     * @apiNote Sum, min, max, and average are all special cases of reduction.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   397
     * Summing a stream of numbers can be expressed as:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   398
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   399
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   400
     *     long sum = integers.reduce(0, (a, b) -> a+b);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   401
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   402
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   403
     * or more compactly:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   404
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   405
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   406
     *     long sum = integers.reduce(0, Long::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   407
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   408
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   409
     * <p>While this may seem a more roundabout way to perform an aggregation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   410
     * compared to simply mutating a running total in a loop, reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   411
     * operations parallelize more gracefully, without needing additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   412
     * synchronization and with greatly reduced risk of data races.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   413
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   414
     * @param identity the identity value for the accumulating function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   415
     * @param op an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   416
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   417
     *                    stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   418
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   419
     * @see #sum()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   420
     * @see #min()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   421
     * @see #max()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   422
     * @see #average()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   423
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   424
    long reduce(long identity, LongBinaryOperator op);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   425
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   426
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   427
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   428
     * elements of this stream, using an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   429
     * <a href="package-summary.html#Associativity">associative</a> accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   430
     * function, and returns an {@code OptionalLong} describing the reduced value,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   431
     * if any. This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   432
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   433
     *     boolean foundAny = false;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   434
     *     long result = null;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   435
     *     for (long element : this stream) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   436
     *         if (!foundAny) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   437
     *             foundAny = true;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   438
     *             result = element;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   439
     *         }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   440
     *         else
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   441
     *             result = accumulator.apply(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   442
     *     }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   443
     *     return foundAny ? OptionalLong.of(result) : OptionalLong.empty();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   444
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   445
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   446
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   447
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   448
     * <p>The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   449
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   450
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   451
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   452
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   453
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   454
     * @param op an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   455
     *           <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   456
     *           stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   457
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   458
     * @see #reduce(long, LongBinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   459
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   460
    OptionalLong reduce(LongBinaryOperator op);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   461
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   462
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   463
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   464
     * 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
   465
     * reduction is one in which the reduced value is a mutable result container,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   466
     * 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
   467
     * the state of the result rather than by replacing the result.  This
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   468
     * produces a result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   469
     * <pre>{@code
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   470
     *     R result = supplier.get();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   471
     *     for (long element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   472
     *         accumulator.accept(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   473
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   474
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   475
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   476
     * <p>Like {@link #reduce(long, LongBinaryOperator)}, {@code collect} operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   477
     * can be parallelized without requiring additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   478
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   479
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   480
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   481
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   482
     * @param <R> type of the result
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   483
     * @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
   484
     *                 parallel execution, this function may be called
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   485
     *                 multiple times and must return a fresh value each time.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   486
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   487
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   488
     *                    stateless</a> function for incorporating an additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   489
     *                    element into a result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   490
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   491
     *                 <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   492
     *                 stateless</a> function for combining two values, which
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   493
     *                 must be compatible with the accumulator function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   494
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   495
     * @see Stream#collect(Supplier, BiConsumer, BiConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   496
     */
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   497
    <R> R collect(Supplier<R> supplier,
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   498
                  ObjLongConsumer<R> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   499
                  BiConsumer<R, R> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   500
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   501
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   502
     * 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
   503
     * of a <a href="package-summary.html#Reduction">reduction</a>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   504
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   505
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   506
     *     return reduce(0, Long::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   507
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   508
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   509
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   510
     * operation</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   511
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   512
     * @return the sum of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   513
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   514
    long sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   515
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   516
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   517
     * Returns an {@code OptionalLong} describing the minimum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   518
     * 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
   519
     * case of a <a href="package-summary.html#Reduction">reduction</a>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   520
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   521
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   522
     *     return reduce(Long::min);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   523
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   524
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   525
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   526
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   527
     * @return an {@code OptionalLong} containing the minimum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   528
     * stream, or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   529
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   530
    OptionalLong min();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   531
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   532
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   533
     * Returns an {@code OptionalLong} describing the maximum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   534
     * 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
   535
     * case of a <a href="package-summary.html#Reduction">reduction</a>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   536
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   537
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   538
     *     return reduce(Long::max);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   539
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   540
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   541
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   542
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   543
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   544
     * @return an {@code OptionalLong} containing the maximum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   545
     * stream, or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   546
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   547
    OptionalLong max();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   548
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   549
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   550
     * 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
   551
     * a <a href="package-summary.html#Reduction">reduction</a> and is
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   552
     * equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   553
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   554
     *     return map(e -> 1L).sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   555
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   556
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   557
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   558
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   559
     * @return the count of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   560
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   561
    long count();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   562
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   563
    /**
19214
e5901820c3c1 8015318: Extend Collector with 'finish' operation
briangoetz
parents: 18825
diff changeset
   564
     * Returns an {@code OptionalDouble} describing the arithmetic mean of elements of
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   565
     * this stream, or an empty optional if this stream is empty.  This is a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   566
     * special case of a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   567
     * <a href="package-summary.html#Reduction">reduction</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   568
     *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   569
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   570
     * operation</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   571
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   572
     * @return an {@code OptionalDouble} containing the average element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   573
     * stream, or an empty optional if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   574
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   575
    OptionalDouble average();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   576
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   577
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   578
     * Returns a {@code LongSummaryStatistics} describing various summary data
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   579
     * about the elements of this stream.  This is a special case of a
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   580
     * <a href="package-summary.html#Reduction">reduction</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   581
     *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   582
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   583
     * operation</a>.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   584
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   585
     * @return a {@code LongSummaryStatistics} describing various summary data
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   586
     * about the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   587
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   588
    LongSummaryStatistics summaryStatistics();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   589
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   590
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   591
     * Returns whether any elements of this stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   592
     * predicate.  May not evaluate the predicate on all elements if not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   593
     * necessary for determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   594
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   595
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   596
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   597
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   598
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   599
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   600
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   601
     * @return {@code true} if any elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   602
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   603
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   604
    boolean anyMatch(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   605
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   606
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   607
     * Returns whether all elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   608
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   609
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   610
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   611
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   612
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   613
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   614
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   615
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   616
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   617
     * @return {@code true} if all elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   618
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   619
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   620
    boolean allMatch(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   621
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   622
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   623
     * Returns whether no elements of this stream match the provided  predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   624
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   625
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   626
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   627
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   628
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   629
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   630
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   631
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   632
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   633
     * @return {@code true} if no elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   634
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   635
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   636
    boolean noneMatch(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   637
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   638
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   639
     * Returns an {@link OptionalLong} describing the first element of this
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   640
     * stream, or an empty {@code OptionalLong} if the stream is empty.  If the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   641
     * stream has no encounter order, then any element may be returned.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   642
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   643
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   644
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   645
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   646
     * @return an {@code OptionalLong} describing the first element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   647
     * stream, or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   648
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   649
    OptionalLong findFirst();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   650
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   651
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   652
     * Returns an {@link OptionalLong} describing some element of the stream, or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   653
     * an empty {@code OptionalLong} if the stream is empty.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   654
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   655
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   656
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   657
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   658
     * <p>The behavior of this operation is explicitly nondeterministic; it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   659
     * free to select any element in the stream.  This is to allow for maximal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   660
     * performance in parallel operations; the cost is that multiple invocations
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   661
     * 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
   662
     * is desired, use {@link #findFirst()} instead.)
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   663
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   664
     * @return an {@code OptionalLong} describing some element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   665
     * or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   666
     * @see #findFirst()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   667
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   668
    OptionalLong findAny();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   669
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   670
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   671
     * Returns a {@code DoubleStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   672
     * converted to {@code double}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   673
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   674
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   675
     * operation</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   676
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   677
     * @return a {@code DoubleStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   678
     * converted to {@code double}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   679
     */
18154
5ede18269905 8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents: 17914
diff changeset
   680
    DoubleStream asDoubleStream();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   681
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   682
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   683
     * Returns a {@code Stream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   684
     * each boxed to a {@code Long}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   685
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   686
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   687
     * operation</a>.
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   688
     *
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   689
     * @return a {@code Stream} consistent of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   690
     * each boxed to {@code Long}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   691
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   692
    Stream<Long> boxed();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   693
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   694
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   695
    LongStream sequential();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   696
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   697
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   698
    LongStream parallel();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   699
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   700
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   701
    PrimitiveIterator.OfLong iterator();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   702
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   703
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   704
    Spliterator.OfLong spliterator();
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   705
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   706
    // Static factories
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   707
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   708
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   709
     * Returns a builder for a {@code LongStream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   710
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   711
     * @return a stream builder
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   712
     */
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   713
    public static Builder builder() {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   714
        return new Streams.LongStreamBuilderImpl();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   715
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   716
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   717
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   718
     * Returns an empty sequential {@code LongStream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   719
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   720
     * @return an empty sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   721
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   722
    public static LongStream empty() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   723
        return StreamSupport.longStream(Spliterators.emptyLongSpliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   724
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   725
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   726
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   727
     * Returns a sequential {@code LongStream} containing a single element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   728
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   729
     * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   730
     * @return a singleton sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   731
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   732
    public static LongStream of(long t) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   733
        return StreamSupport.longStream(new Streams.LongStreamBuilderImpl(t), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   734
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   735
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   736
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   737
     * Returns a sequential ordered stream whose elements are the specified values.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   738
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   739
     * @param values the elements of the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   740
     * @return the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   741
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   742
    public static LongStream of(long... values) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   743
        return Arrays.stream(values);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   744
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   745
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   746
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   747
     * Returns an infinite sequential ordered {@code LongStream} produced by iterative
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   748
     * application of a function {@code f} to an initial element {@code seed},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   749
     * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   750
     * {@code f(f(seed))}, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   751
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   752
     * <p>The first element (position {@code 0}) in the {@code LongStream} will
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   753
     * be the provided {@code seed}.  For {@code n > 0}, the element at position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   754
     * {@code n}, will be the result of applying the function {@code f} to the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   755
     * element at position {@code n - 1}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   756
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   757
     * @param seed the initial element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   758
     * @param f a function to be applied to to the previous element to produce
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   759
     *          a new element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   760
     * @return a new sequential {@code LongStream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   761
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   762
    public static LongStream iterate(final long seed, final LongUnaryOperator f) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   763
        Objects.requireNonNull(f);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   764
        final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   765
            long t = seed;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   766
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   767
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   768
            public boolean hasNext() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   769
                return true;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   770
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   771
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   772
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   773
            public long nextLong() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   774
                long v = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   775
                t = f.applyAsLong(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   776
                return v;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   777
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   778
        };
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   779
        return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   780
                iterator,
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   781
                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   782
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   783
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   784
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   785
     * Returns a sequential stream where each element is generated by
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   786
     * the provided {@code LongSupplier}.  This is suitable for generating
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   787
     * constant streams, streams of random elements, etc.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   788
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   789
     * @param s the {@code LongSupplier} for generated elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   790
     * @return a new sequential {@code LongStream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   791
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   792
    public static LongStream generate(LongSupplier s) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   793
        Objects.requireNonNull(s);
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 18158
diff changeset
   794
        return StreamSupport.longStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   795
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   796
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   797
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   798
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   799
     * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   800
     * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   801
     * {@code 1}.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   802
     *
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   803
     * @apiNote
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   804
     * <p>An equivalent sequence of increasing values can be produced
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   805
     * sequentially using a {@code for} loop as follows:
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   806
     * <pre>{@code
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   807
     *     for (long i = startInclusive; i < endExclusive ; i++) { ... }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   808
     * }</pre>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   809
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   810
     * @param startInclusive the (inclusive) initial value
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   811
     * @param endExclusive the exclusive upper bound
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   812
     * @return a sequential {@code LongStream} for the range of {@code long}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   813
     *         elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   814
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   815
    public static LongStream range(long startInclusive, final long endExclusive) {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   816
        if (startInclusive >= endExclusive) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   817
            return empty();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   818
        } else if (endExclusive - startInclusive < 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   819
            // Size of range > Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   820
            // Split the range in two and concatenate
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   821
            // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   822
            // the lower range, [Long.MIN_VALUE, 0) will be further split in two
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   823
            long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   824
            return concat(range(startInclusive, m), range(m, endExclusive));
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   825
        } else {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   826
            return StreamSupport.longStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   827
                    new Streams.RangeLongSpliterator(startInclusive, endExclusive, false), false);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   828
        }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   829
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   830
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   831
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   832
     * Returns a sequential ordered {@code LongStream} from {@code startInclusive}
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   833
     * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   834
     * {@code 1}.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   835
     *
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   836
     * @apiNote
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   837
     * <p>An equivalent sequence of increasing values can be produced
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   838
     * sequentially using a {@code for} loop as follows:
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   839
     * <pre>{@code
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   840
     *     for (long i = startInclusive; i <= endInclusive ; i++) { ... }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   841
     * }</pre>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   842
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   843
     * @param startInclusive the (inclusive) initial value
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   844
     * @param endInclusive the inclusive upper bound
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   845
     * @return a sequential {@code LongStream} for the range of {@code long}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   846
     *         elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   847
     */
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   848
    public static LongStream rangeClosed(long startInclusive, final long endInclusive) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   849
        if (startInclusive > endInclusive) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   850
            return empty();
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   851
        } else if (endInclusive - startInclusive + 1 <= 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   852
            // Size of range > Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   853
            // Split the range in two and concatenate
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   854
            // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   855
            // the lower range, [Long.MIN_VALUE, 0), and upper range,
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   856
            // [0, Long.MAX_VALUE], will both be further split in two
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   857
            long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   858
            return concat(range(startInclusive, m), rangeClosed(m, endInclusive));
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   859
        } else {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   860
            return StreamSupport.longStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   861
                    new Streams.RangeLongSpliterator(startInclusive, endInclusive, true), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   862
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   863
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   864
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   865
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   866
     * Creates a lazily concatenated stream whose elements are all the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   867
     * elements of the first stream followed by all the elements of the
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   868
     * second stream. The resulting stream is ordered if both
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   869
     * 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
   870
     * streams is parallel.  When the resulting stream is closed, the close
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19214
diff changeset
   871
     * handlers for both input streams are invoked.
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   872
     *
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   873
     * @param a the first stream
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   874
     * @param b the second stream
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   875
     * @return the concatenation of the two input streams
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   876
     */
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   877
    public static LongStream concat(LongStream a, LongStream b) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   878
        Objects.requireNonNull(a);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   879
        Objects.requireNonNull(b);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   880
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   881
        Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong(
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   882
                a.spliterator(), b.spliterator());
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19214
diff changeset
   883
        LongStream stream = StreamSupport.longStream(split, a.isParallel() || b.isParallel());
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 19214
diff changeset
   884
        return stream.onClose(Streams.composedClose(a, b));
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   885
    }
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   886
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   887
    /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   888
     * A mutable builder for a {@code LongStream}.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   889
     *
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   890
     * <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
   891
     * 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
   892
     * phase, after which elements may not be added.  The built phase begins
18825
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   893
     * 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
   894
     * 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
   895
     * stream builder, in the order they were added.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   896
     *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   897
     * @see LongStream#builder()
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   898
     * @since 1.8
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   899
     */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   900
    public interface Builder extends LongConsumer {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   901
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   902
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   903
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   904
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   905
         * @throws IllegalStateException if the builder has already transitioned
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   906
         * to the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   907
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   908
        @Override
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   909
        void accept(long t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   910
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   911
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   912
         * Adds an element to the stream being built.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   913
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   914
         * @implSpec
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   915
         * The default implementation behaves as if:
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   916
         * <pre>{@code
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   917
         *     accept(t)
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   918
         *     return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   919
         * }</pre>
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   920
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   921
         * @param t the element to add
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   922
         * @return {@code this} builder
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   923
         * @throws IllegalStateException if the builder has already transitioned
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   924
         * to the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   925
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   926
        default Builder add(long t) {
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   927
            accept(t);
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   928
            return this;
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   929
        }
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   930
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   931
        /**
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   932
         * Builds the stream, transitioning this builder to the built state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   933
         * An {@code IllegalStateException} is thrown if there are further
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   934
         * 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
   935
         * state.
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   936
         *
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   937
         * @return the built stream
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   938
         * @throws IllegalStateException if the builder has already transitioned
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   939
         * to the built state
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   940
         */
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   941
        LongStream build();
06636235cd12 8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents: 18822
diff changeset
   942
    }
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   943
}