Skip to content

Commit c8f1199

Browse files
committed
Follow the correct logic of 'any' operator. See #385
1 parent d70e1d6 commit c8f1199

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4192,8 +4192,6 @@ private boolean isInternalImplementation(Object o) {
41924192
* Returns an {@link Observable} that emits <code>true</code> if the source
41934193
* {@link Observable} is not empty, otherwise <code>false</code>.
41944194
*
4195-
* @param source
4196-
* The source {@link Observable} to check if not empty.
41974195
* @return A subscription function for creating the target Observable.
41984196
* @see <a href=
41994197
* "http://msdn.microsoft.com/en-us/library/hh229905(v=vs.103).aspx"
@@ -4204,17 +4202,18 @@ public Observable<Boolean> any() {
42044202
}
42054203

42064204
/**
4207-
* Returns an {@link Observable} that emits <code>true</code> if all items
4208-
* of the source {@link Observable} satisfy the given condition, otherwise
4209-
* <code>false</code>.
4205+
* Returns an {@link Observable} that emits <code>true</code> if any element
4206+
* of the source {@link Observable} satisfies the given condition, otherwise
4207+
* <code>false</code>. Note: always emit <code>false</code> if the source
4208+
* {@link Observable} is empty.
42104209
*
42114210
* @param predicate
4212-
* The condition all items have to satisfy.
4211+
* The condition to test every element.
42134212
* @return A subscription function for creating the target Observable.
4214-
*
42154213
* @see <a href=
42164214
* "http://msdn.microsoft.com/en-us/library/hh211993(v=vs.103).aspx"
4217-
* >MSDN: Observable.Any</a>
4215+
* >MSDN: Observable.Any</a> Note: the description in this page is
4216+
* wrong.
42184217
*/
42194218
public Observable<Boolean> any(Func1<? super T, Boolean> predicate) {
42204219
return create(OperationAny.any(this, predicate));

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

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import rx.util.functions.Func1;
1818

1919
/**
20-
* Returns an {@link Observable} that emits <code>true</code> if all items of an
21-
* observable sequence satisfy a condition, otherwise <code>false</code>.
20+
* Returns an {@link Observable} that emits <code>true</code> if any element of
21+
* an observable sequence satisfies a condition, otherwise <code>false</code>.
2222
*/
2323
public final class OperationAny {
2424

@@ -36,15 +36,16 @@ public static <T> OnSubscribeFunc<Boolean> any(
3636
}
3737

3838
/**
39-
* Returns an {@link Observable} that emits <code>true</code> if all items
40-
* of the source {@link Observable} satisfy the given condition, otherwise
41-
* <code>false</code>.
39+
* Returns an {@link Observable} that emits <code>true</code> if any element
40+
* of the source {@link Observable} satisfies the given condition, otherwise
41+
* <code>false</code>. Note: always emit <code>false</code> if the source
42+
* {@link Observable} is empty.
4243
*
4344
* @param source
44-
* The source {@link Observable} to check if all items in it
45-
* satisfy the given condition
45+
* The source {@link Observable} to check if any element
46+
* satisfies the given condition.
4647
* @param predicate
47-
* The condition all items have to satisfy.
48+
* The condition to test every element.
4849
* @return A subscription function for creating the target Observable.
4950
*/
5051
public static <T> OnSubscribeFunc<Boolean> any(
@@ -71,16 +72,13 @@ public Subscription onSubscribe(final Observer<? super Boolean> observer) {
7172
private final AtomicBoolean hasEmitted = new AtomicBoolean(
7273
false);
7374

74-
private volatile boolean isNotEmpty = false;
75-
7675
@Override
7776
public void onNext(T value) {
78-
isNotEmpty = true;
7977
try {
8078
if (hasEmitted.get() == false) {
81-
if (predicate.call(value) == false
79+
if (predicate.call(value) == true
8280
&& hasEmitted.getAndSet(true) == false) {
83-
observer.onNext(false);
81+
observer.onNext(true);
8482
observer.onCompleted();
8583
// this will work if the sequence is
8684
// asynchronous, it
@@ -106,7 +104,7 @@ public void onError(Throwable ex) {
106104
@Override
107105
public void onCompleted() {
108106
if (!hasEmitted.get()) {
109-
observer.onNext(isNotEmpty);
107+
observer.onNext(false);
110108
observer.onCompleted();
111109
}
112110
}
@@ -164,13 +162,13 @@ public void testAnyWithEmpty() {
164162

165163
@Test
166164
public void testAnyWithPredicate1() {
167-
Observable<Integer> w = Observable.from(1, 2);
165+
Observable<Integer> w = Observable.from(1, 2, 3);
168166
Observable<Boolean> observable = Observable.create(any(w,
169167
new Func1<Integer, Boolean>() {
170168

171169
@Override
172170
public Boolean call(Integer t1) {
173-
return t1 < 3;
171+
return t1 < 2;
174172
}
175173
}));
176174

@@ -192,7 +190,7 @@ public void testAnyWithPredicate2() {
192190

193191
@Override
194192
public Boolean call(Integer t1) {
195-
return t1 < 3;
193+
return t1 < 1;
196194
}
197195
}));
198196

0 commit comments

Comments
 (0)