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