OperationConditionals { public static <K, R> OnSubscribeFunc<R> switchCase( Func0<? extends K> caseSelector, Map<? super K, ? extends Observable<? extends R>> mapOfCases, Observable<? extends R> defaultCase) { return new SwitchCase<K, R>(caseSelector, mapOfCases, defaultCase); } private  OperationConditionals(); static OnSubscribeFunc<R> switchCase(
            Func0<? extends K> caseSelector,
            Map<? super K, ? extends Observable<? extends R>> mapOfCases,
            Observable<? extends R> defaultCase); static OnSubscribeFunc<R> ifThen(
            Func0<Boolean> condition,
            Observable<? extends R> then,
            Observable<? extends R> orElse); static OnSubscribeFunc<T> doWhile(Observable<? extends T> source, Func0<Boolean> postCondition); static OnSubscribeFunc<T> whileDo(Observable<? extends T> source, Func0<Boolean> preCondition);  }
@Test public void testSimple() { Observable<Integer> source1 = Observable.from(1, 2, 3); Observable<Integer> source2 = Observable.from(4, 5, 6); Map<Integer, Observable<Integer>> map = new HashMap<Integer, Observable<Integer>>(); map.put(1, source1); map.put(2, source2); Observable<Integer> result = Statement.switchCase(func, map); observe(result, 1, 2, 3); observe(result, 4, 5, 6); observe(result); }
@Test public void testDefaultCase() { Observable<Integer> source1 = Observable.from(1, 2, 3); Observable<Integer> source2 = Observable.from(4, 5, 6); Map<Integer, Observable<Integer>> map = new HashMap<Integer, Observable<Integer>>(); map.put(1, source1); Observable<Integer> result = Statement.switchCase(func, map, source2); observe(result, 1, 2, 3); observe(result, 4, 5, 6); observe(result, 4, 5, 6); }
@Test public void testCaseSelectorThrows() { Observable<Integer> source1 = Observable.from(1, 2, 3); Map<Integer, Observable<Integer>> map = new HashMap<Integer, Observable<Integer>>(); map.put(1, source1); Observable<Integer> result = Statement.switchCase(funcError, map); observe(result, 1, 2, 3); observeError(result, RuntimeException.class); }
@Test public void testMapGetThrows() { Observable<Integer> source1 = Observable.from(1, 2, 3); Observable<Integer> source2 = Observable.from(4, 5, 6); Map<Integer, Observable<Integer>> map = new HashMap<Integer, Observable<Integer>>() { @Override public Observable<Integer> get(Object key) { if (key.equals(2)) { throw new RuntimeException("Forced failure!"); } return super.get(key); } }; map.put(1, source1); map.put(2, source2); Observable<Integer> result = Statement.switchCase(func, map); observe(result, 1, 2, 3); observeError(result, RuntimeException.class); }
@Test public void testMapContainsKeyThrows() { Observable<Integer> source1 = Observable.from(1, 2, 3); Map<Integer, Observable<Integer>> map = new HashMap<Integer, Observable<Integer>>() { @Override public boolean containsKey(Object key) { if (key.equals(2)) { throw new RuntimeException("Forced failure!"); } return super.containsKey(key); } }; map.put(1, source1); Observable<Integer> result = Statement.switchCase(func, map); observe(result, 1, 2, 3); observeError(result, RuntimeException.class); }
@Test public void testChosenObservableThrows() { Observable<Integer> source1 = Observable.from(1, 2, 3); Observable<Integer> source2 = Observable.error(new RuntimeException("Forced failure")); Map<Integer, Observable<Integer>> map = new HashMap<Integer, Observable<Integer>>(); map.put(1, source1); map.put(2, source2); Observable<Integer> result = Statement.switchCase(func, map); observe(result, 1, 2, 3); observeError(result, RuntimeException.class); }