Created
November 25, 2016 18:43
-
-
Save plzen/b43f7c65b8f53e0301ed39743b90733d to your computer and use it in GitHub Desktop.
Safe subList operator for RxJava
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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