├── RxCondition ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── safframework │ └── rxcondition │ ├── FlowableIfThen.java │ ├── MaybeIfThen.java │ ├── CompletableIfThen.java │ ├── ObservableIfThen.java │ ├── CompletableSwitchCase.java │ ├── SingleIfThen.java │ ├── MaybeSwitchCase.java │ ├── SingleSwitchCase.java │ ├── FlowableSwitchCase.java │ ├── ObservableSwitchCase.java │ └── Statement.java ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── README.md └── LICENSE /RxCondition/.gitignore: -------------------------------------------------------------------------------- 1 | # Java class files 2 | build/ 3 | 4 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'rxcondition' 2 | include 'RxCondition' 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fengzhizi715/RxConditions/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Java class files 2 | *.class 3 | 4 | .idea 5 | .DS_Store 6 | 7 | .gradle 8 | build/ 9 | gradlew 10 | gradlew.bat 11 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /RxCondition/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'java' 2 | apply plugin: 'com.novoda.bintray-release' 3 | 4 | sourceCompatibility = 1.8 5 | 6 | repositories { 7 | mavenCentral() 8 | } 9 | 10 | dependencies { 11 | testCompile group: 'junit', name: 'junit', version: '4.11' 12 | 13 | compile "io.reactivex.rxjava2:rxjava:2.1.3" 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | //加上这些 21 | tasks.withType(Javadoc) { 22 | options{ encoding "UTF-8" 23 | charSet 'UTF-8' 24 | links "http://docs.oracle.com/javase/7/docs/api" 25 | } 26 | } 27 | } 28 | 29 | publish{ 30 | userOrg = 'fengzhizi715' 31 | groupId = 'tony-common' 32 | artifactId = 'rxconditions' 33 | publishVersion = '1.1.0' 34 | desc = 'this is a computational expressions library use rxjava2' 35 | website = 'https://github.com/fengzhizi715/RxCondition' 36 | } -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/FlowableIfThen.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.functions.BooleanSupplier; 5 | import io.reactivex.internal.subscriptions.EmptySubscription; 6 | import org.reactivestreams.Publisher; 7 | import org.reactivestreams.Subscriber; 8 | 9 | /** 10 | * Created by Tony Shen on 2017/5/9. 11 | */ 12 | 13 | final class FlowableIfThen extends Flowable { 14 | 15 | final BooleanSupplier condition; 16 | 17 | final Publisher then; 18 | 19 | final Publisher orElse; 20 | 21 | FlowableIfThen(BooleanSupplier condition, Publisher then, 22 | Publisher orElse) { 23 | this.condition = condition; 24 | this.then = then; 25 | this.orElse = orElse; 26 | } 27 | 28 | @Override 29 | protected void subscribeActual(Subscriber s) { 30 | boolean b; 31 | 32 | try { 33 | b = condition.getAsBoolean(); 34 | } catch (Throwable ex) { 35 | EmptySubscription.error(ex, s); 36 | return; 37 | } 38 | 39 | if (b) { 40 | then.subscribe(s); 41 | } else { 42 | orElse.subscribe(s); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/MaybeIfThen.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.Maybe; 4 | import io.reactivex.MaybeObserver; 5 | import io.reactivex.MaybeSource; 6 | import io.reactivex.ObservableSource; 7 | import io.reactivex.functions.BooleanSupplier; 8 | import io.reactivex.internal.disposables.EmptyDisposable; 9 | 10 | /** 11 | * Created by tony on 2017/9/12. 12 | */ 13 | final class MaybeIfThen extends Maybe{ 14 | 15 | final BooleanSupplier condition; 16 | 17 | final MaybeSource then; 18 | 19 | final MaybeSource orElse; 20 | 21 | MaybeIfThen(BooleanSupplier condition, MaybeSource then, 22 | MaybeSource orElse) { 23 | this.condition = condition; 24 | this.then = then; 25 | this.orElse = orElse; 26 | } 27 | 28 | @Override 29 | protected void subscribeActual(MaybeObserver observer) { 30 | boolean b; 31 | 32 | try { 33 | b = condition.getAsBoolean(); 34 | } catch (Throwable ex) { 35 | EmptyDisposable.error(ex, observer); 36 | return; 37 | } 38 | 39 | if (b) { 40 | then.subscribe(observer); 41 | } else { 42 | orElse.subscribe(observer); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/CompletableIfThen.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.Completable; 4 | import io.reactivex.CompletableObserver; 5 | import io.reactivex.CompletableSource; 6 | import io.reactivex.MaybeSource; 7 | import io.reactivex.functions.BooleanSupplier; 8 | import io.reactivex.internal.disposables.EmptyDisposable; 9 | 10 | /** 11 | * Created by tony on 2017/9/14. 12 | */ 13 | final class CompletableIfThen extends Completable { 14 | 15 | final BooleanSupplier condition; 16 | 17 | final CompletableSource then; 18 | 19 | final CompletableSource orElse; 20 | 21 | CompletableIfThen(BooleanSupplier condition, CompletableSource then, 22 | CompletableSource orElse) { 23 | this.condition = condition; 24 | this.then = then; 25 | this.orElse = orElse; 26 | } 27 | 28 | @Override 29 | protected void subscribeActual(CompletableObserver observer) { 30 | boolean b; 31 | 32 | try { 33 | b = condition.getAsBoolean(); 34 | } catch (Throwable ex) { 35 | EmptyDisposable.error(ex, observer); 36 | return; 37 | } 38 | 39 | if (b) { 40 | then.subscribe(observer); 41 | } else { 42 | orElse.subscribe(observer); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/ObservableIfThen.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.ObservableSource; 5 | import io.reactivex.Observer; 6 | import io.reactivex.functions.BooleanSupplier; 7 | import io.reactivex.internal.disposables.EmptyDisposable; 8 | 9 | /** 10 | * Created by Tony Shen on 2017/5/9. 11 | */ 12 | 13 | final class ObservableIfThen extends Observable { 14 | 15 | final BooleanSupplier condition; 16 | 17 | final ObservableSource then; 18 | 19 | final ObservableSource orElse; 20 | 21 | ObservableIfThen(BooleanSupplier condition, ObservableSource then, 22 | ObservableSource orElse) { 23 | this.condition = condition; 24 | this.then = then; 25 | this.orElse = orElse; 26 | } 27 | 28 | @Override 29 | protected void subscribeActual(Observer observer) { 30 | boolean b; 31 | 32 | try { 33 | b = condition.getAsBoolean(); 34 | } catch (Throwable ex) { 35 | EmptyDisposable.error(ex, observer); 36 | return; 37 | } 38 | 39 | if (b) { 40 | then.subscribe(observer); 41 | } else { 42 | orElse.subscribe(observer); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/CompletableSwitchCase.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.*; 4 | import io.reactivex.internal.disposables.EmptyDisposable; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.Callable; 8 | 9 | /** 10 | * Created by tony on 2017/9/14. 11 | */ 12 | final class CompletableSwitchCase extends Completable { 13 | 14 | final Callable caseSelector; 15 | 16 | final Map mapOfCases; 17 | 18 | final CompletableSource defaultCase; 19 | 20 | CompletableSwitchCase(Callable caseSelector, 21 | Map mapOfCases, 22 | CompletableSource defaultCase) { 23 | this.caseSelector = caseSelector; 24 | this.mapOfCases = mapOfCases; 25 | this.defaultCase = defaultCase; 26 | } 27 | 28 | @Override 29 | protected void subscribeActual(CompletableObserver observer) { 30 | K key; 31 | CompletableSource source; 32 | 33 | try { 34 | key = caseSelector.call(); 35 | 36 | source = mapOfCases.get(key); 37 | } catch (Throwable ex) { 38 | EmptyDisposable.error(ex, observer); 39 | return; 40 | } 41 | 42 | if (source == null) { 43 | source = defaultCase; 44 | } 45 | 46 | source.subscribe(observer); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/SingleIfThen.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.Single; 4 | import io.reactivex.SingleObserver; 5 | import io.reactivex.SingleSource; 6 | import io.reactivex.annotations.NonNull; 7 | import io.reactivex.functions.BooleanSupplier; 8 | import io.reactivex.internal.disposables.EmptyDisposable; 9 | import io.reactivex.internal.subscriptions.EmptySubscription; 10 | import org.reactivestreams.Publisher; 11 | 12 | /** 13 | * Created by tony on 2017/9/14. 14 | */ 15 | final class SingleIfThen extends Single { 16 | 17 | final BooleanSupplier condition; 18 | 19 | final SingleSource then; 20 | 21 | final SingleSource orElse; 22 | 23 | SingleIfThen(BooleanSupplier condition, SingleSource then, 24 | SingleSource orElse) { 25 | this.condition = condition; 26 | this.then = then; 27 | this.orElse = orElse; 28 | } 29 | 30 | @Override 31 | protected void subscribeActual(@NonNull SingleObserver observer) { 32 | boolean b; 33 | 34 | try { 35 | b = condition.getAsBoolean(); 36 | } catch (Throwable ex) { 37 | EmptyDisposable.error(ex, observer); 38 | return; 39 | } 40 | 41 | if (b) { 42 | then.subscribe(observer); 43 | } else { 44 | orElse.subscribe(observer); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/MaybeSwitchCase.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.*; 4 | import io.reactivex.internal.disposables.EmptyDisposable; 5 | 6 | import java.util.Map; 7 | import java.util.concurrent.Callable; 8 | 9 | /** 10 | * Created by tony on 2017/9/14. 11 | */ 12 | final class MaybeSwitchCase extends Maybe { 13 | 14 | final Callable caseSelector; 15 | 16 | final Map> mapOfCases; 17 | 18 | final MaybeSource defaultCase; 19 | 20 | MaybeSwitchCase(Callable caseSelector, 21 | Map> mapOfCases, 22 | MaybeSource defaultCase) { 23 | this.caseSelector = caseSelector; 24 | this.mapOfCases = mapOfCases; 25 | this.defaultCase = defaultCase; 26 | } 27 | 28 | @Override 29 | protected void subscribeActual(MaybeObserver observer) { 30 | K key; 31 | MaybeSource source; 32 | 33 | try { 34 | key = caseSelector.call(); 35 | 36 | source = mapOfCases.get(key); 37 | } catch (Throwable ex) { 38 | EmptyDisposable.error(ex, observer); 39 | return; 40 | } 41 | 42 | if (source == null) { 43 | source = defaultCase; 44 | } 45 | 46 | source.subscribe(observer); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/SingleSwitchCase.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.*; 4 | import io.reactivex.annotations.NonNull; 5 | import io.reactivex.internal.disposables.EmptyDisposable; 6 | 7 | import java.util.Map; 8 | import java.util.concurrent.Callable; 9 | 10 | /** 11 | * Created by tony on 2017/9/14. 12 | */ 13 | final class SingleSwitchCase extends Single { 14 | 15 | final Callable caseSelector; 16 | 17 | final Map> mapOfCases; 18 | 19 | final SingleSource defaultCase; 20 | 21 | SingleSwitchCase(Callable caseSelector, 22 | Map> mapOfCases, 23 | SingleSource defaultCase) { 24 | this.caseSelector = caseSelector; 25 | this.mapOfCases = mapOfCases; 26 | this.defaultCase = defaultCase; 27 | } 28 | 29 | @Override 30 | protected void subscribeActual(@NonNull SingleObserver observer) { 31 | K key; 32 | SingleSource source; 33 | 34 | try { 35 | key = caseSelector.call(); 36 | 37 | source = mapOfCases.get(key); 38 | } catch (Throwable ex) { 39 | EmptyDisposable.error(ex, observer); 40 | return; 41 | } 42 | 43 | if (source == null) { 44 | source = defaultCase; 45 | } 46 | 47 | source.subscribe(observer); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/FlowableSwitchCase.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.internal.subscriptions.EmptySubscription; 5 | import org.reactivestreams.Publisher; 6 | import org.reactivestreams.Subscriber; 7 | 8 | import java.util.Map; 9 | import java.util.concurrent.Callable; 10 | 11 | /** 12 | * Created by Tony Shen on 2017/5/9. 13 | */ 14 | 15 | final class FlowableSwitchCase extends Flowable { 16 | 17 | final Callable caseSelector; 18 | 19 | final Map> mapOfCases; 20 | 21 | final Publisher defaultCase; 22 | 23 | FlowableSwitchCase(Callable caseSelector, 24 | Map> mapOfCases, 25 | Publisher defaultCase) { 26 | this.caseSelector = caseSelector; 27 | this.mapOfCases = mapOfCases; 28 | this.defaultCase = defaultCase; 29 | } 30 | 31 | @Override 32 | protected void subscribeActual(Subscriber s) { 33 | K key; 34 | Publisher source; 35 | 36 | try { 37 | key = caseSelector.call(); 38 | source = mapOfCases.get(key); 39 | } catch (Throwable ex) { 40 | EmptySubscription.error(ex, s); 41 | return; 42 | } 43 | 44 | if (source == null) { 45 | source = defaultCase; 46 | } 47 | 48 | source.subscribe(s); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/ObservableSwitchCase.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.ObservableSource; 5 | import io.reactivex.Observer; 6 | import io.reactivex.internal.disposables.EmptyDisposable; 7 | 8 | import java.util.Map; 9 | import java.util.concurrent.Callable; 10 | 11 | /** 12 | * Created by Tony Shen on 2017/5/10. 13 | */ 14 | 15 | final class ObservableSwitchCase extends Observable { 16 | 17 | final Callable caseSelector; 18 | 19 | final Map> mapOfCases; 20 | 21 | final ObservableSource defaultCase; 22 | 23 | ObservableSwitchCase(Callable caseSelector, 24 | Map> mapOfCases, 25 | ObservableSource defaultCase) { 26 | this.caseSelector = caseSelector; 27 | this.mapOfCases = mapOfCases; 28 | this.defaultCase = defaultCase; 29 | } 30 | 31 | @Override 32 | protected void subscribeActual(Observer observer) { 33 | K key; 34 | ObservableSource source; 35 | 36 | try { 37 | key = caseSelector.call(); 38 | 39 | source = mapOfCases.get(key); 40 | } catch (Throwable ex) { 41 | EmptyDisposable.error(ex, observer); 42 | return; 43 | } 44 | 45 | if (source == null) { 46 | source = defaultCase; 47 | } 48 | 49 | source.subscribe(observer); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /RxCondition/src/main/java/com/safframework/rxcondition/Statement.java: -------------------------------------------------------------------------------- 1 | package com.safframework.rxcondition; 2 | 3 | import io.reactivex.*; 4 | import io.reactivex.functions.BooleanSupplier; 5 | import io.reactivex.plugins.RxJavaPlugins; 6 | import org.reactivestreams.Publisher; 7 | 8 | import java.util.Map; 9 | import java.util.concurrent.Callable; 10 | 11 | /** 12 | * Created by Tony Shen on 2017/5/9. 13 | */ 14 | 15 | public final class Statement { 16 | 17 | public static Observable ifThen(BooleanSupplier condition, Observable then) { 18 | return ifThen(condition, then, Observable. empty()); 19 | } 20 | 21 | public static Observable ifThen(BooleanSupplier condition, Observable then, 22 | Observable orElse) { 23 | return RxJavaPlugins.onAssembly(new ObservableIfThen(condition, then, orElse)); 24 | } 25 | 26 | public static Flowable ifThen(BooleanSupplier condition, Publisher then) { 27 | 28 | return ifThen(condition, then, Flowable.empty()); 29 | } 30 | 31 | public static Flowable ifThen(BooleanSupplier condition, Publisher then, 32 | Flowable orElse) { 33 | 34 | return RxJavaPlugins.onAssembly(new FlowableIfThen(condition, then, orElse)); 35 | } 36 | 37 | public static Maybe ifThen(BooleanSupplier condition, Maybe then) { 38 | 39 | return ifThen(condition, then, Maybe.empty()); 40 | } 41 | 42 | public static Maybe ifThen(BooleanSupplier condition, Maybe then, 43 | Maybe orElse) { 44 | 45 | return RxJavaPlugins.onAssembly(new MaybeIfThen(condition, then, orElse)); 46 | } 47 | 48 | public static Single ifThen(BooleanSupplier condition, Single then) { 49 | 50 | return ifThen(condition, then, Single.never()); 51 | } 52 | 53 | public static Single ifThen(BooleanSupplier condition, Single then, 54 | Single orElse) { 55 | 56 | return RxJavaPlugins.onAssembly(new SingleIfThen(condition, then, orElse)); 57 | } 58 | 59 | public static Completable ifThen(BooleanSupplier condition, Completable then) { 60 | 61 | return ifThen(condition, then, Completable.complete()); 62 | } 63 | 64 | public static Completable ifThen(BooleanSupplier condition, Completable then, 65 | Completable orElse) { 66 | 67 | return RxJavaPlugins.onAssembly(new CompletableIfThen(condition, then, orElse)); 68 | } 69 | 70 | public static Observable switchCase(Callable caseSelector, 71 | Map> mapOfCases, 72 | Observable defaultCase) { 73 | return RxJavaPlugins.onAssembly(new ObservableSwitchCase(caseSelector, mapOfCases, defaultCase)); 74 | } 75 | 76 | public static Flowable switchCase(Callable caseSelector, 77 | Map> mapOfCases, 78 | Publisher defaultCase) { 79 | 80 | return RxJavaPlugins.onAssembly(new FlowableSwitchCase(caseSelector, mapOfCases, defaultCase)); 81 | } 82 | 83 | public static Maybe switchCase(Callable caseSelector, 84 | Map> mapOfCases, 85 | Maybe defaultCase) { 86 | return RxJavaPlugins.onAssembly(new MaybeSwitchCase(caseSelector, mapOfCases, defaultCase)); 87 | } 88 | 89 | public static Single switchCase(Callable caseSelector, 90 | Map> mapOfCases, 91 | Single defaultCase) { 92 | return RxJavaPlugins.onAssembly(new SingleSwitchCase(caseSelector, mapOfCases, defaultCase)); 93 | } 94 | 95 | public static Completable switchCase(Callable caseSelector, 96 | Map mapOfCases, 97 | Completable defaultCase) { 98 | return RxJavaPlugins.onAssembly(new CompletableSwitchCase(caseSelector, mapOfCases, defaultCase)); 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RxCondition 2 | [![@Tony沈哲 on weibo](https://img.shields.io/badge/weibo-%40Tony%E6%B2%88%E5%93%B2-blue.svg)](http://www.weibo.com/fengzhizi715) 3 | [ ![Download](https://api.bintray.com/packages/fengzhizi715/maven/rxconditions/images/download.svg) ](https://bintray.com/fengzhizi715/maven/rxconditions/_latestVersion) 4 | [![GitHub release](https://img.shields.io/badge/release-1.1.0-red.svg)](https://github.com/fengzhizi715/RxConditions/releases) 5 | [![License](https://img.shields.io/badge/license-Apache%202-lightgrey.svg)](https://www.apache.org/licenses/LICENSE-2.0.html) 6 | 7 | 8 | 通常而言,Rx如果遇到if条件语句、switch case语句时需要先选择分支条件,然后再进行链式调用。RxCondition产生的目的就是为了在这些情况下也能顺利地使用链式调用。 9 | 10 | 我在查找RxJava的条件、布尔操作符时,没有找到符合我需求的操作符。于是,我在网上找到了[RxJavaComputationExpressions](https://github.com/ReactiveX/RxJavaComputationExpressions), 做了一些修改将RxJava1升级到RxJava2,增加了对Flowable、Maybe、Single、Completable的支持。 11 | 12 | # 下载安装 13 | 对于Android项目,Android Studio默认使用jcenter。 14 | 15 | 对于Java项目如果使用gradle构建,由于默认不是使用jcenter,需要在相应module的build.gradle中配置 16 | 17 | ```groovy 18 | repositories { 19 | mavenCentral() 20 | jcenter() 21 | } 22 | ``` 23 | 24 | Gradle: 25 | 26 | ```groovy 27 | compile 'tony-common:rxconditions:1.1.0' 28 | ``` 29 | 30 | Maven: 31 | 32 | ```groovy 33 | 34 | tony-common 35 | rxconditions 36 | 1.1.0 37 | pom 38 | 39 | ``` 40 | 41 | 42 | # 使用方法: 43 | ## 1.ifThen用法 44 | 45 | if条件语句传统的写法: 46 | ```java 47 | Observable observable = null; 48 | if (flag) { 49 | 50 | observable = Observable.create(new ObservableOnSubscribe() { 51 | @Override 52 | public void subscribe(@NonNull ObservableEmitter e) throws Exception { 53 | e.onNext("this is true"); 54 | } 55 | }); 56 | } else { 57 | observable = Observable.create(new ObservableOnSubscribe() { 58 | @Override 59 | public void subscribe(@NonNull ObservableEmitter e) throws Exception { 60 | e.onNext("this is false"); 61 | } 62 | }); 63 | } 64 | 65 | observable.subscribe(new Consumer() { 66 | @Override 67 | public void accept(@NonNull String s) throws Exception { 68 | System.out.println("s="+s); 69 | } 70 | }); 71 | ``` 72 | 73 | 使用了ifThen()以后的写法: 74 | ```java 75 | Statement.ifThen(new BooleanSupplier() { 76 | @Override 77 | public boolean getAsBoolean() throws Exception { 78 | return flag; 79 | } 80 | }, Observable.create(new ObservableOnSubscribe() { 81 | @Override 82 | public void subscribe(@NonNull ObservableEmitter e) throws Exception { 83 | e.onNext("this is true"); 84 | } 85 | }),Observable.create(new ObservableOnSubscribe() { 86 | @Override 87 | public void subscribe(@NonNull ObservableEmitter e) throws Exception { 88 | e.onNext("this is false"); 89 | } 90 | })).subscribe(new Consumer() { 91 | @Override 92 | public void accept(@NonNull String s) throws Exception { 93 | System.out.println("s="+s); 94 | } 95 | }); 96 | ``` 97 | ifThen(BooleanSupplier condition, Observable then,Observable orElse),其中第一个Observable是条件为true时执行的,第二个Observable则是条件为false时执行。 98 | 99 | 使用lambda表达式,进一步简化写法: 100 | ```java 101 | Statement.ifThen(()->{ 102 | return flag; 103 | },Observable.create((e)->{ 104 | e.onNext("this is true"); 105 | }),Observable.create((e)->{ 106 | e.onNext("this is false"); 107 | })).subscribe((Consume) (s) -> {System.out.println("s="+s)}); 108 | ``` 109 | 110 | 当然,ifThen也支持Flowable: 111 | ```java 112 | Statement.ifThen(()->{ 113 | return flag; 114 | },Flowable.just("this is true"), Flowable.just("this is false")) 115 | .subscribe((Consume) (s) -> {System.out.println("s="+s)}); 116 | ``` 117 | 118 | ## 2.switchCase用法 119 | 120 | switch case语句传统的写法: 121 | ```java 122 | Flowable flowable = null; 123 | switch(type) { 124 | case 0: 125 | flowable = Flowable.just("this is 0"); 126 | break; 127 | case 1: 128 | flowable = Flowable.just("this is 1"); 129 | break; 130 | case 2: 131 | flowable = Flowable.just("this is 2"); 132 | break; 133 | case 3: 134 | flowable = Flowable.just("this is 3"); 135 | break; 136 | 137 | default: 138 | flowable = Flowable.just("this is default"); 139 | break; 140 | } 141 | flowable.subscribe(new Consumer() { 142 | @Override 143 | public void accept(@NonNull String s) throws Exception { 144 | System.out.println("s="+s); 145 | } 146 | }); 147 | ``` 148 | 149 | 使用了switchCase()以后的写法: 150 | ```java 151 | Map> maps = new HashMap<>(); 152 | maps.put(0,Flowable.just("this is 0")); 153 | maps.put(1,Flowable.just("this is 1")); 154 | maps.put(2,Flowable.just("this is 2")); 155 | maps.put(3,Flowable.just("this is 3")); 156 | 157 | Statement.switchCase(new Callable() { 158 | 159 | @Override 160 | public Integer call() throws Exception { 161 | return type; 162 | } 163 | },maps,Flowable.just("this is default")) 164 | .subscribe(new Consumer() { 165 | @Override 166 | public void accept(@NonNull String s) throws Exception { 167 | System.out.println("s="+s); 168 | } 169 | }); 170 | ``` 171 | 首先,将各个分支情况放入maps中。 172 | 其次,switchCase()的第一个参数是caseSelector,用于返回maps的key。最后一个参数是defaultCase,相当于switch case语句中的default语句。 173 | 174 | 改成lambda表达: 175 | ```java 176 | Map> maps = new HashMap<>(); 177 | maps.put(0,Flowable.just("this is 0")); 178 | maps.put(1,Flowable.just("this is 1")); 179 | maps.put(2,Flowable.just("this is 2")); 180 | maps.put(3,Flowable.just("this is 3")); 181 | 182 | Statement.switchCase((Callable)()-> {return type;},maps,Flowable.just("this is default")) 183 | .subscribe((Consumer)(s) -> {System.out.println("s="+s);}); 184 | ``` 185 | 186 | switchCase()中,第一个参数返回的是Map中的key,它支持范型,所以switchCase()相对于switch case语句而已能够支持更多种类型。 187 | 188 | 189 | License 190 | ------- 191 | 192 | Copyright (C) 2017 Tony Shen. 193 | 194 | Licensed under the Apache License, Version 2.0 (the "License"); 195 | you may not use this file except in compliance with the License. 196 | You may obtain a copy of the License at 197 | 198 | http://www.apache.org/licenses/LICENSE-2.0 199 | 200 | Unless required by applicable law or agreed to in writing, software 201 | distributed under the License is distributed on an "AS IS" BASIS, 202 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 203 | See the License for the specific language governing permissions and 204 | limitations under the License. 205 | 206 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------