Skip to content

Commit b2fe579

Browse files
Merge pull request #1742 from benjchristensen/issue-1571
EmptyObserver and TestObserver
2 parents ea13546 + 35c4fa1 commit b2fe579

File tree

5 files changed

+31
-53
lines changed

5 files changed

+31
-53
lines changed

src/main/java/rx/internal/operators/BufferUntilSubscriber.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121
import rx.Observer;
2222
import rx.Subscriber;
2323
import rx.functions.Action0;
24-
import rx.observers.EmptyObserver;
25-
import rx.observers.Subscribers;
24+
import rx.observers.Observers;
2625
import rx.subjects.Subject;
2726
import rx.subscriptions.Subscriptions;
2827

@@ -51,9 +50,6 @@
5150
*/
5251
public class BufferUntilSubscriber<T> extends Subject<T, T> {
5352

54-
@SuppressWarnings("rawtypes")
55-
private final static Observer EMPTY_OBSERVER = new EmptyObserver();
56-
5753
/**
5854
* @warn create() undescribed
5955
* @return
@@ -96,7 +92,7 @@ public void call(final Subscriber<? super T> s) {
9692
s.add(Subscriptions.create(new Action0() {
9793
@Override
9894
public void call() {
99-
state.observerRef = EMPTY_OBSERVER;
95+
state.observerRef = Observers.empty();
10096
}
10197
}));
10298
boolean win = false;

src/main/java/rx/observers/EmptyObserver.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/main/java/rx/observers/Observers.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ public final void onNext(Object args) {
4646

4747
/**
4848
* Returns an inert {@link Observer} that does nothing in response to the emissions or notifications from
49-
* any {@code Observable} it subscribes to. This is different, however, from an {@link EmptyObserver}, in
50-
* that it will throw an exception if its {@link Observer#onError onError} method is called (whereas
51-
* {@code EmptyObserver} will swallow the error in such a case).
49+
* any {@code Observable} it subscribes to but will throw an exception if its {@link Observer#onError onError} method is called.
5250
*
5351
* @return an inert {@code Observer}
5452
*/
@@ -59,8 +57,8 @@ public static <T> Observer<T> empty() {
5957

6058
/**
6159
* Creates an {@link Observer} that receives the emissions of any {@code Observable} it subscribes to via
62-
* {@link Observer#onNext onNext} but ignores {@link Observer#onError onError} and
63-
* {@link Observer#onCompleted onCompleted} notifications.
60+
* {@link Observer#onNext onNext} but ignores {@link Observer#onCompleted onCompleted} notifications.
61+
* It will throws an {@link OnErrorNotImplementedException} if {@link Observer#onError onError} is invoked.
6462
*
6563
* @param onNext
6664
* a function that handles each item emitted by an {@code Observable}

src/main/java/rx/observers/TestObserver.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public TestObserver(Observer<T> delegate) {
3636
this.delegate = delegate;
3737
}
3838

39+
@SuppressWarnings("unchecked")
3940
public TestObserver() {
40-
this.delegate = Observers.empty();
41+
this.delegate = (Observer<T>) INERT;
4142
}
4243

4344
@Override
@@ -153,4 +154,23 @@ public void assertTerminalEvent() {
153154
}
154155
}
155156

157+
// do nothing ... including swallowing errors
158+
private static Observer<Object> INERT = new Observer<Object>() {
159+
160+
@Override
161+
public void onCompleted() {
162+
163+
}
164+
165+
@Override
166+
public void onError(Throwable e) {
167+
168+
}
169+
170+
@Override
171+
public void onNext(Object t) {
172+
173+
}
174+
175+
};
156176
}

src/test/java/rx/observers/TestObserverTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,10 @@ public void testWrappingMockWhenUnsubscribeInvolved() {
119119
inOrder.verify(mockObserver, times(1)).onCompleted();
120120
inOrder.verifyNoMoreInteractions();
121121
}
122+
123+
@Test
124+
public void testErrorSwallowed() {
125+
Observable.error(new RuntimeException()).subscribe(new TestObserver<Object>());
126+
}
122127

123128
}

0 commit comments

Comments
 (0)