Class BlockingObservable<T>
- java.lang.Object
-
- rx.observables.BlockingObservable<T>
-
- Type Parameters:
T- the type of item emitted by theBlockingObservable
public final class BlockingObservable<T> extends java.lang.ObjectBlockingObservableis a variety ofObservablethat 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 aBlockingObservablethis is usually a sign that you should rethink your design).You construct a
BlockingObservablefrom anObservablewithfrom(Observable)orObservable.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:

-
-
Field Summary
Fields Modifier and Type Field Description private Observable<? extends T>o(package private) static java.lang.ObjectON_STARTConstant to indicate the onStart method should be called.(package private) static java.lang.ObjectSET_PRODUCERConstant indicating the setProducer method should be called.(package private) static java.lang.ObjectUNSUBSCRIBEIndicates an unsubscription happened
-
Constructor Summary
Constructors Modifier Constructor Description privateBlockingObservable(Observable<? extends T> o)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private TblockForSingle(Observable<? extends T> observable)Helper method which handles the actual blocking for a single response.Tfirst()Returns the first item emitted by thisBlockingObservable, or throwsNoSuchElementExceptionif it emits no items.Tfirst(Func1<? super T,java.lang.Boolean> predicate)Returns the first item emitted by thisBlockingObservablethat matches a predicate, or throwsNoSuchElementExceptionif it emits no such item.TfirstOrDefault(T defaultValue)Returns the first item emitted by thisBlockingObservable, or a default value if it emits no items.TfirstOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)Returns the first item emitted by thisBlockingObservablethat matches a predicate, or a default value if it emits no such items.voidforEach(Action1<? super T> onNext)Invokes a method on each item emitted by thisBlockingObservableand blocks until the Observable completes.static <T> BlockingObservable<T>from(Observable<? extends T> o)Converts anObservableinto aBlockingObservable.java.util.Iterator<T>getIterator()Returns anIteratorthat iterates over all items emitted by thisBlockingObservable.Tlast()Returns the last item emitted by thisBlockingObservable, or throwsNoSuchElementExceptionif thisBlockingObservableemits no items.Tlast(Func1<? super T,java.lang.Boolean> predicate)Returns the last item emitted by thisBlockingObservablethat matches a predicate, or throwsNoSuchElementExceptionif it emits no such items.TlastOrDefault(T defaultValue)Returns the last item emitted by thisBlockingObservable, or a default value if it emits no items.TlastOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)Returns the last item emitted by thisBlockingObservablethat matches a predicate, or a default value if it emits no such items.java.lang.Iterable<T>latest()Returns anIterablethat returns the latest item emitted by thisBlockingObservable, waiting if necessary for one to become available.java.lang.Iterable<T>mostRecent(T initialValue)Returns anIterablethat always returns the item most recently emitted by thisBlockingObservable.java.lang.Iterable<T>next()Returns anIterablethat blocks until thisBlockingObservableemits another item, then returns that item.Tsingle()If thisBlockingObservablecompletes after emitting a single item, return that item, otherwise throw aNoSuchElementException.Tsingle(Func1<? super T,java.lang.Boolean> predicate)If thisBlockingObservablecompletes after emitting a single item that matches a given predicate, return that item, otherwise throw aNoSuchElementException.TsingleOrDefault(T defaultValue)If thisBlockingObservablecompletes 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.TsingleOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)If thisBlockingObservablecompletes 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.voidsubscribe()Runs the source observable to a terminal event, ignoring any values and rethrowing any exception.voidsubscribe(Action1<? super T> onNext)Subscribes to the source and calls the given action on the current thread and rethrows any exception wrapped into OnErrorNotImplementedException.voidsubscribe(Action1<? super T> onNext, Action1<? super java.lang.Throwable> onError)Subscribes to the source and calls the given actions on the current thread.voidsubscribe(Action1<? super T> onNext, Action1<? super java.lang.Throwable> onError, Action0 onCompleted)Subscribes to the source and calls the given actions on the current thread.voidsubscribe(Observer<? super T> observer)Subscribes to the source and calls back the Observer methods on the current thread.voidsubscribe(Subscriber<? super T> subscriber)Subscribes to the source and calls the Subscriber methods on the current thread.java.util.concurrent.Future<T>toFuture()Returns aFuturerepresenting the single value emitted by thisBlockingObservable.java.lang.Iterable<T>toIterable()Converts thisBlockingObservableinto anIterable.
-
-
-
Field Detail
-
o
private final Observable<? extends T> o
-
ON_START
static final java.lang.Object ON_START
Constant to indicate the onStart method should be called.
-
SET_PRODUCER
static final java.lang.Object SET_PRODUCER
Constant indicating the setProducer method should be called.
-
UNSUBSCRIBE
static final java.lang.Object UNSUBSCRIBE
Indicates an unsubscription happened
-
-
Constructor Detail
-
BlockingObservable
private BlockingObservable(Observable<? extends T> o)
-
-
Method Detail
-
from
public static <T> BlockingObservable<T> from(Observable<? extends T> o)
Converts anObservableinto aBlockingObservable.- Type Parameters:
T- the observed value type- Parameters:
o- theObservableyou want to convert- Returns:
- a
BlockingObservableversion ofo
-
forEach
public void forEach(Action1<? super T> onNext)
Invokes a method on each item emitted by thisBlockingObservableand 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 theonNextaction is executed on the emission thread instead of the current thread.- Parameters:
onNext- theAction1to invoke for each item emitted by theBlockingObservable- Throws:
java.lang.RuntimeException- if an error occurs- See Also:
- ReactiveX documentation: Subscribe,
subscribe(Action1)
-
getIterator
public java.util.Iterator<T> getIterator()
Returns anIteratorthat iterates over all items emitted by thisBlockingObservable.
- Returns:
- an
Iteratorthat can iterate over the items emitted by thisBlockingObservable - See Also:
- ReactiveX documentation: To
-
first
public T first()
Returns the first item emitted by thisBlockingObservable, or throwsNoSuchElementExceptionif it emits no items.- Returns:
- the first item emitted by this
BlockingObservable - Throws:
java.util.NoSuchElementException- if thisBlockingObservableemits no items- See Also:
- ReactiveX documentation: First
-
first
public T first(Func1<? super T,java.lang.Boolean> predicate)
Returns the first item emitted by thisBlockingObservablethat matches a predicate, or throwsNoSuchElementExceptionif it emits no such item.- Parameters:
predicate- a predicate function to evaluate items emitted by thisBlockingObservable- Returns:
- the first item emitted by this
BlockingObservablethat matches the predicate - Throws:
java.util.NoSuchElementException- if thisBlockingObservableemits no such items- See Also:
- ReactiveX documentation: First
-
firstOrDefault
public T firstOrDefault(T defaultValue)
Returns the first item emitted by thisBlockingObservable, or a default value if it emits no items.- Parameters:
defaultValue- a default value to return if thisBlockingObservableemits no items- Returns:
- the first item emitted by this
BlockingObservable, or the default value if it emits no items - See Also:
- ReactiveX documentation: First
-
firstOrDefault
public T firstOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)
Returns the first item emitted by thisBlockingObservablethat matches a predicate, or a default value if it emits no such items.- Parameters:
defaultValue- a default value to return if thisBlockingObservableemits no matching itemspredicate- a predicate function to evaluate items emitted by thisBlockingObservable- Returns:
- the first item emitted by this
BlockingObservablethat matches the predicate, or the default value if thisBlockingObservableemits no matching items - See Also:
- ReactiveX documentation: First
-
last
public T last()
Returns the last item emitted by thisBlockingObservable, or throwsNoSuchElementExceptionif thisBlockingObservableemits no items.
- Returns:
- the last item emitted by this
BlockingObservable - Throws:
java.util.NoSuchElementException- if thisBlockingObservableemits no items- See Also:
- ReactiveX documentation: Last
-
last
public T last(Func1<? super T,java.lang.Boolean> predicate)
Returns the last item emitted by thisBlockingObservablethat matches a predicate, or throwsNoSuchElementExceptionif it emits no such items.
- Parameters:
predicate- a predicate function to evaluate items emitted by theBlockingObservable- Returns:
- the last item emitted by the
BlockingObservablethat matches the predicate - Throws:
java.util.NoSuchElementException- if thisBlockingObservableemits no items- See Also:
- ReactiveX documentation: Last
-
lastOrDefault
public T lastOrDefault(T defaultValue)
Returns the last item emitted by thisBlockingObservable, or a default value if it emits no items.
- Parameters:
defaultValue- a default value to return if thisBlockingObservableemits no items- Returns:
- the last item emitted by the
BlockingObservable, or the default value if it emits no items - See Also:
- ReactiveX documentation: Last
-
lastOrDefault
public T lastOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)
Returns the last item emitted by thisBlockingObservablethat matches a predicate, or a default value if it emits no such items.
- Parameters:
defaultValue- a default value to return if thisBlockingObservableemits no matching itemspredicate- a predicate function to evaluate items emitted by thisBlockingObservable- Returns:
- the last item emitted by this
BlockingObservablethat matches the predicate, or the default value if it emits no matching items - See Also:
- ReactiveX documentation: Last
-
mostRecent
public java.lang.Iterable<T> mostRecent(T initialValue)
Returns anIterablethat always returns the item most recently emitted by thisBlockingObservable.
- Parameters:
initialValue- the initial value that theIterablesequence will yield if thisBlockingObservablehas not yet emitted an item- Returns:
- an
Iterablethat on each iteration returns the item that thisBlockingObservablehas most recently emitted - See Also:
- ReactiveX documentation: First
-
next
public java.lang.Iterable<T> next()
Returns anIterablethat blocks until thisBlockingObservableemits another item, then returns that item.
- Returns:
- an
Iterablethat blocks upon each iteration until thisBlockingObservableemits a new item, whereupon the Iterable returns that item - See Also:
- ReactiveX documentation: TakeLast
-
latest
public java.lang.Iterable<T> latest()
Returns anIterablethat returns the latest item emitted by thisBlockingObservable, waiting if necessary for one to become available.If this
BlockingObservableproduces items faster thanIterator.nexttakes them,onNextevents might be skipped, butonErrororonCompletedevents are not.Note also that an
onNextdirectly followed byonCompletedmight hide theonNextevent.- Returns:
- an Iterable that always returns the latest item emitted by this
BlockingObservable - See Also:
- ReactiveX documentation: First
-
single
public T single()
If thisBlockingObservablecompletes after emitting a single item, return that item, otherwise throw aNoSuchElementException.
- Returns:
- the single item emitted by this
BlockingObservable - See Also:
- ReactiveX documentation: First
-
single
public T single(Func1<? super T,java.lang.Boolean> predicate)
If thisBlockingObservablecompletes 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
BlockingObservablethat matches the predicate - See Also:
- ReactiveX documentation: First
-
singleOrDefault
public T singleOrDefault(T defaultValue)
If thisBlockingObservablecompletes 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 thisBlockingObservableemits no items- Returns:
- the single item emitted by this
BlockingObservable, or the default value if it emits no items - See Also:
- ReactiveX documentation: First
-
singleOrDefault
public T singleOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)
If thisBlockingObservablecompletes 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 thisBlockingObservableemits no matching itemspredicate- a predicate function to evaluate items emitted by thisBlockingObservable- Returns:
- the single item emitted by the
BlockingObservablethat matches the predicate, or the default value if no such items are emitted - See Also:
- ReactiveX documentation: First
-
toFuture
public java.util.concurrent.Future<T> toFuture()
Returns aFuturerepresenting the single value emitted by thisBlockingObservable.If
BlockingObservableemits more than one item,Futurewill receive anIllegalArgumentException. IfBlockingObservableis empty,Futurewill receive anNoSuchElementException.If the
BlockingObservablemay emit more than one item, useObservable.toList().toBlocking().toFuture().
- Returns:
- a
Futurethat expects a single item to be emitted by thisBlockingObservable - See Also:
- ReactiveX documentation: To
-
toIterable
public java.lang.Iterable<T> toIterable()
Converts thisBlockingObservableinto anIterable.
- Returns:
- an
Iterableversion of thisBlockingObservable - See Also:
- ReactiveX documentation: To
-
blockForSingle
private T blockForSingle(Observable<? extends T> observable)
Helper method which handles the actual blocking for a single response.If the
Observableerrors, it will be thrown right away.- Returns:
- the actual item
-
subscribe
@Experimental public void subscribe()
Runs the source observable to a terminal event, ignoring any values and rethrowing any exception.
-
subscribe
@Experimental public void subscribe(Observer<? super T> observer)
Subscribes to the source and calls back the Observer methods on the current thread.- Parameters:
observer- the observer to call event methods on
-
subscribe
@Experimental public void subscribe(Subscriber<? super T> subscriber)
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
@Experimental public void subscribe(Action1<? super T> onNext)
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:
forEach(Action1)
-
subscribe
@Experimental public void subscribe(Action1<? super T> onNext, Action1<? super java.lang.Throwable> onError)
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 java.lang.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.
-
-