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