Conditional and Boolean Operators(条件和布尔操作符)
public final Observable<java.lang.Boolean> all(Func1<? super T,java.lang.Boolean> predicate)
Observable<Integer> values = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.onNext(0);
subscriber.onNext(10);
subscriber.onNext(20);
subscriber.onNext(2);
subscriber.onCompleted();
}
});
Subscription evenNumbers = values
.all(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer integer) {
return integer % 2 == 0;
}
})
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log(aBoolean+"");
}
});
结果:
true
Complete!
Observable<Long> values = Observable.interval(150, TimeUnit.MILLISECONDS).take(5);
Subscription subscription = values
.all(new Func1<Long, Boolean>() {
@Override
public Boolean call(Long aLong) {
return aLong<3;
}
})
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("First:Complete!");
}
@Override
public void onError(Throwable e) {
log("First:"+e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log("First:"+aBoolean);
}
});
Subscription subscription2 = values
.subscribe(new Observer<Long>() {
@Override
public void onCompleted() {
log("Second:Complete!");
}
@Override
public void onError(Throwable e) {
log("Second:"+e.getMessage().toString());
}
@Override
public void onNext(Long aLong) {
log("Second:"+aLong);
}
});
结果:
Second:0
Second:1
Second:2
First:false
First:Complete!
Second:3
Second:4
Second:Complete!
Observable<Integer> values = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.onNext(0);
subscriber.onNext(2);
subscriber.onError(new Exception("Oops"));
}
});
Subscription subscription = values
.all(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer integer) {
return integer % 2 == 0;
}
})
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log(aBoolean+"");
}
});
结果:
Oops
将上面的subscriber.onNext(0);
改为subscriber.onNext(1);
结果:
false
Complete!
ofType操作符类似于filter操作符,区别在于ofType操作符是按照类型对结果进行过滤,其流程图如下:
Observable.just(0, "1", 2, "3").ofType(String.class)
.subscribe(new Action1<Object>() {
@Override
public void call(Object object) {
log(object.toString() + ":" + object.getClass());
}
});
结果:
1:class java.lang.String
3:class java.lang.String
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
values.take(10) // 获取前 10 个数据 的 Observable
.single(new Func1<Long, Boolean>() {
@Override
public Boolean call(Long aLong) {
return aLong == 5L;
}
}) // 有且仅有一个 数据为 5L
.subscribe(new Observer<Long>() {
@Override
public void onCompleted() {
log("Single1:Complete!");
}
@Override
public void onError(Throwable e) {
log("Single1:" + e.getMessage().toString());
}
@Override
public void onNext(Long aLong) {
log("Single1:" + aLong);
}
});
values
.single(new Func1<Long, Boolean>() {
@Override
public Boolean call(Long aLong) {
return aLong == 5L;
}
}) // 由于源 Observable 为无限的,所以这个不会打印任何东西
.subscribe(new Observer<Long>() {
@Override
public void onCompleted() {
log("Single2:Complete!");
}
@Override
public void onError(Throwable e) {
log("Single2:" + e.getMessage().toString());
}
@Override
public void onNext(Long aLong) {
log("Single2:" + aLong);
}
});
结果:
Single1: 5
Single1: Complete!
和前面的类似,使用 singleOrDefault 可以返回一个默认值。
Observable<Integer> values = Observable.range(0, 2);
Subscription subscription = values
.exists(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer integer) {
return integer > 2;
}
})
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log(aBoolean + "");
}
});
结果:
false
Complete!
上面示例中只发射了 0 和1 两个数据,而这两个数据都不满足大于2的条件,所以返回结果为 false。
如果我们多发射几个数据,则就会满足条件了。
Observable<Integer> values = Observable.range(0, 4);
结果:
true
Complete!
Observable<Long> values = Observable.timer(1000, TimeUnit.MILLISECONDS);
Subscription subscription = values
.isEmpty()
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log(aBoolean + "");
}
});
结果:
false
Complete!
只要源 Observable 发射了一个数据,isEmpty 就立刻返回 false, 只有当源 Observable 完成了并且没有发射数据,isEmpty 才返回 true。
Observable<Long> values = Observable.interval(100, TimeUnit.MILLISECONDS);
Subscription subscription = values
.contains(4L)
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log(aBoolean + "");
}
});
结果:
true
Complete!
Observable<Integer> values = Observable.empty();
Subscription subscription = values
.defaultIfEmpty(2)
.subscribe(new Observer<Integer>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Integer integer) {
log(integer+"");
}
});
结果:
2
Complete!
Observable<Integer> values = Observable.error(new Exception("Oops"));
Subscription subscription = values
.defaultIfEmpty(2)
.subscribe(new Observer<Integer>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Integer integer) {
log(integer+"");
}
});
结果:
Oops
Observable<String> strings = Observable.just("1", "2", "3");
Observable<Integer> ints = Observable.just(1, 2, 3);
Observable.sequenceEqual(strings, ints, new Func2<Serializable, Serializable, Boolean>() {
@Override
public Boolean call(Serializable serializable, Serializable serializable2) {
return serializable.equals(serializable2.toString());
}
})//Observable.sequenceEqual(strings, ints)
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log(aBoolean + "");
}
});
结果:
true
Complete!
Observable<Integer> values = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> subscriber) {
subscriber.onNext(1);
subscriber.onNext(2);
subscriber.onError(new Exception("Oops"));
}
});
Observable.sequenceEqual(values, values)
.subscribe(new Observer<Boolean>() {
@Override
public void onCompleted() {
log("Complete!");
}
@Override
public void onError(Throwable e) {
log(e.getMessage().toString());
}
@Override
public void onNext(Boolean aBoolean) {
log(aBoolean + "");
}
});
结果:
Oops
项目源码 GitHub求赞,谢谢!
引用:
RxJava 教程第二部分:事件流基础之 检查数据 - 云在千峰