author | henryjen |
Fri, 06 Sep 2013 22:20:01 -0700 | |
changeset 19850 | 93b368e54c1c |
parent 19800 | 6e1fef53ea55 |
child 20866 | 36155ee613ef |
permissions | -rw-r--r-- |
17167 | 1 |
/* |
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
|
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 |
||
19850 | 27 |
import java.nio.charset.Charset; |
28 |
import java.nio.file.Files; |
|
29 |
import java.nio.file.Path; |
|
17195 | 30 |
import java.util.Arrays; |
19850 | 31 |
import java.util.Collection; |
17167 | 32 |
import java.util.LongSummaryStatistics; |
17195 | 33 |
import java.util.Objects; |
17167 | 34 |
import java.util.OptionalDouble; |
35 |
import java.util.OptionalLong; |
|
36 |
import java.util.PrimitiveIterator; |
|
37 |
import java.util.Spliterator; |
|
17195 | 38 |
import java.util.Spliterators; |
19850 | 39 |
import java.util.concurrent.ConcurrentHashMap; |
17167 | 40 |
import java.util.function.BiConsumer; |
41 |
import java.util.function.Function; |
|
42 |
import java.util.function.LongBinaryOperator; |
|
43 |
import java.util.function.LongConsumer; |
|
44 |
import java.util.function.LongFunction; |
|
45 |
import java.util.function.LongPredicate; |
|
17195 | 46 |
import java.util.function.LongSupplier; |
17167 | 47 |
import java.util.function.LongToDoubleFunction; |
48 |
import java.util.function.LongToIntFunction; |
|
49 |
import java.util.function.LongUnaryOperator; |
|
50 |
import java.util.function.ObjLongConsumer; |
|
51 |
import java.util.function.Supplier; |
|
52 |
||
53 |
/** |
|
19850 | 54 |
* A sequence of elements supporting sequential and parallel aggregate |
55 |
* operations. The following example illustrates an aggregate operation using |
|
56 |
* {@link Stream} and {@link LongStream}: |
|
57 |
* |
|
58 |
* <pre>{@code |
|
59 |
* long sum = widgets.stream() |
|
60 |
* .filter(w -> w.getColor() == RED) |
|
61 |
* .mapToLong(w -> w.getWeight()) |
|
62 |
* .sum(); |
|
63 |
* }</pre> |
|
64 |
* |
|
65 |
* In this example, {@code widgets} is a {@code Collection<Widget>}. We create |
|
66 |
* a stream of {@code Widget} objects via {@link Collection#stream Collection.stream()}, |
|
67 |
* filter it to produce a stream containing only the red widgets, and then |
|
68 |
* transform it into a stream of {@code long} values representing the weight of |
|
69 |
* each red widget. Then this stream is summed to produce a total weight. |
|
17167 | 70 |
* |
19850 | 71 |
* <p>To perform a computation, stream |
72 |
* <a href="package-summary.html#StreamOps">operations</a> are composed into a |
|
73 |
* <em>stream pipeline</em>. A stream pipeline consists of a source (which |
|
74 |
* might be an array, a collection, a generator function, an IO channel, |
|
75 |
* etc), zero or more <em>intermediate operations</em> (which transform a |
|
76 |
* stream into another stream, such as {@link LongStream#filter(LongPredicate)}), and a |
|
77 |
* <em>terminal operation</em> (which produces a result or side-effect, such |
|
78 |
* as {@link LongStream#sum()} or {@link LongStream#forEach(LongConsumer)}). |
|
79 |
* Streams are lazy; computation on the source data is only performed when the |
|
80 |
* terminal operation is initiated, and source elements are consumed only |
|
81 |
* as needed. |
|
82 |
* |
|
83 |
* <p>Collections and streams, while bearing some superficial similarities, |
|
84 |
* have different goals. Collections are primarily concerned with the efficient |
|
85 |
* management of, and access to, their elements. By contrast, streams do not |
|
86 |
* provide a means to directly access or manipulate their elements, and are |
|
87 |
* instead concerned with declaratively describing their source and the |
|
88 |
* computational operations which will be performed in aggregate on that source. |
|
89 |
* However, if the provided stream operations do not offer the desired |
|
90 |
* functionality, the {@link #iterator()} and {@link #spliterator()} operations |
|
91 |
* can be used to perform a controlled traversal. |
|
17167 | 92 |
* |
19850 | 93 |
* <p>A stream pipeline, like the "widgets" example above, can be viewed as |
94 |
* a <em>query</em> on the stream source. Unless the source was explicitly |
|
95 |
* designed for concurrent modification (such as a {@link ConcurrentHashMap}), |
|
96 |
* unpredictable or erroneous behavior may result from modifying the stream |
|
97 |
* source while it is being queried. |
|
98 |
* |
|
99 |
* <p>Most stream operations accept parameters that describe user-specified |
|
100 |
* behavior, such as the lambda expression {@code w -> w.getWeight()} passed to |
|
101 |
* {@code mapToLong} in the example above. Such parameters are always instances |
|
102 |
* of a <a href="../function/package-summary.html">functional interface</a> such |
|
103 |
* as {@link java.util.function.Function}, and are often lambda expressions or |
|
104 |
* method references. These parameters can never be null, should not modify the |
|
105 |
* stream source, and should be |
|
106 |
* <a href="package-summary.html#NonInterference">effectively stateless</a> |
|
107 |
* (their result should not depend on any state that might change during |
|
108 |
* execution of the stream pipeline.) |
|
17167 | 109 |
* |
19850 | 110 |
* <p>A stream should be operated on (invoking an intermediate or terminal stream |
111 |
* operation) only once. This rules out, for example, "forked" streams, where |
|
112 |
* the same source feeds two or more pipelines, or multiple traversals of the |
|
113 |
* same stream. A stream implementation may throw {@link IllegalStateException} |
|
114 |
* if it detects that the stream is being reused. However, since some stream |
|
115 |
* operations may return their receiver rather than a new stream object, it may |
|
116 |
* not be possible to detect reuse in all cases. |
|
17167 | 117 |
* |
19850 | 118 |
* <p>Streams have a {@link #close()} method and implement {@link AutoCloseable}, |
119 |
* but nearly all stream instances do not actually need to be closed after use. |
|
120 |
* Generally, only streams whose source is an IO channel (such as those returned |
|
121 |
* by {@link Files#lines(Path, Charset)}) will require closing. Most streams |
|
122 |
* are backed by collections, arrays, or generating functions, which require no |
|
123 |
* special resource management. (If a stream does require closing, it can be |
|
124 |
* declared as a resource in a {@code try}-with-resources statement.) |
|
125 |
* |
|
126 |
* <p>Stream pipelines may execute either sequentially or in |
|
127 |
* <a href="package-summary.html#Parallelism">parallel</a>. This |
|
128 |
* execution mode is a property of the stream. Streams are created |
|
129 |
* with an initial choice of sequential or parallel execution. (For example, |
|
130 |
* {@link Collection#stream() Collection.stream()} creates a sequential stream, |
|
131 |
* and {@link Collection#parallelStream() Collection.parallelStream()} creates |
|
132 |
* a parallel one.) This choice of execution mode may be modified by the |
|
133 |
* {@link #sequential()} or {@link #parallel()} methods, and may be queried with |
|
134 |
* the {@link #isParallel()} method. |
|
17167 | 135 |
* |
136 |
* @since 1.8 |
|
137 |
* @see <a href="package-summary.html">java.util.stream</a> |
|
138 |
*/ |
|
139 |
public interface LongStream extends BaseStream<Long, LongStream> { |
|
140 |
||
141 |
/** |
|
142 |
* Returns a stream consisting of the elements of this stream that match |
|
143 |
* the given predicate. |
|
144 |
* |
|
145 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
146 |
* operation</a>. |
|
147 |
* |
|
148 |
* @param predicate a <a href="package-summary.html#NonInterference"> |
|
149 |
* non-interfering, stateless</a> predicate to apply to |
|
150 |
* each element to determine if it should be included |
|
151 |
* @return the new stream |
|
152 |
*/ |
|
153 |
LongStream filter(LongPredicate predicate); |
|
154 |
||
155 |
/** |
|
156 |
* Returns a stream consisting of the results of applying the given |
|
157 |
* function to the elements of this stream. |
|
158 |
* |
|
159 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
160 |
* operation</a>. |
|
161 |
* |
|
162 |
* @param mapper a <a href="package-summary.html#NonInterference"> |
|
163 |
* non-interfering, stateless</a> function to apply to each |
|
164 |
* element |
|
165 |
* @return the new stream |
|
166 |
*/ |
|
167 |
LongStream map(LongUnaryOperator mapper); |
|
168 |
||
169 |
/** |
|
170 |
* Returns an object-valued {@code Stream} consisting of the results of |
|
171 |
* applying the given function to the elements of this stream. |
|
172 |
* |
|
173 |
* <p>This is an <a href="package-summary.html#StreamOps"> |
|
174 |
* intermediate operation</a>. |
|
175 |
* |
|
176 |
* @param <U> the element type of the new stream |
|
177 |
* @param mapper a <a href="package-summary.html#NonInterference"> |
|
178 |
* non-interfering, stateless</a> function to apply to each |
|
179 |
* element |
|
180 |
* @return the new stream |
|
181 |
*/ |
|
182 |
<U> Stream<U> mapToObj(LongFunction<? extends U> mapper); |
|
183 |
||
184 |
/** |
|
185 |
* Returns an {@code IntStream} consisting of the results of applying the |
|
186 |
* given function to the elements of this stream. |
|
187 |
* |
|
188 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
189 |
* operation</a>. |
|
190 |
* |
|
191 |
* @param mapper a <a href="package-summary.html#NonInterference"> |
|
192 |
* non-interfering, stateless</a> function to apply to each |
|
193 |
* element |
|
194 |
* @return the new stream |
|
195 |
*/ |
|
196 |
IntStream mapToInt(LongToIntFunction mapper); |
|
197 |
||
198 |
/** |
|
199 |
* Returns a {@code DoubleStream} consisting of the results of applying the |
|
200 |
* given function to the elements of this stream. |
|
201 |
* |
|
202 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
203 |
* operation</a>. |
|
204 |
* |
|
205 |
* @param mapper a <a href="package-summary.html#NonInterference"> |
|
206 |
* non-interfering, stateless</a> function to apply to each |
|
207 |
* element |
|
208 |
* @return the new stream |
|
209 |
*/ |
|
210 |
DoubleStream mapToDouble(LongToDoubleFunction mapper); |
|
211 |
||
212 |
/** |
|
213 |
* Returns a stream consisting of the results of replacing each element of |
|
214 |
* this stream with the contents of the stream produced by applying the |
|
19850 | 215 |
* provided mapping function to each element. (If the result of the mapping |
216 |
* function is {@code null}, this is treated as if the result was an empty |
|
217 |
* stream.) |
|
17167 | 218 |
* |
219 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
220 |
* operation</a>. |
|
221 |
* |
|
222 |
* @param mapper a <a href="package-summary.html#NonInterference"> |
|
223 |
* non-interfering, stateless</a> function to apply to |
|
224 |
* each element which produces an {@code LongStream} of new |
|
225 |
* values |
|
226 |
* @return the new stream |
|
227 |
* @see Stream#flatMap(Function) |
|
228 |
*/ |
|
229 |
LongStream flatMap(LongFunction<? extends LongStream> mapper); |
|
230 |
||
231 |
/** |
|
232 |
* Returns a stream consisting of the distinct elements of this stream. |
|
233 |
* |
|
234 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
235 |
* intermediate operation</a>. |
|
236 |
* |
|
237 |
* @return the new stream |
|
238 |
*/ |
|
239 |
LongStream distinct(); |
|
240 |
||
241 |
/** |
|
242 |
* Returns a stream consisting of the elements of this stream in sorted |
|
243 |
* order. |
|
244 |
* |
|
245 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
246 |
* intermediate operation</a>. |
|
247 |
* |
|
248 |
* @return the new stream |
|
249 |
*/ |
|
250 |
LongStream sorted(); |
|
251 |
||
252 |
/** |
|
253 |
* Returns a stream consisting of the elements of this stream, additionally |
|
254 |
* performing the provided action on each element as elements are consumed |
|
255 |
* from the resulting stream. |
|
256 |
* |
|
257 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
|
258 |
* operation</a>. |
|
259 |
* |
|
260 |
* <p>For parallel stream pipelines, the action may be called at |
|
261 |
* whatever time and in whatever thread the element is made available by the |
|
262 |
* upstream operation. If the action modifies shared state, |
|
263 |
* it is responsible for providing the required synchronization. |
|
264 |
* |
|
265 |
* @apiNote This method exists mainly to support debugging, where you want |
|
266 |
* to see the elements as they flow past a certain point in a pipeline: |
|
267 |
* <pre>{@code |
|
268 |
* list.stream() |
|
269 |
* .filter(filteringFunction) |
|
19850 | 270 |
* .peek(e -> System.out.println("Filtered value: " + e)); |
17167 | 271 |
* .map(mappingFunction) |
19850 | 272 |
* .peek(e -> System.out.println("Mapped value: " + e)); |
17167 | 273 |
* .collect(Collectors.toLongSummaryStastistics()); |
274 |
* }</pre> |
|
275 |
* |
|
19850 | 276 |
* @param action a <a href="package-summary.html#NonInterference"> |
277 |
* non-interfering</a> action to perform on the elements as |
|
278 |
* they are consumed from the stream |
|
17167 | 279 |
* @return the new stream |
280 |
*/ |
|
19850 | 281 |
LongStream peek(LongConsumer action); |
17167 | 282 |
|
283 |
/** |
|
284 |
* Returns a stream consisting of the elements of this stream, truncated |
|
285 |
* to be no longer than {@code maxSize} in length. |
|
286 |
* |
|
287 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
288 |
* stateful intermediate operation</a>. |
|
289 |
* |
|
290 |
* @param maxSize the number of elements the stream should be limited to |
|
291 |
* @return the new stream |
|
292 |
* @throws IllegalArgumentException if {@code maxSize} is negative |
|
293 |
*/ |
|
294 |
LongStream limit(long maxSize); |
|
295 |
||
296 |
/** |
|
297 |
* Returns a stream consisting of the remaining elements of this stream |
|
19850 | 298 |
* after discarding the first {@code startInclusive} elements of the stream. |
299 |
* If this stream contains fewer than {@code startInclusive} elements then an |
|
17167 | 300 |
* empty stream will be returned. |
301 |
* |
|
302 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
303 |
* intermediate operation</a>. |
|
304 |
* |
|
305 |
* @param startInclusive the number of leading elements to skip |
|
306 |
* @return the new stream |
|
307 |
* @throws IllegalArgumentException if {@code startInclusive} is negative |
|
308 |
*/ |
|
309 |
LongStream substream(long startInclusive); |
|
310 |
||
311 |
/** |
|
312 |
* Returns a stream consisting of the remaining elements of this stream |
|
19850 | 313 |
* after discarding the first {@code startInclusive} elements and truncating |
314 |
* the result to be no longer than {@code endExclusive - startInclusive} |
|
315 |
* elements in length. If this stream contains fewer than |
|
316 |
* {@code startInclusive} elements then an empty stream will be returned. |
|
17167 | 317 |
* |
318 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
319 |
* stateful intermediate operation</a>. |
|
320 |
* |
|
321 |
* @param startInclusive the starting position of the substream, inclusive |
|
322 |
* @param endExclusive the ending position of the substream, exclusive |
|
323 |
* @return the new stream |
|
324 |
* @throws IllegalArgumentException if {@code startInclusive} or |
|
325 |
* {@code endExclusive} is negative or {@code startInclusive} is greater |
|
326 |
* than {@code endExclusive} |
|
327 |
*/ |
|
328 |
LongStream substream(long startInclusive, long endExclusive); |
|
329 |
||
330 |
/** |
|
331 |
* Performs an action for each element of this stream. |
|
332 |
* |
|
333 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
334 |
* operation</a>. |
|
335 |
* |
|
336 |
* <p>For parallel stream pipelines, this operation does <em>not</em> |
|
337 |
* guarantee to respect the encounter order of the stream, as doing so |
|
338 |
* would sacrifice the benefit of parallelism. For any given element, the |
|
339 |
* action may be performed at whatever time and in whatever thread the |
|
340 |
* library chooses. If the action accesses shared state, it is |
|
341 |
* responsible for providing the required synchronization. |
|
342 |
* |
|
343 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
344 |
* non-interfering</a> action to perform on the elements |
|
345 |
*/ |
|
346 |
void forEach(LongConsumer action); |
|
347 |
||
348 |
/** |
|
349 |
* Performs an action for each element of this stream, guaranteeing that |
|
350 |
* each element is processed in encounter order for streams that have a |
|
351 |
* defined encounter order. |
|
352 |
* |
|
353 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
354 |
* operation</a>. |
|
355 |
* |
|
356 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
357 |
* non-interfering</a> action to perform on the elements |
|
358 |
* @see #forEach(LongConsumer) |
|
359 |
*/ |
|
360 |
void forEachOrdered(LongConsumer action); |
|
361 |
||
362 |
/** |
|
363 |
* Returns an array containing the elements of this stream. |
|
364 |
* |
|
365 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
366 |
* operation</a>. |
|
367 |
* |
|
368 |
* @return an array containing the elements of this stream |
|
369 |
*/ |
|
370 |
long[] toArray(); |
|
371 |
||
372 |
/** |
|
373 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
374 |
* elements of this stream, using the provided identity value and an |
|
375 |
* <a href="package-summary.html#Associativity">associative</a> |
|
376 |
* accumulation function, and returns the reduced value. This is equivalent |
|
377 |
* to: |
|
378 |
* <pre>{@code |
|
379 |
* long result = identity; |
|
380 |
* for (long element : this stream) |
|
381 |
* result = accumulator.apply(result, element) |
|
382 |
* return result; |
|
383 |
* }</pre> |
|
384 |
* |
|
385 |
* but is not constrained to execute sequentially. |
|
386 |
* |
|
387 |
* <p>The {@code identity} value must be an identity for the accumulator |
|
388 |
* function. This means that for all {@code x}, |
|
389 |
* {@code accumulator.apply(identity, x)} is equal to {@code x}. |
|
390 |
* The {@code accumulator} function must be an |
|
391 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
392 |
* |
|
393 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
394 |
* operation</a>. |
|
395 |
* |
|
396 |
* @apiNote Sum, min, max, and average are all special cases of reduction. |
|
397 |
* Summing a stream of numbers can be expressed as: |
|
398 |
* |
|
399 |
* <pre>{@code |
|
400 |
* long sum = integers.reduce(0, (a, b) -> a+b); |
|
401 |
* }</pre> |
|
402 |
* |
|
403 |
* or more compactly: |
|
404 |
* |
|
405 |
* <pre>{@code |
|
406 |
* long sum = integers.reduce(0, Long::sum); |
|
407 |
* }</pre> |
|
408 |
* |
|
409 |
* <p>While this may seem a more roundabout way to perform an aggregation |
|
410 |
* compared to simply mutating a running total in a loop, reduction |
|
411 |
* operations parallelize more gracefully, without needing additional |
|
412 |
* synchronization and with greatly reduced risk of data races. |
|
413 |
* |
|
414 |
* @param identity the identity value for the accumulating function |
|
415 |
* @param op an <a href="package-summary.html#Associativity">associative</a> |
|
416 |
* <a href="package-summary.html#NonInterference">non-interfering, |
|
417 |
* stateless</a> function for combining two values |
|
418 |
* @return the result of the reduction |
|
419 |
* @see #sum() |
|
420 |
* @see #min() |
|
421 |
* @see #max() |
|
422 |
* @see #average() |
|
423 |
*/ |
|
424 |
long reduce(long identity, LongBinaryOperator op); |
|
425 |
||
426 |
/** |
|
427 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
428 |
* elements of this stream, using an |
|
429 |
* <a href="package-summary.html#Associativity">associative</a> accumulation |
|
430 |
* function, and returns an {@code OptionalLong} describing the reduced value, |
|
431 |
* if any. This is equivalent to: |
|
432 |
* <pre>{@code |
|
433 |
* boolean foundAny = false; |
|
434 |
* long result = null; |
|
435 |
* for (long element : this stream) { |
|
436 |
* if (!foundAny) { |
|
437 |
* foundAny = true; |
|
438 |
* result = element; |
|
439 |
* } |
|
440 |
* else |
|
441 |
* result = accumulator.apply(result, element); |
|
442 |
* } |
|
443 |
* return foundAny ? OptionalLong.of(result) : OptionalLong.empty(); |
|
444 |
* }</pre> |
|
445 |
* |
|
446 |
* but is not constrained to execute sequentially. |
|
447 |
* |
|
448 |
* <p>The {@code accumulator} function must be an |
|
449 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
450 |
* |
|
451 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
452 |
* operation</a>. |
|
453 |
* |
|
454 |
* @param op an <a href="package-summary.html#Associativity">associative</a> |
|
455 |
* <a href="package-summary.html#NonInterference">non-interfering, |
|
456 |
* stateless</a> function for combining two values |
|
457 |
* @return the result of the reduction |
|
458 |
* @see #reduce(long, LongBinaryOperator) |
|
459 |
*/ |
|
460 |
OptionalLong reduce(LongBinaryOperator op); |
|
461 |
||
462 |
/** |
|
463 |
* Performs a <a href="package-summary.html#MutableReduction">mutable |
|
464 |
* reduction</a> operation on the elements of this stream. A mutable |
|
19850 | 465 |
* reduction is one in which the reduced value is a mutable result container, |
17167 | 466 |
* such as an {@code ArrayList}, and elements are incorporated by updating |
19850 | 467 |
* the state of the result rather than by replacing the result. This |
17167 | 468 |
* produces a result equivalent to: |
469 |
* <pre>{@code |
|
19850 | 470 |
* R result = supplier.get(); |
17167 | 471 |
* for (long element : this stream) |
472 |
* accumulator.accept(result, element); |
|
473 |
* return result; |
|
474 |
* }</pre> |
|
475 |
* |
|
476 |
* <p>Like {@link #reduce(long, LongBinaryOperator)}, {@code collect} operations |
|
477 |
* can be parallelized without requiring additional synchronization. |
|
478 |
* |
|
479 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
480 |
* operation</a>. |
|
481 |
* |
|
482 |
* @param <R> type of the result |
|
19850 | 483 |
* @param supplier a function that creates a new result container. For a |
484 |
* parallel execution, this function may be called |
|
485 |
* multiple times and must return a fresh value each time. |
|
17167 | 486 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a> |
487 |
* <a href="package-summary.html#NonInterference">non-interfering, |
|
488 |
* stateless</a> function for incorporating an additional |
|
489 |
* element into a result |
|
490 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a> |
|
491 |
* <a href="package-summary.html#NonInterference">non-interfering, |
|
492 |
* stateless</a> function for combining two values, which |
|
493 |
* must be compatible with the accumulator function |
|
494 |
* @return the result of the reduction |
|
495 |
* @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
|
496 |
*/ |
|
19850 | 497 |
<R> R collect(Supplier<R> supplier, |
17167 | 498 |
ObjLongConsumer<R> accumulator, |
499 |
BiConsumer<R, R> combiner); |
|
500 |
||
501 |
/** |
|
502 |
* Returns the sum of elements in this stream. This is a special case |
|
19850 | 503 |
* of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 504 |
* and is equivalent to: |
505 |
* <pre>{@code |
|
506 |
* return reduce(0, Long::sum); |
|
507 |
* }</pre> |
|
508 |
* |
|
19850 | 509 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
510 |
* operation</a>. |
|
511 |
* |
|
17167 | 512 |
* @return the sum of elements in this stream |
513 |
*/ |
|
514 |
long sum(); |
|
515 |
||
516 |
/** |
|
517 |
* Returns an {@code OptionalLong} describing the minimum element of this |
|
518 |
* stream, or an empty optional if this stream is empty. This is a special |
|
19850 | 519 |
* case of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 520 |
* and is equivalent to: |
521 |
* <pre>{@code |
|
522 |
* return reduce(Long::min); |
|
523 |
* }</pre> |
|
524 |
* |
|
525 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
526 |
* |
|
527 |
* @return an {@code OptionalLong} containing the minimum element of this |
|
528 |
* stream, or an empty {@code OptionalLong} if the stream is empty |
|
529 |
*/ |
|
530 |
OptionalLong min(); |
|
531 |
||
532 |
/** |
|
533 |
* Returns an {@code OptionalLong} describing the maximum element of this |
|
534 |
* stream, or an empty optional if this stream is empty. This is a special |
|
19850 | 535 |
* case of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 536 |
* and is equivalent to: |
537 |
* <pre>{@code |
|
538 |
* return reduce(Long::max); |
|
539 |
* }</pre> |
|
540 |
* |
|
541 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
542 |
* operation</a>. |
|
543 |
* |
|
544 |
* @return an {@code OptionalLong} containing the maximum element of this |
|
545 |
* stream, or an empty {@code OptionalLong} if the stream is empty |
|
546 |
*/ |
|
547 |
OptionalLong max(); |
|
548 |
||
549 |
/** |
|
550 |
* Returns the count of elements in this stream. This is a special case of |
|
19850 | 551 |
* a <a href="package-summary.html#Reduction">reduction</a> and is |
17167 | 552 |
* equivalent to: |
553 |
* <pre>{@code |
|
554 |
* return map(e -> 1L).sum(); |
|
555 |
* }</pre> |
|
556 |
* |
|
557 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
558 |
* |
|
559 |
* @return the count of elements in this stream |
|
560 |
*/ |
|
561 |
long count(); |
|
562 |
||
563 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18825
diff
changeset
|
564 |
* Returns an {@code OptionalDouble} describing the arithmetic mean of elements of |
17167 | 565 |
* this stream, or an empty optional if this stream is empty. This is a |
566 |
* special case of a |
|
19850 | 567 |
* <a href="package-summary.html#Reduction">reduction</a>. |
568 |
* |
|
569 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
570 |
* operation</a>. |
|
17167 | 571 |
* |
572 |
* @return an {@code OptionalDouble} containing the average element of this |
|
573 |
* stream, or an empty optional if the stream is empty |
|
574 |
*/ |
|
575 |
OptionalDouble average(); |
|
576 |
||
577 |
/** |
|
578 |
* Returns a {@code LongSummaryStatistics} describing various summary data |
|
579 |
* about the elements of this stream. This is a special case of a |
|
19850 | 580 |
* <a href="package-summary.html#Reduction">reduction</a>. |
581 |
* |
|
582 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
583 |
* operation</a>. |
|
17167 | 584 |
* |
585 |
* @return a {@code LongSummaryStatistics} describing various summary data |
|
586 |
* about the elements of this stream |
|
587 |
*/ |
|
588 |
LongSummaryStatistics summaryStatistics(); |
|
589 |
||
590 |
/** |
|
591 |
* Returns whether any elements of this stream match the provided |
|
592 |
* predicate. May not evaluate the predicate on all elements if not |
|
593 |
* necessary for determining the result. |
|
594 |
* |
|
595 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
596 |
* terminal operation</a>. |
|
597 |
* |
|
598 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering, |
|
599 |
* stateless</a> predicate to apply to elements of this |
|
600 |
* stream |
|
601 |
* @return {@code true} if any elements of the stream match the provided |
|
602 |
* predicate otherwise {@code false} |
|
603 |
*/ |
|
604 |
boolean anyMatch(LongPredicate predicate); |
|
605 |
||
606 |
/** |
|
607 |
* Returns whether all elements of this stream match the provided predicate. |
|
608 |
* May not evaluate the predicate on all elements if not necessary for |
|
609 |
* determining the result. |
|
610 |
* |
|
611 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
612 |
* terminal operation</a>. |
|
613 |
* |
|
614 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering, |
|
615 |
* stateless</a> predicate to apply to elements of this |
|
616 |
* stream |
|
617 |
* @return {@code true} if all elements of the stream match the provided |
|
618 |
* predicate otherwise {@code false} |
|
619 |
*/ |
|
620 |
boolean allMatch(LongPredicate predicate); |
|
621 |
||
622 |
/** |
|
623 |
* Returns whether no elements of this stream match the provided predicate. |
|
624 |
* May not evaluate the predicate on all elements if not necessary for |
|
625 |
* determining the result. |
|
626 |
* |
|
627 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
628 |
* terminal operation</a>. |
|
629 |
* |
|
630 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering, |
|
631 |
* stateless</a> predicate to apply to elements of this |
|
632 |
* stream |
|
633 |
* @return {@code true} if no elements of the stream match the provided |
|
634 |
* predicate otherwise {@code false} |
|
635 |
*/ |
|
636 |
boolean noneMatch(LongPredicate predicate); |
|
637 |
||
638 |
/** |
|
639 |
* Returns an {@link OptionalLong} describing the first element of this |
|
19850 | 640 |
* stream, or an empty {@code OptionalLong} if the stream is empty. If the |
641 |
* stream has no encounter order, then any element may be returned. |
|
17167 | 642 |
* |
643 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
644 |
* terminal operation</a>. |
|
645 |
* |
|
646 |
* @return an {@code OptionalLong} describing the first element of this |
|
647 |
* stream, or an empty {@code OptionalLong} if the stream is empty |
|
648 |
*/ |
|
649 |
OptionalLong findFirst(); |
|
650 |
||
651 |
/** |
|
652 |
* Returns an {@link OptionalLong} describing some element of the stream, or |
|
653 |
* an empty {@code OptionalLong} if the stream is empty. |
|
654 |
* |
|
655 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
656 |
* terminal operation</a>. |
|
657 |
* |
|
658 |
* <p>The behavior of this operation is explicitly nondeterministic; it is |
|
659 |
* free to select any element in the stream. This is to allow for maximal |
|
660 |
* performance in parallel operations; the cost is that multiple invocations |
|
19850 | 661 |
* on the same source may not return the same result. (If a stable result |
662 |
* is desired, use {@link #findFirst()} instead.) |
|
17167 | 663 |
* |
664 |
* @return an {@code OptionalLong} describing some element of this stream, |
|
665 |
* or an empty {@code OptionalLong} if the stream is empty |
|
666 |
* @see #findFirst() |
|
667 |
*/ |
|
668 |
OptionalLong findAny(); |
|
669 |
||
670 |
/** |
|
671 |
* Returns a {@code DoubleStream} consisting of the elements of this stream, |
|
672 |
* converted to {@code double}. |
|
673 |
* |
|
19850 | 674 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
675 |
* operation</a>. |
|
676 |
* |
|
17167 | 677 |
* @return a {@code DoubleStream} consisting of the elements of this stream, |
678 |
* converted to {@code double} |
|
679 |
*/ |
|
18154
5ede18269905
8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents:
17914
diff
changeset
|
680 |
DoubleStream asDoubleStream(); |
17167 | 681 |
|
682 |
/** |
|
683 |
* Returns a {@code Stream} consisting of the elements of this stream, |
|
684 |
* each boxed to a {@code Long}. |
|
685 |
* |
|
19850 | 686 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
687 |
* operation</a>. |
|
688 |
* |
|
17167 | 689 |
* @return a {@code Stream} consistent of the elements of this stream, |
690 |
* each boxed to {@code Long} |
|
691 |
*/ |
|
692 |
Stream<Long> boxed(); |
|
693 |
||
694 |
@Override |
|
695 |
LongStream sequential(); |
|
696 |
||
697 |
@Override |
|
698 |
LongStream parallel(); |
|
699 |
||
700 |
@Override |
|
701 |
PrimitiveIterator.OfLong iterator(); |
|
702 |
||
703 |
@Override |
|
704 |
Spliterator.OfLong spliterator(); |
|
17195 | 705 |
|
706 |
// Static factories |
|
707 |
||
708 |
/** |
|
709 |
* Returns a builder for a {@code LongStream}. |
|
710 |
* |
|
711 |
* @return a stream builder |
|
712 |
*/ |
|
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
713 |
public static Builder builder() { |
17195 | 714 |
return new Streams.LongStreamBuilderImpl(); |
715 |
} |
|
716 |
||
717 |
/** |
|
718 |
* Returns an empty sequential {@code LongStream}. |
|
719 |
* |
|
720 |
* @return an empty sequential stream |
|
721 |
*/ |
|
722 |
public static LongStream empty() { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
723 |
return StreamSupport.longStream(Spliterators.emptyLongSpliterator(), false); |
17195 | 724 |
} |
725 |
||
726 |
/** |
|
727 |
* Returns a sequential {@code LongStream} containing a single element. |
|
728 |
* |
|
729 |
* @param t the single element |
|
730 |
* @return a singleton sequential stream |
|
731 |
*/ |
|
732 |
public static LongStream of(long t) { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
733 |
return StreamSupport.longStream(new Streams.LongStreamBuilderImpl(t), false); |
17195 | 734 |
} |
735 |
||
736 |
/** |
|
19850 | 737 |
* Returns a sequential ordered stream whose elements are the specified values. |
17195 | 738 |
* |
739 |
* @param values the elements of the new stream |
|
740 |
* @return the new stream |
|
741 |
*/ |
|
742 |
public static LongStream of(long... values) { |
|
743 |
return Arrays.stream(values); |
|
744 |
} |
|
745 |
||
746 |
/** |
|
19850 | 747 |
* Returns an infinite sequential ordered {@code LongStream} produced by iterative |
17195 | 748 |
* application of a function {@code f} to an initial element {@code seed}, |
749 |
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, |
|
750 |
* {@code f(f(seed))}, etc. |
|
751 |
* |
|
752 |
* <p>The first element (position {@code 0}) in the {@code LongStream} will |
|
753 |
* be the provided {@code seed}. For {@code n > 0}, the element at position |
|
754 |
* {@code n}, will be the result of applying the function {@code f} to the |
|
755 |
* element at position {@code n - 1}. |
|
756 |
* |
|
757 |
* @param seed the initial element |
|
758 |
* @param f a function to be applied to to the previous element to produce |
|
759 |
* a new element |
|
760 |
* @return a new sequential {@code LongStream} |
|
761 |
*/ |
|
762 |
public static LongStream iterate(final long seed, final LongUnaryOperator f) { |
|
763 |
Objects.requireNonNull(f); |
|
764 |
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() { |
|
765 |
long t = seed; |
|
766 |
||
767 |
@Override |
|
768 |
public boolean hasNext() { |
|
769 |
return true; |
|
770 |
} |
|
771 |
||
772 |
@Override |
|
773 |
public long nextLong() { |
|
774 |
long v = t; |
|
775 |
t = f.applyAsLong(t); |
|
776 |
return v; |
|
777 |
} |
|
778 |
}; |
|
779 |
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize( |
|
780 |
iterator, |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
781 |
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); |
17195 | 782 |
} |
783 |
||
784 |
/** |
|
19850 | 785 |
* Returns a sequential stream where each element is generated by |
786 |
* the provided {@code LongSupplier}. This is suitable for generating |
|
787 |
* constant streams, streams of random elements, etc. |
|
17195 | 788 |
* |
789 |
* @param s the {@code LongSupplier} for generated elements |
|
790 |
* @return a new sequential {@code LongStream} |
|
791 |
*/ |
|
792 |
public static LongStream generate(LongSupplier s) { |
|
793 |
Objects.requireNonNull(s); |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
18158
diff
changeset
|
794 |
return StreamSupport.longStream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
795 |
new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false); |
17195 | 796 |
} |
797 |
||
798 |
/** |
|
19850 | 799 |
* Returns a sequential ordered {@code LongStream} from {@code startInclusive} |
17195 | 800 |
* (inclusive) to {@code endExclusive} (exclusive) by an incremental step of |
18158 | 801 |
* {@code 1}. |
17195 | 802 |
* |
18158 | 803 |
* @apiNote |
804 |
* <p>An equivalent sequence of increasing values can be produced |
|
805 |
* sequentially using a {@code for} loop as follows: |
|
17195 | 806 |
* <pre>{@code |
18158 | 807 |
* for (long i = startInclusive; i < endExclusive ; i++) { ... } |
17195 | 808 |
* }</pre> |
809 |
* |
|
810 |
* @param startInclusive the (inclusive) initial value |
|
811 |
* @param endExclusive the exclusive upper bound |
|
812 |
* @return a sequential {@code LongStream} for the range of {@code long} |
|
813 |
* elements |
|
814 |
*/ |
|
815 |
public static LongStream range(long startInclusive, final long endExclusive) { |
|
18158 | 816 |
if (startInclusive >= endExclusive) { |
817 |
return empty(); |
|
818 |
} else if (endExclusive - startInclusive < 0) { |
|
819 |
// Size of range > Long.MAX_VALUE |
|
820 |
// Split the range in two and concatenate |
|
821 |
// Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then |
|
822 |
// the lower range, [Long.MIN_VALUE, 0) will be further split in two |
|
18820 | 823 |
long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1; |
824 |
return concat(range(startInclusive, m), range(m, endExclusive)); |
|
18158 | 825 |
} else { |
826 |
return StreamSupport.longStream( |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
827 |
new Streams.RangeLongSpliterator(startInclusive, endExclusive, false), false); |
18158 | 828 |
} |
17195 | 829 |
} |
830 |
||
831 |
/** |
|
19850 | 832 |
* Returns a sequential ordered {@code LongStream} from {@code startInclusive} |
18158 | 833 |
* (inclusive) to {@code endInclusive} (inclusive) by an incremental step of |
834 |
* {@code 1}. |
|
17195 | 835 |
* |
18158 | 836 |
* @apiNote |
17195 | 837 |
* <p>An equivalent sequence of increasing values can be produced |
838 |
* sequentially using a {@code for} loop as follows: |
|
839 |
* <pre>{@code |
|
18158 | 840 |
* for (long i = startInclusive; i <= endInclusive ; i++) { ... } |
17195 | 841 |
* }</pre> |
842 |
* |
|
843 |
* @param startInclusive the (inclusive) initial value |
|
18158 | 844 |
* @param endInclusive the inclusive upper bound |
17195 | 845 |
* @return a sequential {@code LongStream} for the range of {@code long} |
846 |
* elements |
|
847 |
*/ |
|
18158 | 848 |
public static LongStream rangeClosed(long startInclusive, final long endInclusive) { |
849 |
if (startInclusive > endInclusive) { |
|
17195 | 850 |
return empty(); |
18158 | 851 |
} else if (endInclusive - startInclusive + 1 <= 0) { |
852 |
// Size of range > Long.MAX_VALUE |
|
853 |
// Split the range in two and concatenate |
|
854 |
// Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then |
|
855 |
// the lower range, [Long.MIN_VALUE, 0), and upper range, |
|
856 |
// [0, Long.MAX_VALUE], will both be further split in two |
|
18820 | 857 |
long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1; |
858 |
return concat(range(startInclusive, m), rangeClosed(m, endInclusive)); |
|
17195 | 859 |
} else { |
18158 | 860 |
return StreamSupport.longStream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
861 |
new Streams.RangeLongSpliterator(startInclusive, endInclusive, true), false); |
17195 | 862 |
} |
863 |
} |
|
18820 | 864 |
|
865 |
/** |
|
19850 | 866 |
* Creates a lazily concatenated stream whose elements are all the |
867 |
* elements of the first stream followed by all the elements of the |
|
868 |
* second stream. The resulting stream is ordered if both |
|
18820 | 869 |
* of the input streams are ordered, and parallel if either of the input |
19800 | 870 |
* streams is parallel. When the resulting stream is closed, the close |
871 |
* handlers for both input streams are invoked. |
|
18820 | 872 |
* |
873 |
* @param a the first stream |
|
19850 | 874 |
* @param b the second stream |
875 |
* @return the concatenation of the two input streams |
|
18820 | 876 |
*/ |
877 |
public static LongStream concat(LongStream a, LongStream b) { |
|
878 |
Objects.requireNonNull(a); |
|
879 |
Objects.requireNonNull(b); |
|
880 |
||
881 |
Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong( |
|
882 |
a.spliterator(), b.spliterator()); |
|
19800 | 883 |
LongStream stream = StreamSupport.longStream(split, a.isParallel() || b.isParallel()); |
884 |
return stream.onClose(Streams.composedClose(a, b)); |
|
18820 | 885 |
} |
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
886 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
887 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
888 |
* A mutable builder for a {@code LongStream}. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
889 |
* |
19850 | 890 |
* <p>A stream builder has a lifecycle, which starts in a building |
891 |
* phase, during which elements can be added, and then transitions to a built |
|
892 |
* 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
|
893 |
* 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
|
894 |
* 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
|
895 |
* stream builder, in the order they were added. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
896 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
897 |
* @see LongStream#builder() |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
898 |
* @since 1.8 |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
899 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
900 |
public interface Builder extends LongConsumer { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
901 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
902 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
903 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
904 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
905 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
906 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
907 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
908 |
@Override |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
909 |
void accept(long t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
910 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
911 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
912 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
913 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
914 |
* @implSpec |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
915 |
* The default implementation behaves as if: |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
916 |
* <pre>{@code |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
917 |
* accept(t) |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
918 |
* return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
919 |
* }</pre> |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
920 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
921 |
* @param t the element to add |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
922 |
* @return {@code this} builder |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
923 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
924 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
925 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
926 |
default Builder add(long t) { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
927 |
accept(t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
928 |
return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
929 |
} |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
930 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
931 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
932 |
* Builds the stream, transitioning this builder to the built state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
933 |
* An {@code IllegalStateException} is thrown if there are further |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
934 |
* 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
|
935 |
* state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
936 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
937 |
* @return the built stream |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
938 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
939 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
940 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
941 |
LongStream build(); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
942 |
} |
17167 | 943 |
} |