Skip to content

Commit

Permalink
Merge pull request #191 from prabirshrestha/where
Browse files Browse the repository at this point in the history
Where alias to filter
  • Loading branch information
benjchristensen committed Mar 15, 2013
2 parents 4a41496 + 141a9f8 commit f4968d6
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
31 changes: 31 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import rx.operators.OperationDefer;
import rx.operators.OperationDematerialize;
import rx.operators.OperationFilter;
import rx.operators.OperationWhere;
import rx.operators.OperationMap;
import rx.operators.OperationMaterialize;
import rx.operators.OperationMerge;
Expand Down Expand Up @@ -722,6 +723,21 @@ public Boolean call(T t1) {
});
}

/**
* Filters an Observable by discarding any of its emissions that do not meet some test.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/filter.png">
*
* @param that
* the Observable to filter
* @param predicate
* a function that evaluates the items emitted by the source Observable, returning <code>true</code> if they pass the filter
* @return an Observable that emits only those items in the original Observable that the filter evaluates as true
*/
public static <T> Observable<T> where(Observable<T> that, Func1<T, Boolean> predicate) {
return _create(OperationWhere.where(that, predicate));
}

/**
* Converts an {@link Iterable} sequence to an Observable sequence.
*
Expand Down Expand Up @@ -2419,6 +2435,21 @@ public Boolean call(T t1) {
});
}

/**
* Filters an Observable by discarding any of its emissions that do not meet some test.
* <p>
* <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/filter.png">
*
* @param predicate
* a function that evaluates the items emitted by the source Observable, returning
* <code>true</code> if they pass the filter
* @return an Observable that emits only those items in the original Observable that the filter
* evaluates as <code>true</code>
*/
public Observable<T> where(Func1<T, Boolean> predicate) {
return where(this, predicate);
}

/**
* Returns the last element of an observable sequence with a specified source.
*
Expand Down
61 changes: 61 additions & 0 deletions rxjava-core/src/main/java/rx/operators/OperationWhere.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package rx.operators;

import org.junit.Test;
import org.mockito.Mockito;

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.util.functions.Func1;

import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

public final class OperationWhere {

public static <T> Func1<Observer<T>, Subscription> where(Observable<T> that, Func1<T, Boolean> predicate) {
return OperationFilter.filter(that, predicate);
}

public static class UnitTest {

@Test
public void testWhere() {
Observable<String> w = Observable.toObservable("one", "two", "three");
Observable<String> observable = Observable.create(where(w, new Func1<String, Boolean>() {

@Override
public Boolean call(String t1) {
return t1.equals("two");
}
}));

@SuppressWarnings("unchecked")
Observer<String> aObserver = mock(Observer.class);
observable.subscribe(aObserver);
verify(aObserver, Mockito.never()).onNext("one");
verify(aObserver, times(1)).onNext("two");
verify(aObserver, Mockito.never()).onNext("three");
verify(aObserver, Mockito.never()).onError(any(Exception.class));
verify(aObserver, times(1)).onCompleted();
}
}

}

0 comments on commit f4968d6

Please sign in to comment.