T - the element type to observepublic abstract class DefaultObserver<T> extends java.lang.Object implements Observer<T>, java.io.Closeable
This implementation ensures that its onNext, onError and onFinish methods are never interleaved and won't be executed after an error or finish message. In addition, a Throwable thrown from the onNext() method is routed through the error() method.
The close method is idempotent and can be called multiple times.
Extend this class when you need something more than a decorator or filter around some Observer passed to the custom register method, e.g., observers with multiple sub-observers registered to multiple sources or observers with an accompanied scheduled task.
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
closeOnTermination
Should the observer close() itself on error or finish()?
|
protected boolean |
completed
The completion flag, it will be set by the close method once.
|
protected java.util.concurrent.locks.Lock |
lock
The lock that ensures sequential and exclusive runs for the observer's methods.
|
| Constructor and Description |
|---|
DefaultObserver(boolean complete)
Constructor.
|
DefaultObserver(java.util.concurrent.locks.Lock lock,
boolean complete)
Constructor.
|
| Modifier and Type | Method and Description |
|---|---|
void |
close() |
void |
error(java.lang.Throwable ex)
An exception is received.
|
void |
finish()
No more values to expect.
|
java.util.concurrent.locks.Lock |
getLock()
Returns the internal lock which might be
shared among multiple instances.
|
boolean |
isCompleted() |
void |
next(T value)
The next value is received.
|
protected void |
onClose()
Called by close to close down any associated resources with this instance.
|
protected abstract void |
onError(java.lang.Throwable ex)
The alternative error() method, which is called by the original error() method.
|
protected abstract void |
onFinish()
The alternative finish() method, which is called by the original finish() method.
|
protected abstract void |
onNext(T value)
The alternative next() method, which is called by the original next() method
under lock.
|
@Nonnull protected final java.util.concurrent.locks.Lock lock
protected boolean completed
protected final boolean closeOnTermination
public DefaultObserver(boolean complete)
complete - should set the completion status on an error or finish?public DefaultObserver(@Nonnull
java.util.concurrent.locks.Lock lock,
boolean complete)
lock - the lock instance, nonnulcomplete - should set the completion status on an error or finish?protected abstract void onNext(T value)
value - the valueprotected abstract void onError(@Nonnull
java.lang.Throwable ex)
ex - the exceptionprotected abstract void onFinish()
protected void onClose()
The close() method ensures that the lock is held this code executes.
public final void next(T value)
Observerpublic final void close()
close in interface java.io.Closeableclose in interface java.lang.AutoCloseablepublic final void error(@Nonnull
java.lang.Throwable ex)
BaseObservererror in interface BaseObserverex - the exceptionpublic final void finish()
BaseObserverfinish in interface BaseObserverpublic java.util.concurrent.locks.Lock getLock()
public boolean isCompleted()