Top Description Fields Constructors Methods
rx.observables

public final Class BlockingObservable<T>

extends Object
Class Inheritance
Type Parameters
<T>
the type of item emitted by the BlockingObservable
Imports
java.util.*, java.util.concurrent.*, java.util.concurrent.atomic.AtomicReference, rx.*, .Observable, .Observer, rx.exceptions.*, rx.functions.*, rx.internal.operators.*, rx.internal.util.*, rx.subscriptions.Subscriptions

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
RxJava wiki: Blocking Observable Operators

Field Summary

Modifier and TypeField and Description
private final Observable<? extends T>
o

pack-priv static final Object
ON_START

Constant to indicate the onStart method should be called.

pack-priv static final Object
SET_PRODUCER

Constant indicating the setProducer method should be called.

pack-priv static final Object
UNSUBSCRIBE

Indicates an unsubscription happened

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private T

Returns:

the actual item
blockForSingle
(final Observable<? extends T> observable)

Helper method which handles the actual blocking for a single response.

public T

Returns:

the first item emitted by this BlockingObservable
first
()

Returns the first item emitted by this BlockingObservable, or throws NoSuchElementException if it emits no items.

public T

Returns:

the first item emitted by this BlockingObservable that matches the predicate
first
(Func1<? super T, Boolean>
a predicate function to evaluate items emitted by this BlockingObservable
predicate
)

Returns the first item emitted by this BlockingObservable that matches a predicate, or throws NoSuchElementException if it emits no such item.

public T

Returns:

the first item emitted by this BlockingObservable, or the default value if it emits no items
firstOrDefault
(T
a default value to return if this BlockingObservable emits no items
defaultValue
)

Returns the first item emitted by this BlockingObservable, or a default value if it emits no items.

public T

Returns:

the first item emitted by this BlockingObservable that matches the predicate, or the default value if this BlockingObservable emits no matching items
firstOrDefault
(T
a default value to return if this BlockingObservable emits no matching items
defaultValue
,
Func1<? super T, Boolean>
a predicate function to evaluate items emitted by this BlockingObservable
predicate
)

Returns the first item emitted by this BlockingObservable that matches a predicate, or a default value if it emits no such items.

public void
forEach(final Action1<? super T>
the Action1 to invoke for each item emitted by the BlockingObservable
onNext
)

Invokes a method on each item emitted by this BlockingObservable and blocks until the Observable completes.

public static <
the observed value type
T
>
BlockingObservable<T>

Returns:

a BlockingObservable version of o
from
(final Observable<? extends T>
the Observable you want to convert
o
)

Converts an Observable into a BlockingObservable.

public Iterator<T>

Returns:

an Iterator that can iterate over the items emitted by this BlockingObservable
getIterator
()

Returns an Iterator that iterates over all items emitted by this BlockingObservable.

public T

Returns:

the last item emitted by this BlockingObservable
last
()

Returns the last item emitted by this BlockingObservable, or throws NoSuchElementException if this BlockingObservable emits no items.

public T

Returns:

the last item emitted by the BlockingObservable that matches the predicate
last
(final Func1<? super T, Boolean>
a predicate function to evaluate items emitted by the BlockingObservable
predicate
)

Returns the last item emitted by this BlockingObservable that matches a predicate, or throws NoSuchElementException if it emits no such items.

public T

Returns:

the last item emitted by the BlockingObservable, or the default value if it emits no items
lastOrDefault
(T
a default value to return if this BlockingObservable emits no items
defaultValue
)

Returns the last item emitted by this BlockingObservable, or a default value if it emits no items.

public T

Returns:

the last item emitted by this BlockingObservable that matches the predicate, or the default value if it emits no matching items
lastOrDefault
(T
a default value to return if this BlockingObservable emits no matching items
defaultValue
,
Func1<? super T, Boolean>
a predicate function to evaluate items emitted by this BlockingObservable
predicate
)

Returns the last item emitted by this BlockingObservable that matches a predicate, or a default value if it emits no such items.

public Iterable<T>

Returns:

an Iterable that always returns the latest item emitted by this BlockingObservable
latest
()

Returns an Iterable that returns the latest item emitted by this BlockingObservable, waiting if necessary for one to become available.

public Iterable<T>

Returns:

an Iterable that on each iteration returns the item that this BlockingObservable has most recently emitted
mostRecent
(T
the initial value that the Iterable sequence will yield if this BlockingObservable has not yet emitted an item
initialValue
)

Returns an Iterable that always returns the item most recently emitted by this BlockingObservable.

public Iterable<T>

Returns:

an Iterable that blocks upon each iteration until this BlockingObservable emits a new item, whereupon the Iterable returns that item
next
()

Returns an Iterable that blocks until this BlockingObservable emits another item, then returns that item.

public T

Returns:

the single item emitted by this BlockingObservable
single
()

If this BlockingObservable completes after emitting a single item, return that item, otherwise throw a NoSuchElementException.

public T

Returns:

the single item emitted by this BlockingObservable that matches the predicate
single
(Func1<? super T, Boolean>
a predicate function to evaluate items emitted by this BlockingObservable
predicate
)

If this BlockingObservable completes after emitting a single item that matches a given predicate, return that item, otherwise throw a NoSuchElementException.

public T

Returns:

the single item emitted by this BlockingObservable, or the default value if it emits no items
singleOrDefault
(T
a default value to return if this BlockingObservable emits no items
defaultValue
)

If this BlockingObservable completes after emitting a single item, return that item; if it emits more than one item, throw an IllegalArgumentException; if it emits no items, return a default value.

public T

Returns:

the single item emitted by the BlockingObservable that matches the predicate, or the default value if no such items are emitted
singleOrDefault
(T
a default value to return if this BlockingObservable emits no matching items
defaultValue
,
Func1<? super T, Boolean>
a predicate function to evaluate items emitted by this BlockingObservable
predicate
)

If this BlockingObservable completes after emitting a single item that matches a predicate, return that item; if it emits more than one such item, throw an IllegalArgumentException; if it emits no items, return a default value.

public void
subscribe()

Runs the source observable to a terminal event, ignoring any values and rethrowing any exception.

public void
subscribe(Observer<? super T>
the observer to call event methods on
observer
)

Subscribes to the source and calls back the Observer methods on the current thread.

public void
subscribe(Subscriber<? super T>
the subscriber to forward events and calls to in the current thread
subscriber
)

Subscribes to the source and calls the Subscriber methods on the current thread.

public void
subscribe(final Action1<? super T>
the callback action for each source value
onNext
)

Subscribes to the source and calls the given action on the current thread and rethrows any exception wrapped into OnErrorNotImplementedException.

public void
subscribe(final Action1<? super T>
the callback action for each source value
onNext
,
final Action1<? super Throwable>
the callback action for an error event
onError
)

Subscribes to the source and calls the given actions on the current thread.

public void
subscribe(final Action1<? super T>
the callback action for each source value
onNext
,
final Action1<? super Throwable>
the callback action for an error event
onError
,
final Action0
the callback action for the completion event.
onCompleted
)

Subscribes to the source and calls the given actions on the current thread.

public Future<T>

Returns:

a Future that expects a single item to be emitted by this BlockingObservable
toFuture
()

Returns a Future representing the single value emitted by this BlockingObservable.

public Iterable<T>

Returns:

an Iterable version of this BlockingObservable
toIterable
()

Converts this BlockingObservable into an Iterable.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

oback to summary
private final Observable<? extends T> o
ON_STARTback to summary
pack-priv static final Object ON_START

Constant to indicate the onStart method should be called.

SET_PRODUCERback to summary
pack-priv static final Object SET_PRODUCER

Constant indicating the setProducer method should be called.

UNSUBSCRIBEback to summary
pack-priv static final Object UNSUBSCRIBE

Indicates an unsubscription happened

Constructor Detail

BlockingObservableback to summary
private BlockingObservable(Observable<? extends T> o)

Method Detail

blockForSingleback to summary
private T blockForSingle(final Observable<? extends T> observable)

Helper method which handles the actual blocking for a single response.

If the Observable errors, it will be thrown right away.

Returns:T

the actual item

firstback to summary
public T first()

Returns the first item emitted by this BlockingObservable, or throws NoSuchElementException if it emits no items.

Returns:T

the first item emitted by this BlockingObservable

Exceptions
NoSuchElementException:
if this BlockingObservable emits no items
See Also
ReactiveX documentation: First
firstback to summary
public T first(Func1<? super T, Boolean> predicate)

Returns the first item emitted by this BlockingObservable that matches a predicate, or throws NoSuchElementException if it emits no such item.

Parameters
predicate:Func1<? super T, Boolean>

a predicate function to evaluate items emitted by this BlockingObservable

Returns:T

the first item emitted by this BlockingObservable that matches the predicate

Exceptions
NoSuchElementException:
if this BlockingObservable emits no such items
See Also
ReactiveX documentation: First
firstOrDefaultback to summary
public T firstOrDefault(T defaultValue)

Returns the first item emitted by this BlockingObservable, or a default value if it emits no items.

Parameters
defaultValue:T

a default value to return if this BlockingObservable emits no items

Returns:T

the first item emitted by this BlockingObservable, or the default value if it emits no items

See Also
ReactiveX documentation: First
firstOrDefaultback to summary
public T firstOrDefault(T defaultValue, Func1<? super T, Boolean> predicate)

Returns the first item emitted by this BlockingObservable that matches a predicate, or a default value if it emits no such items.

Parameters
defaultValue:T

a default value to return if this BlockingObservable emits no matching items

predicate:Func1<? super T, Boolean>

a predicate function to evaluate items emitted by this BlockingObservable

Returns:T

the first item emitted by this BlockingObservable that matches the predicate, or the default value if this BlockingObservable emits no matching items

See Also
ReactiveX documentation: First
forEachback to summary
public void forEach(final Action1<? super T> onNext)

Invokes a method on each item emitted by this BlockingObservable 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 the Subscriber#onCompleted() or Subscriber#onError(Throwable) methods. If the underlying Observable terminates with an error, rather than calling onError, this method will throw an exception.

The difference between this method and subscribe(Action1) is that the onNext action is executed on the emission thread instead of the current thread.

Parameters
onNext:Action1<? super T>

the Action1 to invoke for each item emitted by the BlockingObservable

Exceptions
RuntimeException:
if an error occurs
See Also
ReactiveX documentation: Subscribe, subscribe(Action1)
fromback to summary
public static <T> BlockingObservable<T> from(final Observable<? extends T> o)

Converts an Observable into a BlockingObservable.

Parameters
<T>
the observed value type
o:Observable<? extends T>

the Observable you want to convert

Returns:BlockingObservable<T>

a BlockingObservable version of o

getIteratorback to summary
public Iterator<T> getIterator()

Returns an Iterator that iterates over all items emitted by this BlockingObservable.

Returns:Iterator<T>

an Iterator that can iterate over the items emitted by this BlockingObservable

Annotations
@SuppressWarnings:unchecked, cast
See Also
ReactiveX documentation: To
lastback to summary
public T last()

Returns the last item emitted by this BlockingObservable, or throws NoSuchElementException if this BlockingObservable emits no items.

Returns:T

the last item emitted by this BlockingObservable

Exceptions
NoSuchElementException:
if this BlockingObservable emits no items
See Also
ReactiveX documentation: Last
lastback to summary
public T last(final Func1<? super T, Boolean> predicate)

Returns the last item emitted by this BlockingObservable that matches a predicate, or throws NoSuchElementException if it emits no such items.

Parameters
predicate:Func1<? super T, Boolean>

a predicate function to evaluate items emitted by the BlockingObservable

Returns:T

the last item emitted by the BlockingObservable that matches the predicate

Exceptions
NoSuchElementException:
if this BlockingObservable emits no items
See Also
ReactiveX documentation: Last
lastOrDefaultback to summary
public T lastOrDefault(T defaultValue)

Returns the last item emitted by this BlockingObservable, or a default value if it emits no items.

Parameters
defaultValue:T

a default value to return if this BlockingObservable emits no items

Returns:T

the last item emitted by the BlockingObservable, or the default value if it emits no items

See Also
ReactiveX documentation: Last
lastOrDefaultback to summary
public T lastOrDefault(T defaultValue, Func1<? super T, Boolean> predicate)

Returns the last item emitted by this BlockingObservable that matches a predicate, or a default value if it emits no such items.

Parameters
defaultValue:T

a default value to return if this BlockingObservable emits no matching items

predicate:Func1<? super T, Boolean>

a predicate function to evaluate items emitted by this BlockingObservable

Returns:T

the last item emitted by this BlockingObservable that matches the predicate, or the default value if it emits no matching items

See Also
ReactiveX documentation: Last
latestback to summary
public Iterable<T> latest()

Returns an Iterable that returns the latest item emitted by this BlockingObservable, waiting if necessary for one to become available.

If this BlockingObservable produces items faster than Iterator.next takes them, onNext events might be skipped, but onError or onCompleted events are not.

Note also that an onNext directly followed by onCompleted might hide the onNext event.

Returns:Iterable<T>

an Iterable that always returns the latest item emitted by this BlockingObservable

Annotations
@SuppressWarnings:cast, unchecked
See Also
ReactiveX documentation: First
mostRecentback to summary
public Iterable<T> mostRecent(T initialValue)

Returns an Iterable that always returns the item most recently emitted by this BlockingObservable.

Parameters
initialValue:T

the initial value that the Iterable sequence will yield if this BlockingObservable has not yet emitted an item

Returns:Iterable<T>

an Iterable that on each iteration returns the item that this BlockingObservable has most recently emitted

See Also
ReactiveX documentation: First
nextback to summary
public Iterable<T> next()

Returns an Iterable that blocks until this BlockingObservable emits another item, then returns that item.

Returns:Iterable<T>

an Iterable that blocks upon each iteration until this BlockingObservable emits a new item, whereupon the Iterable returns that item

Annotations
@SuppressWarnings:cast, unchecked
See Also
ReactiveX documentation: TakeLast
singleback to summary
public T single()

If this BlockingObservable completes after emitting a single item, return that item, otherwise throw a NoSuchElementException.

Returns:T

the single item emitted by this BlockingObservable

See Also
ReactiveX documentation: First
singleback to summary
public T single(Func1<? super T, Boolean> predicate)

If this BlockingObservable completes after emitting a single item that matches a given predicate, return that item, otherwise throw a NoSuchElementException.

Parameters
predicate:Func1<? super T, Boolean>

a predicate function to evaluate items emitted by this BlockingObservable

Returns:T

the single item emitted by this BlockingObservable that matches the predicate

See Also
ReactiveX documentation: First
singleOrDefaultback to summary
public T singleOrDefault(T defaultValue)

If this BlockingObservable completes after emitting a single item, return that item; if it emits more than one item, throw an IllegalArgumentException; if it emits no items, return a default value.

Parameters
defaultValue:T

a default value to return if this BlockingObservable emits no items

Returns:T

the single item emitted by this BlockingObservable, or the default value if it emits no items

See Also
ReactiveX documentation: First
singleOrDefaultback to summary
public T singleOrDefault(T defaultValue, Func1<? super T, Boolean> predicate)

If this BlockingObservable completes after emitting a single item that matches a predicate, return that item; if it emits more than one such item, throw an IllegalArgumentException; if it emits no items, return a default value.

Parameters
defaultValue:T

a default value to return if this BlockingObservable emits no matching items

predicate:Func1<? super T, Boolean>

a predicate function to evaluate items emitted by this BlockingObservable

Returns:T

the single item emitted by the BlockingObservable that matches the predicate, or the default value if no such items are emitted

See Also
ReactiveX documentation: First
subscribeback to summary
public void subscribe()

Runs the source observable to a terminal event, ignoring any values and rethrowing any exception.

Since
1.3
subscribeback to summary
public void subscribe(Observer<? super T> observer)

Subscribes to the source and calls back the Observer methods on the current thread.

Parameters
observer:Observer<? super T>

the observer to call event methods on

Since
1.3
subscribeback to summary
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:Subscriber<? super T>

the subscriber to forward events and calls to in the current thread

Annotations
@SuppressWarnings:unchecked
Since
1.3
subscribeback to summary
public void subscribe(final 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:Action1<? super T>

the callback action for each source value

Since
1.3
See Also
forEach(Action1)
subscribeback to summary
public void subscribe(final Action1<? super T> onNext, final Action1<? super Throwable> onError)

Subscribes to the source and calls the given actions on the current thread.

Parameters
onNext:Action1<? super T>

the callback action for each source value

onError:Action1<? super Throwable>

the callback action for an error event

Since
1.3
subscribeback to summary
public void subscribe(final Action1<? super T> onNext, final Action1<? super Throwable> onError, final Action0 onCompleted)

Subscribes to the source and calls the given actions on the current thread.

Parameters
onNext:Action1<? super T>

the callback action for each source value

onError:Action1<? super Throwable>

the callback action for an error event

onCompleted:Action0

the callback action for the completion event.

Since
1.3
toFutureback to summary
public Future<T> toFuture()

Returns a Future representing the single value emitted by this BlockingObservable.

If BlockingObservable emits more than one item, java.util.concurrent.Future will receive an java.lang.IllegalArgumentException. If BlockingObservable is empty, java.util.concurrent.Future will receive an java.util.NoSuchElementException.

If the BlockingObservable may emit more than one item, use Observable.toList().toBlocking().toFuture().

Returns:Future<T>

a Future that expects a single item to be emitted by this BlockingObservable

Annotations
@SuppressWarnings:cast, unchecked
See Also
ReactiveX documentation: To
toIterableback to summary
public Iterable<T> toIterable()

Converts this BlockingObservable into an Iterable.

Returns:Iterable<T>

an Iterable version of this BlockingObservable

See Also
ReactiveX documentation: To