1717import 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 */
2323public 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