Skip to content

Instantly share code, notes, and snippets.

@undownding
Created June 8, 2016 03:41
Show Gist options
  • Save undownding/ed392b4a6435ceee630f3ed76770382e to your computer and use it in GitHub Desktop.
Save undownding/ed392b4a6435ceee630f3ed76770382e to your computer and use it in GitHub Desktop.
package com.common.rx;
import rx.Subscriber;
/**
* Created by undownding on 16-6-8.
*/
public class SubscriberBuilder<T> {
private OnNextListener next;
private OnErrorListener error;
private Runnable completed;
public SubscriberBuilder<T> onNext(OnNextListener l) {
this.next = l;
return this;
}
public SubscriberBuilder<T> onError(OnErrorListener l) {
this.error = l;
return this;
}
public SubscriberBuilder<T> onCompleted(Runnable r) {
this.completed = r;
return this;
}
public Subscriber<T> build() {
return new Subscriber<T>() {
@Override
public void onCompleted() {
if (completed != null) {
completed.run();
}
}
@Override
public void onError(Throwable e) {
if (error != null) {
error.onError(e);
}
}
@Override
public void onNext(T data) {
if (next != null) {
next.onNext(data);
}
}
};
}
public interface OnNextListener<T> {
void onNext(T data);
}
public interface OnErrorListener {
void onError(Throwable e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment