309 * handlers for both the returned and this stream are invoked.
310 *
311 * @apiNote
312 * While {@code takeWhile()} is generally a cheap operation on sequential
313 * stream pipelines, it can be quite expensive on ordered parallel
314 * pipelines, since the operation is constrained to return not just any
315 * valid prefix, but the longest prefix of elements in the encounter order.
316 * Using an unordered stream source (such as {@link #generate(IntSupplier)})
317 * or removing the ordering constraint with {@link #unordered()} may result
318 * in significant speedups of {@code takeWhile()} in parallel pipelines, if
319 * the semantics of your situation permit. If consistency with encounter
320 * order is required, and you are experiencing poor performance or memory
321 * utilization with {@code takeWhile()} in parallel pipelines, switching to
322 * sequential execution with {@link #sequential()} may improve performance.
323 *
324 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
325 * <a href="package-summary.html#Statelessness">stateless</a>
326 * predicate to apply to elements to determine the longest
327 * prefix of elements.
328 * @return the new stream
329 * @since 1.9
330 */
331 default IntStream takeWhile(IntPredicate predicate) {
332 Objects.requireNonNull(predicate);
333 // Reuses the unordered spliterator, which, when encounter is present,
334 // is safe to use as long as it configured not to split
335 return StreamSupport.intStream(
336 new WhileOps.UnorderedWhileSpliterator.OfInt.Taking(spliterator(), true, predicate),
337 isParallel()).onClose(this::close);
338 }
339
340 /**
341 * Returns, if this stream is ordered, a stream consisting of the remaining
342 * elements of this stream after dropping the longest prefix of elements
343 * that match the given predicate. Otherwise returns, if this stream is
344 * unordered, a stream consisting of the remaining elements of this stream
345 * after dropping a subset of elements that match the given predicate.
346 *
347 * <p>If this stream is ordered then the longest prefix is a contiguous
348 * sequence of elements of this stream that match the given predicate. The
349 * first element of the sequence is the first element of this stream, and
375 * handlers for both the returned and this stream are invoked.
376 *
377 * @apiNote
378 * While {@code dropWhile()} is generally a cheap operation on sequential
379 * stream pipelines, it can be quite expensive on ordered parallel
380 * pipelines, since the operation is constrained to return not just any
381 * valid prefix, but the longest prefix of elements in the encounter order.
382 * Using an unordered stream source (such as {@link #generate(IntSupplier)})
383 * or removing the ordering constraint with {@link #unordered()} may result
384 * in significant speedups of {@code dropWhile()} in parallel pipelines, if
385 * the semantics of your situation permit. If consistency with encounter
386 * order is required, and you are experiencing poor performance or memory
387 * utilization with {@code dropWhile()} in parallel pipelines, switching to
388 * sequential execution with {@link #sequential()} may improve performance.
389 *
390 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
391 * <a href="package-summary.html#Statelessness">stateless</a>
392 * predicate to apply to elements to determine the longest
393 * prefix of elements.
394 * @return the new stream
395 * @since 1.9
396 */
397 default IntStream dropWhile(IntPredicate predicate) {
398 Objects.requireNonNull(predicate);
399 // Reuses the unordered spliterator, which, when encounter is present,
400 // is safe to use as long as it configured not to split
401 return StreamSupport.intStream(
402 new WhileOps.UnorderedWhileSpliterator.OfInt.Dropping(spliterator(), true, predicate),
403 isParallel()).onClose(this::close);
404 }
405
406 /**
407 * Performs an action for each element of this stream.
408 *
409 * <p>This is a <a href="package-summary.html#StreamOps">terminal
410 * operation</a>.
411 *
412 * <p>For parallel stream pipelines, this operation does <em>not</em>
413 * guarantee to respect the encounter order of the stream, as doing so
414 * would sacrifice the benefit of parallelism. For any given element, the
415 * action may be performed at whatever time and in whatever thread the
|
309 * handlers for both the returned and this stream are invoked.
310 *
311 * @apiNote
312 * While {@code takeWhile()} is generally a cheap operation on sequential
313 * stream pipelines, it can be quite expensive on ordered parallel
314 * pipelines, since the operation is constrained to return not just any
315 * valid prefix, but the longest prefix of elements in the encounter order.
316 * Using an unordered stream source (such as {@link #generate(IntSupplier)})
317 * or removing the ordering constraint with {@link #unordered()} may result
318 * in significant speedups of {@code takeWhile()} in parallel pipelines, if
319 * the semantics of your situation permit. If consistency with encounter
320 * order is required, and you are experiencing poor performance or memory
321 * utilization with {@code takeWhile()} in parallel pipelines, switching to
322 * sequential execution with {@link #sequential()} may improve performance.
323 *
324 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
325 * <a href="package-summary.html#Statelessness">stateless</a>
326 * predicate to apply to elements to determine the longest
327 * prefix of elements.
328 * @return the new stream
329 * @since 9
330 */
331 default IntStream takeWhile(IntPredicate predicate) {
332 Objects.requireNonNull(predicate);
333 // Reuses the unordered spliterator, which, when encounter is present,
334 // is safe to use as long as it configured not to split
335 return StreamSupport.intStream(
336 new WhileOps.UnorderedWhileSpliterator.OfInt.Taking(spliterator(), true, predicate),
337 isParallel()).onClose(this::close);
338 }
339
340 /**
341 * Returns, if this stream is ordered, a stream consisting of the remaining
342 * elements of this stream after dropping the longest prefix of elements
343 * that match the given predicate. Otherwise returns, if this stream is
344 * unordered, a stream consisting of the remaining elements of this stream
345 * after dropping a subset of elements that match the given predicate.
346 *
347 * <p>If this stream is ordered then the longest prefix is a contiguous
348 * sequence of elements of this stream that match the given predicate. The
349 * first element of the sequence is the first element of this stream, and
375 * handlers for both the returned and this stream are invoked.
376 *
377 * @apiNote
378 * While {@code dropWhile()} is generally a cheap operation on sequential
379 * stream pipelines, it can be quite expensive on ordered parallel
380 * pipelines, since the operation is constrained to return not just any
381 * valid prefix, but the longest prefix of elements in the encounter order.
382 * Using an unordered stream source (such as {@link #generate(IntSupplier)})
383 * or removing the ordering constraint with {@link #unordered()} may result
384 * in significant speedups of {@code dropWhile()} in parallel pipelines, if
385 * the semantics of your situation permit. If consistency with encounter
386 * order is required, and you are experiencing poor performance or memory
387 * utilization with {@code dropWhile()} in parallel pipelines, switching to
388 * sequential execution with {@link #sequential()} may improve performance.
389 *
390 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
391 * <a href="package-summary.html#Statelessness">stateless</a>
392 * predicate to apply to elements to determine the longest
393 * prefix of elements.
394 * @return the new stream
395 * @since 9
396 */
397 default IntStream dropWhile(IntPredicate predicate) {
398 Objects.requireNonNull(predicate);
399 // Reuses the unordered spliterator, which, when encounter is present,
400 // is safe to use as long as it configured not to split
401 return StreamSupport.intStream(
402 new WhileOps.UnorderedWhileSpliterator.OfInt.Dropping(spliterator(), true, predicate),
403 isParallel()).onClose(this::close);
404 }
405
406 /**
407 * Performs an action for each element of this stream.
408 *
409 * <p>This is a <a href="package-summary.html#StreamOps">terminal
410 * operation</a>.
411 *
412 * <p>For parallel stream pipelines, this operation does <em>not</em>
413 * guarantee to respect the encounter order of the stream, as doing so
414 * would sacrifice the benefit of parallelism. For any given element, the
415 * action may be performed at whatever time and in whatever thread the
|