forked from kangjianwei/LearningJDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseStream.java
183 lines (172 loc) · 7.44 KB
/
BaseStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package java.util.stream;
import java.util.Iterator;
import java.util.Spliterator;
/**
* Base interface for streams, which are sequences of elements supporting sequential and parallel aggregate operations.
* The following example illustrates an aggregate operation using the stream types {@link Stream} and {@link IntStream},
* computing the sum of the weights of the red widgets:
*
* <pre>{@code
* int sum = widgets.stream()
* .filter(w -> w.getColor() == RED)
* .mapToInt(w -> w.getWeight())
* .sum();
* }</pre>
*
* See the class documentation for {@link Stream} and the package documentation
* for <a href="package-summary.html">java.util.stream</a> for additional specification of streams,
* stream operations, stream pipelines, and parallelism, which governs the behavior of all stream types.
*
* @param <T> the type of the stream elements
* @param <S> the type of the stream implementing {@code BaseStream}
*
* @see Stream
* @see IntStream
* @see LongStream
* @see DoubleStream
* @see <a href="package-summary.html">java.util.stream</a>
* @since 1.8
*/
// 流的基本接口。流是支持顺序和并行聚合操作的元素序列。
public interface BaseStream<T, S extends BaseStream<T, S>> extends AutoCloseable {
/**
* Returns an iterator for the elements of this stream.
*
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>.
*
* @return the element iterator for this stream
*/
// 返回流中元素的Iterator(迭代器)
Iterator<T> iterator();
/**
* Returns a spliterator for the elements of this stream.
*
* <p>This is a <a href="package-summary.html#StreamOps">terminal
* operation</a>.
*
* <p>
* The returned spliterator should report the set of characteristics derived
* from the stream pipeline (namely the characteristics derived from the
* stream source spliterator and the intermediate operations).
* Implementations may report a sub-set of those characteristics. For
* example, it may be too expensive to compute the entire set for some or
* all possible stream pipelines.
*
* @return the element spliterator for this stream
*/
// 返回流中元素的Spliterator(可分割的迭代器)
Spliterator<T> spliterator();
/**
* Returns whether this stream, if a terminal operation were to be executed,
* would execute in parallel. Calling this method after invoking an
* terminal stream operation method may yield unpredictable results.
*
* @return {@code true} if this stream would execute in parallel if executed
*/
// 是否需要并行处理
boolean isParallel();
/**
* Returns an equivalent stream that is
* <a href="package-summary.html#Ordering">unordered</a>. May return
* itself, either because the stream was already unordered, or because
* the underlying stream state was modified to be unordered.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* @return an unordered stream
*/
/*
* 中间操作,返回等效的无序流。
* 可能会返回自身,因为该流已经是无序的,或流的状态已经被修改为无序。
*/
S unordered();
/**
* Returns an equivalent stream that is sequential. May return
* itself, either because the stream was already sequential, or because
* the underlying stream state was modified to be sequential.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* @return a sequential stream
*/
/*
* 中间操作,返回顺序的等效流。
* 可能会返回自身,因为该流已经是顺序的,或流的状态已经被修改为顺序。
*/
S sequential();
/**
* Returns an equivalent stream that is parallel. May return
* itself, either because the stream was already parallel, or because
* the underlying stream state was modified to be parallel.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* @return a parallel stream
*/
/*
* 中间操作,返回并行的等效流。
* 可能会返回自身,因为流已经是并行的,或流的状态已经被修改为并行。
*/
S parallel();
/**
* Returns an equivalent stream with an additional close handler. Close
* handlers are run when the {@link #close()} method
* is called on the stream, and are executed in the order they were
* added. All close handlers are run, even if earlier close handlers throw
* exceptions. If any close handler throws an exception, the first
* exception thrown will be relayed to the caller of {@code close()}, with
* any remaining exceptions added to that exception as suppressed exceptions
* (unless one of the remaining exceptions is the same exception as the
* first exception, since an exception cannot suppress itself.) May
* return itself.
*
* <p>This is an <a href="package-summary.html#StreamOps">intermediate
* operation</a>.
*
* @param closeHandler A task to execute when the stream is closed
*
* @return a stream with a handler that is run if the stream is closed
*/
/*
* 中间操作,返回附加close操作的等效流,可能会返回自身。
* 在流上调用close()方法时,将运行关闭处理程序,并按其添加的顺序执行。
* 即使先前的关闭处理程序抛出异常,也会运行所有关闭处理程序。
* 如果任何关闭处理程序抛出异常,则抛出的第一个异常将被转播到close()的调用者,并将任何剩余的异常作为抑制异常添加到该异常中(除非剩下的异常之一与第一个异常相同,因为异常无法抑制自己)。
*/
S onClose(Runnable closeHandler);
/**
* Closes this stream, causing all close handlers for this stream pipeline to be called.
*
* @see AutoCloseable#close()
*/
// 关闭此流,这将导致调用此流水线的所有关闭处理程序。
@Override
void close();
}