From fae7a9b3283a3e895f7bf1ca8d999a67b7c14d59 Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Thu, 6 Feb 2014 12:20:25 -0800 Subject: [PATCH] Subscribers/Observers.empty() --- .../src/main/java/rx/observers/Observers.java | 37 +++++++------- .../main/java/rx/observers/Subscribers.java | 48 +++++++++---------- .../main/java/rx/observers/TestObserver.java | 4 +- .../java/rx/observers/TestSubscriber.java | 5 +- .../rx/operators/OperatorZipIterable.java | 2 +- 5 files changed, 46 insertions(+), 50 deletions(-) diff --git a/rxjava-core/src/main/java/rx/observers/Observers.java b/rxjava-core/src/main/java/rx/observers/Observers.java index 9a7d1a816f..860e3c0a10 100644 --- a/rxjava-core/src/main/java/rx/observers/Observers.java +++ b/rxjava-core/src/main/java/rx/observers/Observers.java @@ -1,34 +1,35 @@ package rx.observers; import rx.Observer; +import rx.Subscriber; import rx.util.OnErrorNotImplementedException; import rx.util.functions.Action0; import rx.util.functions.Action1; public class Observers { - /** - * Create an empty Observer that ignores all events. - */ - public static final Observer create() { - return new Observer() { + private static final Observer EMPTY = new Observer() { - @Override - public final void onCompleted() { - // do nothing - } + @Override + public final void onCompleted() { + // do nothing + } - @Override - public final void onError(Throwable e) { - throw new OnErrorNotImplementedException(e); - } + @Override + public final void onError(Throwable e) { + throw new OnErrorNotImplementedException(e); + } - @Override - public final void onNext(T args) { - // do nothing - } + @Override + public final void onNext(Object args) { + // do nothing + } - }; + }; + + @SuppressWarnings("unchecked") + public static Observer empty() { + return (Observer) EMPTY; } /** diff --git a/rxjava-core/src/main/java/rx/observers/Subscribers.java b/rxjava-core/src/main/java/rx/observers/Subscribers.java index 4cf5e48d14..156656851f 100644 --- a/rxjava-core/src/main/java/rx/observers/Subscribers.java +++ b/rxjava-core/src/main/java/rx/observers/Subscribers.java @@ -8,6 +8,30 @@ public class Subscribers { + private static final Subscriber EMPTY = new Subscriber() { + + @Override + public final void onCompleted() { + // do nothing + } + + @Override + public final void onError(Throwable e) { + throw new OnErrorNotImplementedException(e); + } + + @Override + public final void onNext(Object args) { + // do nothing + } + + }; + + @SuppressWarnings("unchecked") + public static Subscriber empty() { + return (Subscriber) EMPTY; + } + public static Subscriber from(final Observer o) { return new Subscriber() { @@ -29,30 +53,6 @@ public void onNext(T t) { }; } - /** - * Create an empty Subscriber that ignores all events. - */ - public static final Subscriber create() { - return new Subscriber() { - - @Override - public final void onCompleted() { - // do nothing - } - - @Override - public final void onError(Throwable e) { - throw new OnErrorNotImplementedException(e); - } - - @Override - public final void onNext(T args) { - // do nothing - } - - }; - } - /** * Create an Subscriber that receives `onNext` and ignores `onError` and `onCompleted`. */ diff --git a/rxjava-core/src/main/java/rx/observers/TestObserver.java b/rxjava-core/src/main/java/rx/observers/TestObserver.java index a6198ada5b..2ef3685792 100644 --- a/rxjava-core/src/main/java/rx/observers/TestObserver.java +++ b/rxjava-core/src/main/java/rx/observers/TestObserver.java @@ -28,7 +28,6 @@ */ public class TestObserver implements Observer { - private final Observer EMPTY = new EmptyObserver(); private final Observer delegate; private final ArrayList onNextEvents = new ArrayList(); @@ -39,9 +38,8 @@ public TestObserver(Observer delegate) { this.delegate = delegate; } - @SuppressWarnings("unchecked") public TestObserver() { - this.delegate = (Observer) EMPTY; + this.delegate = Observers.empty(); } @Override diff --git a/rxjava-core/src/main/java/rx/observers/TestSubscriber.java b/rxjava-core/src/main/java/rx/observers/TestSubscriber.java index 097c6e8e24..2eb28a767d 100644 --- a/rxjava-core/src/main/java/rx/observers/TestSubscriber.java +++ b/rxjava-core/src/main/java/rx/observers/TestSubscriber.java @@ -26,8 +26,6 @@ */ public class TestSubscriber extends Subscriber { - private final Subscriber EMPTY = Subscribers.create(); - private final TestObserver testObserver; public TestSubscriber(Subscriber delegate) { @@ -38,9 +36,8 @@ public TestSubscriber(Observer delegate) { this.testObserver = new TestObserver(delegate); } - @SuppressWarnings("unchecked") public TestSubscriber() { - this.testObserver = new TestObserver((Subscriber) EMPTY); + this.testObserver = new TestObserver(Subscribers.empty()); } @Override diff --git a/rxjava-core/src/main/java/rx/operators/OperatorZipIterable.java b/rxjava-core/src/main/java/rx/operators/OperatorZipIterable.java index eac5f80809..0ded7f0d5d 100644 --- a/rxjava-core/src/main/java/rx/operators/OperatorZipIterable.java +++ b/rxjava-core/src/main/java/rx/operators/OperatorZipIterable.java @@ -37,7 +37,7 @@ public Subscriber call(final Subscriber subscriber) { try { if (!iterator.hasNext()) { subscriber.onCompleted(); - return Subscribers.create(); + return Subscribers.empty(); } } catch (Throwable e) { subscriber.onError(e);