From 084ac6fe7a9f34b89d40035f633be8a5ceaf7fdc Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Thu, 20 Feb 2014 09:12:16 -0800 Subject: [PATCH 1/2] Deprecate Direct Access to Scheduler Implementations This enforces the convention of using `Schedulers.*` and then makes the RxJavaDefaultSchedulers plugin more reliable. --- .../main/java/rx/schedulers/ExecutorScheduler.java | 11 ++++++++++- .../main/java/rx/schedulers/ImmediateScheduler.java | 10 +++++++++- .../main/java/rx/schedulers/NewThreadScheduler.java | 8 ++++++-- .../src/main/java/rx/schedulers/Schedulers.java | 6 +++--- .../main/java/rx/schedulers/TrampolineScheduler.java | 9 +++++++++ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/rxjava-core/src/main/java/rx/schedulers/ExecutorScheduler.java b/rxjava-core/src/main/java/rx/schedulers/ExecutorScheduler.java index ed5006aa22..c6f1344320 100644 --- a/rxjava-core/src/main/java/rx/schedulers/ExecutorScheduler.java +++ b/rxjava-core/src/main/java/rx/schedulers/ExecutorScheduler.java @@ -36,10 +36,20 @@ public class ExecutorScheduler extends Scheduler { private final Executor executor; + /** + * @deprecated Use Schedulers.executor(); + * @return + */ + @Deprecated public ExecutorScheduler(Executor executor) { this.executor = executor; } + /** + * @deprecated Use Schedulers.executor(); + * @return + */ + @Deprecated public ExecutorScheduler(ScheduledExecutorService executor) { this.executor = executor; } @@ -50,7 +60,6 @@ public Subscription schedule(Action1 action) { inner.schedule(action); return inner.innerSubscription; } - @Override public Subscription schedule(Action1 action, long delayTime, TimeUnit unit) { diff --git a/rxjava-core/src/main/java/rx/schedulers/ImmediateScheduler.java b/rxjava-core/src/main/java/rx/schedulers/ImmediateScheduler.java index eca1a2f5d7..e5f5ca6e8a 100644 --- a/rxjava-core/src/main/java/rx/schedulers/ImmediateScheduler.java +++ b/rxjava-core/src/main/java/rx/schedulers/ImmediateScheduler.java @@ -28,10 +28,19 @@ public final class ImmediateScheduler extends Scheduler { private static final ImmediateScheduler INSTANCE = new ImmediateScheduler(); + /** + * @deprecated Use Schedulers.immediate(); + * @return + */ + @Deprecated public static ImmediateScheduler getInstance() { return INSTANCE; } + /* package */static ImmediateScheduler instance() { + return INSTANCE; + } + /* package accessible for unit tests */ImmediateScheduler() { } @@ -49,7 +58,6 @@ public Subscription schedule(Action1 action, long delayTime, TimeUnit uni return inner.innerSubscription; } - private class InnerImmediateScheduler extends Scheduler.Inner implements Subscription { final BooleanSubscription innerSubscription = new BooleanSubscription(); diff --git a/rxjava-core/src/main/java/rx/schedulers/NewThreadScheduler.java b/rxjava-core/src/main/java/rx/schedulers/NewThreadScheduler.java index 78239b79ad..975918f292 100644 --- a/rxjava-core/src/main/java/rx/schedulers/NewThreadScheduler.java +++ b/rxjava-core/src/main/java/rx/schedulers/NewThreadScheduler.java @@ -46,12 +46,16 @@ public Thread newThread(Runnable r) { } }; + /** + * @deprecated Use Schedulers.newThread(); + * @return + */ @Deprecated public static NewThreadScheduler getInstance() { return INSTANCE; } - - /* package */ static NewThreadScheduler instance() { + + /* package */static NewThreadScheduler instance() { return INSTANCE; } diff --git a/rxjava-core/src/main/java/rx/schedulers/Schedulers.java b/rxjava-core/src/main/java/rx/schedulers/Schedulers.java index 0c607d6bed..7d2a6a1816 100644 --- a/rxjava-core/src/main/java/rx/schedulers/Schedulers.java +++ b/rxjava-core/src/main/java/rx/schedulers/Schedulers.java @@ -88,7 +88,7 @@ public Scheduler call() { * @return {@link ImmediateScheduler} instance */ public static Scheduler immediate() { - return ImmediateScheduler.getInstance(); + return ImmediateScheduler.instance(); } /** @@ -99,7 +99,7 @@ public static Scheduler immediate() { */ @Deprecated public static Scheduler currentThread() { - return TrampolineScheduler.getInstance(); + return TrampolineScheduler.instance(); } /** @@ -108,7 +108,7 @@ public static Scheduler currentThread() { * @return {@link TrampolineScheduler} instance */ public static Scheduler trampoline() { - return TrampolineScheduler.getInstance(); + return TrampolineScheduler.instance(); } /** diff --git a/rxjava-core/src/main/java/rx/schedulers/TrampolineScheduler.java b/rxjava-core/src/main/java/rx/schedulers/TrampolineScheduler.java index 2539fe4d4a..314a43dc7d 100644 --- a/rxjava-core/src/main/java/rx/schedulers/TrampolineScheduler.java +++ b/rxjava-core/src/main/java/rx/schedulers/TrampolineScheduler.java @@ -30,9 +30,18 @@ public class TrampolineScheduler extends Scheduler { private static final TrampolineScheduler INSTANCE = new TrampolineScheduler(); + /** + * @deprecated Use Schedulers.trampoline(); + * @return + */ + @Deprecated public static TrampolineScheduler getInstance() { return INSTANCE; } + + /* package */ static TrampolineScheduler instance() { + return INSTANCE; + } @Override public Subscription schedule(Action1 action) { From 07ce114ebf0e6a7a190f2e2d708a749dd323dcd1 Mon Sep 17 00:00:00 2001 From: Ben Christensen Date: Thu, 20 Feb 2014 09:15:59 -0800 Subject: [PATCH 2/2] Scheduler instances should be or behave like singletons - remove the Func0 factory signature for Scheduler creation. --- .../rx/plugins/RxJavaDefaultSchedulers.java | 19 +++++--- .../RxJavaDefaultSchedulersDefault.java | 16 ++++--- .../main/java/rx/schedulers/Schedulers.java | 46 +++++-------------- 3 files changed, 33 insertions(+), 48 deletions(-) diff --git a/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulers.java b/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulers.java index 067df7bc2c..5adc50e0d1 100644 --- a/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulers.java +++ b/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulers.java @@ -16,7 +16,6 @@ package rx.plugins; import rx.Scheduler; -import rx.functions.Func0; /** * Define alternate Scheduler implementations to be returned by the `Schedulers` factory methods. @@ -27,17 +26,23 @@ public abstract class RxJavaDefaultSchedulers { /** - * Factory of Scheduler to return from {@link Schedulers.computation()} or null if default should be used. + * Scheduler to return from {@link Schedulers.computation()} or null if default should be used. + * + * This instance should be or behave like a stateless singleton; */ - public abstract Func0 getComputationSchedulerFactory(); + public abstract Scheduler getComputationScheduler(); /** - * Factory of Scheduler to return from {@link Schedulers.io()} or null if default should be used. + * Scheduler to return from {@link Schedulers.io()} or null if default should be used. + * + * This instance should be or behave like a stateless singleton; */ - public abstract Func0 getIOSchedulerFactory(); + public abstract Scheduler getIOScheduler(); /** - * Factory of Scheduler to return from {@link Schedulers.newThread()} or null if default should be used. + * Scheduler to return from {@link Schedulers.newThread()} or null if default should be used. + * + * This instance should be or behave like a stateless singleton; */ - public abstract Func0 getNewThreadSchedulerFactory(); + public abstract Scheduler getNewThreadScheduler(); } diff --git a/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulersDefault.java b/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulersDefault.java index 6e7fa348eb..b386c25aa6 100644 --- a/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulersDefault.java +++ b/rxjava-core/src/main/java/rx/plugins/RxJavaDefaultSchedulersDefault.java @@ -16,7 +16,6 @@ package rx.plugins; import rx.Scheduler; -import rx.functions.Func0; /** * Default implementation of {@link RxJavaErrorHandler} that does nothing. @@ -27,20 +26,23 @@ public class RxJavaDefaultSchedulersDefault extends RxJavaDefaultSchedulers { private static RxJavaDefaultSchedulersDefault INSTANCE = new RxJavaDefaultSchedulersDefault(); - public Func0 getComputationSchedulerFactory() { - return null; + public static RxJavaDefaultSchedulers getInstance() { + return INSTANCE; } - public Func0 getIOSchedulerFactory() { + @Override + public Scheduler getComputationScheduler() { return null; } - public Func0 getNewThreadSchedulerFactory() { + @Override + public Scheduler getIOScheduler() { return null; } - public static RxJavaDefaultSchedulers getInstance() { - return INSTANCE; + @Override + public Scheduler getNewThreadScheduler() { + return null; } } diff --git a/rxjava-core/src/main/java/rx/schedulers/Schedulers.java b/rxjava-core/src/main/java/rx/schedulers/Schedulers.java index 7d2a6a1816..7bbfe61eaa 100644 --- a/rxjava-core/src/main/java/rx/schedulers/Schedulers.java +++ b/rxjava-core/src/main/java/rx/schedulers/Schedulers.java @@ -23,7 +23,6 @@ import java.util.concurrent.atomic.AtomicLong; import rx.Scheduler; -import rx.functions.Func0; import rx.plugins.RxJavaPlugins; /** @@ -31,53 +30,32 @@ */ public class Schedulers { - private final Func0 computationScheduler; - private final Func0 ioScheduler; - private final Func0 newThreadScheduler; + private final Scheduler computationScheduler; + private final Scheduler ioScheduler; + private final Scheduler newThreadScheduler; private static final Schedulers INSTANCE = new Schedulers(); private Schedulers() { - Func0 c = RxJavaPlugins.getInstance().getDefaultSchedulers().getComputationSchedulerFactory(); + Scheduler c = RxJavaPlugins.getInstance().getDefaultSchedulers().getComputationScheduler(); if (c != null) { computationScheduler = c; } else { - computationScheduler = new Func0() { - - @Override - public Scheduler call() { - return executor(createComputationExecutor()); - } - - }; + computationScheduler = executor(createComputationExecutor()); } - Func0 io = RxJavaPlugins.getInstance().getDefaultSchedulers().getIOSchedulerFactory(); + Scheduler io = RxJavaPlugins.getInstance().getDefaultSchedulers().getIOScheduler(); if (io != null) { ioScheduler = io; } else { - ioScheduler = new Func0() { - - @Override - public Scheduler call() { - return executor(createIOExecutor()); - } - - }; + ioScheduler = executor(createIOExecutor()); } - Func0 nt = RxJavaPlugins.getInstance().getDefaultSchedulers().getNewThreadSchedulerFactory(); + Scheduler nt = RxJavaPlugins.getInstance().getDefaultSchedulers().getNewThreadScheduler(); if (nt != null) { newThreadScheduler = nt; } else { - newThreadScheduler = new Func0() { - - @Override - public Scheduler call() { - return NewThreadScheduler.instance(); - } - - }; + newThreadScheduler = NewThreadScheduler.instance(); } } @@ -117,7 +95,7 @@ public static Scheduler trampoline() { * @return {@link NewThreadScheduler} instance */ public static Scheduler newThread() { - return INSTANCE.newThreadScheduler.call(); + return INSTANCE.newThreadScheduler; } /** @@ -167,7 +145,7 @@ public static Scheduler threadPoolForComputation() { * @return {@link Scheduler} for computation-bound work. */ public static Scheduler computation() { - return INSTANCE.computationScheduler.call(); + return INSTANCE.computationScheduler; } /** @@ -199,7 +177,7 @@ public static Scheduler threadPoolForIO() { * @return {@link ExecutorScheduler} for IO-bound work. */ public static Scheduler io() { - return INSTANCE.ioScheduler.call(); + return INSTANCE.ioScheduler; } private static ScheduledExecutorService createComputationExecutor() {