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>
Iterable
which supports generation of BlockingIterator
s.
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
Modifier and TypeInterfaceDescriptionstatic interface
ABlockingIterable
that supports to dynamically emitting items usingBlockingIterable.Processor.next(Object)
. -
Method Summary
Modifier and TypeMethodDescriptiondefault void
Mimics the behavior ofIterable.forEach(Consumer)
but applies atimeout
duration for the overall completion of this method.default void
forEach
(Consumer<? super T> action, LongSupplier timeoutSupplier, TimeUnit unit) Mimics the behavior ofIterable.forEach(Consumer)
but uses thetimeoutSupplier
to determine the timeout value for interactions with theBlockingIterator
.iterator()
default BlockingSpliterator<T>
The same behavior asIterable.spliterator()
, but returns aBlockingSpliterator
view.
-
Method Details
-
iterator
BlockingIterator<T> iterator() -
forEach
default void forEach(Consumer<? super T> action, LongSupplier timeoutSupplier, TimeUnit unit) throws TimeoutException Mimics the behavior ofIterable.forEach(Consumer)
but uses thetimeoutSupplier
to determine the timeout value for interactions with theBlockingIterator
.By default the
timeoutSupplier
will be used for each interaction withBlockingIterator.hasNext(long, TimeUnit)
andBlockingIterator.next(long, TimeUnit)
. However implementations ofBlockingIterable
may decide to only apply the timeout when they are not sure if an interaction with theBlockingIterator
will block or not.Note: This method can sneaky-throw an
InterruptedException
when a blocking operation internally does so. The reason it's not declared is that theIterator
andAutoCloseable
interfaces do not declare checked exceptions andBlockingIterator
extends them to allow use in try-with-resources and enhanced for loop.- Parameters:
action
- The action to be performed for each element.timeoutSupplier
- ALongSupplier
that 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 thetimeout
duration.
-
forEach
default void forEach(Consumer<? super T> action, long timeout, TimeUnit unit) throws TimeoutException Mimics the behavior ofIterable.forEach(Consumer)
but applies atimeout
duration for the overall completion of this method. Thetimeout
is adjusted for each interaction with theBlockingIterator
which may block.Note that the
timeout
duration is an approximation and this duration maybe exceeded if data is available without blocking.By default the
timeout
will be used for each interaction withBlockingIterator.hasNext(long, TimeUnit)
andBlockingIterator.next(long, TimeUnit)
. However implementations ofBlockingIterable
may decide to only apply the timeout when they are not be sure if an interaction with theBlockingIterator
will block or not.Note: This method can sneaky-throw an
InterruptedException
when a blocking operation internally does so. The reason it's not declared is that theIterator
andAutoCloseable
interfaces do not declare checked exceptions andBlockingIterator
extends 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 thetimeout
duration.
-
spliterator
The same behavior asIterable.spliterator()
, but returns aBlockingSpliterator
view.Calling
BlockingSpliterator.close()
may result in closing of the underlyingBlockingIterator
.- Specified by:
spliterator
in interfaceIterable<T>
- Returns:
- a
BlockingSpliterator
over the elements described by thisBlockingIterable
.
-