Skip to content

Commit f4968d6

Browse files
Merge pull request ReactiveX#191 from prabirshrestha/where
Where alias to filter
2 parents 4a41496 + 141a9f8 commit f4968d6

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import rx.operators.OperationDefer;
4141
import rx.operators.OperationDematerialize;
4242
import rx.operators.OperationFilter;
43+
import rx.operators.OperationWhere;
4344
import rx.operators.OperationMap;
4445
import rx.operators.OperationMaterialize;
4546
import rx.operators.OperationMerge;
@@ -722,6 +723,21 @@ public Boolean call(T t1) {
722723
});
723724
}
724725

726+
/**
727+
* Filters an Observable by discarding any of its emissions that do not meet some test.
728+
* <p>
729+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/filter.png">
730+
*
731+
* @param that
732+
* the Observable to filter
733+
* @param predicate
734+
* a function that evaluates the items emitted by the source Observable, returning <code>true</code> if they pass the filter
735+
* @return an Observable that emits only those items in the original Observable that the filter evaluates as true
736+
*/
737+
public static <T> Observable<T> where(Observable<T> that, Func1<T, Boolean> predicate) {
738+
return _create(OperationWhere.where(that, predicate));
739+
}
740+
725741
/**
726742
* Converts an {@link Iterable} sequence to an Observable sequence.
727743
*
@@ -2419,6 +2435,21 @@ public Boolean call(T t1) {
24192435
});
24202436
}
24212437

2438+
/**
2439+
* Filters an Observable by discarding any of its emissions that do not meet some test.
2440+
* <p>
2441+
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/filter.png">
2442+
*
2443+
* @param predicate
2444+
* a function that evaluates the items emitted by the source Observable, returning
2445+
* <code>true</code> if they pass the filter
2446+
* @return an Observable that emits only those items in the original Observable that the filter
2447+
* evaluates as <code>true</code>
2448+
*/
2449+
public Observable<T> where(Func1<T, Boolean> predicate) {
2450+
return where(this, predicate);
2451+
}
2452+
24222453
/**
24232454
* Returns the last element of an observable sequence with a specified source.
24242455
*
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2013 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 org.junit.Test;
19+
import org.mockito.Mockito;
20+
21+
import rx.Observable;
22+
import rx.Observer;
23+
import rx.Subscription;
24+
import rx.util.functions.Func1;
25+
26+
import static org.mockito.Matchers.any;
27+
import static org.mockito.Mockito.mock;
28+
import static org.mockito.Mockito.times;
29+
import static org.mockito.Mockito.verify;
30+
31+
public final class OperationWhere {
32+
33+
public static <T> Func1<Observer<T>, Subscription> where(Observable<T> that, Func1<T, Boolean> predicate) {
34+
return OperationFilter.filter(that, predicate);
35+
}
36+
37+
public static class UnitTest {
38+
39+
@Test
40+
public void testWhere() {
41+
Observable<String> w = Observable.toObservable("one", "two", "three");
42+
Observable<String> observable = Observable.create(where(w, new Func1<String, Boolean>() {
43+
44+
@Override
45+
public Boolean call(String t1) {
46+
return t1.equals("two");
47+
}
48+
}));
49+
50+
@SuppressWarnings("unchecked")
51+
Observer<String> aObserver = mock(Observer.class);
52+
observable.subscribe(aObserver);
53+
verify(aObserver, Mockito.never()).onNext("one");
54+
verify(aObserver, times(1)).onNext("two");
55+
verify(aObserver, Mockito.never()).onNext("three");
56+
verify(aObserver, Mockito.never()).onError(any(Exception.class));
57+
verify(aObserver, times(1)).onCompleted();
58+
}
59+
}
60+
61+
}

0 commit comments

Comments
 (0)