jdk/src/share/classes/java/util/stream/LongStream.java
author psandoz
Wed, 03 Jul 2013 21:43:49 +0200
changeset 18822 4b6be7c19547
parent 18820 a87cdd6a8834
child 18825 06636235cd12
permissions -rw-r--r--
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method Reviewed-by: henryjen, briangoetz
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
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    27
import java.util.Arrays;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    28
import java.util.LongSummaryStatistics;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    29
import java.util.Objects;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    30
import java.util.OptionalDouble;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    31
import java.util.OptionalLong;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    32
import java.util.PrimitiveIterator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    33
import java.util.Spliterator;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    34
import java.util.Spliterators;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    35
import java.util.function.BiConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    36
import java.util.function.Function;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    37
import java.util.function.LongBinaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    38
import java.util.function.LongConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    39
import java.util.function.LongFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    40
import java.util.function.LongPredicate;
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
    41
import java.util.function.LongSupplier;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    42
import java.util.function.LongToDoubleFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    43
import java.util.function.LongToIntFunction;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    44
import java.util.function.LongUnaryOperator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    45
import java.util.function.ObjLongConsumer;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    46
import java.util.function.Supplier;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    47
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    48
/**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    49
 * A sequence of primitive long elements supporting sequential and parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    50
 * bulk operations. Streams support lazy intermediate operations (transforming
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    51
 * a stream to another stream) such as {@code filter} and {@code map}, and terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    52
 * operations (consuming the contents of a stream to produce a result or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    53
 * side-effect), such as {@code forEach}, {@code findFirst}, and {@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    54
 * iterator}.  Once an operation has been performed on a stream, it
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    55
 * is considered <em>consumed</em> and no longer usable for other operations.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    56
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    57
 * <p>For sequential stream pipelines, all operations are performed in the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    58
 * <a href="package-summary.html#Ordering">encounter order</a> of the pipeline
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    59
 * source, if the pipeline source has a defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    60
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    61
 * <p>For parallel stream pipelines, unless otherwise specified, intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    62
 * stream operations preserve the <a href="package-summary.html#Ordering">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    63
 * encounter order</a> of their source, and terminal operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    64
 * respect the encounter order of their source, if the source
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    65
 * has an encounter order.  Provided that and parameters to stream operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    66
 * satisfy the <a href="package-summary.html#NonInterference">non-interference
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    67
 * requirements</a>, and excepting differences arising from the absence of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    68
 * a defined encounter order, the result of a stream pipeline should be the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    69
 * stable across multiple executions of the same operations on the same source.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    70
 * However, the timing and thread in which side-effects occur (for those
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    71
 * operations which are allowed to produce side-effects, such as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    72
 * {@link #forEach(LongConsumer)}), are explicitly nondeterministic for parallel
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    73
 * execution of stream pipelines.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    74
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    75
 * <p>Unless otherwise noted, passing a {@code null} argument to any stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    76
 * method may result in a {@link NullPointerException}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    77
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    78
 * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    79
 * Streams are not data structures; they do not manage the storage for their
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    80
 * elements, nor do they support access to individual elements.  However,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    81
 * you can use the {@link #iterator()} or {@link #spliterator()} operations to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    82
 * perform a controlled traversal.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    83
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    84
 * @since 1.8
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    85
 * @see <a href="package-summary.html">java.util.stream</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    86
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    87
public interface LongStream extends BaseStream<Long, LongStream> {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    88
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    89
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    90
     * Returns a stream consisting of the elements of this stream that match
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    91
     * the given predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    92
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    93
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    94
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    95
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    96
     * @param predicate a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    97
     *                  non-interfering, stateless</a> predicate to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    98
     *                  each element to determine if it should be included
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    99
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   100
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   101
    LongStream filter(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   102
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   103
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   104
     * Returns a stream consisting of the results of applying the given
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   105
     * function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   106
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   107
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   108
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   109
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   110
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   111
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   112
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   113
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   114
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   115
    LongStream map(LongUnaryOperator mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   116
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   117
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   118
     * Returns an object-valued {@code Stream} consisting of the results of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   119
     * applying the given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   120
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   121
     * <p>This is an <a href="package-summary.html#StreamOps">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   122
     *     intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   123
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   124
     * @param <U> the element type of the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   125
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   126
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   127
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   128
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   129
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   130
    <U> Stream<U> mapToObj(LongFunction<? extends U> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   131
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   132
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   133
     * Returns an {@code IntStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   134
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   135
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   136
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   137
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   138
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   139
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   140
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   141
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   142
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   143
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   144
    IntStream mapToInt(LongToIntFunction mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   145
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   146
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   147
     * Returns a {@code DoubleStream} consisting of the results of applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   148
     * given function to the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   149
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   150
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   151
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   152
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   153
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   154
     *               non-interfering, stateless</a> function to apply to each
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   155
     *               element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   156
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   157
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   158
    DoubleStream mapToDouble(LongToDoubleFunction mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   159
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   160
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   161
     * Returns a stream consisting of the results of replacing each element of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   162
     * this stream with the contents of the stream produced by applying the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   163
     * provided mapping function to each element.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   164
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   165
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   166
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   167
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   168
     * @apiNote
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   169
     * The {@code flatMap()} operation has the effect of applying a one-to-many
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   170
     * tranformation to the elements of the stream, and then flattening the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   171
     * resulting elements into a new stream. For example, if {@code orders}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   172
     * is a stream of purchase orders, and each purchase order contains a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   173
     * collection of line items, then the following produces a stream of line
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   174
     * items:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   175
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   176
     *     orderStream.flatMap(order -> order.getLineItems().stream())...
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   177
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   178
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   179
     * @param mapper a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   180
     *               non-interfering, stateless</a> function to apply to
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   181
     *               each element which produces an {@code LongStream} of new
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   182
     *               values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   183
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   184
     * @see Stream#flatMap(Function)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   185
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   186
    LongStream flatMap(LongFunction<? extends LongStream> mapper);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   187
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   188
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   189
     * Returns a stream consisting of the distinct elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   190
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   191
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   192
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   193
     *
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
    LongStream distinct();
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 stream consisting of the elements of this stream in sorted
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   200
     * order.
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 a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   203
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   204
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   205
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   206
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   207
    LongStream sorted();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   208
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   209
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   210
     * Returns a stream consisting of the elements of this stream, additionally
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   211
     * performing the provided action on each element as elements are consumed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   212
     * from the resulting stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   213
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   214
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   215
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   216
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   217
     * <p>For parallel stream pipelines, the action may be called at
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   218
     * whatever time and in whatever thread the element is made available by the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   219
     * upstream operation.  If the action modifies shared state,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   220
     * it is responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   221
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   222
     * @apiNote This method exists mainly to support debugging, where you want
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   223
     * to see the elements as they flow past a certain point in a pipeline:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   224
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   225
     *     list.stream()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   226
     *         .filter(filteringFunction)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   227
     *         .peek(e -> {System.out.println("Filtered value: " + e); });
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   228
     *         .map(mappingFunction)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   229
     *         .peek(e -> {System.out.println("Mapped value: " + e); });
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   230
     *         .collect(Collectors.toLongSummaryStastistics());
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   231
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   232
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   233
     * @param consumer a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   234
     *                 non-interfering</a> action to perform on the elements as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   235
     *                 they are consumed from the stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   236
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   237
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   238
    LongStream peek(LongConsumer consumer);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   239
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   240
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   241
     * Returns a stream consisting of the elements of this stream, truncated
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   242
     * to be no longer than {@code maxSize} in length.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   243
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   244
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   245
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   246
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   247
     * @param maxSize the number of elements the stream should be limited to
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
     * @throws IllegalArgumentException if {@code maxSize} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   250
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   251
    LongStream limit(long maxSize);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   252
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   253
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   254
     * Returns a stream consisting of the remaining elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   255
     * after indexing {@code startInclusive} elements into the stream. If the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   256
     * {@code startInclusive} index lies past the end of this stream then an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   257
     * empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   258
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   259
     * <p>This is a <a href="package-summary.html#StreamOps">stateful
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   260
     * intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   261
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   262
     * @param startInclusive the number of leading elements to skip
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   263
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   264
     * @throws IllegalArgumentException if {@code startInclusive} is negative
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   265
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   266
    LongStream substream(long startInclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   267
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   268
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   269
     * Returns a stream consisting of the remaining elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   270
     * after indexing {@code startInclusive} elements into the stream and
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   271
     * truncated to contain no more than {@code endExclusive - startInclusive}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   272
     * elements. If the {@code startInclusive} index lies past the end
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   273
     * of this stream then an empty stream will be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   274
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   275
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   276
     * stateful intermediate operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   277
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   278
     * @param startInclusive the starting position of the substream, inclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   279
     * @param endExclusive the ending position of the substream, exclusive
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   280
     * @return the new stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   281
     * @throws IllegalArgumentException if {@code startInclusive} or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   282
     * {@code endExclusive} is negative or {@code startInclusive} is greater
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   283
     * than {@code endExclusive}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   284
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   285
    LongStream substream(long startInclusive, long endExclusive);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   286
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   287
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   288
     * Performs an action for each element of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   289
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   290
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   291
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   292
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   293
     * <p>For parallel stream pipelines, this operation does <em>not</em>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   294
     * guarantee to respect the encounter order of the stream, as doing so
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   295
     * would sacrifice the benefit of parallelism.  For any given element, the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   296
     * action may be performed at whatever time and in whatever thread the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   297
     * library chooses.  If the action accesses shared state, it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   298
     * responsible for providing the required synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   299
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   300
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   301
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   302
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   303
    void forEach(LongConsumer action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   304
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   305
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   306
     * Performs an action for each element of this stream, guaranteeing that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   307
     * each element is processed in encounter order for streams that have a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   308
     * defined encounter order.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   309
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   310
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   311
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   312
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   313
     * @param action a <a href="package-summary.html#NonInterference">
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   314
     *               non-interfering</a> action to perform on the elements
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   315
     * @see #forEach(LongConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   316
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   317
    void forEachOrdered(LongConsumer action);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   318
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   319
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   320
     * Returns an array containing the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   321
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   322
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   323
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   324
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   325
     * @return an array containing the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   326
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   327
    long[] toArray();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   328
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   329
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   330
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   331
     * elements of this stream, using the provided identity value and an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   332
     * <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   333
     * accumulation function, and returns the reduced value.  This is equivalent
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   334
     * to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   335
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   336
     *     long result = identity;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   337
     *     for (long element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   338
     *         result = accumulator.apply(result, element)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   339
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   340
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   341
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   342
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   343
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   344
     * <p>The {@code identity} value must be an identity for the accumulator
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   345
     * function. This means that for all {@code x},
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   346
     * {@code accumulator.apply(identity, x)} is equal to {@code x}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   347
     * The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   348
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   349
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   350
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   351
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   352
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   353
     * @apiNote Sum, min, max, and average are all special cases of reduction.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   354
     * Summing a stream of numbers can be expressed as:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   355
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   356
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   357
     *     long sum = integers.reduce(0, (a, b) -> a+b);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   358
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   359
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   360
     * or more compactly:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   361
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   362
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   363
     *     long sum = integers.reduce(0, Long::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   364
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   365
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   366
     * <p>While this may seem a more roundabout way to perform an aggregation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   367
     * compared to simply mutating a running total in a loop, reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   368
     * operations parallelize more gracefully, without needing additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   369
     * synchronization and with greatly reduced risk of data races.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   370
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   371
     * @param identity the identity value for the accumulating function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   372
     * @param op an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   373
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   374
     *                    stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   375
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   376
     * @see #sum()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   377
     * @see #min()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   378
     * @see #max()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   379
     * @see #average()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   380
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   381
    long reduce(long identity, LongBinaryOperator op);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   382
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   383
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   384
     * Performs a <a href="package-summary.html#Reduction">reduction</a> on the
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   385
     * elements of this stream, using an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   386
     * <a href="package-summary.html#Associativity">associative</a> accumulation
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   387
     * function, and returns an {@code OptionalLong} describing the reduced value,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   388
     * if any. This is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   389
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   390
     *     boolean foundAny = false;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   391
     *     long result = null;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   392
     *     for (long element : this stream) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   393
     *         if (!foundAny) {
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   394
     *             foundAny = true;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   395
     *             result = element;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   396
     *         }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   397
     *         else
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   398
     *             result = accumulator.apply(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   399
     *     }
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   400
     *     return foundAny ? OptionalLong.of(result) : OptionalLong.empty();
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
     * but is not constrained to execute sequentially.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   404
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   405
     * <p>The {@code accumulator} function must be an
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   406
     * <a href="package-summary.html#Associativity">associative</a> function.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   407
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   408
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   409
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   410
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   411
     * @param op an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   412
     *           <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   413
     *           stateless</a> function for combining two values
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   414
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   415
     * @see #reduce(long, LongBinaryOperator)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   416
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   417
    OptionalLong reduce(LongBinaryOperator op);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   418
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   419
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   420
     * Performs a <a href="package-summary.html#MutableReduction">mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   421
     * reduction</a> operation on the elements of this stream.  A mutable
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   422
     * reduction is one in which the reduced value is a mutable value holder,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   423
     * such as an {@code ArrayList}, and elements are incorporated by updating
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   424
     * the state of the result, rather than by replacing the result.  This
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   425
     * produces a result equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   426
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   427
     *     R result = resultFactory.get();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   428
     *     for (long element : this stream)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   429
     *         accumulator.accept(result, element);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   430
     *     return result;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   431
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   432
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   433
     * <p>Like {@link #reduce(long, LongBinaryOperator)}, {@code collect} operations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   434
     * can be parallelized without requiring additional synchronization.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   435
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   436
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   437
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   438
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   439
     * @param <R> type of the result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   440
     * @param resultFactory a function that creates a new result container.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   441
     *                      For a parallel execution, this function may be
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   442
     *                      called multiple times and must return a fresh value
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   443
     *                      each time.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   444
     * @param accumulator an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   445
     *                    <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   446
     *                    stateless</a> function for incorporating an additional
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   447
     *                    element into a result
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   448
     * @param combiner an <a href="package-summary.html#Associativity">associative</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   449
     *                 <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   450
     *                 stateless</a> function for combining two values, which
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   451
     *                 must be compatible with the accumulator function
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   452
     * @return the result of the reduction
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   453
     * @see Stream#collect(Supplier, BiConsumer, BiConsumer)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   454
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   455
    <R> R collect(Supplier<R> resultFactory,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   456
                  ObjLongConsumer<R> accumulator,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   457
                  BiConsumer<R, R> combiner);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   458
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   459
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   460
     * Returns the sum of elements in this stream.  This is a special case
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   461
     * of a <a href="package-summary.html#MutableReduction">reduction</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   462
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   463
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   464
     *     return reduce(0, Long::sum);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   465
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   466
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   467
     * @return the sum of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   468
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   469
    long sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   470
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   471
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   472
     * Returns an {@code OptionalLong} describing the minimum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   473
     * stream, or an empty optional if this stream is empty.  This is a special
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   474
     * case of a <a href="package-summary.html#MutableReduction">reduction</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   475
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   476
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   477
     *     return reduce(Long::min);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   478
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   479
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   480
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   481
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   482
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   483
     * @return an {@code OptionalLong} containing the minimum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   484
     * stream, or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   485
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   486
    OptionalLong min();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   487
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   488
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   489
     * Returns an {@code OptionalLong} describing the maximum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   490
     * stream, or an empty optional if this stream is empty.  This is a special
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   491
     * case of a <a href="package-summary.html#MutableReduction">reduction</a>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   492
     * and is equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   493
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   494
     *     return reduce(Long::max);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   495
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   496
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   497
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   498
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   499
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   500
     * @return an {@code OptionalLong} containing the maximum element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   501
     * stream, or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   502
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   503
    OptionalLong max();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   504
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   505
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   506
     * Returns the count of elements in this stream.  This is a special case of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   507
     * a <a href="package-summary.html#MutableReduction">reduction</a> and is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   508
     * equivalent to:
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   509
     * <pre>{@code
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   510
     *     return map(e -> 1L).sum();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   511
     * }</pre>
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   512
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   513
     * <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   514
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   515
     * @return the count of elements in this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   516
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   517
    long count();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   518
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   519
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   520
     * Returns an {@code OptionalDouble} describing the average of elements of
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   521
     * this stream, or an empty optional if this stream is empty.  This is a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   522
     * special case of a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   523
     * <a href="package-summary.html#MutableReduction">reduction</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   524
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   525
     * @return an {@code OptionalDouble} containing the average element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   526
     * stream, or an empty optional if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   527
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   528
    OptionalDouble average();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   529
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   530
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   531
     * Returns a {@code LongSummaryStatistics} describing various summary data
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   532
     * about the elements of this stream.  This is a special case of a
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   533
     * <a href="package-summary.html#MutableReduction">reduction</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   534
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   535
     * @return a {@code LongSummaryStatistics} describing various summary data
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   536
     * about the elements of this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   537
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   538
    LongSummaryStatistics summaryStatistics();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   539
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   540
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   541
     * Returns whether any elements of this stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   542
     * predicate.  May not evaluate the predicate on all elements if not
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   543
     * necessary for determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   544
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   545
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   546
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   547
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   548
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   549
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   550
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   551
     * @return {@code true} if any elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   552
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   553
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   554
    boolean anyMatch(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   555
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   556
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   557
     * Returns whether all elements of this stream match the provided predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   558
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   559
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   560
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   561
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   562
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   563
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   564
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   565
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   566
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   567
     * @return {@code true} if all elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   568
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   569
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   570
    boolean allMatch(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   571
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   572
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   573
     * Returns whether no elements of this stream match the provided  predicate.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   574
     * May not evaluate the predicate on all elements if not necessary for
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   575
     * determining the result.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   576
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   577
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   578
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   579
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   580
     * @param predicate a <a href="package-summary.html#NonInterference">non-interfering,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   581
     *                  stateless</a> predicate to apply to elements of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   582
     *                  stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   583
     * @return {@code true} if no elements of the stream match the provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   584
     * predicate otherwise {@code false}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   585
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   586
    boolean noneMatch(LongPredicate predicate);
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   587
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   588
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   589
     * Returns an {@link OptionalLong} describing the first element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   590
     * stream (in the encounter order), or an empty {@code OptionalLong} if the
17914
91e138d3b298 8014393: Minor typo in the spec for j.u.stream.Stream.findFirst()
psandoz
parents: 17195
diff changeset
   591
     * stream is empty.  If the stream has no encounter order, then any element
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   592
     * may be returned.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   593
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   594
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   595
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   596
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   597
     * @return an {@code OptionalLong} describing the first element of this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   598
     * stream, or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   599
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   600
    OptionalLong findFirst();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   601
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   602
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   603
     * Returns an {@link OptionalLong} describing some element of the stream, or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   604
     * an empty {@code OptionalLong} if the stream is empty.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   605
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   606
     * <p>This is a <a href="package-summary.html#StreamOps">short-circuiting
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   607
     * terminal operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   608
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   609
     * <p>The behavior of this operation is explicitly nondeterministic; it is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   610
     * free to select any element in the stream.  This is to allow for maximal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   611
     * performance in parallel operations; the cost is that multiple invocations
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   612
     * on the same source may not return the same result.  (If the first element
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   613
     * in the encounter order is desired, use {@link #findFirst()} instead.)
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   614
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   615
     * @return an {@code OptionalLong} describing some element of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   616
     * or an empty {@code OptionalLong} if the stream is empty
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   617
     * @see #findFirst()
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   618
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   619
    OptionalLong findAny();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   620
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   621
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   622
     * Returns a {@code DoubleStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   623
     * converted to {@code double}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   624
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   625
     * @return a {@code DoubleStream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   626
     * converted to {@code double}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   627
     */
18154
5ede18269905 8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents: 17914
diff changeset
   628
    DoubleStream asDoubleStream();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   629
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   630
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   631
     * Returns a {@code Stream} consisting of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   632
     * each boxed to a {@code Long}.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   633
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   634
     * @return a {@code Stream} consistent of the elements of this stream,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   635
     * each boxed to {@code Long}
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   636
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   637
    Stream<Long> boxed();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   638
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   639
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   640
    LongStream sequential();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   641
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   642
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   643
    LongStream parallel();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   644
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   645
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   646
    PrimitiveIterator.OfLong iterator();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   647
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   648
    @Override
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   649
    Spliterator.OfLong spliterator();
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   650
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   651
    // Static factories
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   652
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   653
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   654
     * Returns a builder for a {@code LongStream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   655
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   656
     * @return a stream builder
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   657
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   658
    public static StreamBuilder.OfLong builder() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   659
        return new Streams.LongStreamBuilderImpl();
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   660
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   661
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   662
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   663
     * Returns an empty sequential {@code LongStream}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   664
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   665
     * @return an empty sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   666
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   667
    public static LongStream empty() {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   668
        return StreamSupport.longStream(Spliterators.emptyLongSpliterator(), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   669
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   670
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   671
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   672
     * Returns a sequential {@code LongStream} containing a single element.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   673
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   674
     * @param t the single element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   675
     * @return a singleton sequential stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   676
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   677
    public static LongStream of(long t) {
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   678
        return StreamSupport.longStream(new Streams.LongStreamBuilderImpl(t), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   679
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   680
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   681
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   682
     * Returns a sequential stream whose elements are the specified values.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   683
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   684
     * @param values the elements of the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   685
     * @return the new stream
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   686
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   687
    public static LongStream of(long... values) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   688
        return Arrays.stream(values);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   689
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   690
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   691
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   692
     * Returns an infinite sequential {@code LongStream} produced by iterative
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   693
     * application of a function {@code f} to an initial element {@code seed},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   694
     * producing a {@code Stream} consisting of {@code seed}, {@code f(seed)},
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   695
     * {@code f(f(seed))}, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   696
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   697
     * <p>The first element (position {@code 0}) in the {@code LongStream} will
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   698
     * be the provided {@code seed}.  For {@code n > 0}, the element at position
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   699
     * {@code n}, will be the result of applying the function {@code f} to the
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   700
     * element at position {@code n - 1}.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   701
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   702
     * @param seed the initial element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   703
     * @param f a function to be applied to to the previous element to produce
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   704
     *          a new element
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   705
     * @return a new sequential {@code LongStream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   706
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   707
    public static LongStream iterate(final long seed, final LongUnaryOperator f) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   708
        Objects.requireNonNull(f);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   709
        final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   710
            long t = seed;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   711
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   712
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   713
            public boolean hasNext() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   714
                return true;
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
            @Override
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   718
            public long nextLong() {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   719
                long v = t;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   720
                t = f.applyAsLong(t);
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   721
                return v;
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   722
            }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   723
        };
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   724
        return StreamSupport.longStream(Spliterators.spliteratorUnknownSize(
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   725
                iterator,
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   726
                Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   727
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   728
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   729
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   730
     * Returns a sequential {@code LongStream} where each element is generated
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   731
     * by a {@code LongSupplier}.  This is suitable for generating constant
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   732
     * streams, streams of random elements, etc.
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   733
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   734
     * @param s the {@code LongSupplier} for generated elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   735
     * @return a new sequential {@code LongStream}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   736
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   737
    public static LongStream generate(LongSupplier s) {
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   738
        Objects.requireNonNull(s);
18572
53b8b8c30086 8012987: Optimizations for Stream.limit/substream
psandoz
parents: 18158
diff changeset
   739
        return StreamSupport.longStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   740
                new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   741
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   742
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   743
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   744
     * Returns a sequential {@code LongStream} from {@code startInclusive}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   745
     * (inclusive) to {@code endExclusive} (exclusive) by an incremental step of
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   746
     * {@code 1}.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   747
     *
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   748
     * @apiNote
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   749
     * <p>An equivalent sequence of increasing values can be produced
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   750
     * sequentially using a {@code for} loop as follows:
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   751
     * <pre>{@code
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   752
     *     for (long i = startInclusive; i < endExclusive ; i++) { ... }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   753
     * }</pre>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   754
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   755
     * @param startInclusive the (inclusive) initial value
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   756
     * @param endExclusive the exclusive upper bound
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   757
     * @return a sequential {@code LongStream} for the range of {@code long}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   758
     *         elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   759
     */
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   760
    public static LongStream range(long startInclusive, final long endExclusive) {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   761
        if (startInclusive >= endExclusive) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   762
            return empty();
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   763
        } else if (endExclusive - startInclusive < 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   764
            // Size of range > Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   765
            // Split the range in two and concatenate
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   766
            // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   767
            // the lower range, [Long.MIN_VALUE, 0) will be further split in two
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   768
            long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   769
            return concat(range(startInclusive, m), range(m, endExclusive));
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   770
        } else {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   771
            return StreamSupport.longStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   772
                    new Streams.RangeLongSpliterator(startInclusive, endExclusive, false), false);
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   773
        }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   774
    }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   775
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   776
    /**
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   777
     * Returns a sequential {@code LongStream} from {@code startInclusive}
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   778
     * (inclusive) to {@code endInclusive} (inclusive) by an incremental step of
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   779
     * {@code 1}.
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   780
     *
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   781
     * @apiNote
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   782
     * <p>An equivalent sequence of increasing values can be produced
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   783
     * sequentially using a {@code for} loop as follows:
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   784
     * <pre>{@code
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   785
     *     for (long i = startInclusive; i <= endInclusive ; i++) { ... }
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   786
     * }</pre>
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   787
     *
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   788
     * @param startInclusive the (inclusive) initial value
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   789
     * @param endInclusive the inclusive upper bound
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   790
     * @return a sequential {@code LongStream} for the range of {@code long}
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   791
     *         elements
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   792
     */
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   793
    public static LongStream rangeClosed(long startInclusive, final long endInclusive) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   794
        if (startInclusive > endInclusive) {
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   795
            return empty();
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   796
        } else if (endInclusive - startInclusive + 1 <= 0) {
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   797
            // Size of range > Long.MAX_VALUE
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   798
            // Split the range in two and concatenate
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   799
            // Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   800
            // the lower range, [Long.MIN_VALUE, 0), and upper range,
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   801
            // [0, Long.MAX_VALUE], will both be further split in two
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   802
            long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1;
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   803
            return concat(range(startInclusive, m), rangeClosed(m, endInclusive));
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   804
        } else {
18158
d5a620310f97 8015895: Int/LongStream.range/rangeClosed
psandoz
parents: 18154
diff changeset
   805
            return StreamSupport.longStream(
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   806
                    new Streams.RangeLongSpliterator(startInclusive, endInclusive, true), false);
17195
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   807
        }
e897ad52979e 8012650: Arrays streams methods
briangoetz
parents: 17167
diff changeset
   808
    }
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   809
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   810
    /**
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   811
     * Creates a lazy concatenated {@code LongStream} whose elements are all the
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   812
     * elements of a first {@code LongStream} succeeded by all the elements of the
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   813
     * second {@code LongStream}. The resulting stream is ordered if both
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   814
     * of the input streams are ordered, and parallel if either of the input
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   815
     * streams is parallel.
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   816
     *
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   817
     * @param a the first stream
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   818
     * @param b the second stream to concatenate on to end of the first stream
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   819
     * @return the concatenation of the two streams
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   820
     */
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   821
    public static LongStream concat(LongStream a, LongStream b) {
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   822
        Objects.requireNonNull(a);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   823
        Objects.requireNonNull(b);
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   824
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   825
        Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong(
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   826
                a.spliterator(), b.spliterator());
18822
4b6be7c19547 8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents: 18820
diff changeset
   827
        return StreamSupport.longStream(split, a.isParallel() || b.isParallel());
18820
a87cdd6a8834 8015315: Stream.concat methods
mduigou
parents: 18572
diff changeset
   828
    }
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   829
}