Skip to content

Commit f4d468f

Browse files
committed
OperatorAll
1 parent 4e0ce47 commit f4d468f

File tree

4 files changed

+84
-102
lines changed

4 files changed

+84
-102
lines changed

rxjava-core/src/main/java/rx/Observable.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
import rx.observers.SafeSubscriber;
4949
import rx.operators.OnSubscribeFromIterable;
5050
import rx.operators.OnSubscribeRange;
51-
import rx.operators.OperationAll;
51+
import rx.operators.OperatorAll;
5252
import rx.operators.OperationAny;
5353
import rx.operators.OperationAsObservable;
5454
import rx.operators.OperationBuffer;
@@ -2941,7 +2941,7 @@ public final static <T1, T2, T3, T4, T5, T6, T7, T8, T9, R> Observable<R> zip(Ob
29412941
* @see <a href="https://github.com/Netflix/RxJava/wiki/Conditional-and-Boolean-Operators#wiki-all">RxJava Wiki: all()</a>
29422942
*/
29432943
public final Observable<Boolean> all(Func1<? super T, Boolean> predicate) {
2944-
return create(OperationAll.all(this, predicate));
2944+
return lift(new OperatorAll<T>(predicate));
29452945
}
29462946

29472947
/**

rxjava-core/src/main/java/rx/operators/OperationAll.java

Lines changed: 0 additions & 90 deletions
This file was deleted.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright 2014 Netflix, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package rx.operators;
17+
18+
import java.util.concurrent.atomic.AtomicBoolean;
19+
20+
import rx.Observable;
21+
import rx.Observable.OnSubscribeFunc;
22+
import rx.Observable.Operator;
23+
import rx.Observer;
24+
import rx.Subscriber;
25+
import rx.Subscription;
26+
import rx.functions.Func1;
27+
28+
/**
29+
* Returns an Observable that emits a Boolean that indicates whether all items emitted by an
30+
* Observable satisfy a condition.
31+
* <p>
32+
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/all.png">
33+
*/
34+
public final class OperatorAll<T> implements Operator<Boolean, T> {
35+
private final Func1<? super T, Boolean> predicate;
36+
37+
public OperatorAll(Func1<? super T, Boolean> predicate) {
38+
this.predicate = predicate;
39+
}
40+
41+
@Override
42+
public Subscriber<? super T> call(final Subscriber<? super Boolean> child) {
43+
Subscriber s = new Subscriber<T>() {
44+
boolean done;
45+
@Override
46+
public void onNext(T t) {
47+
boolean result = predicate.call(t);
48+
if (!result && !done) {
49+
done = true;
50+
child.onNext(false);
51+
child.onCompleted();
52+
unsubscribe();
53+
}
54+
}
55+
56+
@Override
57+
public void onError(Throwable e) {
58+
child.onError(e);
59+
}
60+
61+
@Override
62+
public void onCompleted() {
63+
if (!done) {
64+
done = true;
65+
child.onNext(true);
66+
child.onCompleted();
67+
}
68+
}
69+
};
70+
child.add(s);
71+
return s;
72+
}
73+
}

rxjava-core/src/test/java/rx/operators/OperationAllTest.java renamed to rxjava-core/src/test/java/rx/operators/OperatorAllTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,27 @@
1818
import static org.mockito.Mockito.mock;
1919
import static org.mockito.Mockito.verify;
2020
import static org.mockito.Mockito.verifyNoMoreInteractions;
21-
import static rx.operators.OperationAll.all;
2221

2322
import org.junit.Test;
2423

2524
import rx.Observable;
2625
import rx.Observer;
2726
import rx.functions.Func1;
2827

29-
public class OperationAllTest {
28+
public class OperatorAllTest {
3029

3130
@Test
3231
@SuppressWarnings("unchecked")
3332
public void testAll() {
3433
Observable<String> obs = Observable.from("one", "two", "six");
3534

3635
Observer<Boolean> observer = mock(Observer.class);
37-
Observable.create(all(obs, new Func1<String, Boolean>() {
36+
obs.all(new Func1<String, Boolean>() {
3837
@Override
3938
public Boolean call(String s) {
4039
return s.length() == 3;
4140
}
42-
})).subscribe(observer);
41+
}).subscribe(observer);
4342

4443
verify(observer).onNext(true);
4544
verify(observer).onCompleted();
@@ -52,12 +51,12 @@ public void testNotAll() {
5251
Observable<String> obs = Observable.from("one", "two", "three", "six");
5352

5453
Observer<Boolean> observer = mock(Observer.class);
55-
Observable.create(all(obs, new Func1<String, Boolean>() {
54+
obs.all(new Func1<String, Boolean>() {
5655
@Override
5756
public Boolean call(String s) {
5857
return s.length() == 3;
5958
}
60-
})).subscribe(observer);
59+
}).subscribe(observer);
6160

6261
verify(observer).onNext(false);
6362
verify(observer).onCompleted();
@@ -70,12 +69,12 @@ public void testEmpty() {
7069
Observable<String> obs = Observable.empty();
7170

7271
Observer<Boolean> observer = mock(Observer.class);
73-
Observable.create(all(obs, new Func1<String, Boolean>() {
72+
obs.all(new Func1<String, Boolean>() {
7473
@Override
7574
public Boolean call(String s) {
7675
return s.length() == 3;
7776
}
78-
})).subscribe(observer);
77+
}).subscribe(observer);
7978

8079
verify(observer).onNext(true);
8180
verify(observer).onCompleted();
@@ -89,12 +88,12 @@ public void testError() {
8988
Observable<String> obs = Observable.error(error);
9089

9190
Observer<Boolean> observer = mock(Observer.class);
92-
Observable.create(all(obs, new Func1<String, Boolean>() {
91+
obs.all(new Func1<String, Boolean>() {
9392
@Override
9493
public Boolean call(String s) {
9594
return s.length() == 3;
9695
}
97-
})).subscribe(observer);
96+
}).subscribe(observer);
9897

9998
verify(observer).onError(error);
10099
verifyNoMoreInteractions(observer);

0 commit comments

Comments
 (0)