Skip to content

Commit 210b08c

Browse files
Merge pull request #424 from zsxwing/ignore-elements
Implemented the 'IgnoreElements' operator
2 parents abc8bec + 5e68f25 commit 210b08c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4477,6 +4477,17 @@ public Boolean call(T t) {
44774477
}).cast(klass);
44784478
}
44794479

4480+
/**
4481+
* Ignores all values in an observable sequence and only calls onCompleted or onError method.
4482+
*
4483+
* @return An empty observable sequence that only call onCompleted, or onError method.
4484+
*
4485+
* @see <a href="http://msdn.microsoft.com/en-us/library/hh229242(v=vs.103).aspx">MSDN: Observable.IgnoreElements</a>
4486+
*/
4487+
public Observable<T> ignoreElements() {
4488+
return filter(alwaysFalse());
4489+
}
4490+
44804491
/**
44814492
* Whether a given {@link Function} is an internal implementation inside rx.* packages or not.
44824493
* <p>

rxjava-core/src/main/java/rx/util/functions/Functions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ public static <T> Func1<? super T, Boolean> alwaysTrue() {
327327
return AlwaysTrue.INSTANCE;
328328
}
329329

330+
public static <T> Func1<? super T, Boolean> alwaysFalse() {
331+
return AlwaysFalse.INSTANCE;
332+
}
333+
330334
public static <T> Func1<T, T> identity() {
331335
return new Func1<T, T>() {
332336
@Override
@@ -345,4 +349,12 @@ public Boolean call(Object o) {
345349
}
346350
}
347351

352+
private enum AlwaysFalse implements Func1<Object, Boolean> {
353+
INSTANCE;
354+
355+
@Override
356+
public Boolean call(Object o) {
357+
return false;
358+
}
359+
}
348360
}

rxjava-core/src/test/java/rx/ObservableTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,4 +838,15 @@ public void testContainsWithEmptyObservable() {
838838
org.mockito.Matchers.any(Throwable.class));
839839
verify(aObserver, times(1)).onCompleted();
840840
}
841+
842+
public void testIgnoreElements() {
843+
Observable<Integer> observable = Observable.from(1, 2, 3).ignoreElements();
844+
845+
@SuppressWarnings("unchecked")
846+
Observer<Integer> aObserver = mock(Observer.class);
847+
observable.subscribe(aObserver);
848+
verify(aObserver, never()).onNext(any(Integer.class));
849+
verify(aObserver, never()).onError(any(Throwable.class));
850+
verify(aObserver, times(1)).onCompleted();
851+
}
841852
}

0 commit comments

Comments
 (0)