From ad33a87ea2b168847d99d4fb3d4242d87bed8a5d Mon Sep 17 00:00:00 2001 From: Harshit Bangar Date: Sat, 21 Oct 2017 14:37:45 +0530 Subject: [PATCH] Renamed package, fixed javadoc and addressed other review comments --- .../LifeCycleProviderImpl.java | 52 ------------------- .../build.gradle | 0 .../gradle.properties | 2 +- .../rxlifecycle/RXLifecycleInterop.java | 30 +++++------ .../rxlifecycle}/package-info.java | 5 +- .../rxlifecycle/RXLifecycleInteropTest.java | 42 +++++++-------- .../rxlifecycle/TestLifecycleProvider.java | 52 +++++++++++++++++++ settings.gradle | 2 +- 8 files changed, 92 insertions(+), 93 deletions(-) delete mode 100644 autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/LifeCycleProviderImpl.java rename {autodispose-rxlifecycle-interop => autodispose-rxlifecycle}/build.gradle (100%) rename {autodispose-rxlifecycle-interop => autodispose-rxlifecycle}/gradle.properties (55%) rename autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInterop.java => autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInterop.java (52%) rename {autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop => autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle}/package-info.java (74%) rename autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInteropTest.java => autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInteropTest.java (72%) create mode 100644 autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/TestLifecycleProvider.java diff --git a/autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/LifeCycleProviderImpl.java b/autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/LifeCycleProviderImpl.java deleted file mode 100644 index 44ae32c50..000000000 --- a/autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/LifeCycleProviderImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.ubercab.autodispose.rxlifecycleinterop; - -import com.trello.rxlifecycle2.LifecycleProvider; -import com.trello.rxlifecycle2.LifecycleTransformer; -import com.trello.rxlifecycle2.OutsideLifecycleException; -import com.trello.rxlifecycle2.RxLifecycle; -import io.reactivex.Observable; -import io.reactivex.functions.Function; -import io.reactivex.subjects.BehaviorSubject; -import javax.annotation.Nonnull; - -final class LifeCycleProviderImpl implements LifecycleProvider { - - private static final Function CORRESPONDING_EVENTS = new Function() { - @Override public Event apply(Event event) - throws Exception { - switch (event) { - case CREATE: - return Event.DESTROY; - default: - throw new OutsideLifecycleException("Life cycle ended"); - } - } - }; - private final BehaviorSubject behaviorSubject = BehaviorSubject.create(); - - @Nonnull @Override public Observable lifecycle() { - return behaviorSubject.hide(); - } - - @Nonnull @Override - public LifecycleTransformer bindUntilEvent(@Nonnull Event event) { - return RxLifecycle.bindUntilEvent(behaviorSubject, event); - } - - @Nonnull @Override public LifecycleTransformer bindToLifecycle() { - return RxLifecycle.bind(behaviorSubject, CORRESPONDING_EVENTS); - } - - void onCreate() { - behaviorSubject.onNext(Event.CREATE); - } - - void onDestroy() { - behaviorSubject.onNext(Event.DESTROY); - } - - enum Event { - CREATE, - DESTROY - } -} diff --git a/autodispose-rxlifecycle-interop/build.gradle b/autodispose-rxlifecycle/build.gradle similarity index 100% rename from autodispose-rxlifecycle-interop/build.gradle rename to autodispose-rxlifecycle/build.gradle diff --git a/autodispose-rxlifecycle-interop/gradle.properties b/autodispose-rxlifecycle/gradle.properties similarity index 55% rename from autodispose-rxlifecycle-interop/gradle.properties rename to autodispose-rxlifecycle/gradle.properties index 860728240..b80f21a53 100755 --- a/autodispose-rxlifecycle-interop/gradle.properties +++ b/autodispose-rxlifecycle/gradle.properties @@ -1,3 +1,3 @@ POM_NAME=AutoDispose (RXLifeCycle Interop) -POM_ARTIFACT_ID=autodispose-rxlifecycle-interop +POM_ARTIFACT_ID=autodispose-rxlifecycle POM_PACKAGING=jar diff --git a/autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInterop.java b/autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInterop.java similarity index 52% rename from autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInterop.java rename to autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInterop.java index 1ab25e2d5..74c0b8bc1 100644 --- a/autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInterop.java +++ b/autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInterop.java @@ -1,34 +1,34 @@ -package com.ubercab.autodispose.rxlifecycleinterop; +package com.ubercab.autodispose.rxlifecycle; import com.trello.rxlifecycle2.LifecycleProvider; import com.trello.rxlifecycle2.OutsideLifecycleException; -import com.trello.rxlifecycle2.RxLifecycle; -import com.uber.autodispose.AutoDispose; import com.uber.autodispose.LifecycleEndedException; import com.uber.autodispose.ScopeProvider; import io.reactivex.Maybe; /** - * Interop class for {@link RxLifecycle}. It provides static utility methods to convert {@link - * LifecycleProvider} to {@link ScopeProvider} - *

There are several static utility converter - * methods such as {@link #bindLifecycle(LifecycleProvider)} for {@link - * LifecycleProvider#bindToLifecycle()} and {@link #bindUntilEvent(LifecycleProvider, Object)} for - * {@link LifecycleProvider#bindUntilEvent(Object)}

+ * Interop class for RxLifecycle. It provides static utility methods to convert {@link + * LifecycleProvider} to {@link ScopeProvider}. * - * Unlike {@link AutoDispose}, {@link RxLifecycle} treats the {@link OutsideLifecycleException} + *

There are several static utility converter + * methods such as {@link #fromBindLifecycle(LifecycleProvider)} for {@link + * LifecycleProvider#bindToLifecycle()} and {@link #fromBindUntilEvent(LifecycleProvider, Object)} for + * {@link LifecycleProvider#bindUntilEvent(Object)}. + *

+ * + * Note: RxLifecycle treats the {@link OutsideLifecycleException} * as normal terminal event. There is no mapping to {@link LifecycleEndedException} and in such - * cases the stream is normally disposed
+ * cases the stream is normally disposed. */ -public final class RXLifeCycleInterop { +public final class RXLifecycleInterop { - private RXLifeCycleInterop() { + private RXLifecycleInterop() { throw new AssertionError("No Instances"); } private static final Object DEFAULT_THROWAWAY_OBJECT = new Object(); - static ScopeProvider bindLifecycle(final LifecycleProvider provider) { + public static ScopeProvider fromBindLifecycle(final LifecycleProvider provider) { return new ScopeProvider() { @Override public Maybe requestScope() { return provider.lifecycle() @@ -40,7 +40,7 @@ static ScopeProvider bindLifecycle(final LifecycleProvider provider) { }; } - static ScopeProvider bindUntilEvent(final LifecycleProvider provider, final E event) { + public static ScopeProvider fromBindUntilEvent(final LifecycleProvider provider, final E event) { return new ScopeProvider() { @Override public Maybe requestScope() { return provider.lifecycle() diff --git a/autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop/package-info.java b/autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle/package-info.java similarity index 74% rename from autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop/package-info.java rename to autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle/package-info.java index 8f4d68a7e..4c713e9d0 100644 --- a/autodispose-rxlifecycle-interop/src/main/java/com/ubercab/autodispose/rxlifecycleinterop/package-info.java +++ b/autodispose-rxlifecycle/src/main/java/com/ubercab/autodispose/rxlifecycle/package-info.java @@ -15,9 +15,8 @@ */ /** - * These are Observers AutoDispose uses when scoping an observable. They are exposed as a public API - * to allow for consumers to watch for them if they want, such as in RxJava plugins. + * Interop for RxLifecycle. */ @com.uber.javaxextras.FieldsMethodsAndParametersAreNonNullByDefault -package com.ubercab.autodispose.rxlifecycleinterop; +package com.ubercab.autodispose.rxlifecycle; diff --git a/autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInteropTest.java b/autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInteropTest.java similarity index 72% rename from autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInteropTest.java rename to autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInteropTest.java index b1734a046..0bef19489 100644 --- a/autodispose-rxlifecycle-interop/src/test/java/com/ubercab/autodispose/rxlifecycleinterop/RXLifeCycleInteropTest.java +++ b/autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/RXLifecycleInteropTest.java @@ -1,36 +1,31 @@ -package com.ubercab.autodispose.rxlifecycleinterop; +package com.ubercab.autodispose.rxlifecycle; import com.uber.autodispose.AutoDispose; import com.uber.autodispose.test.RecordingObserver; import io.reactivex.disposables.Disposable; import io.reactivex.observers.TestObserver; import io.reactivex.subjects.PublishSubject; -import org.junit.Before; import org.junit.Test; import static com.google.common.truth.Truth.assertThat; -public class RXLifeCycleInteropTest { +public class RXLifecycleInteropTest { private static final RecordingObserver.Logger LOGGER = new RecordingObserver.Logger() { @Override public void log(String message) { - System.out.println(RXLifeCycleInteropTest.class.getSimpleName() + ": " + message); + System.out.println(RXLifecycleInteropTest.class.getSimpleName() + ": " + message); } }; - private LifeCycleProviderImpl lifeCycleProvider = new LifeCycleProviderImpl(); - - @Before - public void setup() { - lifeCycleProvider.onCreate(); - } + private TestLifecycleProvider lifecycleProvider = new TestLifecycleProvider(); @Test public void bindLifecycle_normalTermination_completeTheStream() { + lifecycleProvider.emitCreate(); TestObserver o = new TestObserver<>(); PublishSubject source = PublishSubject.create(); Disposable d = source.to(AutoDispose.with( - RXLifeCycleInterop.bindLifecycle(lifeCycleProvider)).forObservable()) + RXLifecycleInterop.fromBindLifecycle(lifecycleProvider)).forObservable()) .subscribeWith(o); o.assertSubscribed(); @@ -49,10 +44,11 @@ public void bindLifecycle_normalTermination_completeTheStream() { @Test public void bindLifecycle_normalTermination_unsubscribe() { + lifecycleProvider.emitCreate(); RecordingObserver o = new RecordingObserver<>(LOGGER); PublishSubject source = PublishSubject.create(); source.to(AutoDispose.with( - RXLifeCycleInterop.bindLifecycle(lifeCycleProvider)).forObservable()) + RXLifecycleInterop.fromBindLifecycle(lifecycleProvider)).forObservable()) .subscribe(o); o.takeSubscribe(); @@ -61,20 +57,21 @@ public void bindLifecycle_normalTermination_unsubscribe() { source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifeCycleProvider.onDestroy(); + lifecycleProvider.emitDestroy(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); } @Test - public void bindLifecycle_errorTermination_unsubscribe() { + public void bindLifecycle_outsideLifecycleBound_unsubscribe() { + lifecycleProvider.emitCreate(); RecordingObserver o = new RecordingObserver<>(LOGGER); PublishSubject source = PublishSubject.create(); - lifeCycleProvider.onDestroy(); + lifecycleProvider.emitDestroy(); source .to(AutoDispose.with( - RXLifeCycleInterop.bindLifecycle(lifeCycleProvider)).forObservable()) + RXLifecycleInterop.fromBindLifecycle(lifecycleProvider)).forObservable()) .subscribe(o); o.takeSubscribe(); @@ -87,10 +84,12 @@ public void bindLifecycle_errorTermination_unsubscribe() { @Test public void bindUntilEvent_normalTermination_completeTheStream() { + lifecycleProvider.emitCreate(); TestObserver o = new TestObserver<>(); PublishSubject source = PublishSubject.create(); - Disposable d = source.to(AutoDispose.with(RXLifeCycleInterop.bindUntilEvent(lifeCycleProvider, - LifeCycleProviderImpl.Event.DESTROY)).forObservable()) + Disposable d = source.to(AutoDispose.with(RXLifecycleInterop.fromBindUntilEvent( + lifecycleProvider, + TestLifecycleProvider.Event.DESTROY)).forObservable()) .subscribeWith(o); o.assertSubscribed(); @@ -109,10 +108,11 @@ public void bindUntilEvent_normalTermination_completeTheStream() { @Test public void bindUntilEvent_interruptedTermination_unsubscribe() { + lifecycleProvider.emitCreate(); RecordingObserver o = new RecordingObserver<>(LOGGER); PublishSubject source = PublishSubject.create(); - source.to(AutoDispose.with(RXLifeCycleInterop.bindUntilEvent(lifeCycleProvider, - LifeCycleProviderImpl.Event.DESTROY)).forObservable()) + source.to(AutoDispose.with(RXLifecycleInterop.fromBindUntilEvent(lifecycleProvider, + TestLifecycleProvider.Event.DESTROY)).forObservable()) .subscribe(o); o.takeSubscribe(); @@ -121,7 +121,7 @@ public void bindUntilEvent_interruptedTermination_unsubscribe() { source.onNext(1); assertThat(o.takeNext()).isEqualTo(1); - lifeCycleProvider.onDestroy(); + lifecycleProvider.emitDestroy(); source.onNext(2); o.assertNoMoreEvents(); assertThat(source.hasObservers()).isFalse(); diff --git a/autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/TestLifecycleProvider.java b/autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/TestLifecycleProvider.java new file mode 100644 index 000000000..281146ea2 --- /dev/null +++ b/autodispose-rxlifecycle/src/test/java/com/ubercab/autodispose/rxlifecycle/TestLifecycleProvider.java @@ -0,0 +1,52 @@ +package com.ubercab.autodispose.rxlifecycle; + +import com.trello.rxlifecycle2.LifecycleProvider; +import com.trello.rxlifecycle2.LifecycleTransformer; +import com.trello.rxlifecycle2.OutsideLifecycleException; +import com.trello.rxlifecycle2.RxLifecycle; +import io.reactivex.Observable; +import io.reactivex.functions.Function; +import io.reactivex.subjects.BehaviorSubject; + +final class TestLifecycleProvider implements LifecycleProvider { + + private static final Function CORRESPONDING_EVENTS = new Function() { + @Override public Event apply(Event event) + throws Exception { + switch (event) { + case CREATE: + return Event.DESTROY; + default: + throw new OutsideLifecycleException("Lifecycle ended"); + } + } + }; + + private final BehaviorSubject lifecycle = BehaviorSubject.create(); + + @Override public Observable lifecycle() { + return lifecycle.hide(); + } + + @Override + public LifecycleTransformer bindUntilEvent(Event event) { + return RxLifecycle.bindUntilEvent(lifecycle, event); + } + + @Override public LifecycleTransformer bindToLifecycle() { + return RxLifecycle.bind(lifecycle, CORRESPONDING_EVENTS); + } + + void emitCreate() { + lifecycle.onNext(Event.CREATE); + } + + void emitDestroy() { + lifecycle.onNext(Event.DESTROY); + } + + enum Event { + CREATE, + DESTROY + } +} diff --git a/settings.gradle b/settings.gradle index 90bac0b39..a74b395c8 100755 --- a/settings.gradle +++ b/settings.gradle @@ -23,7 +23,7 @@ include ':android:autodispose-android-archcomponents-test' include ':android:autodispose-android-archcomponents-test-kotlin' include ':autodispose' include ':autodispose-kotlin' +include 'autodispose-rxlifecycle' include ':sample' include ':test-utils' -include 'autodispose-rxlifecycle-interop'