最近公司人事变动太大,工作比较忙,会尽量保持一周一更,以下正文开始:
Conditional and Boolean Operators:Operators that evaluate one or more Observables or items emitted by Observables
条件布尔操作常用用于对Observable的一些条件判断,或者对Observable元数据进行一些过滤操作;
常见分类如下:
all:determine whether all items emitted by an Observable meet some criteria
all作用判断Observable是否所有的发射都满足某一个条件
原理图如下:
示例代码以及源码如下
private fun operatorsAll() =
Observable.range(1, 4).all { it < 5 }.subscribe(getBaseSingleObserver("operatorsAll"))
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsAll: onSubscribe
* com.ypz.rxjavademo I/operatorsAll: onNext:value-true
* 相关源码如下
* */
/**
* Returns a Single that emits a Boolean that indicates whether all of the items emitted by the source
* ObservableSource satisfy a condition.
*
* @param predicate
* a function that evaluates an item and returns a Boolean
* @return a Single that emits {@code true} if all items emitted by the source ObservableSource satisfy the
* predicate; otherwise, {@code false}
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<Boolean> all(Predicate<? super T> predicate) {
ObjectHelper.requireNonNull(predicate, "predicate is null");
return RxJavaPlugins.onAssembly(new ObservableAllSingle<T>(this, predicate));
}
/**
* A functional interface (callback) that returns true or false for the given input value.
* @param the first value
*/
public interface Predicate<T> {
/**
* Test the given input value and return a boolean.
* @param t the value
* @return the boolean result
* @throws Exception on error
*/
boolean test(@NonNull T t) throws Exception;
}
public final class ObservableAllSingle<T> extends Single<Boolean> implements FuseToObservable<Boolean> {
//省略部分源码。。。。。。。
@Override
public Observable<Boolean> fuseToObservable() {
return RxJavaPlugins.onAssembly(new ObservableAll<T>(source, predicate));
}
static final class AllObserver<T> implements Observer<T>, Disposable {
@Override
public void onNext(T t) {
if (done) {
return;
}
boolean b;
try {
b = predicate.test(t);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
upstream.dispose();
onError(e);
return;
}
if (!b) {
done = true;
upstream.dispose();
downstream.onSuccess(false);
}
}
}
}
结合代码可以得到:
Amb:given two or more source Observables, emit all of the items from only the first of these Observables to emit an item or notification
Amb作用判断两个或多个Observable的发射问题:当有一个Observable最先开始发射事件时,则取该Observable其余Observable则丢弃。
原理图如下:
示例代码以及源码如下
private fun operatorsAmb() {
Observable.amb(mutableListOf(
Observable.just(1).delay(1, TimeUnit.SECONDS),
Observable.just(5)
)).subscribe(getBaseObsever("operatorsAmb"))
}
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsAmb: onSubscribe
* com.ypz.rxjavademo I/operatorsAmb: onNextValue:5
* 相关源码:
* */
/**
* Mirrors the one ObservableSource in an Iterable of several ObservableSources that first either emits an item or sends
* a termination notification.
*
* @param the common element type
* @param sources
* an Iterable of ObservableSource sources competing to react first. A subscription to each source will
* occur in the same order as in the Iterable.
* @return an Observable that emits the same sequence as whichever of the source ObservableSources first
* emitted an item or sent a termination notification
*/
@CheckReturnValue
@NonNull
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Observable<T> amb(Iterable<? extends ObservableSource<? extends T>> sources) {
ObjectHelper.requireNonNull(sources, "sources is null");
return RxJavaPlugins.onAssembly(new ObservableAmb<T>(null, sources));
}
public final class ObservableAmb<T> extends Observable<T> {
final ObservableSource<? extends T>[] sources;
final Iterable<? extends ObservableSource<? extends T>> sourcesIterable;
public ObservableAmb(ObservableSource<? extends T>[] sources, Iterable<? extends ObservableSource<? extends T>> sourcesIterable) {
this.sources = sources;
this.sourcesIterable = sourcesIterable;
}
@Override
@SuppressWarnings("unchecked")
public void subscribeActual(Observer<? super T> observer) {
ObservableSource<? extends T>[] sources = this.sources;
int count = 0;
if (sources == null) {
sources = new Observable[8];
try {
for (ObservableSource<? extends T> p : sourcesIterable) {
if (p == null) {
EmptyDisposable.error(new NullPointerException("One of the sources is null"), observer);
return;
}
if (count == sources.length) {
ObservableSource<? extends T>[] b = new ObservableSource[count + (count >> 2)];
System.arraycopy(sources, 0, b, 0, count);
sources = b;
}
sources[count++] = p;
}
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
EmptyDisposable.error(e, observer);
return;
}
} else {
count = sources.length;
}
if (count == 0) {
EmptyDisposable.complete(observer);
return;
} else
if (count == 1) {
sources[0].subscribe(observer);
return;
}
AmbCoordinator<T> ac = new AmbCoordinator<T>(observer, count);
ac.subscribe(sources);
}
}
结合代码可以得到:
Contains:determine whether an Observable emits a particular item or not
Contains作用判断Observable是否所有的发射元素是否包含某一个元素
原理图如下:
示例代码以及源码如下
private fun operatorsContains() =
Observable.range(1, 6).contains(3).subscribe(getBaseSingleObserver("operatorsContains"))
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsContains: onSubscribe
* com.ypz.rxjavademo I/operatorsContains: onNext:value-true
* 相关源码如下
* */
/**
* Returns a Single that emits a Boolean that indicates whether the source ObservableSource emitted a
* specified item.
* @param element
* the item to search for in the emissions from the source ObservableSource
* @return a Single that emits {@code true} if the specified item is emitted by the source ObservableSource,
* or {@code false} if the source ObservableSource completes without emitting that item
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<Boolean> contains(final Object element) {
ObjectHelper.requireNonNull(element, "element is null");
return any(Functions.equalsWith(element));
}
public static <T> Predicate<T> equalsWith(T value) {
return new EqualsPredicate<T>(value);
}
static final class EqualsPredicate<T> implements Predicate<T> {
final T value;
EqualsPredicate(T value) {
this.value = value;
}
@Override
public boolean test(T t) throws Exception {
return ObjectHelper.equals(t, value);
}
}
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Single<Boolean> any(Predicate<? super T> predicate) {
ObjectHelper.requireNonNull(predicate, "predicate is null");
return RxJavaPlugins.onAssembly(new ObservableAnySingle<T>(this, predicate));
}
结合代码可以得到:
DefaultIfEmpty:emit items from the source Observable, or a default item if the source Observable emits nothing
DefaultIfEmpty作用当Observable是一个Empty的Observable的时候会发射出一个默认的数据出去
原理图如下:
示例代码以及源码如下
/**
* Returns an Observable that emits the items emitted by the source ObservableSource or a specified default item
* if the source ObservableSource is empty.
*
* @param defaultItem
* the item to emit if the source ObservableSource emits no items
* @return an Observable that emits either the specified default item if the source ObservableSource emits no
* items, or the items emitted by the source ObservableSource
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> defaultIfEmpty(T defaultItem) {
ObjectHelper.requireNonNull(defaultItem, "defaultItem is null");
return switchIfEmpty(just(defaultItem));
}
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> switchIfEmpty(ObservableSource<? extends T> other) {
ObjectHelper.requireNonNull(other, "other is null");
return RxJavaPlugins.onAssembly(new ObservableSwitchIfEmpty<T>(this, other));
}
结合代码可以得到:
SequenceEqual:determine whether two Observables emit the same sequence of items
SequenceEqual作用判断两个Observable是否一致
原理图如下:
示例代码以及源码如下
private fun operatorsSequenceEqual() {
Observable.sequenceEqual(
Observable.range(1, 5),
Observable.range(1, 5)
).subscribe(getBaseSingleObserver<Boolean>("operatorsSequenceEqual-直接比较"))
}
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsSequenceEqual-直接比较: onSubscribe
* com.ypz.rxjavademo I/operatorsSequenceEqual-直接比较: onNext:value-true
* 相关源码如下
* */
/**
* Returns a Single that emits a Boolean value that indicates whether two ObservableSource sequences are the
* same by comparing the items emitted by each ObservableSource pairwise.
*
* @param source1
* the first ObservableSource to compare
* @param source2
* the second ObservableSource to compare
* @param
* the type of items emitted by each ObservableSource
* @return a Single that emits a Boolean value that indicates whether the two sequences are the same
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Single<Boolean> sequenceEqual(ObservableSource<? extends T> source1, ObservableSource<? extends T> source2) {
return sequenceEqual(source1, source2, ObjectHelper.equalsPredicate(), bufferSize());
}
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Single<Boolean> sequenceEqual(ObservableSource<? extends T> source1, ObservableSource<? extends T> source2,
BiPredicate<? super T, ? super T> isEqual, int bufferSize) {
ObjectHelper.requireNonNull(source1, "source1 is null");
ObjectHelper.requireNonNull(source2, "source2 is null");
ObjectHelper.requireNonNull(isEqual, "isEqual is null");
ObjectHelper.verifyPositive(bufferSize, "bufferSize");
return RxJavaPlugins.onAssembly(new ObservableSequenceEqualSingle<T>(source1, source2, isEqual, bufferSize));
}
public final class ObservableSequenceEqualSingle<T> extends Single<Boolean> implements FuseToObservable<Boolean> {
static final class EqualCoordinator<T> extends AtomicInteger implements Disposable {
void drain() {
if (getAndIncrement() != 0) {
return;
}
int missed = 1;
EqualObserver<T>[] as = observers;
final EqualObserver<T> observer1 = as[0];
final SpscLinkedArrayQueue<T> q1 = observer1.queue;
final EqualObserver<T> observer2 = as[1];
final SpscLinkedArrayQueue<T> q2 = observer2.queue;
for (;;) {
for (;;) {
if (cancelled) {
q1.clear();
q2.clear();
return;
}
boolean d1 = observer1.done;
if (d1) {
Throwable e = observer1.error;
if (e != null) {
cancel(q1, q2);
downstream.onError(e);
return;
}
}
boolean d2 = observer2.done;
if (d2) {
Throwable e = observer2.error;
if (e != null) {
cancel(q1, q2);
downstream.onError(e);
return;
}
}
if (v1 == null) {
v1 = q1.poll();
}
boolean e1 = v1 == null;
if (v2 == null) {
v2 = q2.poll();
}
boolean e2 = v2 == null;
if (d1 && d2 && e1 && e2) {
downstream.onSuccess(true);
return;
}
if ((d1 && d2) && (e1 != e2)) {
cancel(q1, q2);
downstream.onSuccess(false);
return;
}
if (!e1 && !e2) {
boolean c;
try {
c = comparer.test(v1, v2);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);
cancel(q1, q2);
downstream.onError(ex);
return;
}
if (!c) {
cancel(q1, q2);
downstream.onSuccess(false);
return;
}
v1 = null;
v2 = null;
}
if (e1 || e2) {
break;
}
}
missed = addAndGet(-missed);
if (missed == 0) {
break;
}
}
}
}
static final class EqualObserver<T> implements Observer<T> {
@Override
public void onNext(T t) {
queue.offer(t);
parent.drain();
}
}
}
结合代码可以得到:
示例代码以及源码如下
private fun operatorsSequenceEqual() {
Observable.sequenceEqual(
Observable.range(1, 5),
Observable.range(1, 5),
object : BiPredicate<Int, Int> {
override fun test(t1: Int, t2: Int): Boolean {
return (t1) == (t2 - 1)
}
}
).subscribe(getBaseSingleObserver<Boolean>("operatorsSequenceEqual-BiPredicate"))
}
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsSequenceEqual-BiPredicate: onSubscribe
* com.ypz.rxjavademo I/operatorsSequenceEqual-BiPredicate: onNext:value-false
* 相关源码如下
* */
/**
* Returns a Single that emits a Boolean value that indicates whether two ObservableSource sequences are the
* same by comparing the items emitted by each ObservableSource pairwise based on the results of a specified
* equality function.
*
* @param source1
* the first ObservableSource to compare
* @param source2
* the second ObservableSource to compare
* @param isEqual
* a function used to compare items emitted by each ObservableSource
* @param
* the type of items emitted by each ObservableSource
* @return a Single that emits a Boolean value that indicates whether the two ObservableSource two sequences
* are the same according to the specified function
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Single<Boolean> sequenceEqual(ObservableSource<? extends T> source1, ObservableSource<? extends T> source2,
BiPredicate<? super T, ? super T> isEqual) {
return sequenceEqual(source1, source2, isEqual, bufferSize());
}
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public static <T> Single<Boolean> sequenceEqual(ObservableSource<? extends T> source1, ObservableSource<? extends T> source2,
BiPredicate<? super T, ? super T> isEqual, int bufferSize) {
ObjectHelper.requireNonNull(source1, "source1 is null");
ObjectHelper.requireNonNull(source2, "source2 is null");
ObjectHelper.requireNonNull(isEqual, "isEqual is null");
ObjectHelper.verifyPositive(bufferSize, "bufferSize");
return RxJavaPlugins.onAssembly(new ObservableSequenceEqualSingle<T>(source1, source2, isEqual, bufferSize));
}
结合代码可以得到:
SkipUntil:discard items emitted by an Observable until a second Observable emits an item
SkipUntil作用到达某一个条件前丢弃需要发射的数据
原理图如下:
示例代码以及源码如下
private fun operatorsSikpUnitl() {
Observable.intervalRange(1, 9, 0, 1, TimeUnit.SECONDS).skipUntil(Observable.timer(4, TimeUnit.SECONDS)).subscribe(getBaseObsever("operatorsSikpUnitl"))
}
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsSikpUnitl: onSubscribe
* com.ypz.rxjavademo I/operatorsSikpUnitl: onNextValue:5
* com.ypz.rxjavademo I/operatorsSikpUnitl: onNextValue:6
* com.ypz.rxjavademo I/operatorsSikpUnitl: onNextValue:7
* com.ypz.rxjavademo I/operatorsSikpUnitl: onNextValue:8
* com.ypz.rxjavademo I/operatorsSikpUnitl: onNextValue:9
* 相关源码如下:
* */
/**
* Returns an Observable that skips items emitted by the source ObservableSource until a second ObservableSource emits
* an item.
*
* @param the element type of the other ObservableSource
* @param other
* the second ObservableSource that has to emit an item before the source ObservableSource's elements begin
* to be mirrored by the resulting ObservableSource
* @return an Observable that skips items from the source ObservableSource until the second ObservableSource emits an
* item, then emits the remaining items
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final <U> Observable<T> skipUntil(ObservableSource<U> other) {
ObjectHelper.requireNonNull(other, "other is null");
return RxJavaPlugins.onAssembly(new ObservableSkipUntil<T, U>(this, other));
}
public final class ObservableSkipUntil<T, U> extends AbstractObservableWithUpstream<T, T> {
final ObservableSource<U> other;
public ObservableSkipUntil(ObservableSource<T> source, ObservableSource<U> other) {
super(source);
this.other = other;
}
@Override
public void subscribeActual(Observer<? super T> child) {
final SerializedObserver<T> serial = new SerializedObserver<T>(child);
final ArrayCompositeDisposable frc = new ArrayCompositeDisposable(2);
serial.onSubscribe(frc);
final SkipUntilObserver<T> sus = new SkipUntilObserver<T>(serial, frc);
other.subscribe(new SkipUntil(frc, sus, serial));
source.subscribe(sus);
}
final class SkipUntil implements Observer<U> {
@Override
public void onNext(U t) {
upstream.dispose();
sus.notSkipping = true;
}
}
static final class SkipUntilObserver<T> implements Observer<T> {
@Override
public void onNext(T t) {
if (notSkippingLocal) {
downstream.onNext(t);
} else
if (notSkipping) {
notSkippingLocal = true;
downstream.onNext(t);
}
}
}
}
结合代码可以得到:
SkipWhile:discard items emitted by an Observable until a specified condition becomes false
SkipWhile作用判断Observable发射的数据直到满足某个条件前都会将数据跳过
原理图如下:
示例代码以及源码如下
private fun operatorsSikpWhile() {
Observable.range(1, 9).skipWhile(object : Predicate<Int> {
override fun test(t: Int): Boolean = (t <= 3)
}).subscribe(getBaseObsever("operatorsSikpWhile"))
}
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsSikpWhile: onSubscribe
* com.ypz.rxjavademo I/operatorsSikpWhile: onNextValue:4
* com.ypz.rxjavademo I/operatorsSikpWhile: onNextValue:5
* com.ypz.rxjavademo I/operatorsSikpWhile: onNextValue:6
* com.ypz.rxjavademo I/operatorsSikpWhile: onNextValue:7
* com.ypz.rxjavademo I/operatorsSikpWhile: onNextValue:8
* com.ypz.rxjavademo I/operatorsSikpWhile: onNextValue:9
* 相关源码如下:
* */
/**
* Returns an Observable that skips all items emitted by the source ObservableSource as long as a specified
* condition holds true, but emits all further source items as soon as the condition becomes false.
*
* @param predicate
* a function to test each item emitted from the source ObservableSource
* @return an Observable that begins emitting items emitted by the source ObservableSource when the specified
* predicate becomes false
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> skipWhile(Predicate<? super T> predicate) {
ObjectHelper.requireNonNull(predicate, "predicate is null");
return RxJavaPlugins.onAssembly(new ObservableSkipWhile<T>(this, predicate));
}
public final class ObservableSkipWhile<T> extends AbstractObservableWithUpstream<T, T> {
final Predicate<? super T> predicate;
public ObservableSkipWhile(ObservableSource<T> source, Predicate<? super T> predicate) {
super(source);
this.predicate = predicate;
}
static final class SkipWhileObserver<T> implements Observer<T>, Disposable {
final Observer<? super T> downstream;
final Predicate<? super T> predicate;
Disposable upstream;
boolean notSkipping;
SkipWhileObserver(Observer<? super T> actual, Predicate<? super T> predicate) {
this.downstream = actual;
this.predicate = predicate;
}
@Override
public void onNext(T t) {
if (notSkipping) {
downstream.onNext(t);
} else {
boolean b;
try {
b = predicate.test(t);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
upstream.dispose();
downstream.onError(e);
return;
}
if (!b) {
notSkipping = true;
downstream.onNext(t);
}
}
}
}
}
结合代码可以得到:
TakeUntil:discard any items emitted by an Observable after a second Observable emits an item or terminates
TakeUntil作用与SkipUntil类似不过区别在于它是直到某个条件成立时则不在发射数据
原理图如下:
示例代码以及源码如下
private fun operatorsTakeUntil() {
Observable.range(1, 9).takeUntil(object : Predicate<Int> {
override fun test(t: Int): Boolean = t == 3
}).subscribe(getBaseObsever("operatorsTakeUntil"))
}
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsTakeUntil: onSubscribe
* com.ypz.rxjavademo I/operatorsTakeUntil: onNextValue:1
* com.ypz.rxjavademo I/operatorsTakeUntil: onNextValue:2
* com.ypz.rxjavademo I/operatorsTakeUntil: onNextValue:3
* */
/**
* Returns an Observable that emits items emitted by the source Observable, checks the specified predicate
* for each item, and then completes when the condition is satisfied.
*
* @param stopPredicate
* a function that evaluates an item emitted by the source Observable and returns a Boolean
* @return an Observable that first emits items emitted by the source Observable, checks the specified
* condition after each item, and then completes when the condition is satisfied.
* @see Observable#takeWhile(Predicate)
* @since 1.1.0
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> takeUntil(Predicate<? super T> stopPredicate) {
ObjectHelper.requireNonNull(stopPredicate, "stopPredicate is null");
return RxJavaPlugins.onAssembly(new ObservableTakeUntilPredicate<T>(this, stopPredicate));
}
public final class ObservableTakeUntilPredicate<T> extends AbstractObservableWithUpstream<T, T> {
final Predicate<? super T> predicate;
public ObservableTakeUntilPredicate(ObservableSource<T> source, Predicate<? super T> predicate) {
super(source);
this.predicate = predicate;
}
@Override
public void subscribeActual(Observer<? super T> observer) {
source.subscribe(new TakeUntilPredicateObserver<T>(observer, predicate));
}
static final class TakeUntilPredicateObserver<T> implements Observer<T>, Disposable {
final Observer<? super T> downstream;
final Predicate<? super T> predicate;
Disposable upstream;
boolean done;
TakeUntilPredicateObserver(Observer<? super T> downstream, Predicate<? super T> predicate) {
this.downstream = downstream;
this.predicate = predicate;
}
@Override
public void onNext(T t) {
if (!done) {
downstream.onNext(t);
boolean b;
try {
b = predicate.test(t);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
upstream.dispose();
onError(e);
return;
}
if (b) {
done = true;
upstream.dispose();
downstream.onComplete();
}
}
}
}
}
结合代码可以得到:
TakeWhile:mirror items emitted by an Observable until a specified condition becomes false
TakeWhile作用当条件不成立的时候不在发射数据
原理图如下:
示例代码以及源码如下
private fun operatorsTakeWhen() {
Observable.range(1, 9).takeWhile(object : Predicate<Int> {
override fun test(t: Int): Boolean = t <= 3
}).subscribe(getBaseObsever("operatorsTakeUntil"))
}
/**
* 运行结果如下:
* com.ypz.rxjavademo I/operatorsTakeUntil: onSubscribe
* com.ypz.rxjavademo I/operatorsTakeUntil: onNextValue:1
* com.ypz.rxjavademo I/operatorsTakeUntil: onNextValue:2
* com.ypz.rxjavademo I/operatorsTakeUntil: onNextValue:3
* */
/**
* Returns an Observable that emits items emitted by the source ObservableSource so long as each item satisfied a
* specified condition, and then completes as soon as this condition is not satisfied.
*
* @param predicate
* a function that evaluates an item emitted by the source ObservableSource and returns a Boolean
* @return an Observable that emits the items from the source ObservableSource so long as each item satisfies the
* condition defined by {@code predicate}, then completes
* @see Observable#takeUntil(Predicate)
*/
@CheckReturnValue
@SchedulerSupport(SchedulerSupport.NONE)
public final Observable<T> takeWhile(Predicate<? super T> predicate) {
ObjectHelper.requireNonNull(predicate, "predicate is null");
return RxJavaPlugins.onAssembly(new ObservableTakeWhile<T>(this, predicate));
}
public final class ObservableTakeWhile<T> extends AbstractObservableWithUpstream<T, T> {
final Predicate<? super T> predicate;
public ObservableTakeWhile(ObservableSource<T> source, Predicate<? super T> predicate) {
super(source);
this.predicate = predicate;
}
@Override
public void subscribeActual(Observer<? super T> t) {
source.subscribe(new TakeWhileObserver<T>(t, predicate));
}
static final class TakeWhileObserver<T> implements Observer<T>, Disposable {
final Observer<? super T> downstream;
final Predicate<? super T> predicate;
Disposable upstream;
boolean done;
TakeWhileObserver(Observer<? super T> actual, Predicate<? super T> predicate) {
this.downstream = actual;
this.predicate = predicate;
}
@Override
public void onSubscribe(Disposable d) {
if (DisposableHelper.validate(this.upstream, d)) {
this.upstream = d;
downstream.onSubscribe(this);
}
}
@Override
public void onNext(T t) {
if (done) {
return;
}
boolean b;
try {
b = predicate.test(t);
} catch (Throwable e) {
Exceptions.throwIfFatal(e);
upstream.dispose();
onError(e);
return;
}
if (!b) {
done = true;
upstream.dispose();
downstream.onComplete();
return;
}
downstream.onNext(t);
}
}
}
结合代码可以得到: