Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented the 'Contains' operator #410

Merged
merged 3 commits into from
Oct 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3151,6 +3151,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 @@ -741,4 +741,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();
}
}