author | psandoz |
Fri, 31 Jul 2015 12:29:01 +0200 | |
changeset 32002 | e378c0dc767e |
parent 31644 | dbca10d053fd |
child 35302 | e4d2275861c3 |
permissions | -rw-r--r-- |
17167 | 1 |
/* |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
2 |
* Copyright (c) 2013, 2015, 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 |
* |
|
19850 | 212 |
* @param action a <a href="package-summary.html#NonInterference"> |
213 |
* non-interfering</a> action to perform on the elements as |
|
214 |
* they are consumed from the stream |
|
17167 | 215 |
* @return the new stream |
216 |
*/ |
|
19850 | 217 |
LongStream peek(LongConsumer action); |
17167 | 218 |
|
219 |
/** |
|
220 |
* Returns a stream consisting of the elements of this stream, truncated |
|
221 |
* to be no longer than {@code maxSize} in length. |
|
222 |
* |
|
223 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
224 |
* stateful intermediate operation</a>. |
|
225 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
226 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
227 |
* 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
|
228 |
* 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
|
229 |
* 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
|
230 |
* 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
|
231 |
* <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
|
232 |
* 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
|
233 |
* 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
|
234 |
* 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
|
235 |
* 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
|
236 |
* 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
|
237 |
* {@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
|
238 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
239 |
* |
17167 | 240 |
* @param maxSize the number of elements the stream should be limited to |
241 |
* @return the new stream |
|
242 |
* @throws IllegalArgumentException if {@code maxSize} is negative |
|
243 |
*/ |
|
244 |
LongStream limit(long maxSize); |
|
245 |
||
246 |
/** |
|
247 |
* 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
|
248 |
* 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
|
249 |
* If this stream contains fewer than {@code n} elements then an |
17167 | 250 |
* empty stream will be returned. |
251 |
* |
|
252 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
|
253 |
* intermediate operation</a>. |
|
254 |
* |
|
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
255 |
* @apiNote |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
256 |
* 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
|
257 |
* 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
|
258 |
* 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
|
259 |
* 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
|
260 |
* <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
|
261 |
* 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
|
262 |
* 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
|
263 |
* 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
|
264 |
* 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
|
265 |
* 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
|
266 |
* {@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
|
267 |
* with {@link #sequential()} may improve performance. |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
268 |
* |
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
269 |
* @param n the number of leading elements to skip |
17167 | 270 |
* @return the new stream |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
271 |
* @throws IllegalArgumentException if {@code n} is negative |
17167 | 272 |
*/ |
20866
36155ee613ef
8025910: rename substream(long) -> skip and remove substream(long,long)
mduigou
parents:
19850
diff
changeset
|
273 |
LongStream skip(long n); |
17167 | 274 |
|
275 |
/** |
|
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
276 |
* 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
|
277 |
* 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
|
278 |
* 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
|
279 |
* 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
|
280 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
281 |
* <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
|
282 |
* 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
|
283 |
* 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
|
284 |
* the element immediately following the last element of the sequence does |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
285 |
* not match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
286 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
287 |
* <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
|
288 |
* 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
|
289 |
* nondeterministic; it is free to take any subset of matching elements |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
290 |
* (which includes the empty set). |
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>Independent of whether this stream is ordered or unordered if all |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
293 |
* 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
|
294 |
* 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
|
295 |
* 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
|
296 |
* taken (the result is an empty stream). |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
297 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
298 |
* <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
|
299 |
* stateful intermediate operation</a>. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
300 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
301 |
* @implSpec |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
302 |
* The default implementation obtains the {@link #spliterator() spliterator} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
303 |
* 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
|
304 |
* 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
|
305 |
* the wrapped spliterator. The returned stream preserves the execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
306 |
* characteristics of this stream (namely parallel or sequential execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
307 |
* as per {@link #isParallel()}) but the wrapped spliterator may choose to |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
308 |
* not support splitting. When the returned stream is closed, the close |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
309 |
* handlers for both the returned and this stream are invoked. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
310 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
311 |
* @apiNote |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
312 |
* While {@code takeWhile()} is generally a cheap operation on sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
313 |
* stream pipelines, it can be quite expensive on ordered parallel |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
314 |
* pipelines, since the operation is constrained to return not just any |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
315 |
* 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
|
316 |
* Using an unordered stream source (such as |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
317 |
* {@link #generate(LongSupplier)}) or removing the ordering constraint with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
318 |
* {@link #unordered()} may result in significant speedups of |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
319 |
* {@code takeWhile()} in parallel pipelines, if the semantics of your |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
320 |
* situation permit. If consistency with encounter order is required, and |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
321 |
* you are experiencing poor performance or memory utilization with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
322 |
* {@code takeWhile()} in parallel pipelines, switching to sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
323 |
* execution with {@link #sequential()} may improve performance. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
324 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
325 |
* @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
|
326 |
* <a href="package-summary.html#Statelessness">stateless</a> |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
327 |
* predicate to apply to elements to determine the longest |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
328 |
* prefix of elements. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
329 |
* @return the new stream |
32002
e378c0dc767e
8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents:
31644
diff
changeset
|
330 |
* @since 1.9 |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
331 |
*/ |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
332 |
default LongStream takeWhile(LongPredicate predicate) { |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
333 |
Objects.requireNonNull(predicate); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
334 |
// Reuses the unordered spliterator, which, when encounter is present, |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
335 |
// 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
|
336 |
return StreamSupport.longStream( |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
337 |
new WhileOps.UnorderedWhileSpliterator.OfLong.Taking(spliterator(), true, predicate), |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
338 |
isParallel()).onClose(this::close); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
339 |
} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
340 |
|
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
341 |
/** |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
342 |
* 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
|
343 |
* elements of this stream after dropping the longest prefix of elements |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
344 |
* that match the given predicate. Otherwise returns, if this stream is |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
345 |
* unordered, a stream consisting of the remaining elements of this stream |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
346 |
* after dropping a subset of elements that match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
347 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
348 |
* <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
|
349 |
* 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
|
350 |
* 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
|
351 |
* the element immediately following the last element of the sequence does |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
352 |
* not match the given predicate. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
353 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
354 |
* <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
|
355 |
* 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
|
356 |
* nondeterministic; it is free to drop any subset of matching elements |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
357 |
* (which includes the empty set). |
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>Independent of whether this stream is ordered or unordered if all |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
360 |
* elements of this stream match the given predicate then this operation |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
361 |
* 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
|
362 |
* 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
|
363 |
* result is the same as the input). |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
364 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
365 |
* <p>This is a <a href="package-summary.html#StreamOps">stateful |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
366 |
* intermediate operation</a>. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
367 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
368 |
* @implSpec |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
369 |
* The default implementation obtains the {@link #spliterator() spliterator} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
370 |
* 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
|
371 |
* 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
|
372 |
* the wrapped spliterator. The returned stream preserves the execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
373 |
* characteristics of this stream (namely parallel or sequential execution |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
374 |
* as per {@link #isParallel()}) but the wrapped spliterator may choose to |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
375 |
* not support splitting. When the returned stream is closed, the close |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
376 |
* handlers for both the returned and this stream are invoked. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
377 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
378 |
* @apiNote |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
379 |
* While {@code dropWhile()} is generally a cheap operation on sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
380 |
* stream pipelines, it can be quite expensive on ordered parallel |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
381 |
* pipelines, since the operation is constrained to return not just any |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
382 |
* 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
|
383 |
* Using an unordered stream source (such as |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
384 |
* {@link #generate(LongSupplier)}) or removing the ordering constraint with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
385 |
* {@link #unordered()} may result in significant speedups of |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
386 |
* {@code dropWhile()} in parallel pipelines, if the semantics of your |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
387 |
* situation permit. If consistency with encounter order is required, and |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
388 |
* you are experiencing poor performance or memory utilization with |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
389 |
* {@code dropWhile()} in parallel pipelines, switching to sequential |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
390 |
* execution with {@link #sequential()} may improve performance. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
391 |
* |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
392 |
* @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
|
393 |
* <a href="package-summary.html#Statelessness">stateless</a> |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
394 |
* predicate to apply to elements to determine the longest |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
395 |
* prefix of elements. |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
396 |
* @return the new stream |
32002
e378c0dc767e
8130828: Fix some typos and omissions in the the j.u.stream JavaDoc
psandoz
parents:
31644
diff
changeset
|
397 |
* @since 1.9 |
31644
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
398 |
*/ |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
399 |
default LongStream dropWhile(LongPredicate predicate) { |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
400 |
Objects.requireNonNull(predicate); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
401 |
// Reuses the unordered spliterator, which, when encounter is present, |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
402 |
// 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
|
403 |
return StreamSupport.longStream( |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
404 |
new WhileOps.UnorderedWhileSpliterator.OfLong.Dropping(spliterator(), true, predicate), |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
405 |
isParallel()).onClose(this::close); |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
406 |
} |
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
407 |
|
dbca10d053fd
8071597: Add Stream dropWhile and takeWhile operations
psandoz
parents:
29489
diff
changeset
|
408 |
/** |
17167 | 409 |
* Performs an action for each element of this stream. |
410 |
* |
|
411 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
412 |
* operation</a>. |
|
413 |
* |
|
414 |
* <p>For parallel stream pipelines, this operation does <em>not</em> |
|
415 |
* guarantee to respect the encounter order of the stream, as doing so |
|
416 |
* would sacrifice the benefit of parallelism. For any given element, the |
|
417 |
* action may be performed at whatever time and in whatever thread the |
|
418 |
* library chooses. If the action accesses shared state, it is |
|
419 |
* responsible for providing the required synchronization. |
|
420 |
* |
|
421 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
422 |
* non-interfering</a> action to perform on the elements |
|
423 |
*/ |
|
424 |
void forEach(LongConsumer action); |
|
425 |
||
426 |
/** |
|
427 |
* Performs an action for each element of this stream, guaranteeing that |
|
428 |
* each element is processed in encounter order for streams that have a |
|
429 |
* defined encounter order. |
|
430 |
* |
|
431 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
432 |
* operation</a>. |
|
433 |
* |
|
434 |
* @param action a <a href="package-summary.html#NonInterference"> |
|
435 |
* non-interfering</a> action to perform on the elements |
|
436 |
* @see #forEach(LongConsumer) |
|
437 |
*/ |
|
438 |
void forEachOrdered(LongConsumer action); |
|
439 |
||
440 |
/** |
|
441 |
* Returns an array containing the elements of this stream. |
|
442 |
* |
|
443 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
444 |
* operation</a>. |
|
445 |
* |
|
446 |
* @return an array containing the elements of this stream |
|
447 |
*/ |
|
448 |
long[] toArray(); |
|
449 |
||
450 |
/** |
|
451 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
452 |
* elements of this stream, using the provided identity value and an |
|
453 |
* <a href="package-summary.html#Associativity">associative</a> |
|
454 |
* accumulation function, and returns the reduced value. This is equivalent |
|
455 |
* to: |
|
456 |
* <pre>{@code |
|
457 |
* long result = identity; |
|
458 |
* for (long element : this stream) |
|
22996
0ce70f4ef909
8029944: Primitive Stream reduce method documentation pseudo code misidentifies apply method
michaelm
parents:
22352
diff
changeset
|
459 |
* result = accumulator.applyAsLong(result, element) |
17167 | 460 |
* return result; |
461 |
* }</pre> |
|
462 |
* |
|
463 |
* but is not constrained to execute sequentially. |
|
464 |
* |
|
465 |
* <p>The {@code identity} value must be an identity for the accumulator |
|
466 |
* function. This means that for all {@code x}, |
|
467 |
* {@code accumulator.apply(identity, x)} is equal to {@code x}. |
|
468 |
* The {@code accumulator} function must be an |
|
469 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
470 |
* |
|
471 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
472 |
* operation</a>. |
|
473 |
* |
|
474 |
* @apiNote Sum, min, max, and average are all special cases of reduction. |
|
475 |
* Summing a stream of numbers can be expressed as: |
|
476 |
* |
|
477 |
* <pre>{@code |
|
478 |
* long sum = integers.reduce(0, (a, b) -> a+b); |
|
479 |
* }</pre> |
|
480 |
* |
|
481 |
* or more compactly: |
|
482 |
* |
|
483 |
* <pre>{@code |
|
484 |
* long sum = integers.reduce(0, Long::sum); |
|
485 |
* }</pre> |
|
486 |
* |
|
487 |
* <p>While this may seem a more roundabout way to perform an aggregation |
|
488 |
* compared to simply mutating a running total in a loop, reduction |
|
489 |
* operations parallelize more gracefully, without needing additional |
|
490 |
* synchronization and with greatly reduced risk of data races. |
|
491 |
* |
|
492 |
* @param identity the identity value for the accumulating function |
|
21339 | 493 |
* @param op an <a href="package-summary.html#Associativity">associative</a>, |
494 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
495 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
496 |
* function for combining two values |
|
17167 | 497 |
* @return the result of the reduction |
498 |
* @see #sum() |
|
499 |
* @see #min() |
|
500 |
* @see #max() |
|
501 |
* @see #average() |
|
502 |
*/ |
|
503 |
long reduce(long identity, LongBinaryOperator op); |
|
504 |
||
505 |
/** |
|
506 |
* Performs a <a href="package-summary.html#Reduction">reduction</a> on the |
|
507 |
* elements of this stream, using an |
|
508 |
* <a href="package-summary.html#Associativity">associative</a> accumulation |
|
509 |
* function, and returns an {@code OptionalLong} describing the reduced value, |
|
510 |
* if any. This is equivalent to: |
|
511 |
* <pre>{@code |
|
512 |
* boolean foundAny = false; |
|
513 |
* long result = null; |
|
514 |
* for (long element : this stream) { |
|
515 |
* if (!foundAny) { |
|
516 |
* foundAny = true; |
|
517 |
* result = element; |
|
518 |
* } |
|
519 |
* else |
|
22996
0ce70f4ef909
8029944: Primitive Stream reduce method documentation pseudo code misidentifies apply method
michaelm
parents:
22352
diff
changeset
|
520 |
* result = accumulator.applyAsLong(result, element); |
17167 | 521 |
* } |
522 |
* return foundAny ? OptionalLong.of(result) : OptionalLong.empty(); |
|
523 |
* }</pre> |
|
524 |
* |
|
525 |
* but is not constrained to execute sequentially. |
|
526 |
* |
|
527 |
* <p>The {@code accumulator} function must be an |
|
528 |
* <a href="package-summary.html#Associativity">associative</a> function. |
|
529 |
* |
|
530 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
531 |
* operation</a>. |
|
532 |
* |
|
21339 | 533 |
* @param op an <a href="package-summary.html#Associativity">associative</a>, |
534 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
535 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
536 |
* function for combining two values |
|
17167 | 537 |
* @return the result of the reduction |
538 |
* @see #reduce(long, LongBinaryOperator) |
|
539 |
*/ |
|
540 |
OptionalLong reduce(LongBinaryOperator op); |
|
541 |
||
542 |
/** |
|
543 |
* Performs a <a href="package-summary.html#MutableReduction">mutable |
|
544 |
* reduction</a> operation on the elements of this stream. A mutable |
|
19850 | 545 |
* reduction is one in which the reduced value is a mutable result container, |
17167 | 546 |
* such as an {@code ArrayList}, and elements are incorporated by updating |
19850 | 547 |
* the state of the result rather than by replacing the result. This |
17167 | 548 |
* produces a result equivalent to: |
549 |
* <pre>{@code |
|
19850 | 550 |
* R result = supplier.get(); |
17167 | 551 |
* for (long element : this stream) |
552 |
* accumulator.accept(result, element); |
|
553 |
* return result; |
|
554 |
* }</pre> |
|
555 |
* |
|
556 |
* <p>Like {@link #reduce(long, LongBinaryOperator)}, {@code collect} operations |
|
557 |
* can be parallelized without requiring additional synchronization. |
|
558 |
* |
|
559 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
560 |
* operation</a>. |
|
561 |
* |
|
562 |
* @param <R> type of the result |
|
19850 | 563 |
* @param supplier a function that creates a new result container. For a |
564 |
* parallel execution, this function may be called |
|
565 |
* multiple times and must return a fresh value each time. |
|
21339 | 566 |
* @param accumulator an <a href="package-summary.html#Associativity">associative</a>, |
567 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
568 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
569 |
* function for incorporating an additional element into a result |
|
570 |
* @param combiner an <a href="package-summary.html#Associativity">associative</a>, |
|
571 |
* <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
572 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
573 |
* function for combining two values, which must be |
|
574 |
* compatible with the accumulator function |
|
17167 | 575 |
* @return the result of the reduction |
576 |
* @see Stream#collect(Supplier, BiConsumer, BiConsumer) |
|
577 |
*/ |
|
19850 | 578 |
<R> R collect(Supplier<R> supplier, |
17167 | 579 |
ObjLongConsumer<R> accumulator, |
580 |
BiConsumer<R, R> combiner); |
|
581 |
||
582 |
/** |
|
583 |
* Returns the sum of elements in this stream. This is a special case |
|
19850 | 584 |
* of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 585 |
* and is equivalent to: |
586 |
* <pre>{@code |
|
587 |
* return reduce(0, Long::sum); |
|
588 |
* }</pre> |
|
589 |
* |
|
19850 | 590 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
591 |
* operation</a>. |
|
592 |
* |
|
17167 | 593 |
* @return the sum of elements in this stream |
594 |
*/ |
|
595 |
long sum(); |
|
596 |
||
597 |
/** |
|
598 |
* Returns an {@code OptionalLong} describing the minimum element of this |
|
599 |
* stream, or an empty optional if this stream is empty. This is a special |
|
19850 | 600 |
* case of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 601 |
* and is equivalent to: |
602 |
* <pre>{@code |
|
603 |
* return reduce(Long::min); |
|
604 |
* }</pre> |
|
605 |
* |
|
606 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
607 |
* |
|
608 |
* @return an {@code OptionalLong} containing the minimum element of this |
|
609 |
* stream, or an empty {@code OptionalLong} if the stream is empty |
|
610 |
*/ |
|
611 |
OptionalLong min(); |
|
612 |
||
613 |
/** |
|
614 |
* Returns an {@code OptionalLong} describing the maximum element of this |
|
615 |
* stream, or an empty optional if this stream is empty. This is a special |
|
19850 | 616 |
* case of a <a href="package-summary.html#Reduction">reduction</a> |
17167 | 617 |
* and is equivalent to: |
618 |
* <pre>{@code |
|
619 |
* return reduce(Long::max); |
|
620 |
* }</pre> |
|
621 |
* |
|
622 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
623 |
* operation</a>. |
|
624 |
* |
|
625 |
* @return an {@code OptionalLong} containing the maximum element of this |
|
626 |
* stream, or an empty {@code OptionalLong} if the stream is empty |
|
627 |
*/ |
|
628 |
OptionalLong max(); |
|
629 |
||
630 |
/** |
|
631 |
* Returns the count of elements in this stream. This is a special case of |
|
19850 | 632 |
* a <a href="package-summary.html#Reduction">reduction</a> and is |
17167 | 633 |
* equivalent to: |
634 |
* <pre>{@code |
|
635 |
* return map(e -> 1L).sum(); |
|
636 |
* }</pre> |
|
637 |
* |
|
638 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal operation</a>. |
|
639 |
* |
|
29489 | 640 |
* @apiNote |
641 |
* An implementation may choose to not execute the stream pipeline (either |
|
642 |
* sequentially or in parallel) if it is capable of computing the count |
|
643 |
* directly from the stream source. In such cases no source elements will |
|
644 |
* be traversed and no intermediate operations will be evaluated. |
|
645 |
* Behavioral parameters with side-effects, which are strongly discouraged |
|
646 |
* except for harmless cases such as debugging, may be affected. For |
|
647 |
* example, consider the following stream: |
|
648 |
* <pre>{@code |
|
649 |
* LongStream s = LongStream.of(1, 2, 3, 4); |
|
650 |
* long count = s.peek(System.out::println).count(); |
|
651 |
* }</pre> |
|
652 |
* The number of elements covered by the stream source is known and the |
|
653 |
* intermediate operation, {@code peek}, does not inject into or remove |
|
654 |
* elements from the stream (as may be the case for {@code flatMap} or |
|
655 |
* {@code filter} operations). Thus the count is 4 and there is no need to |
|
656 |
* execute the pipeline and, as a side-effect, print out the elements. |
|
657 |
* |
|
17167 | 658 |
* @return the count of elements in this stream |
659 |
*/ |
|
660 |
long count(); |
|
661 |
||
662 |
/** |
|
19214
e5901820c3c1
8015318: Extend Collector with 'finish' operation
briangoetz
parents:
18825
diff
changeset
|
663 |
* Returns an {@code OptionalDouble} describing the arithmetic mean of elements of |
17167 | 664 |
* this stream, or an empty optional if this stream is empty. This is a |
665 |
* special case of a |
|
19850 | 666 |
* <a href="package-summary.html#Reduction">reduction</a>. |
667 |
* |
|
668 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
669 |
* operation</a>. |
|
17167 | 670 |
* |
671 |
* @return an {@code OptionalDouble} containing the average element of this |
|
672 |
* stream, or an empty optional if the stream is empty |
|
673 |
*/ |
|
674 |
OptionalDouble average(); |
|
675 |
||
676 |
/** |
|
677 |
* Returns a {@code LongSummaryStatistics} describing various summary data |
|
678 |
* about the elements of this stream. This is a special case of a |
|
19850 | 679 |
* <a href="package-summary.html#Reduction">reduction</a>. |
680 |
* |
|
681 |
* <p>This is a <a href="package-summary.html#StreamOps">terminal |
|
682 |
* operation</a>. |
|
17167 | 683 |
* |
684 |
* @return a {@code LongSummaryStatistics} describing various summary data |
|
685 |
* about the elements of this stream |
|
686 |
*/ |
|
687 |
LongSummaryStatistics summaryStatistics(); |
|
688 |
||
689 |
/** |
|
690 |
* Returns whether any elements of this stream match the provided |
|
691 |
* predicate. May not evaluate the predicate on all elements if not |
|
21339 | 692 |
* necessary for determining the result. If the stream is empty then |
693 |
* {@code false} is returned and the predicate is not evaluated. |
|
17167 | 694 |
* |
695 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
696 |
* terminal operation</a>. |
|
697 |
* |
|
21339 | 698 |
* @apiNote |
699 |
* This method evaluates the <em>existential quantification</em> of the |
|
700 |
* predicate over the elements of the stream (for some x P(x)). |
|
701 |
* |
|
702 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
703 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
704 |
* predicate to apply to elements of this stream |
|
17167 | 705 |
* @return {@code true} if any elements of the stream match the provided |
21339 | 706 |
* predicate, otherwise {@code false} |
17167 | 707 |
*/ |
708 |
boolean anyMatch(LongPredicate predicate); |
|
709 |
||
710 |
/** |
|
711 |
* Returns whether all elements of this stream match the provided predicate. |
|
712 |
* May not evaluate the predicate on all elements if not necessary for |
|
21339 | 713 |
* determining the result. If the stream is empty then {@code true} is |
714 |
* returned and the predicate is not evaluated. |
|
17167 | 715 |
* |
716 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
717 |
* terminal operation</a>. |
|
718 |
* |
|
21339 | 719 |
* @apiNote |
720 |
* This method evaluates the <em>universal quantification</em> of the |
|
721 |
* predicate over the elements of the stream (for all x P(x)). If the |
|
722 |
* stream is empty, the quantification is said to be <em>vacuously |
|
723 |
* satisfied</em> and is always {@code true} (regardless of P(x)). |
|
724 |
* |
|
725 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
726 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
727 |
* predicate to apply to elements of this stream |
|
728 |
* @return {@code true} if either all elements of the stream match the |
|
729 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 730 |
*/ |
731 |
boolean allMatch(LongPredicate predicate); |
|
732 |
||
733 |
/** |
|
21339 | 734 |
* Returns whether no elements of this stream match the provided predicate. |
17167 | 735 |
* May not evaluate the predicate on all elements if not necessary for |
21339 | 736 |
* determining the result. If the stream is empty then {@code true} is |
737 |
* returned and the predicate is not evaluated. |
|
17167 | 738 |
* |
739 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
740 |
* terminal operation</a>. |
|
741 |
* |
|
21339 | 742 |
* @apiNote |
743 |
* This method evaluates the <em>universal quantification</em> of the |
|
744 |
* negated predicate over the elements of the stream (for all x ~P(x)). If |
|
745 |
* the stream is empty, the quantification is said to be vacuously satisfied |
|
746 |
* and is always {@code true}, regardless of P(x). |
|
747 |
* |
|
748 |
* @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>, |
|
749 |
* <a href="package-summary.html#Statelessness">stateless</a> |
|
750 |
* predicate to apply to elements of this stream |
|
751 |
* @return {@code true} if either no elements of the stream match the |
|
752 |
* provided predicate or the stream is empty, otherwise {@code false} |
|
17167 | 753 |
*/ |
754 |
boolean noneMatch(LongPredicate predicate); |
|
755 |
||
756 |
/** |
|
757 |
* Returns an {@link OptionalLong} describing the first element of this |
|
19850 | 758 |
* stream, or an empty {@code OptionalLong} if the stream is empty. If the |
759 |
* stream has no encounter order, then any element may be returned. |
|
17167 | 760 |
* |
761 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
762 |
* terminal operation</a>. |
|
763 |
* |
|
764 |
* @return an {@code OptionalLong} describing the first element of this |
|
765 |
* stream, or an empty {@code OptionalLong} if the stream is empty |
|
766 |
*/ |
|
767 |
OptionalLong findFirst(); |
|
768 |
||
769 |
/** |
|
770 |
* Returns an {@link OptionalLong} describing some element of the stream, or |
|
771 |
* an empty {@code OptionalLong} if the stream is empty. |
|
772 |
* |
|
773 |
* <p>This is a <a href="package-summary.html#StreamOps">short-circuiting |
|
774 |
* terminal operation</a>. |
|
775 |
* |
|
776 |
* <p>The behavior of this operation is explicitly nondeterministic; it is |
|
777 |
* free to select any element in the stream. This is to allow for maximal |
|
778 |
* performance in parallel operations; the cost is that multiple invocations |
|
19850 | 779 |
* on the same source may not return the same result. (If a stable result |
780 |
* is desired, use {@link #findFirst()} instead.) |
|
17167 | 781 |
* |
782 |
* @return an {@code OptionalLong} describing some element of this stream, |
|
783 |
* or an empty {@code OptionalLong} if the stream is empty |
|
784 |
* @see #findFirst() |
|
785 |
*/ |
|
786 |
OptionalLong findAny(); |
|
787 |
||
788 |
/** |
|
789 |
* Returns a {@code DoubleStream} consisting of the elements of this stream, |
|
790 |
* converted to {@code double}. |
|
791 |
* |
|
19850 | 792 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
793 |
* operation</a>. |
|
794 |
* |
|
17167 | 795 |
* @return a {@code DoubleStream} consisting of the elements of this stream, |
796 |
* converted to {@code double} |
|
797 |
*/ |
|
18154
5ede18269905
8015798: Rename IntStream.longs/doubles and LongStream.doubles to asXxxStream
psandoz
parents:
17914
diff
changeset
|
798 |
DoubleStream asDoubleStream(); |
17167 | 799 |
|
800 |
/** |
|
801 |
* Returns a {@code Stream} consisting of the elements of this stream, |
|
802 |
* each boxed to a {@code Long}. |
|
803 |
* |
|
19850 | 804 |
* <p>This is an <a href="package-summary.html#StreamOps">intermediate |
805 |
* operation</a>. |
|
806 |
* |
|
17167 | 807 |
* @return a {@code Stream} consistent of the elements of this stream, |
808 |
* each boxed to {@code Long} |
|
809 |
*/ |
|
810 |
Stream<Long> boxed(); |
|
811 |
||
812 |
@Override |
|
813 |
LongStream sequential(); |
|
814 |
||
815 |
@Override |
|
816 |
LongStream parallel(); |
|
817 |
||
818 |
@Override |
|
819 |
PrimitiveIterator.OfLong iterator(); |
|
820 |
||
821 |
@Override |
|
822 |
Spliterator.OfLong spliterator(); |
|
17195 | 823 |
|
824 |
// Static factories |
|
825 |
||
826 |
/** |
|
827 |
* Returns a builder for a {@code LongStream}. |
|
828 |
* |
|
829 |
* @return a stream builder |
|
830 |
*/ |
|
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
831 |
public static Builder builder() { |
17195 | 832 |
return new Streams.LongStreamBuilderImpl(); |
833 |
} |
|
834 |
||
835 |
/** |
|
836 |
* Returns an empty sequential {@code LongStream}. |
|
837 |
* |
|
838 |
* @return an empty sequential stream |
|
839 |
*/ |
|
840 |
public static LongStream empty() { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
841 |
return StreamSupport.longStream(Spliterators.emptyLongSpliterator(), false); |
17195 | 842 |
} |
843 |
||
844 |
/** |
|
845 |
* Returns a sequential {@code LongStream} containing a single element. |
|
846 |
* |
|
847 |
* @param t the single element |
|
848 |
* @return a singleton sequential stream |
|
849 |
*/ |
|
850 |
public static LongStream of(long t) { |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
851 |
return StreamSupport.longStream(new Streams.LongStreamBuilderImpl(t), false); |
17195 | 852 |
} |
853 |
||
854 |
/** |
|
19850 | 855 |
* Returns a sequential ordered stream whose elements are the specified values. |
17195 | 856 |
* |
857 |
* @param values the elements of the new stream |
|
858 |
* @return the new stream |
|
859 |
*/ |
|
860 |
public static LongStream of(long... values) { |
|
861 |
return Arrays.stream(values); |
|
862 |
} |
|
863 |
||
864 |
/** |
|
19850 | 865 |
* Returns an infinite sequential ordered {@code LongStream} produced by iterative |
17195 | 866 |
* application of a function {@code f} to an initial element {@code seed}, |
867 |
* producing a {@code Stream} consisting of {@code seed}, {@code f(seed)}, |
|
868 |
* {@code f(f(seed))}, etc. |
|
869 |
* |
|
870 |
* <p>The first element (position {@code 0}) in the {@code LongStream} will |
|
871 |
* be the provided {@code seed}. For {@code n > 0}, the element at position |
|
872 |
* {@code n}, will be the result of applying the function {@code f} to the |
|
873 |
* element at position {@code n - 1}. |
|
874 |
* |
|
875 |
* @param seed the initial element |
|
25526 | 876 |
* @param f a function to be applied to the previous element to produce |
17195 | 877 |
* a new element |
878 |
* @return a new sequential {@code LongStream} |
|
879 |
*/ |
|
880 |
public static LongStream iterate(final long seed, final LongUnaryOperator f) { |
|
881 |
Objects.requireNonNull(f); |
|
882 |
final PrimitiveIterator.OfLong iterator = new PrimitiveIterator.OfLong() { |
|
883 |
long t = seed; |
|
884 |
||
885 |
@Override |
|
886 |
public boolean hasNext() { |
|
887 |
return true; |
|
888 |
} |
|
889 |
||
890 |
@Override |
|
891 |
public long nextLong() { |
|
892 |
long v = t; |
|
893 |
t = f.applyAsLong(t); |
|
894 |
return v; |
|
895 |
} |
|
896 |
}; |
|
897 |
return StreamSupport.longStream(Spliterators.spliteratorUnknownSize( |
|
898 |
iterator, |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
899 |
Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL), false); |
17195 | 900 |
} |
901 |
||
902 |
/** |
|
21339 | 903 |
* Returns an infinite sequential unordered stream where each element is |
904 |
* generated by the provided {@code LongSupplier}. This is suitable for |
|
905 |
* generating constant streams, streams of random elements, etc. |
|
17195 | 906 |
* |
907 |
* @param s the {@code LongSupplier} for generated elements |
|
21339 | 908 |
* @return a new infinite sequential unordered {@code LongStream} |
17195 | 909 |
*/ |
910 |
public static LongStream generate(LongSupplier s) { |
|
911 |
Objects.requireNonNull(s); |
|
18572
53b8b8c30086
8012987: Optimizations for Stream.limit/substream
psandoz
parents:
18158
diff
changeset
|
912 |
return StreamSupport.longStream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
913 |
new StreamSpliterators.InfiniteSupplyingSpliterator.OfLong(Long.MAX_VALUE, s), false); |
17195 | 914 |
} |
915 |
||
916 |
/** |
|
19850 | 917 |
* Returns a sequential ordered {@code LongStream} from {@code startInclusive} |
17195 | 918 |
* (inclusive) to {@code endExclusive} (exclusive) by an incremental step of |
18158 | 919 |
* {@code 1}. |
17195 | 920 |
* |
18158 | 921 |
* @apiNote |
922 |
* <p>An equivalent sequence of increasing values can be produced |
|
923 |
* sequentially using a {@code for} loop as follows: |
|
17195 | 924 |
* <pre>{@code |
18158 | 925 |
* for (long i = startInclusive; i < endExclusive ; i++) { ... } |
17195 | 926 |
* }</pre> |
927 |
* |
|
928 |
* @param startInclusive the (inclusive) initial value |
|
929 |
* @param endExclusive the exclusive upper bound |
|
930 |
* @return a sequential {@code LongStream} for the range of {@code long} |
|
931 |
* elements |
|
932 |
*/ |
|
933 |
public static LongStream range(long startInclusive, final long endExclusive) { |
|
18158 | 934 |
if (startInclusive >= endExclusive) { |
935 |
return empty(); |
|
936 |
} else if (endExclusive - startInclusive < 0) { |
|
937 |
// Size of range > Long.MAX_VALUE |
|
938 |
// Split the range in two and concatenate |
|
939 |
// Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE) then |
|
940 |
// the lower range, [Long.MIN_VALUE, 0) will be further split in two |
|
18820 | 941 |
long m = startInclusive + Long.divideUnsigned(endExclusive - startInclusive, 2) + 1; |
942 |
return concat(range(startInclusive, m), range(m, endExclusive)); |
|
18158 | 943 |
} else { |
944 |
return StreamSupport.longStream( |
|
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
945 |
new Streams.RangeLongSpliterator(startInclusive, endExclusive, false), false); |
18158 | 946 |
} |
17195 | 947 |
} |
948 |
||
949 |
/** |
|
19850 | 950 |
* Returns a sequential ordered {@code LongStream} from {@code startInclusive} |
18158 | 951 |
* (inclusive) to {@code endInclusive} (inclusive) by an incremental step of |
952 |
* {@code 1}. |
|
17195 | 953 |
* |
18158 | 954 |
* @apiNote |
17195 | 955 |
* <p>An equivalent sequence of increasing values can be produced |
956 |
* sequentially using a {@code for} loop as follows: |
|
957 |
* <pre>{@code |
|
18158 | 958 |
* for (long i = startInclusive; i <= endInclusive ; i++) { ... } |
17195 | 959 |
* }</pre> |
960 |
* |
|
961 |
* @param startInclusive the (inclusive) initial value |
|
18158 | 962 |
* @param endInclusive the inclusive upper bound |
17195 | 963 |
* @return a sequential {@code LongStream} for the range of {@code long} |
964 |
* elements |
|
965 |
*/ |
|
18158 | 966 |
public static LongStream rangeClosed(long startInclusive, final long endInclusive) { |
967 |
if (startInclusive > endInclusive) { |
|
17195 | 968 |
return empty(); |
18158 | 969 |
} else if (endInclusive - startInclusive + 1 <= 0) { |
970 |
// Size of range > Long.MAX_VALUE |
|
971 |
// Split the range in two and concatenate |
|
972 |
// Note: if the range is [Long.MIN_VALUE, Long.MAX_VALUE] then |
|
973 |
// the lower range, [Long.MIN_VALUE, 0), and upper range, |
|
974 |
// [0, Long.MAX_VALUE], will both be further split in two |
|
18820 | 975 |
long m = startInclusive + Long.divideUnsigned(endInclusive - startInclusive, 2) + 1; |
976 |
return concat(range(startInclusive, m), rangeClosed(m, endInclusive)); |
|
17195 | 977 |
} else { |
18158 | 978 |
return StreamSupport.longStream( |
18822
4b6be7c19547
8019395: Consolidate StreamSupport.{stream,parallelStream} into a single method
psandoz
parents:
18820
diff
changeset
|
979 |
new Streams.RangeLongSpliterator(startInclusive, endInclusive, true), false); |
17195 | 980 |
} |
981 |
} |
|
18820 | 982 |
|
983 |
/** |
|
19850 | 984 |
* Creates a lazily concatenated stream whose elements are all the |
985 |
* elements of the first stream followed by all the elements of the |
|
21339 | 986 |
* second stream. The resulting stream is ordered if both |
18820 | 987 |
* of the input streams are ordered, and parallel if either of the input |
19800 | 988 |
* streams is parallel. When the resulting stream is closed, the close |
989 |
* handlers for both input streams are invoked. |
|
18820 | 990 |
* |
21339 | 991 |
* @implNote |
992 |
* Use caution when constructing streams from repeated concatenation. |
|
993 |
* 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
|
994 |
* call chains, or even {@code StackOverflowError}. |
21339 | 995 |
* |
18820 | 996 |
* @param a the first stream |
19850 | 997 |
* @param b the second stream |
998 |
* @return the concatenation of the two input streams |
|
18820 | 999 |
*/ |
1000 |
public static LongStream concat(LongStream a, LongStream b) { |
|
1001 |
Objects.requireNonNull(a); |
|
1002 |
Objects.requireNonNull(b); |
|
1003 |
||
1004 |
Spliterator.OfLong split = new Streams.ConcatSpliterator.OfLong( |
|
1005 |
a.spliterator(), b.spliterator()); |
|
19800 | 1006 |
LongStream stream = StreamSupport.longStream(split, a.isParallel() || b.isParallel()); |
1007 |
return stream.onClose(Streams.composedClose(a, b)); |
|
18820 | 1008 |
} |
18825
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1009 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1010 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1011 |
* A mutable builder for a {@code LongStream}. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1012 |
* |
19850 | 1013 |
* <p>A stream builder has a lifecycle, which starts in a building |
1014 |
* phase, during which elements can be added, and then transitions to a built |
|
1015 |
* 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
|
1016 |
* 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
|
1017 |
* 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
|
1018 |
* stream builder, in the order they were added. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1019 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1020 |
* @see LongStream#builder() |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1021 |
* @since 1.8 |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1022 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1023 |
public interface Builder extends LongConsumer { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1024 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1025 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1026 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1027 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1028 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1029 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1030 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1031 |
@Override |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1032 |
void accept(long t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1033 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1034 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1035 |
* Adds an element to the stream being built. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1036 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1037 |
* @implSpec |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1038 |
* The default implementation behaves as if: |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1039 |
* <pre>{@code |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1040 |
* accept(t) |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1041 |
* return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1042 |
* }</pre> |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1043 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1044 |
* @param t the element to add |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1045 |
* @return {@code this} builder |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1046 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1047 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1048 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1049 |
default Builder add(long t) { |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1050 |
accept(t); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1051 |
return this; |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1052 |
} |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1053 |
|
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1054 |
/** |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1055 |
* Builds the stream, transitioning this builder to the built state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1056 |
* An {@code IllegalStateException} is thrown if there are further |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1057 |
* 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
|
1058 |
* state. |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1059 |
* |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1060 |
* @return the built stream |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1061 |
* @throws IllegalStateException if the builder has already transitioned |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1062 |
* to the built state |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1063 |
*/ |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1064 |
LongStream build(); |
06636235cd12
8020062: Nest StreamBuilder interfaces inside relevant Stream interfaces
henryjen
parents:
18822
diff
changeset
|
1065 |
} |
17167 | 1066 |
} |