Skip to content

Commit

Permalink
Merge pull request ReactiveX#410 from zsxwing/contains
Browse files Browse the repository at this point in the history
Implemented the 'Contains' operator
  • Loading branch information
benjchristensen committed Oct 9, 2013
2 parents f2ec21b + f498e0a commit 84dd240
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
16 changes: 16 additions & 0 deletions rxjava-core/src/main/java/rx/Observable.java
Original file line number Diff line number Diff line change
Expand Up @@ -3152,6 +3152,22 @@ public Observable<Boolean> exists(Func1<? super T, Boolean> predicate) {
return create(OperationAny.exists(this, predicate));
}

/**
* Determines whether an observable sequence contains a specified element.
*
* @param value
* The element to search in the sequence.
* @return an Observable that emits if the element is in the source sequence.
* @see <a href="http://msdn.microsoft.com/en-us/library/hh228965(v=vs.103).aspx">MSDN: Observable.Contains</a>
*/
public Observable<Boolean> contains(final T element) {
return exists(new Func1<T, Boolean>() {
public Boolean call(T t1) {
return element == null ? t1 == null : element.equals(t1);
}
});
}

/**
* Registers an {@link Action0} to be called when this Observable invokes {@link Observer#onCompleted onCompleted} or {@link Observer#onError onError}.
* <p>
Expand Down
55 changes: 55 additions & 0 deletions rxjava-core/src/test/java/rx/ObservableTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -783,4 +783,59 @@ public void testOfTypeWithPolymorphism() {
verify(aObserver, times(1)).onCompleted();
}

@Test
public void testContains() {
Observable<Boolean> observable = Observable.from("a", "b", null).contains("b");

@SuppressWarnings("unchecked")
Observer<Object> aObserver = mock(Observer.class);
observable.subscribe(aObserver);
verify(aObserver, times(1)).onNext(true);
verify(aObserver, never()).onNext(false);
verify(aObserver, never()).onError(
org.mockito.Matchers.any(Throwable.class));
verify(aObserver, times(1)).onCompleted();
}

@Test
public void testContainsWithInexistence() {
Observable<Boolean> observable = Observable.from("a", "b", null).contains("c");

@SuppressWarnings("unchecked")
Observer<Object> aObserver = mock(Observer.class);
observable.subscribe(aObserver);
verify(aObserver, times(1)).onNext(false);
verify(aObserver, never()).onNext(true);
verify(aObserver, never()).onError(
org.mockito.Matchers.any(Throwable.class));
verify(aObserver, times(1)).onCompleted();
}

@Test
public void testContainsWithNull() {
Observable<Boolean> observable = Observable.from("a", "b", null).contains(null);

@SuppressWarnings("unchecked")
Observer<Object> aObserver = mock(Observer.class);
observable.subscribe(aObserver);
verify(aObserver, times(1)).onNext(true);
verify(aObserver, never()).onNext(false);
verify(aObserver, never()).onError(
org.mockito.Matchers.any(Throwable.class));
verify(aObserver, times(1)).onCompleted();
}

@Test
public void testContainsWithEmptyObservable() {
Observable<Boolean> observable = Observable.<String>empty().contains("a");

@SuppressWarnings("unchecked")
Observer<Object> aObserver = mock(Observer.class);
observable.subscribe(aObserver);
verify(aObserver, times(1)).onNext(false);
verify(aObserver, never()).onNext(true);
verify(aObserver, never()).onError(
org.mockito.Matchers.any(Throwable.class));
verify(aObserver, times(1)).onCompleted();
}
}

0 comments on commit 84dd240

Please sign in to comment.