/** * Create an {@link SubtypeEffectHandlerBuilder} for handling effects based on their type. * * @param <F> the effect type * @param <E> the event type */ public static <F, E> SubtypeEffectHandlerBuilder<F, E> subtypeEffectHandler() { return new SubtypeEffectHandlerBuilder<>(); }
/** * Creates an {@link ObservableTransformer} that will flatten the provided {@link Action} into the * stream as a {@link Completable} every time it receives an effect from the upstream effects * observable. This will result in calling the provided Action every time an effect is dispatched * to the created effect transformer. * * @param doEffect the {@link Action} to be run every time the effect is requested * @param <F> the type of Effect this transformer handles * @param <E> these transformers are for effects that do not result in any events; however, they * still need to share the same Event type * @return an {@link ObservableTransformer} that can be used with a {@link * SubtypeEffectHandlerBuilder}. */ static <F, E> ObservableTransformer<F, E> fromAction(final Action doEffect) { return fromAction(doEffect, null); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MobiusEffectRouterTest.BEvent) { MobiusEffectRouterTest.BEvent that = (MobiusEffectRouterTest.BEvent) o; return (this.id == that.id()); } return false; }
@Before public void setUp() throws Exception { cConsumer = new TestConsumer<>(); dAction = new TestAction(); ObservableTransformer<TestEffect, TestEvent> router = RxMobius.<TestEffect, TestEvent>subtypeEffectHandler() .addTransformer(A.class, (Observable<A> as) -> as.map(a -> AEvent.create(a.id()))) .addTransformer(B.class, (Observable<B> bs) -> bs.map(b -> BEvent.create(b.id()))) .addConsumer(C.class, cConsumer) .addAction(D.class, dAction) .addFunction(E.class, e -> AEvent.create(e.id())) .build(); publishSubject = PublishSubject.create(); testSubscriber = TestObserver.create(); publishSubject.compose(router).subscribe(testSubscriber); }
@Test public void shouldReportEffectClassCollisionWhenAddingSuperclass() throws Exception { SubtypeEffectHandlerBuilder<TestEffect, TestEvent> builder = RxMobius.<TestEffect, TestEvent>subtypeEffectHandler() .addTransformer(Child.class, childObservable -> null); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("collision"); builder.addTransformer(Parent.class, parentObservable -> null); }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MobiusEffectRouterTest.C) { MobiusEffectRouterTest.C that = (MobiusEffectRouterTest.C) o; return (this.id == that.id()); } return false; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MobiusEffectRouterTest.A) { MobiusEffectRouterTest.A that = (MobiusEffectRouterTest.A) o; return (this.id == that.id()); } return false; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MobiusEffectRouterTest.AEvent) { MobiusEffectRouterTest.AEvent that = (MobiusEffectRouterTest.AEvent) o; return (this.id == that.id()); } return false; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MobiusEffectRouterTest.E) { MobiusEffectRouterTest.E that = (MobiusEffectRouterTest.E) o; return (this.id == that.id()); } return false; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MobiusEffectRouterTest.D) { MobiusEffectRouterTest.D that = (MobiusEffectRouterTest.D) o; return (this.id == that.id()); } return false; }
@Override public boolean equals(Object o) { if (o == this) { return true; } if (o instanceof MobiusEffectRouterTest.B) { MobiusEffectRouterTest.B that = (MobiusEffectRouterTest.B) o; return (this.id == that.id()); } return false; }
static A create(int id) { return new AutoValue_MobiusEffectRouterTest_A(id); } }
static E create(int id) { return new AutoValue_MobiusEffectRouterTest_E(id); } }
static AEvent create(int id) { return new AutoValue_MobiusEffectRouterTest_AEvent(id); } }
static BEvent create(int id) { return new AutoValue_MobiusEffectRouterTest_BEvent(id); } }
static B create(int id) { return new AutoValue_MobiusEffectRouterTest_B(id); } }
static C create(int id) { return new AutoValue_MobiusEffectRouterTest_C(id); } }
static D create(int id) { return new AutoValue_MobiusEffectRouterTest_D(id); } }
static Unhandled create() { return new AutoValue_MobiusEffectRouterTest_Unhandled(); } }
@Test public void shouldReportEffectClassCollisionWhenAddingSubclass() throws Exception { SubtypeEffectHandlerBuilder<TestEffect, TestEvent> builder = RxMobius.<TestEffect, TestEvent>subtypeEffectHandler() .addTransformer(Parent.class, observable -> null); thrown.expect(IllegalArgumentException.class); thrown.expectMessage("collision"); builder.addTransformer(Child.class, observable -> null); }