|
| 1 | +package rx; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | + |
| 5 | +import rx.operators.OperationConditionals; |
| 6 | +import rx.util.functions.Func0; |
| 7 | + |
| 8 | +/** |
| 9 | + * Imperative statements expressed as Observable operators. |
| 10 | + */ |
| 11 | +public class Statement { |
| 12 | + |
| 13 | + /** |
| 14 | + * Return a particular one of several possible Observables based on a case |
| 15 | + * selector. |
| 16 | + * <p> |
| 17 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/switchCase.png"> |
| 18 | + * |
| 19 | + * @param <K> |
| 20 | + * the case key type |
| 21 | + * @param <R> |
| 22 | + * the result value type |
| 23 | + * @param caseSelector |
| 24 | + * the function that produces a case key when an |
| 25 | + * Observer subscribes |
| 26 | + * @param mapOfCases |
| 27 | + * a map that maps a case key to an Observable |
| 28 | + * @return a particular Observable chosen by key from the map of |
| 29 | + * Observables, or an empty Observable if no Observable matches the |
| 30 | + * key |
| 31 | + */ |
| 32 | + public static <K, R> Observable<R> switchCase(Func0<? extends K> caseSelector, |
| 33 | + Map<? super K, ? extends Observable<? extends R>> mapOfCases) { |
| 34 | + return switchCase(caseSelector, mapOfCases, Observable.<R> empty()); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Return a particular one of several possible Observables based on a case |
| 39 | + * selector and run it on the designated scheduler. |
| 40 | + * <p> |
| 41 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/switchCase.s.png"> |
| 42 | + * |
| 43 | + * @param <K> |
| 44 | + * the case key type |
| 45 | + * @param <R> |
| 46 | + * the result value type |
| 47 | + * @param caseSelector |
| 48 | + * the function that produces a case key when an |
| 49 | + * Observer subscribes |
| 50 | + * @param mapOfCases |
| 51 | + * a map that maps a case key to an Observable |
| 52 | + * @param scheduler |
| 53 | + * the scheduler where the empty observable is observed |
| 54 | + * @return a particular Observable chosen by key from the map of |
| 55 | + * Observables, or an empty Observable if no Observable matches the |
| 56 | + * key, but one that runs on the designated scheduler in either case |
| 57 | + */ |
| 58 | + public static <K, R> Observable<R> switchCase(Func0<? extends K> caseSelector, |
| 59 | + Map<? super K, ? extends Observable<? extends R>> mapOfCases, Scheduler scheduler) { |
| 60 | + return switchCase(caseSelector, mapOfCases, Observable.<R> empty(scheduler)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Return a particular one of several possible Observables based on a case |
| 65 | + * selector, or a default Observable if the case selector does not map to |
| 66 | + * a particular one. |
| 67 | + * <p> |
| 68 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/switchCase.png"> |
| 69 | + * |
| 70 | + * @param <K> |
| 71 | + * the case key type |
| 72 | + * @param <R> |
| 73 | + * the result value type |
| 74 | + * @param caseSelector |
| 75 | + * the function that produces a case key when an |
| 76 | + * Observer subscribes |
| 77 | + * @param mapOfCases |
| 78 | + * a map that maps a case key to an Observable |
| 79 | + * @param defaultCase |
| 80 | + * the default Observable if the {@code mapOfCases} doesn't contain a value for the key returned by the {@case caseSelector} |
| 81 | + * @return a particular Observable chosen by key from the map of |
| 82 | + * Observables, or the default case if no Observable matches the key |
| 83 | + */ |
| 84 | + public static <K, R> Observable<R> switchCase(Func0<? extends K> caseSelector, |
| 85 | + Map<? super K, ? extends Observable<? extends R>> mapOfCases, |
| 86 | + Observable<? extends R> defaultCase) { |
| 87 | + return Observable.create(OperationConditionals.switchCase(caseSelector, mapOfCases, defaultCase)); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Return an Observable that replays the emissions from the source |
| 92 | + * Observable, and then continues to replay them so long as a condtion is |
| 93 | + * true. |
| 94 | + * <p> |
| 95 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/doWhile.png"> |
| 96 | + * |
| 97 | + * @param postCondition |
| 98 | + * the post condition to test after the source |
| 99 | + * Observable completes |
| 100 | + * @return an Observable that replays the emissions from the source |
| 101 | + * Observable, and then continues to replay them so long as the post |
| 102 | + * condition is true |
| 103 | + */ |
| 104 | + public static <T> Observable<T> doWhile(Observable<T> source, Func0<Boolean> postCondition) { |
| 105 | + return Observable.create(OperationConditionals.doWhile(source, postCondition)); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Return an Observable that replays the emissions from the source |
| 110 | + * Observable so long as a condtion is true. |
| 111 | + * <p> |
| 112 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/whileDo.png"> |
| 113 | + * |
| 114 | + * @param preCondition |
| 115 | + * the condition to evaluate before subscribing to or |
| 116 | + * replaying the source Observable |
| 117 | + * @return an Observable that replays the emissions from the source |
| 118 | + * Observable so long as <code>preCondition</code> is true |
| 119 | + */ |
| 120 | + public static <T> Observable<T> whileDo(Observable<T> source, Func0<Boolean> preCondition) { |
| 121 | + return Observable.create(OperationConditionals.whileDo(source, preCondition)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Return an Observable that emits the emissions from a specified Observable |
| 126 | + * if a condition evaluates to true, otherwise return an empty Observable. |
| 127 | + * <p> |
| 128 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/ifThen.png"> |
| 129 | + * |
| 130 | + * @param <R> |
| 131 | + * the result value type |
| 132 | + * @param condition |
| 133 | + * the condition that decides whether to emit the emissions |
| 134 | + * from the <code>then</code> Observable |
| 135 | + * @param then |
| 136 | + * the Observable sequence to emit to if {@code condition} is {@code true} |
| 137 | + * @return an Observable that mimics the {@code then} Observable if the {@code condition} function evaluates to true, or an empty |
| 138 | + * Observable otherwise |
| 139 | + */ |
| 140 | + public static <R> Observable<R> ifThen(Func0<Boolean> condition, Observable<? extends R> then) { |
| 141 | + return ifThen(condition, then, Observable.<R> empty()); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Return an Observable that emits the emissions from a specified Observable |
| 146 | + * if a condition evaluates to true, otherwise return an empty Observable |
| 147 | + * that runs on a specified Scheduler. |
| 148 | + * <p> |
| 149 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/ifThen.s.png"> |
| 150 | + * |
| 151 | + * @param <R> |
| 152 | + * the result value type |
| 153 | + * @param condition |
| 154 | + * the condition that decides whether to emit the emissions |
| 155 | + * from the <code>then</code> Observable |
| 156 | + * @param then |
| 157 | + * the Observable sequence to emit to if {@code condition} is {@code true} |
| 158 | + * @param scheduler |
| 159 | + * the Scheduler on which the empty Observable runs if the |
| 160 | + * in case the condition returns false |
| 161 | + * @return an Observable that mimics the {@code then} Observable if the {@code condition} function evaluates to true, or an empty |
| 162 | + * Observable running on the specified Scheduler otherwise |
| 163 | + */ |
| 164 | + public static <R> Observable<R> ifThen(Func0<Boolean> condition, Observable<? extends R> then, Scheduler scheduler) { |
| 165 | + return ifThen(condition, then, Observable.<R> empty(scheduler)); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Return an Observable that emits the emissions from one specified |
| 170 | + * Observable if a condition evaluates to true, or from another specified |
| 171 | + * Observable otherwise. |
| 172 | + * <p> |
| 173 | + * <img width="640" src="https://raw.github.com/wiki/Netflix/RxJava/images/rx-operators/ifThen.e.png"> |
| 174 | + * |
| 175 | + * @param <R> |
| 176 | + * the result value type |
| 177 | + * @param condition |
| 178 | + * the condition that decides which Observable to emit the |
| 179 | + * emissions from |
| 180 | + * @param then |
| 181 | + * the Observable sequence to emit to if {@code condition} is {@code true} |
| 182 | + * @param orElse |
| 183 | + * the Observable sequence to emit to if {@code condition} is {@code false} |
| 184 | + * @return an Observable that mimics either the {@code then} or {@code orElse} Observables depending on a condition function |
| 185 | + */ |
| 186 | + public static <R> Observable<R> ifThen(Func0<Boolean> condition, Observable<? extends R> then, |
| 187 | + Observable<? extends R> orElse) { |
| 188 | + return Observable.create(OperationConditionals.ifThen(condition, then, orElse)); |
| 189 | + } |
| 190 | + |
| 191 | +} |
0 commit comments