Skip to content

Instantly share code, notes, and snippets.

@plzen
Created November 25, 2016 18:43
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 plzen/b43f7c65b8f53e0301ed39743b90733d to your computer and use it in GitHub Desktop.
Save plzen/b43f7c65b8f53e0301ed39743b90733d to your computer and use it in GitHub Desktop.
Safe subList operator for RxJava
public class subList<T> implements Observable.Operator<List<T>, List<T>> {
private int start;
private int length;
public subList(int start, int length) {
this.start = start;
this.length = length;
}
public subList(int length) {
this(0, length);
}
@Override
public Subscriber<? super List<T>> call(Subscriber<? super List<T>> subscriber) {
return new Subscriber<List<T>>() {
@Override
public void onCompleted() {
if (!subscriber.isUnsubscribed()) {
subscriber.onCompleted();
}
}
@Override
public void onError(Throwable e) {
if (!subscriber.isUnsubscribed()) {
subscriber.onError(e);
}
}
@Override
public void onNext(List<T> ts) {
if (!subscriber.isUnsubscribed()) {
int maxValue = Math.min(ts.size(), start + length);
subscriber.onNext(ts.subList(start, maxValue));
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment