Class BlockingObservable<T>
- Type Parameters:
T
- the type of item emitted by theBlockingObservable
BlockingObservable
is a variety of Observable
that provides blocking operators. It can be
useful for testing and demo purposes, but is generally inappropriate for production applications (if you
think you need to use a BlockingObservable
this is usually a sign that you should rethink your
design).
You construct a BlockingObservable
from an Observable
with from(Observable)
or
Observable.toBlocking()
.
The documentation for this interface makes use of a form of marble diagram that has been modified to illustrate blocking operators. The following legend explains these marble diagrams:
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Observable
<? extends T> (package private) static final Object
Constant to indicate the onStart method should be called.(package private) static final Object
Constant indicating the setProducer method should be called.(package private) static final Object
Indicates an unsubscription happened -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate T
blockForSingle
(Observable<? extends T> observable) Helper method which handles the actual blocking for a single response.first()
Returns the first item emitted by thisBlockingObservable
, or throwsNoSuchElementException
if it emits no items.Returns the first item emitted by thisBlockingObservable
that matches a predicate, or throwsNoSuchElementException
if it emits no such item.firstOrDefault
(T defaultValue) Returns the first item emitted by thisBlockingObservable
, or a default value if it emits no items.firstOrDefault
(T defaultValue, Func1<? super T, Boolean> predicate) Returns the first item emitted by thisBlockingObservable
that matches a predicate, or a default value if it emits no such items.void
Invokes a method on each item emitted by thisBlockingObservable
and blocks until the Observable completes.static <T> BlockingObservable
<T> from
(Observable<? extends T> o) Converts anObservable
into aBlockingObservable
.Returns anIterator
that iterates over all items emitted by thisBlockingObservable
.last()
Returns the last item emitted by thisBlockingObservable
, or throwsNoSuchElementException
if thisBlockingObservable
emits no items.Returns the last item emitted by thisBlockingObservable
that matches a predicate, or throwsNoSuchElementException
if it emits no such items.lastOrDefault
(T defaultValue) Returns the last item emitted by thisBlockingObservable
, or a default value if it emits no items.lastOrDefault
(T defaultValue, Func1<? super T, Boolean> predicate) Returns the last item emitted by thisBlockingObservable
that matches a predicate, or a default value if it emits no such items.latest()
Returns anIterable
that returns the latest item emitted by thisBlockingObservable
, waiting if necessary for one to become available.mostRecent
(T initialValue) Returns anIterable
that always returns the item most recently emitted by thisBlockingObservable
.next()
Returns anIterable
that blocks until thisBlockingObservable
emits another item, then returns that item.single()
If thisBlockingObservable
completes after emitting a single item, return that item, otherwise throw aNoSuchElementException
.If thisBlockingObservable
completes after emitting a single item that matches a given predicate, return that item, otherwise throw aNoSuchElementException
.singleOrDefault
(T defaultValue) If thisBlockingObservable
completes after emitting a single item, return that item; if it emits more than one item, throw anIllegalArgumentException
; if it emits no items, return a default value.singleOrDefault
(T defaultValue, Func1<? super T, Boolean> predicate) If thisBlockingObservable
completes after emitting a single item that matches a predicate, return that item; if it emits more than one such item, throw anIllegalArgumentException
; if it emits no items, return a default value.void
Runs the source observable to a terminal event, ignoring any values and rethrowing any exception.void
Subscribes to the source and calls the given action on the current thread and rethrows any exception wrapped into OnErrorNotImplementedException.void
Subscribes to the source and calls the given actions on the current thread.void
Subscribes to the source and calls the given actions on the current thread.void
Subscribes to the source and calls back the Observer methods on the current thread.void
subscribe
(Subscriber<? super T> subscriber) Subscribes to the source and calls the Subscriber methods on the current thread.toFuture()
Returns aFuture
representing the single value emitted by thisBlockingObservable
.Converts thisBlockingObservable
into anIterable
.
-
Field Details
-
o
-
ON_START
Constant to indicate the onStart method should be called. -
SET_PRODUCER
Constant indicating the setProducer method should be called. -
UNSUBSCRIBE
Indicates an unsubscription happened
-
-
Constructor Details
-
BlockingObservable
-
-
Method Details
-
from
Converts anObservable
into aBlockingObservable
.- Type Parameters:
T
- the observed value type- Parameters:
o
- theObservable
you want to convert- Returns:
- a
BlockingObservable
version ofo
-
forEach
Invokes a method on each item emitted by thisBlockingObservable
and blocks until the Observable completes.Note: This will block even if the underlying Observable is asynchronous.
This is similar to
Observable.subscribe(Subscriber)
, but it blocks. Because it blocks it does not need theObserver.onCompleted()
orObserver.onError(Throwable)
methods. If the underlying Observable terminates with an error, rather than callingonError
, this method will throw an exception.The difference between this method and
subscribe(Action1)
is that theonNext
action is executed on the emission thread instead of the current thread.- Parameters:
onNext
- theAction1
to invoke for each item emitted by theBlockingObservable
- Throws:
RuntimeException
- if an error occurs- See Also:
-
getIterator
- Returns:
- an
Iterator
that can iterate over the items emitted by thisBlockingObservable
- See Also:
-
first
Returns the first item emitted by thisBlockingObservable
, or throwsNoSuchElementException
if it emits no items.- Returns:
- the first item emitted by this
BlockingObservable
- Throws:
NoSuchElementException
- if thisBlockingObservable
emits no items- See Also:
-
first
Returns the first item emitted by thisBlockingObservable
that matches a predicate, or throwsNoSuchElementException
if it emits no such item.- Parameters:
predicate
- a predicate function to evaluate items emitted by thisBlockingObservable
- Returns:
- the first item emitted by this
BlockingObservable
that matches the predicate - Throws:
NoSuchElementException
- if thisBlockingObservable
emits no such items- See Also:
-
firstOrDefault
Returns the first item emitted by thisBlockingObservable
, or a default value if it emits no items.- Parameters:
defaultValue
- a default value to return if thisBlockingObservable
emits no items- Returns:
- the first item emitted by this
BlockingObservable
, or the default value if it emits no items - See Also:
-
firstOrDefault
Returns the first item emitted by thisBlockingObservable
that matches a predicate, or a default value if it emits no such items.- Parameters:
defaultValue
- a default value to return if thisBlockingObservable
emits no matching itemspredicate
- a predicate function to evaluate items emitted by thisBlockingObservable
- Returns:
- the first item emitted by this
BlockingObservable
that matches the predicate, or the default value if thisBlockingObservable
emits no matching items - See Also:
-
last
Returns the last item emitted by thisBlockingObservable
, or throwsNoSuchElementException
if thisBlockingObservable
emits no items.- Returns:
- the last item emitted by this
BlockingObservable
- Throws:
NoSuchElementException
- if thisBlockingObservable
emits no items- See Also:
-
last
Returns the last item emitted by thisBlockingObservable
that matches a predicate, or throwsNoSuchElementException
if it emits no such items.- Parameters:
predicate
- a predicate function to evaluate items emitted by theBlockingObservable
- Returns:
- the last item emitted by the
BlockingObservable
that matches the predicate - Throws:
NoSuchElementException
- if thisBlockingObservable
emits no items- See Also:
-
lastOrDefault
Returns the last item emitted by thisBlockingObservable
, or a default value if it emits no items.- Parameters:
defaultValue
- a default value to return if thisBlockingObservable
emits no items- Returns:
- the last item emitted by the
BlockingObservable
, or the default value if it emits no items - See Also:
-
lastOrDefault
Returns the last item emitted by thisBlockingObservable
that matches a predicate, or a default value if it emits no such items.- Parameters:
defaultValue
- a default value to return if thisBlockingObservable
emits no matching itemspredicate
- a predicate function to evaluate items emitted by thisBlockingObservable
- Returns:
- the last item emitted by this
BlockingObservable
that matches the predicate, or the default value if it emits no matching items - See Also:
-
mostRecent
-
next
Returns anIterable
that blocks until thisBlockingObservable
emits another item, then returns that item.- Returns:
- an
Iterable
that blocks upon each iteration until thisBlockingObservable
emits a new item, whereupon the Iterable returns that item - See Also:
-
latest
Returns anIterable
that returns the latest item emitted by thisBlockingObservable
, waiting if necessary for one to become available.If this
BlockingObservable
produces items faster thanIterator.next
takes them,onNext
events might be skipped, butonError
oronCompleted
events are not.Note also that an
onNext
directly followed byonCompleted
might hide theonNext
event.- Returns:
- an Iterable that always returns the latest item emitted by this
BlockingObservable
- See Also:
-
single
If thisBlockingObservable
completes after emitting a single item, return that item, otherwise throw aNoSuchElementException
.- Returns:
- the single item emitted by this
BlockingObservable
- See Also:
-
single
If thisBlockingObservable
completes after emitting a single item that matches a given predicate, return that item, otherwise throw aNoSuchElementException
.- Parameters:
predicate
- a predicate function to evaluate items emitted by thisBlockingObservable
- Returns:
- the single item emitted by this
BlockingObservable
that matches the predicate - See Also:
-
singleOrDefault
If thisBlockingObservable
completes after emitting a single item, return that item; if it emits more than one item, throw anIllegalArgumentException
; if it emits no items, return a default value.- Parameters:
defaultValue
- a default value to return if thisBlockingObservable
emits no items- Returns:
- the single item emitted by this
BlockingObservable
, or the default value if it emits no items - See Also:
-
singleOrDefault
If thisBlockingObservable
completes after emitting a single item that matches a predicate, return that item; if it emits more than one such item, throw anIllegalArgumentException
; if it emits no items, return a default value.- Parameters:
defaultValue
- a default value to return if thisBlockingObservable
emits no matching itemspredicate
- a predicate function to evaluate items emitted by thisBlockingObservable
- Returns:
- the single item emitted by the
BlockingObservable
that matches the predicate, or the default value if no such items are emitted - See Also:
-
toFuture
Returns aFuture
representing the single value emitted by thisBlockingObservable
.If
BlockingObservable
emits more than one item,Future
will receive anIllegalArgumentException
. IfBlockingObservable
is empty,Future
will receive anNoSuchElementException
.If the
BlockingObservable
may emit more than one item, useObservable.toList().toBlocking().toFuture()
.- Returns:
- a
Future
that expects a single item to be emitted by thisBlockingObservable
- See Also:
-
toIterable
- Returns:
- an
Iterable
version of thisBlockingObservable
- See Also:
-
blockForSingle
Helper method which handles the actual blocking for a single response.If the
Observable
errors, it will be thrown right away.- Returns:
- the actual item
-
subscribe
Runs the source observable to a terminal event, ignoring any values and rethrowing any exception. -
subscribe
Subscribes to the source and calls back the Observer methods on the current thread.- Parameters:
observer
- the observer to call event methods on
-
subscribe
Subscribes to the source and calls the Subscriber methods on the current thread.The unsubscription and backpressure is composed through.
- Parameters:
subscriber
- the subscriber to forward events and calls to in the current thread
-
subscribe
Subscribes to the source and calls the given action on the current thread and rethrows any exception wrapped into OnErrorNotImplementedException.The difference between this method and
forEach(Action1)
is that the action is always executed on the current thread.- Parameters:
onNext
- the callback action for each source value- See Also:
-
subscribe
Subscribes to the source and calls the given actions on the current thread.- Parameters:
onNext
- the callback action for each source valueonError
- the callback action for an error event
-
subscribe
@Experimental public void subscribe(Action1<? super T> onNext, Action1<? super Throwable> onError, Action0 onCompleted) Subscribes to the source and calls the given actions on the current thread.- Parameters:
onNext
- the callback action for each source valueonError
- the callback action for an error eventonCompleted
- the callback action for the completion event.
-