RxJava



rx.observables
Class BlockingObservable<T>

java.lang.Object
  extended by rx.observables.BlockingObservable<T>
Type Parameters:
T -

public class BlockingObservable<T>
extends java.lang.Object

An extension of Observable that provides blocking operators.

You construct a BlockingObservable from an Observable with BlockingObservable.from(Observable) or Observable.toBlockingObservable()

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:

For more information see the Blocking Observable Operators page at the RxJava Wiki.


Method Summary
 void forEach(Action1<? super T> onNext)
          Invoke a method on each item emitted by the Observable; block until the Observable completes.
static
<T> BlockingObservable<T>
from(Observable<? extends T> o)
          Convert an Observable into a BlockingObservable.
 java.util.Iterator<T> getIterator()
          Returns an Iterator that iterates over all items emitted by a specified Observable.
 T last()
          Returns the last item emitted by a specified Observable.
 T last(Func1<? super T,java.lang.Boolean> predicate)
          Returns the last item emitted by a specified Observable that matches a predicate.
 T lastOrDefault(T defaultValue)
          Returns the last item emitted by a specified Observable, or a default value if no items are emitted.
 T lastOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)
          Returns the last item emitted by a specified Observable that matches a predicate, or a default value if no such items are emitted.
 java.lang.Iterable<T> mostRecent(T initialValue)
          Returns an Iterable that always returns the item most recently emitted by an Observable.
 java.lang.Iterable<T> next()
          Returns an Iterable that blocks until the Observable emits another item, then returns that item.
 T single()
          If the Observable completes after emitting a single item, return that item, otherwise throw an exception.
 T single(Func1<? super T,java.lang.Boolean> predicate)
          If the Observable completes after emitting a single item that matches a given predicate, return that item, otherwise throw an exception.
 T singleOrDefault(T defaultValue)
          If the Observable completes after emitting a single item, return that item; if it emits more than one item, throw an exception; if it emits no items, return a default value.
 T singleOrDefault(T defaultValue, Func1<? super T,java.lang.Boolean> predicate)
          If the Observable completes after emitting a single item that matches a predicate, return that item; if it emits more than one such item, throw an exception; if it emits no items, return a default value.
 java.util.concurrent.Future<T> toFuture()
          Returns a Future representing the single value emitted by an Observable.
 java.lang.Iterable<T> toIterable()
          Converts an Observable into an Iterable.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

from

public static <T> BlockingObservable<T> from(Observable<? extends T> o)
Convert an Observable into a BlockingObservable.


forEach

public void forEach(Action1<? super T> onNext)
Invoke a method on each item emitted by the Observable; block until the Observable completes.

NOTE: This will block even if the Observable is asynchronous.

This is similar to Observable.subscribe(Observer), but it blocks. Because it blocks it does not need the Observer.onCompleted() or Observer.onError(Throwable) methods.

Parameters:
onNext - the Action1 to invoke for every item emitted by the Observable
Throws:
java.lang.RuntimeException - if an error occurs

getIterator

public java.util.Iterator<T> getIterator()
Returns an Iterator that iterates over all items emitted by a specified Observable.

Returns:
an Iterator that can iterate over the items emitted by the Observable

last

public T last()
Returns the last item emitted by a specified Observable.

Returns:
the last item emitted by the source Observable
Throws:
java.lang.IllegalArgumentException - if source contains no elements

last

public T last(Func1<? super T,java.lang.Boolean> predicate)
Returns the last item emitted by a specified Observable that matches a predicate.

Parameters:
predicate - a predicate function to evaluate items emitted by the Observable
Returns:
the last item emitted by the Observable that matches the predicate

lastOrDefault

public T lastOrDefault(T defaultValue)
Returns the last item emitted by a specified Observable, or a default value if no items are emitted.

Parameters:
defaultValue - a default value to return if the Observable emits no items
Returns:
the last item emitted by the Observable, or the default value if no items are emitted

lastOrDefault

public T lastOrDefault(T defaultValue,
                       Func1<? super T,java.lang.Boolean> predicate)
Returns the last item emitted by a specified Observable that matches a predicate, or a default value if no such items are emitted.

Parameters:
defaultValue - a default value to return if the Observable emits no matching items
predicate - a predicate function to evaluate items emitted by the Observable
Returns:
the last item emitted by the Observable that matches the predicate, or the default value if no matching items are emitted

mostRecent

public java.lang.Iterable<T> mostRecent(T initialValue)
Returns an Iterable that always returns the item most recently emitted by an Observable.

Parameters:
initialValue - the initial value that will be yielded by the Iterable sequence if the Observable has not yet emitted an item
Returns:
an Iterable that on each iteration returns the item that the Observable has most recently emitted

next

public java.lang.Iterable<T> next()
Returns an Iterable that blocks until the Observable emits another item, then returns that item.

Returns:
an Iterable that blocks upon each iteration until the Observable emits a new item, whereupon the Iterable returns that item

single

public T single()
If the Observable completes after emitting a single item, return that item, otherwise throw an exception.

Returns:
the single item emitted by the Observable

single

public T single(Func1<? super T,java.lang.Boolean> predicate)
If the Observable completes after emitting a single item that matches a given predicate, return that item, otherwise throw an exception.

Parameters:
predicate - a predicate function to evaluate items emitted by the Observable
Returns:
the single item emitted by the source Observable that matches the predicate

singleOrDefault

public T singleOrDefault(T defaultValue)
If the Observable completes after emitting a single item, return that item; if it emits more than one item, throw an exception; if it emits no items, return a default value.

Parameters:
defaultValue - a default value to return if the Observable emits no items
Returns:
the single item emitted by the Observable, or the default value if no items are emitted

singleOrDefault

public T singleOrDefault(T defaultValue,
                         Func1<? super T,java.lang.Boolean> predicate)
If the Observable completes after emitting a single item that matches a predicate, return that item; if it emits more than one such item, throw an exception; if it emits no items, return a default value.

Parameters:
defaultValue - a default value to return if the Observable emits no matching items
predicate - a predicate function to evaluate items emitted by the Observable
Returns:
the single item emitted by the Observable that matches the predicate, or the default value if no such items are emitted

toFuture

public java.util.concurrent.Future<T> toFuture()
Returns a Future representing the single value emitted by an Observable.

toFuture() throws an exception if the Observable emits more than one item. If the Observable may emit more than item, use toList().toFuture().

Returns:
a Future that expects a single item to be emitted by the source Observable

toIterable

public java.lang.Iterable<T> toIterable()
Converts an Observable into an Iterable.

Returns:
an Iterable version of the underlying Observable