Interface BlockingIterable<T>
- Type Parameters:
T- the type of elements returned by theBlockingIterator.
- All Superinterfaces:
CloseableIterable<T>,Iterable<T>
- All Known Subinterfaces:
BlockingIterable.Processor<T>,HttpMessageBodyIterable<T>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
Iterable which supports generation of BlockingIterators.
This interface is meant to be the synchronous API equivalent of PublisherSource. Each call to
iterator() is equivalent to calling PublisherSource.subscribe(Subscriber) and typically has the
same characteristics in terms of being able to call the method multiple times and data availability in memory.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceABlockingIterablethat supports to dynamically emitting items usingBlockingIterable.Processor.next(Object). -
Method Summary
Modifier and TypeMethodDescriptiondefault voiddefault voidMimics the behavior offorEach(Consumer)but applies atimeoutduration for the overall completion of this method.default voidforEach(Consumer<? super T> action, LongSupplier timeoutSupplier, TimeUnit unit) Mimics the behavior offorEach(Consumer)but uses thetimeoutSupplierto determine the timeout value for interactions with theBlockingIterator.iterator()default BlockingSpliterator<T>The same behavior asIterable.spliterator(), but returns aBlockingSpliteratorview.
-
Method Details
-
iterator
BlockingIterator<T> iterator() -
forEach
-
forEach
default void forEach(Consumer<? super T> action, LongSupplier timeoutSupplier, TimeUnit unit) throws TimeoutException Mimics the behavior offorEach(Consumer)but uses thetimeoutSupplierto determine the timeout value for interactions with theBlockingIterator.By default, the
timeoutSupplierwill be used for each interaction withBlockingIterator.hasNext(long, TimeUnit)andBlockingIterator.next(long, TimeUnit). However, implementations ofBlockingIterablemay decide to only apply the timeout when they are not sure if an interaction with theBlockingIteratorwill block or not.Note: This method can sneaky-throw an
InterruptedExceptionwhen a blocking operation internally does so. The reason it's not declared is that theIteratorandAutoCloseableinterfaces do not declare checked exceptions andBlockingIteratorextends them to allow use in try-with-resources and enhanced for loop.- Parameters:
action- The action to be performed for each element.timeoutSupplier- ALongSupplierthat provides the timeout duration for the next call toBlockingIterator.hasNext(long, TimeUnit)andBlockingIterator.next(long, TimeUnit). These methods should be consulted for the meaning of non-positive timeout durations.unit- The units for the duration of time.- Throws:
TimeoutException- If an individual call toBlockingIterator.hasNext(long, TimeUnit)takes longer than thetimeoutduration.
-
forEach
default void forEach(Consumer<? super T> action, long timeout, TimeUnit unit) throws TimeoutException Mimics the behavior offorEach(Consumer)but applies atimeoutduration for the overall completion of this method. Thetimeoutis adjusted for each interaction with theBlockingIteratorwhich may block.Note that the
timeoutduration is an approximation and this duration maybe exceeded if data is available without blocking.By default, the
timeoutwill be used for each interaction withBlockingIterator.hasNext(long, TimeUnit)andBlockingIterator.next(long, TimeUnit). However, implementations ofBlockingIterablemay decide to only apply the timeout when they are not sure if an interaction with theBlockingIteratorwill block or not.Note: This method can sneaky-throw an
InterruptedExceptionwhen a blocking operation internally does so. The reason it's not declared is that theIteratorandAutoCloseableinterfaces do not declare checked exceptions andBlockingIteratorextends them to allow use in try-with-resources and enhanced for loop.- Parameters:
action- The action to be performed for each element.timeout- An approximate total duration for the overall completion of this method. This value is used to approximate because the actual duration maybe longer if data is available without blocking.unit- The units for the duration of time.- Throws:
TimeoutException- If the total iteration time as determined byBlockingIterator.hasNext(long, TimeUnit)takes longer than thetimeoutduration.
-
spliterator
The same behavior asIterable.spliterator(), but returns aBlockingSpliteratorview.Calling
BlockingSpliterator.close()may result in closing of the underlyingBlockingIterator.- Specified by:
spliteratorin interfaceIterable<T>- Returns:
- a
BlockingSpliteratorover the elements described by thisBlockingIterable.
-