jdk/src/share/classes/java/util/stream/BaseStream.java
author psandoz
Thu, 16 Jan 2014 18:20:31 +0100
changeset 22289 bb9c71b84919
parent 21339 20e8b81964d5
child 25526 d3cbdae6e9f9
permissions -rw-r--r--
8029452: Fork/Join task ForEachOps.ForEachOrderedTask clarifications and minor improvements Reviewed-by: mduigou, briangoetz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     1
/*
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     4
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    10
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    15
 * accompanied this code).
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    16
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    20
 *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    23
 * questions.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    24
 */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    25
package java.util.stream;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    26
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    27
import java.nio.charset.Charset;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    28
import java.nio.file.Files;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    29
import java.nio.file.Path;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    30
import java.util.Collection;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    31
import java.util.Iterator;
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    32
import java.util.Spliterator;
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    33
import java.util.concurrent.ConcurrentHashMap;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    34
import java.util.function.IntConsumer;
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    35
import java.util.function.Predicate;
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    36
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    37
/**
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    38
 * Base interface for streams, which are sequences of elements supporting
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    39
 * sequential and parallel aggregate operations.  The following example
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    40
 * illustrates an aggregate operation using the stream types {@link Stream}
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    41
 * and {@link IntStream}, computing the sum of the weights of the red widgets:
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    42
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    43
 * <pre>{@code
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    44
 *     int sum = widgets.stream()
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    45
 *                      .filter(w -> w.getColor() == RED)
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    46
 *                      .mapToInt(w -> w.getWeight())
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    47
 *                      .sum();
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    48
 * }</pre>
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    49
 *
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    50
 * See the class documentation for {@link Stream} and the package documentation
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    51
 * for <a href="package-summary.html">java.util.stream</a> for additional
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    52
 * specification of streams, stream operations, stream pipelines, and
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    53
 * parallelism, which governs the behavior of all stream types.
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    54
 *
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    55
 * @param <T> the type of the stream elements
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    56
 * @param <S> the type of of the stream implementing {@code BaseStream}
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    57
 * @since 1.8
21339
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    58
 * @see Stream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    59
 * @see IntStream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    60
 * @see LongStream
20e8b81964d5 8025909: Lambda Library Spec Updates
henryjen
parents: 19850
diff changeset
    61
 * @see DoubleStream
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    62
 * @see <a href="package-summary.html">java.util.stream</a>
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    63
 */
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
    64
public interface BaseStream<T, S extends BaseStream<T, S>>
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
    65
        extends AutoCloseable {
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    66
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    67
     * Returns an iterator for the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    68
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    69
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    70
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    71
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    72
     * @return the element iterator for this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    73
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    74
    Iterator<T> iterator();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    75
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    76
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    77
     * Returns a spliterator for the elements of this stream.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    78
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    79
     * <p>This is a <a href="package-summary.html#StreamOps">terminal
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    80
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    81
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    82
     * @return the element spliterator for this stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    83
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    84
    Spliterator<T> spliterator();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    85
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    86
    /**
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    87
     * Returns whether this stream, if a terminal operation were to be executed,
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    88
     * would execute in parallel.  Calling this method after invoking an
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
    89
     * terminal stream operation method may yield unpredictable results.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    90
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    91
     * @return {@code true} if this stream would execute in parallel if executed
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    92
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    93
    boolean isParallel();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    94
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    95
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    96
     * Returns an equivalent stream that is sequential.  May return
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    97
     * itself, either because the stream was already sequential, or because
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    98
     * the underlying stream state was modified to be sequential.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
    99
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   100
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   101
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   102
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   103
     * @return a sequential stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   104
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   105
    S sequential();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   106
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   107
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   108
     * Returns an equivalent stream that is parallel.  May return
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   109
     * itself, either because the stream was already parallel, or because
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   110
     * the underlying stream state was modified to be parallel.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   111
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   112
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   113
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   114
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   115
     * @return a parallel stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   116
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   117
    S parallel();
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   118
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   119
    /**
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   120
     * Returns an equivalent stream that is
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   121
     * <a href="package-summary.html#Ordering">unordered</a>.  May return
19850
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   122
     * itself, either because the stream was already unordered, or because
93b368e54c1c 8011916: Spec update for java.util.stream
henryjen
parents: 19800
diff changeset
   123
     * the underlying stream state was modified to be unordered.
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   124
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   125
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   126
     * operation</a>.
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   127
     *
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   128
     * @return an unordered stream
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   129
     */
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   130
    S unordered();
19800
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   131
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   132
    /**
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   133
     * Returns an equivalent stream with an additional close handler.  Close
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   134
     * handlers are run when the {@link #close()} method
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   135
     * is called on the stream, and are executed in the order they were
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   136
     * added.  All close handlers are run, even if earlier close handlers throw
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   137
     * exceptions.  If any close handler throws an exception, the first
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   138
     * exception thrown will be relayed to the caller of {@code close()}, with
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   139
     * any remaining exceptions added to that exception as suppressed exceptions
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   140
     * (unless one of the remaining exceptions is the same exception as the
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   141
     * first exception, since an exception cannot suppress itself.)  May
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   142
     * return itself.
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   143
     *
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   144
     * <p>This is an <a href="package-summary.html#StreamOps">intermediate
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   145
     * operation</a>.
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   146
     *
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   147
     * @param closeHandler A task to execute when the stream is closed
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   148
     * @return a stream with a handler that is run if the stream is closed
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   149
     */
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   150
    S onClose(Runnable closeHandler);
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   151
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   152
    /**
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   153
     * Closes this stream, causing all close handlers for this stream pipeline
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   154
     * to be called.
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   155
     *
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   156
     * @see AutoCloseable#close()
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   157
     */
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   158
    @Override
6e1fef53ea55 8017513: Support for closeable streams
henryjen
parents: 18789
diff changeset
   159
    void close();
17167
87067e3340d3 8008682: Inital Streams public API
briangoetz
parents:
diff changeset
   160
}