Skip to content

Instantly share code, notes, and snippets.

@mandybess
Last active November 12, 2015 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mandybess/f69b1ce96ae9d4323a49 to your computer and use it in GitHub Desktop.
Save mandybess/f69b1ce96ae9d4323a49 to your computer and use it in GitHub Desktop.
import rx.Observable.Operator;
import rx.Subscriber;
import rx.functions.Action0;
public final class DoOnEmptyOperator<T> implements Operator<T, T> {
private final Action0 empty;
public DoOnEmptyOperator(Action0 empty) {
this.empty = empty;
}
@Override
public Subscriber<? super T> call(final Subscriber<? super T> child) {
return new Subscriber<T>(child) {
boolean hasValue;
@Override
public void onNext(T t) {
hasValue = true;
child.onNext(t);
}
@Override
public void onError(Throwable e) {
child.onError(e);
}
@Override
public void onCompleted() {
if (!hasValue) {
try {
empty.call();
} catch (Throwable e) {
child.onError(e);
return;
}
}
child.onCompleted();
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment