Interface BlockingIterator<T>
- Type Parameters:
T- the type of elements returned by thisIterator.
- All Superinterfaces:
AutoCloseable,CloseableIterator<T>,Iterator<T>
- All Known Subinterfaces:
HttpMessageBodyIterator<T>
Iterator that is also an AutoCloseable and whose blocking operations support timeout durations.
This interface is meant to be the synchronous API equivalent of PublisherSource.Subscriber coupled with a
PublisherSource.Subscription. If the data is not completely consumed from this Iterable then close()
MUST be called. If the data is completely consumed (e.g. Iterator.hasNext() and/or
hasNext(long, TimeUnit) return false) then this object is implicitly closed.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()This method is used to communicate that you are no longer interested in consuming data.booleanThe equivalent ofIterator.hasNext()but only waits fortimeoutduration amount of time.next()The equivalent ofnext()but only waits fortimeoutduration of time.Methods inherited from interface java.util.Iterator
forEachRemaining, hasNext, remove
-
Method Details
-
hasNext
The equivalent ofIterator.hasNext()but only waits fortimeoutduration amount of time.Note that
close()is not required to interrupt this call.Also 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:
timeout- The duration of time to wait. If this value is non-positive that means the timeout expiration is immediate or in the past. In this case, if this implementation cannot determine if there is more data immediately (e.g. without external dependencies) then aTimeoutExceptionshould be thrown, otherwise the method can return the known status.unit- The units for the duration of time.- Returns:
- See the return value for
Iterator.hasNext(). If this value isfalsethen this object is implicitlyclosed. - Throws:
TimeoutException- if the wait timed out. This object is implicitlyclosedif this occurs.
-
next
The equivalent ofnext()but only waits fortimeoutduration of time.Note that
close()is not required to interrupt this call.Also 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:
timeout- The duration of time to wait. If this value is non-positive that means the timeout expiration is immediate or in the past. In this case, if this implementation cannot determine if there is more data immediately (e.g. without external dependencies) then aTimeoutExceptionshould be thrown, otherwise the method can return the known status.unit- The units for the duration of time.- Returns:
- See the return value for
next(). - Throws:
NoSuchElementException- if the iteration has no more elements.TimeoutException- if the wait timed out. This object is implicitlyclosedif this occurs.
-
next
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. -
close
This method is used to communicate that you are no longer interested in consuming data. This provides a "best effort" notification to the producer of data that you are no longer interested in data from thisIterator. It is still possible that data may be obtained after calling this method, for example if there is data currently queued in memory.If all the data has not been consumed (e.g.
Iterator.hasNext()and/orhasNext(long, TimeUnit)have not yet returnedfalse) this may have transport implications (e.g. if the source of data comes from a socket or file descriptor). If all data has been consumed, or thisBlockingIteratorhas previously been closed this should be a noop.- Specified by:
closein interfaceAutoCloseable- Throws:
Exception
-