From a3175336ff198dfdf4776b7bd3204174207ae66a Mon Sep 17 00:00:00 2001 From: zsxwing Date: Thu, 26 Sep 2013 09:40:18 +0800 Subject: [PATCH 1/3] Merge branch 'master', remote-tracking branch 'origin' From 930e225570a6123263829841acaa1e2da3d51c6f Mon Sep 17 00:00:00 2001 From: zsxwing Date: Thu, 26 Sep 2013 16:12:04 +0800 Subject: [PATCH 2/3] Implemented the 'Contains' operator --- rxjava-core/src/main/java/rx/Observable.java | 16 ++++++++ .../src/test/java/rx/ObservableTests.java | 41 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/rxjava-core/src/main/java/rx/Observable.java b/rxjava-core/src/main/java/rx/Observable.java index 5a0f668dc59..d9af3dc375e 100644 --- a/rxjava-core/src/main/java/rx/Observable.java +++ b/rxjava-core/src/main/java/rx/Observable.java @@ -3151,6 +3151,22 @@ public Observable exists(Func1 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 MSDN: Observable.Contains + */ + public Observable contains(final T element) { + return exists(new Func1() { + 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}. *

diff --git a/rxjava-core/src/test/java/rx/ObservableTests.java b/rxjava-core/src/test/java/rx/ObservableTests.java index 297bb1ff472..1710f16d188 100644 --- a/rxjava-core/src/test/java/rx/ObservableTests.java +++ b/rxjava-core/src/test/java/rx/ObservableTests.java @@ -741,4 +741,45 @@ public void testOfTypeWithPolymorphism() { verify(aObserver, times(1)).onCompleted(); } + @Test + public void testContains() { + Observable observable = Observable.from("a", "b", null).contains("b"); + + @SuppressWarnings("unchecked") + Observer 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 testContainsWithNull() { + Observable observable = Observable.from("a", "b", null).contains(null); + + @SuppressWarnings("unchecked") + Observer 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 observable = Observable.empty().contains("a"); + + @SuppressWarnings("unchecked") + Observer 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(); + } } \ No newline at end of file From f498e0a4d1ee3d5db6a7d6b44eede9fb7787172d Mon Sep 17 00:00:00 2001 From: zsxwing Date: Thu, 26 Sep 2013 16:59:54 +0800 Subject: [PATCH 3/3] Add one more unit test --- rxjava-core/src/test/java/rx/ObservableTests.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/rxjava-core/src/test/java/rx/ObservableTests.java b/rxjava-core/src/test/java/rx/ObservableTests.java index 1710f16d188..3919ab6c9d9 100644 --- a/rxjava-core/src/test/java/rx/ObservableTests.java +++ b/rxjava-core/src/test/java/rx/ObservableTests.java @@ -755,6 +755,20 @@ public void testContains() { verify(aObserver, times(1)).onCompleted(); } + @Test + public void testContainsWithInexistence() { + Observable observable = Observable.from("a", "b", null).contains("c"); + + @SuppressWarnings("unchecked") + Observer 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 observable = Observable.from("a", "b", null).contains(null);