├── .gitattributes ├── .gitignore ├── Chapter01 ├── Ch1_1.java ├── Ch1_2.java └── Ch1_3.java ├── Chapter02 ├── Ch2_1.java ├── Ch2_10.java ├── Ch2_11.java ├── Ch2_12.java ├── Ch2_13.java ├── Ch2_14.java ├── Ch2_15.java ├── Ch2_16.java ├── Ch2_17.java ├── Ch2_18.java ├── Ch2_19.java ├── Ch2_2.java ├── Ch2_20.java ├── Ch2_21.java ├── Ch2_22.java ├── Ch2_23.java ├── Ch2_24.java ├── Ch2_25.java ├── Ch2_26.java ├── Ch2_27.java ├── Ch2_28.java ├── Ch2_29.java ├── Ch2_3.java ├── Ch2_30.java ├── Ch2_31.java ├── Ch2_32.java ├── Ch2_33.java ├── Ch2_34.java ├── Ch2_35.java ├── Ch2_36.java ├── Ch2_4.java ├── Ch2_5.java ├── Ch2_6.java ├── Ch2_7.java ├── Ch2_8.java └── Ch2_9.java ├── Chapter03 ├── Ch3_1.java ├── Ch3_10.java ├── Ch3_11.java ├── Ch3_12.java ├── Ch3_13.java ├── Ch3_14.java ├── Ch3_15.java ├── Ch3_16.java ├── Ch3_17.java ├── Ch3_18.java ├── Ch3_19.java ├── Ch3_2.java ├── Ch3_20.java ├── Ch3_21.java ├── Ch3_22.java ├── Ch3_23.java ├── Ch3_24.java ├── Ch3_25.java ├── Ch3_26.java ├── Ch3_27.java ├── Ch3_28.java ├── Ch3_29.java ├── Ch3_3.java ├── Ch3_30.java ├── Ch3_31.java ├── Ch3_32.java ├── Ch3_33.java ├── Ch3_34.java ├── Ch3_35.java ├── Ch3_36.java ├── Ch3_37.java ├── Ch3_38.java ├── Ch3_39.java ├── Ch3_4.java ├── Ch3_40.java ├── Ch3_41.java ├── Ch3_42.java ├── Ch3_43.java ├── Ch3_44.java ├── Ch3_45.java ├── Ch3_46.java ├── Ch3_47.java ├── Ch3_48.java ├── Ch3_49.java ├── Ch3_5.java ├── Ch3_50.java ├── Ch3_51.java ├── Ch3_52.java ├── Ch3_53.java ├── Ch3_54.java ├── Ch3_6.java ├── Ch3_7.java ├── Ch3_8.java └── Ch3_9.java ├── Chapter04 ├── Ch4_1.java ├── Ch4_10.java ├── Ch4_11.java ├── Ch4_12.java ├── Ch4_13.java ├── Ch4_14.java ├── Ch4_15.java ├── Ch4_16.java ├── Ch4_17.java ├── Ch4_18.java ├── Ch4_19.java ├── Ch4_2.java ├── Ch4_3.java ├── Ch4_4.java ├── Ch4_5.java ├── Ch4_6.java ├── Ch4_7.java ├── Ch4_8.java └── Ch4_9.java ├── Chapter05 ├── Ch5_1.java ├── Ch5_10.java ├── Ch5_11.java ├── Ch5_12.java ├── Ch5_13.java ├── Ch5_14.java ├── Ch5_15.java ├── Ch5_16.java ├── Ch5_17.java ├── Ch5_18.java ├── Ch5_19.java ├── Ch5_2.java ├── Ch5_20.java ├── Ch5_21.java ├── Ch5_22.java ├── Ch5_23.java ├── Ch5_24.java ├── Ch5_25.java ├── Ch5_26.java ├── Ch5_27.java ├── Ch5_3.java ├── Ch5_4.java ├── Ch5_5.java ├── Ch5_6.java ├── Ch5_7.java ├── Ch5_8.java └── Ch5_9.java ├── Chapter06 ├── Ch6_1.java ├── Ch6_10.java ├── Ch6_11.java ├── Ch6_12.java ├── Ch6_13.java ├── Ch6_14.java ├── Ch6_15.java ├── Ch6_16.java ├── Ch6_17.java ├── Ch6_18.java ├── Ch6_19.java ├── Ch6_2.java ├── Ch6_20.java ├── Ch6_3.java ├── Ch6_4.java ├── Ch6_5.java ├── Ch6_6.java ├── Ch6_7.java ├── Ch6_8.java └── Ch6_9.java ├── Chapter07 ├── Ch7_1.java ├── Ch7_10.java ├── Ch7_11.java ├── Ch7_12.java ├── Ch7_13.java ├── Ch7_14.java ├── Ch7_15.java ├── Ch7_16.java ├── Ch7_17.java ├── Ch7_2.java ├── Ch7_3.java ├── Ch7_4.java ├── Ch7_5.java ├── Ch7_6.java ├── Ch7_7.java ├── Ch7_8.java └── Ch7_9.java ├── Chapter08 ├── Ch8_1.java ├── Ch8_10.java ├── Ch8_11.java ├── Ch8_12.java ├── Ch8_13.java ├── Ch8_14.java ├── Ch8_15.java ├── Ch8_16.java ├── Ch8_17.java ├── Ch8_2.java ├── Ch8_3.java ├── Ch8_4.java ├── Ch8_5.java ├── Ch8_6.java ├── Ch8_7.java ├── Ch8_8.java └── Ch8_9.java ├── Chapter09 ├── Ch9_1.java ├── Ch9_10.java ├── Ch9_2.java ├── Ch9_3.java ├── Ch9_4.java ├── Ch9_5.java ├── Ch9_6.java ├── Ch9_7.java ├── Ch9_8.java └── Ch9_9.java ├── Chapter10 ├── build.gradle ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── src │ └── test │ └── java │ ├── Ch10_1.java │ ├── Ch10_10.java │ ├── Ch10_11.java │ ├── Ch10_12.java │ ├── Ch10_13.java │ ├── Ch10_14.java │ ├── Ch10_15.java │ ├── Ch10_2.java │ ├── Ch10_3.java │ ├── Ch10_4.java │ ├── Ch10_5.java │ ├── Ch10_6.java │ ├── Ch10_7.java │ ├── Ch10_8.java │ └── Ch10_9.java ├── Chapter11 ├── code1 │ ├── app │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── androidTest │ │ │ └── java │ │ │ │ └── packtpub │ │ │ │ └── com │ │ │ │ └── rxjavaapp │ │ │ │ └── ExampleInstrumentedTest.java │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ │ └── packtpub │ │ │ │ │ └── com │ │ │ │ │ └── rxjavaapp │ │ │ │ │ └── MainActivity.java │ │ │ └── res │ │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ └── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ └── test │ │ │ └── java │ │ │ └── packtpub │ │ │ └── com │ │ │ └── rxjavaapp │ │ │ └── ExampleUnitTest.java │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── local.properties │ └── settings.gradle ├── code2 │ ├── app │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── androidTest │ │ │ └── java │ │ │ │ └── packtpub │ │ │ │ └── com │ │ │ │ └── rxjavaapp │ │ │ │ └── ExampleInstrumentedTest.java │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ │ └── packtpub │ │ │ │ │ └── com │ │ │ │ │ └── rxjavaapp │ │ │ │ │ └── MainActivity.java │ │ │ └── res │ │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ └── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ └── test │ │ │ └── java │ │ │ └── packtpub │ │ │ └── com │ │ │ └── rxjavaapp │ │ │ └── ExampleUnitTest.java │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── local.properties │ └── settings.gradle ├── code3 │ ├── app │ │ ├── build.gradle │ │ ├── proguard-rules.pro │ │ └── src │ │ │ ├── androidTest │ │ │ └── java │ │ │ │ └── packtpub │ │ │ │ └── com │ │ │ │ └── rxjavaapp │ │ │ │ └── ExampleInstrumentedTest.java │ │ │ ├── main │ │ │ ├── AndroidManifest.xml │ │ │ ├── java │ │ │ │ └── packtpub │ │ │ │ │ └── com │ │ │ │ │ └── rxjavaapp │ │ │ │ │ └── MainActivity.java │ │ │ └── res │ │ │ │ ├── layout │ │ │ │ └── activity_main.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ │ └── values │ │ │ │ ├── colors.xml │ │ │ │ ├── strings.xml │ │ │ │ └── styles.xml │ │ │ └── test │ │ │ └── java │ │ │ └── packtpub │ │ │ └── com │ │ │ └── rxjavaapp │ │ │ └── ExampleUnitTest.java │ ├── build.gradle │ ├── gradle.properties │ ├── gradle │ │ └── wrapper │ │ │ ├── gradle-wrapper.jar │ │ │ └── gradle-wrapper.properties │ ├── gradlew │ ├── gradlew.bat │ ├── local.properties │ └── settings.gradle └── code4 │ └── app │ ├── build.gradle │ ├── proguard-rules.pro │ └── src │ ├── androidTest │ └── java │ │ └── packtpub │ │ └── com │ │ └── rxjavaapp │ │ └── ExampleInstrumentedTest.java │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── packtpub │ │ └── com │ │ └── rxjavaapp │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-mdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ ├── mipmap-xxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png │ └── mipmap-xxxhdpi │ ├── ic_launcher.png │ └── ic_launcher_round.png ├── Chapter12 ├── ch12_1.kt ├── ch12_10.kt ├── ch12_11.kt ├── ch12_12.kt ├── ch12_13.kt ├── ch12_14.kt ├── ch12_15.kt ├── ch12_16.kt ├── ch12_17.kt ├── ch12_18.kt ├── ch12_19.kt ├── ch12_2.kt ├── ch12_20.kt ├── ch12_21.kt ├── ch12_22.kt ├── ch12_3.kt ├── ch12_4.kt ├── ch12_5.kt ├── ch12_6.kt ├── ch12_7.kt ├── ch12_8.kt └── ch12_9.kt ├── LICENSE └── Readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | -------------------------------------------------------------------------------- /Chapter01/Ch1_1.java: -------------------------------------------------------------------------------- 1 | package ch1; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch1_1 { 6 | public static void main(String[] args) { 7 | Observable myStrings = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | myStrings.subscribe(s -> System.out.println(s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter01/Ch1_2.java: -------------------------------------------------------------------------------- 1 | package ch1; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch1_2 { 6 | public static void main(String[] args) { 7 | Observable myStrings = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | myStrings.map(s -> s.length()).subscribe(s -> 11 | System.out.println(s)); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter01/Ch1_3.java: -------------------------------------------------------------------------------- 1 | package ch1; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch1_3 { 8 | public static void main(String[] args) { 9 | Observable secondIntervals = 10 | Observable.interval(1, TimeUnit.SECONDS); 11 | secondIntervals.subscribe(s -> System.out.println(s)); 12 | /* Hold main thread for 5 secondsso Observable above has chance to fire */ 13 | sleep(5000); 14 | } 15 | 16 | public static void sleep(long millis) { 17 | try { 18 | Thread.sleep(millis); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_1.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_1 { 6 | public static void main(String[] args) { 7 | Observable source = Observable.create(emitter -> { 8 | emitter.onNext("Alpha"); 9 | emitter.onNext("Beta"); 10 | emitter.onNext("Gamma"); 11 | emitter.onNext("Delta"); 12 | emitter.onNext("Epsilon"); 13 | emitter.onComplete(); 14 | }); 15 | source.subscribe(s -> System.out.println("RECEIVED: " + s)); 16 | } 17 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_10.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_10 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | source.map(String::length).filter(i -> i >= 5) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_11.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_11 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon"); 9 | //first observer 10 | source.subscribe(s -> System.out.println("Observer 1 Received: " + s)); 11 | //second observer 12 | source.subscribe(s -> System.out.println("Observer 2 Received: " + s)); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_12.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_12 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon"); 9 | //first observer 10 | source.subscribe(s -> System.out.println("Observer 1 Received: " + s)); 11 | //second observer 12 | source.map(String::length).filter(i -> i >= 5) 13 | .subscribe(s -> System.out.println("Observer 2 Received: " + s)); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_14.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | public class Ch2_14 { 7 | public static void main(String[] args) { 8 | ConnectableObservable source = 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon") 10 | .publish(); 11 | //Set up observer 1 12 | source.subscribe(s -> System.out.println("Observer 1: " + s)); 13 | //Set up observer 2 14 | source.map(String::length) 15 | .subscribe(i -> System.out.println("Observer 2: " + i)); 16 | //Fire! 17 | source.connect(); 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_15.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_15 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 10) 8 | .subscribe(s -> System.out.println("RECEIVED: " + s)); 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_16.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_16 { 6 | public static void main(String[] args) { 7 | Observable.range(5, 10) 8 | .subscribe(s -> System.out.println("RECEIVED: " + s)); 9 | } 10 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_17.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch2_17 { 8 | public static void main(String[] args) { 9 | Observable.interval(1, TimeUnit.SECONDS) 10 | .subscribe(s -> System.out.println(s + " Mississippi")); 11 | sleep(5000); 12 | } 13 | 14 | public static void sleep(int millis) { 15 | try { 16 | Thread.sleep(millis); 17 | } catch (InterruptedException e) { 18 | e.printStackTrace(); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_18.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch2_18 { 8 | public static void main(String[] args) { 9 | Observable seconds = Observable.interval(1, 10 | TimeUnit.SECONDS); 11 | //Observer 1 12 | seconds.subscribe(l -> System.out.println("Observer 1: " + l)); 13 | //sleep 5 seconds 14 | sleep(5000); 15 | //Observer 2 16 | seconds.subscribe(l -> System.out.println("Observer 2: " + l)); 17 | //sleep 5 seconds 18 | sleep(5000); 19 | } 20 | 21 | public static void sleep(int millis) { 22 | try { 23 | Thread.sleep(millis); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_19.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch2_19 { 9 | public static void main(String[] args) { 10 | ConnectableObservable seconds = 11 | Observable.interval(1, TimeUnit.SECONDS).publish(); 12 | //observer 1 13 | seconds.subscribe(l -> System.out.println("Observer 1: " + l)); 14 | seconds.connect(); 15 | //sleep 5 seconds 16 | sleep(5000); 17 | //observer 2 18 | seconds.subscribe(l -> System.out.println("Observer 2: " + l)); 19 | //sleep 5 seconds 20 | sleep(5000); 21 | } 22 | 23 | public static void sleep(int millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_2.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_2 { 6 | public static void main(String[] args) { 7 | Observable source = Observable.create(emitter -> { 8 | try { 9 | emitter.onNext("Alpha"); 10 | emitter.onNext("Beta"); 11 | emitter.onNext("Gamma"); 12 | emitter.onNext("Delta"); 13 | emitter.onNext("Epsilon"); 14 | emitter.onComplete(); 15 | } catch (Throwable e) { 16 | emitter.onError(e); 17 | } 18 | }); 19 | source.subscribe(s -> System.out.println("RECEIVED: " + s), 20 | Throwable::printStackTrace); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_20.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_20 { 6 | public static void main(String[] args) { 7 | Observable empty = Observable.empty(); 8 | empty.subscribe(System.out::println, 9 | Throwable::printStackTrace, 10 | () -> System.out.println("Done!")); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_21.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_21 { 6 | public static void main(String[] args) { 7 | Observable empty = Observable.never(); 8 | empty.subscribe(System.out::println, 9 | Throwable::printStackTrace, 10 | () -> System.out.println("Done!")); 11 | sleep(5000); 12 | } 13 | 14 | public static void sleep(int millis) { 15 | try { 16 | Thread.sleep(millis); 17 | } catch (InterruptedException e) { 18 | e.printStackTrace(); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_22.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_22 { 6 | public static void main(String[] args) { 7 | Observable.error(new Exception("Crash and burn!")) 8 | .subscribe(i -> System.out.println("RECEIVED: " + i), 9 | Throwable::printStackTrace, 10 | () -> System.out.println("Done!")); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_23.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_23 { 6 | public static void main(String[] args) { 7 | Observable.error(() -> new Exception("Crash and burn!")) 8 | .subscribe(i -> System.out.println("RECEIVED: " + i), 9 | Throwable::printStackTrace, 10 | () -> System.out.println("Done!")); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_24.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_24 { 6 | private static int start = 1; 7 | private static int count = 5; 8 | 9 | public static void main(String[] args) { 10 | Observable source = Observable.range(start, count); 11 | source.subscribe(i -> System.out.println("Observer 1: " + i)); 12 | //modify count 13 | count = 10; 14 | source.subscribe(i -> System.out.println("Observer 2: " + i)); 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_25.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | class Ch2_25 { 6 | private static int start = 1; 7 | private static int count = 5; 8 | 9 | public static void main(String[] args) { 10 | Observable source = Observable.defer(() -> 11 | Observable.range(start, count)); 12 | source.subscribe(i -> System.out.println("Observer 1: " + i)); 13 | //modify count 14 | count = 10; 15 | source.subscribe(i -> System.out.println("Observer 2: " + i)); 16 | } 17 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_26.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_26 { 6 | public static void main(String[] args) { 7 | Observable.just(1 / 0) 8 | .subscribe(i -> System.out.println("RECEIVED: " + i), 9 | e -> System.out.println("Error Captured: " + e)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_27.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_27 { 6 | public static void main(String[] args) { 7 | Observable.fromCallable(() -> 1 / 0) 8 | .subscribe(i -> System.out.println("Received: " + i), 9 | e -> System.out.println("Error Captured: " + e)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_28.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Single; 4 | 5 | public class Ch2_28 { 6 | public static void main(String[] args) { 7 | Single.just("Hello") 8 | .map(String::length) 9 | .subscribe(System.out::println, 10 | Throwable::printStackTrace); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_29.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_29 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma"); 9 | source.first("Nil") //returns a Single 10 | .subscribe(System.out::println); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_3.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_3 { 6 | public static void main(String[] args) { 7 | Observable source = Observable.create(emitter -> { 8 | try { 9 | emitter.onNext("Alpha"); 10 | emitter.onNext("Beta"); 11 | emitter.onNext("Gamma"); 12 | emitter.onNext("Delta"); 13 | emitter.onNext("Epsilon"); 14 | emitter.onComplete(); 15 | } catch (Throwable e) { 16 | emitter.onError(e); 17 | } 18 | }); 19 | Observable lengths = source.map(String::length); 20 | Observable filtered = lengths.filter(i -> i >= 5); 21 | filtered.subscribe(s -> System.out.println("RECEIVED: " + 22 | s)); 23 | } 24 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_30.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Maybe; 4 | 5 | public class Ch2_30 { 6 | public static void main(String[] args) { 7 | // has emission 8 | Maybe presentSource = Maybe.just(100); 9 | presentSource.subscribe(s -> System.out.println("Process 1 received:" + s), 10 | Throwable::printStackTrace, 11 | () -> System.out.println("Process 1 done!")); 12 | //no emission 13 | Maybe emptySource = Maybe.empty(); 14 | emptySource.subscribe(s -> System.out.println("Process 2 received:" + s), 15 | Throwable::printStackTrace, 16 | () -> System.out.println("Process 2 done!")); 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_31.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_31 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon"); 9 | source.firstElement().subscribe( 10 | s -> System.out.println("RECEIVED " + s), 11 | Throwable::printStackTrace, 12 | () -> System.out.println("Done!")); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_32.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Completable; 4 | 5 | public class Ch2_32 { 6 | public static void main(String[] args) { 7 | Completable.fromRunnable(() -> runProcess()) 8 | .subscribe(() -> System.out.println("Done!")); 9 | } 10 | 11 | public static void runProcess() { 12 | //run process here 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_33.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.disposables.Disposable; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch2_33 { 9 | public static void main(String[] args) { 10 | Observable seconds = 11 | Observable.interval(1, TimeUnit.SECONDS); 12 | Disposable disposable = 13 | seconds.subscribe(l -> System.out.println("Received: " + l)); 14 | //sleep 5 seconds 15 | sleep(5000); 16 | //dispose and stop emissions 17 | disposable.dispose(); 18 | //sleep 5 seconds to prove 19 | //there are no more emissions 20 | sleep(5000); 21 | } 22 | 23 | public static void sleep(int millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_34.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.disposables.Disposable; 5 | import io.reactivex.observers.ResourceObserver; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class Ch2_34 { 10 | public static void main(String[] args) { 11 | Observable source = 12 | Observable.interval(1, TimeUnit.SECONDS); 13 | ResourceObserver myObserver = new 14 | ResourceObserver() { 15 | @Override 16 | public void onNext(Long value) { 17 | System.out.println(value); 18 | } 19 | 20 | @Override 21 | public void onError(Throwable e) { 22 | e.printStackTrace(); 23 | } 24 | 25 | @Override 26 | public void onComplete() { 27 | System.out.println("Done!"); 28 | } 29 | }; 30 | //capture Disposable 31 | Disposable disposable = source.subscribeWith(myObserver); 32 | } 33 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_35.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.disposables.CompositeDisposable; 5 | import io.reactivex.disposables.Disposable; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class Ch2_35 { 10 | private static final CompositeDisposable disposables 11 | = new CompositeDisposable(); 12 | 13 | public static void main(String[] args) { 14 | Observable seconds = 15 | Observable.interval(1, TimeUnit.SECONDS); 16 | //subscribe and capture disposables 17 | Disposable disposable1 = 18 | seconds.subscribe(l -> System.out.println("Observer 1: " + 19 | l)); 20 | Disposable disposable2 = 21 | seconds.subscribe(l -> System.out.println("Observer 2: " + 22 | l)); 23 | //put both disposables into CompositeDisposable 24 | disposables.addAll(disposable1, disposable2); 25 | //sleep 5 seconds 26 | sleep(5000); 27 | //dispose all disposables 28 | disposables.dispose(); 29 | //sleep 5 seconds to prove 30 | //there are no more emissions 31 | sleep(5000); 32 | } 33 | 34 | public static void sleep(int millis) { 35 | try { 36 | Thread.sleep(millis); 37 | } catch (InterruptedException e) { 38 | e.printStackTrace(); 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_36.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_36 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.create(observableEmitter -> { 9 | try { 10 | for (int i = 0; i < 1000; i++) { 11 | while (!observableEmitter.isDisposed()) { 12 | observableEmitter.onNext(i); 13 | } 14 | if (observableEmitter.isDisposed()) 15 | return; 16 | } 17 | observableEmitter.onComplete(); 18 | } catch (Throwable e) { 19 | observableEmitter.onError(e); 20 | } 21 | }); 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_4.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_4 { 6 | public static void main(String[] args) { 7 | Observable source = Observable.create(emitter -> { 8 | try { 9 | emitter.onNext("Alpha"); 10 | emitter.onNext("Beta"); 11 | emitter.onNext("Gamma"); 12 | emitter.onNext("Delta"); 13 | emitter.onNext("Epsilon"); 14 | emitter.onComplete(); 15 | } catch (Throwable e) { 16 | emitter.onError(e); 17 | } 18 | }); 19 | source.map(String::length) 20 | .filter(i -> i >= 5) 21 | .subscribe(s -> System.out.println("RECEIVED: " + s)); 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_5.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_5 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | source.map(String::length).filter(i -> i >= 5) 11 | .subscribe(s -> System.out.println("RECEIVED: " + s)); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_6.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | public class Ch2_6 { 9 | public static void main(String[] args) { 10 | List items = 11 | Arrays.asList("Alpha", "Beta", "Gamma", "Delta", "Epsilon"); 12 | Observable source = Observable.fromIterable(items); 13 | source.map(String::length).filter(i -> i >= 5) 14 | .subscribe(s -> System.out.println("RECEIVED: " + s)); 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_7.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.Observer; 5 | import io.reactivex.disposables.Disposable; 6 | 7 | public class Ch2_7 { 8 | public static void main(String[] args) { 9 | Observable source = 10 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 11 | "Epsilon"); 12 | Observer myObserver = new Observer() { 13 | @Override 14 | public void onSubscribe(Disposable d) { 15 | //do nothing with Disposable, disregard for now 16 | } 17 | 18 | @Override 19 | public void onNext(Integer value) { 20 | System.out.println("RECEIVED: " + value); 21 | } 22 | 23 | @Override 24 | public void onError(Throwable e) { 25 | e.printStackTrace(); 26 | } 27 | 28 | @Override 29 | public void onComplete() { 30 | System.out.println("Done!"); 31 | } 32 | }; 33 | source.map(String::length).filter(i -> i >= 5) 34 | .subscribe(myObserver); 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_8.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_8 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | source.map(String::length).filter(i -> i >= 5) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i), 12 | Throwable::printStackTrace, 13 | () -> System.out.println("Done!")); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter02/Ch2_9.java: -------------------------------------------------------------------------------- 1 | package ch2; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch2_9 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | source.map(String::length).filter(i -> i >= 5) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i), 12 | Throwable::printStackTrace); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_1.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_1 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon") 8 | .filter(s -> s.length() != 5) 9 | .subscribe(s -> System.out.println("RECEIVED: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_10.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_10 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Zeta", "Eta", "Gamma", 8 | "Delta") 9 | .distinctUntilChanged(String::length) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_11.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_11 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Zeta", "Eta", "Gamma", 8 | "Delta") 9 | .elementAt(3) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_12.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.time.LocalDate; 6 | import java.time.format.DateTimeFormatter; 7 | 8 | public class Ch3_12 { 9 | public static void main(String[] args) { 10 | DateTimeFormatter dtf = DateTimeFormatter.ofPattern("M/d/yyyy"); 11 | Observable.just("1/3/2016", "5/9/2016", "10/12/2016") 12 | .map(s -> LocalDate.parse(s, dtf)) 13 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_13.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_13 { 6 | public static void main(String[] args) { 7 | Observable menu = 8 | Observable.just("Coffee", "Tea", "Espresso", "Latte"); 9 | //print menu 10 | menu.startWith("COFFEE SHOP MENU") 11 | .subscribe(System.out::println); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_14.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_14 { 6 | public static void main(String[] args) { 7 | Observable menu = 8 | Observable.just("Coffee", "Tea", "Espresso", "Latte"); 9 | //print menu 10 | menu.startWithArray("COFFEE SHOP MENU", "----------------") 11 | .subscribe(System.out::println); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_15.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_15 { 6 | public static void main(String[] args) { 7 | Observable items = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon"); 9 | items.filter(s -> s.startsWith("Z")) 10 | .defaultIfEmpty("None") 11 | .subscribe(System.out::println); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_16.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_16 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .filter(s -> s.startsWith("Z")) 10 | .switchIfEmpty(Observable.just("Zeta", "Eta", "Theta")) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i), 12 | e -> System.out.println("RECEIVED ERROR: " + e) 13 | ); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_17.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_17 { 6 | public static void main(String[] args) { 7 | Observable.just(6, 2, 5, 7, 1, 4, 9, 8, 3) 8 | .sorted() 9 | .subscribe(System.out::println); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_18.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.Comparator; 6 | 7 | public class Ch3_18 { 8 | public static void main(String[] args) { 9 | Observable.just(6, 2, 5, 7, 1, 4, 9, 8, 3) 10 | .sorted(Comparator.reverseOrder()) 11 | .subscribe(System.out::println); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_19.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_19 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .sorted((x, y) -> Integer.compare(x.length(), y.length())) 10 | .subscribe(System.out::println); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_2.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_2 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon") 8 | .take(3) 9 | .subscribe(s -> System.out.println("RECEIVED: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_20.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch3_20 { 8 | public static void main(String[] args) { 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon") 11 | .delay(3, TimeUnit.SECONDS) 12 | .subscribe(s -> System.out.println("Received: " + s)); 13 | sleep(5000); 14 | } 15 | 16 | public static void sleep(long millis) { 17 | try { 18 | Thread.sleep(millis); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_21.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_21 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .repeat(2) 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_22.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_22 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 3, 7, 10, 2, 14) 8 | .scan((accumulator, next) -> accumulator + next) 9 | .subscribe(s -> System.out.println("Received: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_23.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_23 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .scan(0, (total, next) -> total + 1) 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_24.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_24 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .count() 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_25.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_25 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 3, 7, 10, 2, 14) 8 | .reduce((total, next) -> total + next) 9 | .subscribe(s -> System.out.println("Received: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_26.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_26 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 3, 7, 10, 2, 14) 8 | .reduce("", (total, next) -> total + (total.equals("") ? 9 | "" : 10 | ",") + next) 11 | .subscribe(s -> System.out.println("Received: " + 12 | s)); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_27.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_27 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 3, 7, 11, 2, 14) 8 | .all(i -> i < 10) 9 | .subscribe(s -> System.out.println("Received: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_28.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.time.LocalDate; 6 | 7 | public class Ch3_28 { 8 | public static void main(String[] args) { 9 | Observable.just("2016-01-01", "2016-05-02", "2016-09-12", 10 | "2016-04-03") 11 | .map(LocalDate::parse) 12 | .any(dt -> dt.getMonthValue() >= 6) 13 | .subscribe(s -> System.out.println("Received: " + s)); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_29.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_29 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 10000) 8 | .contains(9563) 9 | .subscribe(s -> System.out.println("Received: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_3.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch3_3 { 8 | public static void main(String[] args) { 9 | Observable.interval(300, TimeUnit.MILLISECONDS) 10 | .take(2, TimeUnit.SECONDS) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 12 | sleep(5000); 13 | } 14 | 15 | public static void sleep(long millis) { 16 | try { 17 | Thread.sleep(millis); 18 | } catch (InterruptedException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_30.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_30 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .toList() 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_31.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_31 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 1000) 8 | .toList(1000) 9 | .subscribe(s -> System.out.println("Received: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_32.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.CopyOnWriteArrayList; 6 | 7 | public class Ch3_32 { 8 | public static void main(String[] args) { 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon") 11 | .toList(CopyOnWriteArrayList::new) 12 | .subscribe(s -> System.out.println("Received: " + s)); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_33.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_33 { 6 | public static void main(String[] args) { 7 | Observable.just(6, 2, 5, 7, 1, 4, 9, 8, 3) 8 | .toSortedList() 9 | .subscribe(s -> System.out.println("Received: " + s)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_34.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_34 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .toMap(s -> s.charAt(0)) 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_35.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_35 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .toMap(s -> s.charAt(0), String::length) 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_36.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ConcurrentHashMap; 6 | 7 | public class Ch3_36 { 8 | public static void main(String[] args) { 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon") 11 | .toMap(s -> s.charAt(0), String::length, 12 | ConcurrentHashMap::new) 13 | .subscribe(s -> System.out.println("Received: " + s)); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_37.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_37 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .toMap(String::length) 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_38.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_38 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .toMultimap(String::length) 10 | .subscribe(s -> System.out.println("Received: " + s)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_39.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.HashSet; 6 | 7 | public class Ch3_39 { 8 | public static void main(String[] args) { 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon") 11 | .collect(HashSet::new, HashSet::add) 12 | .subscribe(s -> System.out.println("Received: " + s)); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_4.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_4 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 100) 8 | .skip(90) 9 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_40.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | import io.reactivex.Observable; 5 | 6 | public class Ch3_40 { 7 | public static void main(String[] args) { 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon") 10 | .collect(ImmutableList::builder, 11 | ImmutableList.Builder::add) 12 | .map(ImmutableList.Builder::build) 13 | .subscribe(s -> System.out.println("Received: " + s)); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_41.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_41 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .subscribe(i -> System.out.println("RECEIVED: " + i), 10 | e -> System.out.println("RECEIVED ERROR: " + e) 11 | ); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_42.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_42 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .onErrorReturnItem(-1) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i), 11 | e -> System.out.println("RECEIVED ERROR: " + e) 12 | ); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_43.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_43 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .onErrorReturn(e -> -1) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i), 11 | e -> System.out.println("RECEIVED ERROR: " + e) 12 | ); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_44.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_44 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> { 9 | try { 10 | return 10 / i; 11 | } catch (ArithmeticException e) { 12 | return -1; 13 | } 14 | }) 15 | .subscribe(i -> System.out.println("RECEIVED: " + 16 | i), 17 | e -> System.out.println("RECEIVED ERROR: " + e) 18 | ); 19 | } 20 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_45.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_45 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .onErrorResumeNext(Observable.just(-1).repeat(3)) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i), 11 | e -> System.out.println("RECEIVED ERROR: " + e) 12 | ); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_46.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_46 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .onErrorResumeNext(Observable.empty()) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i), 11 | e -> System.out.println("RECEIVED ERROR: " + e) 12 | ); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_47.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_47 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .onErrorResumeNext((Throwable e) -> 10 | Observable.just(-1).repeat(3)) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i), 12 | e -> System.out.println("RECEIVED ERROR: " + e) 13 | ); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_48.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_48 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .retry() 10 | .subscribe(i -> System.out.println("RECEIVED: " + i), 11 | e -> System.out.println("RECEIVED ERROR: " + e) 12 | ); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_49.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_49 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .map(i -> 10 / i) 9 | .retry(2) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i), 11 | e -> System.out.println("RECEIVED ERROR: " + e) 12 | ); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_5.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_5 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 100) 8 | .takeWhile(i -> i < 5) 9 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_50.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_50 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .doOnNext(s -> System.out.println("Processing: " + s)) 10 | .map(String::length) 11 | .subscribe(i -> System.out.println("Received: " + i)); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_51.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_51 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .doOnComplete(() -> System.out.println("Source is done emitting!")) 10 | .map(String::length) 11 | .subscribe(i -> System.out.println("Received: " + i)); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_52.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_52 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 2, 4, 0, 3, 2, 8) 8 | .doOnError(e -> System.out.println("Source failed!")) 9 | .map(i -> 10 / i) 10 | .doOnError(e -> System.out.println("Division failed!")) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i), 12 | e -> System.out.println("RECEIVED ERROR: " + e) 13 | ); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_53.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_53 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .doOnSubscribe(d -> System.out.println("Subscribing!")) 10 | .doOnDispose(() -> System.out.println("Disposing!")) 11 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_54.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_54 { 6 | public static void main(String[] args) { 7 | Observable.just(5, 3, 7, 10, 2, 14) 8 | .reduce((total, next) -> total + next) 9 | .doOnSuccess(i -> System.out.println("Emitting: " + i)) 10 | .subscribe(i -> System.out.println("Received: " + i)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_6.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_6 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 100) 8 | .skipWhile(i -> i <= 95) 9 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_7.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_7 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .map(String::length) 10 | .distinct() 11 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_8.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_8 { 6 | public static void main(String[] args) { 7 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 8 | "Epsilon") 9 | .distinct(String::length) 10 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter03/Ch3_9.java: -------------------------------------------------------------------------------- 1 | package ch3; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch3_9 { 6 | public static void main(String[] args) { 7 | Observable.just(1, 1, 1, 2, 2, 3, 3, 2, 1, 1) 8 | .distinctUntilChanged() 9 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_1.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_1 { 6 | public static void main(String[] args) { 7 | Observable source1 = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | Observable source2 = 11 | Observable.just("Zeta", "Eta", "Theta"); 12 | Observable.merge(source1, source2) 13 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_10.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch4_10 { 8 | public static void main(String[] args) { 9 | //emit every second, but only take 2 emissions 10 | Observable source1 = 11 | Observable.interval(1, TimeUnit.SECONDS) 12 | .take(2) 13 | .map(l -> l + 1) // emit elapsed seconds 14 | .map(l -> "Source1: " + l + " seconds"); 15 | //emit every 300 milliseconds 16 | Observable source2 = 17 | Observable.interval(300, TimeUnit.MILLISECONDS) 18 | .map(l -> (l + 1) * 300) // emit elapsed milliseconds 19 | .map(l -> "Source2: " + l + " milliseconds"); 20 | Observable.concat(source1, source2) 21 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 22 | //keep application alive for 5 seconds 23 | sleep(5000); 24 | } 25 | 26 | public static void sleep(long millis) { 27 | try { 28 | Thread.sleep(millis); 29 | } catch (InterruptedException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_11.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_11 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | source.concatMap(s -> Observable.fromArray(s.split(""))) 11 | .subscribe(System.out::println); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_12.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.Arrays; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch4_12 { 9 | public static void main(String[] args) { 10 | //emit every second 11 | Observable source1 = 12 | Observable.interval(1, TimeUnit.SECONDS) 13 | .take(2) 14 | .map(l -> l + 1) // emit elapsed seconds 15 | .map(l -> "Source1: " + l + " seconds"); 16 | //emit every 300 milliseconds 17 | Observable source2 = 18 | Observable.interval(300, TimeUnit.MILLISECONDS) 19 | .map(l -> (l + 1) * 300) // emit elapsed milliseconds 20 | .map(l -> "Source2: " + l + " milliseconds"); 21 | //emit Observable that emits first 22 | Observable.amb(Arrays.asList(source1, source2)) 23 | .subscribe(i -> System.out.println("RECEIVED: " + 24 | i)); 25 | //keep application alive for 5 seconds 26 | sleep(5000); 27 | } 28 | 29 | public static void sleep(long millis) { 30 | try { 31 | Thread.sleep(millis); 32 | } catch (InterruptedException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_13.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_13 { 6 | public static void main(String[] args) { 7 | Observable source1 = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | Observable source2 = Observable.range(1, 6); 11 | Observable.zip(source1, source2, (s, i) -> s + "-" + i) 12 | .subscribe(System.out::println); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_14.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.time.LocalTime; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch4_14 { 9 | public static void main(String[] args) { 10 | Observable strings = 11 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 12 | "Epsilon"); 13 | Observable seconds = 14 | Observable.interval(1, TimeUnit.SECONDS); 15 | Observable.zip(strings, seconds, (s, l) -> s) 16 | .subscribe(s -> 17 | System.out.println("Received " + s + 18 | " at " + LocalTime.now()) 19 | ); 20 | sleep(6000); 21 | } 22 | 23 | public static void sleep(long millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_15.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch4_15 { 8 | public static void main(String[] args) { 9 | Observable source1 = 10 | Observable.interval(300, TimeUnit.MILLISECONDS); 11 | Observable source2 = 12 | Observable.interval(1, TimeUnit.SECONDS); 13 | Observable.combineLatest(source1, source2, 14 | (l1, l2) -> "SOURCE 1: " + l1 + " SOURCE 2: " + l2) 15 | .subscribe(System.out::println); 16 | sleep(3000); 17 | } 18 | 19 | public static void sleep(long millis) { 20 | try { 21 | Thread.sleep(millis); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_16.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch4_16 { 8 | public static void main(String[] args) { 9 | Observable source1 = 10 | Observable.interval(300, TimeUnit.MILLISECONDS); 11 | Observable source2 = 12 | Observable.interval(1, TimeUnit.SECONDS); 13 | source2.withLatestFrom(source1, 14 | (l1, l2) -> "SOURCE 2: " + l1 + " SOURCE 1: " + l2 15 | ).subscribe(System.out::println); 16 | sleep(3000); 17 | } 18 | 19 | public static void sleep(long millis) { 20 | try { 21 | Thread.sleep(millis); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_17.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.GroupedObservable; 5 | 6 | public class Ch4_17 { 7 | public static void main(String[] args) { 8 | Observable source = 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon"); 10 | Observable> byLengths = 11 | source.groupBy(s -> s.length()); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_18.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.GroupedObservable; 5 | 6 | public class Ch4_18 { 7 | public static void main(String[] args) { 8 | Observable source = 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon"); 11 | Observable> byLengths = 12 | source.groupBy(s -> s.length()); 13 | byLengths.flatMapSingle(grp -> grp.toList()) 14 | .subscribe(System.out::println); 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_19.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.GroupedObservable; 5 | 6 | public class Ch4_19 { 7 | public static void main(String[] args) { 8 | Observable source = 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon"); 11 | Observable> byLengths = 12 | source.groupBy(s -> s.length()); 13 | byLengths.flatMapSingle(grp -> 14 | grp.reduce("", (x, y) -> x.equals("") ? y : x + ", " + y) 15 | .map(s -> grp.getKey() + ": " + s) 16 | ).subscribe(System.out::println); 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_2.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_2 { 6 | public static void main(String[] args) { 7 | Observable source1 = 8 | Observable.just("Alpha", "Beta"); 9 | Observable source2 = 10 | Observable.just("Gamma", "Delta"); 11 | Observable source3 = 12 | Observable.just("Epsilon", "Zeta"); 13 | Observable source4 = 14 | Observable.just("Eta", "Theta"); 15 | Observable source5 = 16 | Observable.just("Iota", "Kappa"); 17 | Observable.mergeArray(source1, source2, source3, source4, 18 | source5) 19 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 20 | } 21 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_3.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.Arrays; 6 | import java.util.List; 7 | 8 | public class Ch4_3 { 9 | public static void main(String[] args) { 10 | Observable source1 = 11 | Observable.just("Alpha", "Beta"); 12 | Observable source2 = 13 | Observable.just("Gamma", "Delta"); 14 | Observable source3 = 15 | Observable.just("Epsilon", "Zeta"); 16 | Observable source4 = 17 | Observable.just("Eta", "Theta"); 18 | Observable source5 = 19 | Observable.just("Iota", "Kappa"); 20 | List> sources = 21 | Arrays.asList(source1, source2, source3, source4, 22 | source5); 23 | Observable.merge(sources) 24 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_4.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch4_4 { 8 | public static void main(String[] args) { 9 | //emit every second 10 | Observable source1 = Observable.interval(1, 11 | TimeUnit.SECONDS) 12 | .map(l -> l + 1) // emit elapsed seconds 13 | .map(l -> "Source1: " + l + " seconds"); 14 | //emit every 300 milliseconds 15 | Observable source2 = 16 | Observable.interval(300, TimeUnit.MILLISECONDS) 17 | .map(l -> (l + 1) * 300) // emit elapsed milliseconds 18 | .map(l -> "Source2: " + l + " milliseconds"); 19 | //merge and subscribe 20 | Observable.merge(source1, source2) 21 | .subscribe(System.out::println); 22 | //keep alive for 3 seconds 23 | sleep(3000); 24 | } 25 | 26 | public static void sleep(long millis) { 27 | try { 28 | Thread.sleep(millis); 29 | } catch (InterruptedException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_5.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_5 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | source.flatMap(s -> Observable.fromArray(s.split(""))) 11 | .subscribe(System.out::println); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_6.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_6 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("521934/2342/FOXTROT", "21962/12112/78886/TANGO", "283242/4542/WHISKEY/2348562"); 9 | source.flatMap(s -> Observable.fromArray(s.split("/"))) 10 | .filter(s -> s.matches("[0-9]+")) //use regex to filter integers 11 | .map(Integer::valueOf) 12 | .subscribe(System.out::println); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_7.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch4_7 { 8 | public static void main(String[] args) { 9 | Observable intervalArguments = 10 | Observable.just(2, 3, 10, 7); 11 | intervalArguments.flatMap(i -> 12 | Observable.interval(i, TimeUnit.SECONDS) 13 | .map(i2 -> i + "s interval: " + ((i + 1) * i) + " seconds elapsed") 14 | ).subscribe(System.out::println); 15 | sleep(12000); 16 | } 17 | 18 | public static void sleep(long millis) { 19 | try { 20 | Thread.sleep(millis); 21 | } catch (InterruptedException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_8.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_8 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | source.flatMap(s -> Observable.fromArray(s.split("")), 11 | (s, r) -> 12 | s + "-" + r) 13 | .subscribe(System.out::println); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter04/Ch4_9.java: -------------------------------------------------------------------------------- 1 | package ch4; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch4_9 { 6 | public static void main(String[] args) { 7 | Observable source1 = 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon"); 10 | Observable source2 = 11 | Observable.just("Zeta", "Eta", "Theta"); 12 | Observable.concat(source1, source2) 13 | .subscribe(i -> System.out.println("RECEIVED: " + i)); 14 | } 15 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_1.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch5_1 { 6 | public static void main(String[] args) { 7 | Observable threeIntegers = Observable.range(1, 3); 8 | threeIntegers.subscribe(i -> System.out.println("Observer One: " + i)); 9 | threeIntegers.subscribe(i -> System.out.println("Observer Two: " + i)); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_10.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch5_10 { 9 | public static void main(String[] args) { 10 | 11 | ConnectableObservable threeInts = 12 | Observable.range(1, 3).publish(); 13 | 14 | Observable threeRandoms = threeInts.map(i -> 15 | randomInt()); 16 | 17 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 18 | threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i)); 19 | 20 | threeInts.connect(); 21 | } 22 | 23 | public static int randomInt() { 24 | return ThreadLocalRandom.current().nextInt(100000); 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_11.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch5_11 { 9 | public static void main(String[] args) { 10 | 11 | ConnectableObservable threeRandoms = 12 | Observable.range(1, 3) 13 | .map(i -> randomInt()).publish(); 14 | 15 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 16 | threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i)); 17 | 18 | threeRandoms.connect(); 19 | } 20 | 21 | public static int randomInt() { 22 | return ThreadLocalRandom.current().nextInt(100000); 23 | } 24 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_12.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch5_12 { 9 | public static void main(String[] args) { 10 | 11 | ConnectableObservable threeRandoms = 12 | Observable.range(1, 3) 13 | .map(i -> randomInt()).publish(); 14 | 15 | //Observer 1 - print each random integer 16 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 17 | 18 | //Observer 2 - sum the random integers, then print 19 | threeRandoms.reduce(0, (total, next) -> total + next) 20 | .subscribe(i -> System.out.println("Observer 2: " + i)); 21 | 22 | threeRandoms.connect(); 23 | } 24 | 25 | public static int randomInt() { 26 | return ThreadLocalRandom.current().nextInt(100000); 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_13.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | 7 | public class Ch5_13 { 8 | public static void main(String[] args) { 9 | Observable threeRandoms = Observable.range(1, 3) 10 | .map(i -> randomInt()) 11 | .publish() 12 | .autoConnect(2); 13 | 14 | //Observer 1 - print each random integer 15 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 16 | //Observer 2 - sum the random integers, then print 17 | threeRandoms.reduce(0, (total, next) -> total + next) 18 | .subscribe(i -> System.out.println("Observer 2: " + 19 | i)); 20 | //Observer 3 - receives nothing 21 | threeRandoms.subscribe(i -> System.out.println("Observer 3:" + i)); 22 | } 23 | 24 | public static int randomInt() { 25 | return ThreadLocalRandom.current().nextInt(100000); 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_14.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch5_14 { 8 | public static void main(String[] args) { 9 | Observable seconds = 10 | Observable.interval(1, TimeUnit.SECONDS) 11 | .publish() 12 | .autoConnect(); 13 | //Observer 1 14 | seconds.subscribe(i -> System.out.println("Observer 1: " + 15 | i)); 16 | sleep(3000); 17 | //Observer 2 18 | seconds.subscribe(i -> System.out.println("Observer 2: " + 19 | i)); 20 | sleep(3000); 21 | } 22 | 23 | public static void sleep(long millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_15.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch5_15 { 8 | public static void main(String[] args) { 9 | Observable seconds = 10 | Observable.interval(1, TimeUnit.SECONDS) 11 | .publish() 12 | .refCount(); 13 | //Observer 1 14 | seconds.take(5) 15 | .subscribe(l -> System.out.println("Observer 1: " + 16 | l)); 17 | sleep(3000); 18 | //Observer 2 19 | seconds.take(2) 20 | .subscribe(l -> System.out.println("Observer 2: " + 21 | l)); 22 | sleep(3000); 23 | //there should be no more Observers at this point 24 | //Observer 3 25 | seconds.subscribe(l -> System.out.println("Observer 3: " + 26 | l)); 27 | sleep(3000); 28 | } 29 | 30 | public static void sleep(long millis) { 31 | try { 32 | Thread.sleep(millis); 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_16.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch5_16 { 8 | public static void main(String[] args) { 9 | Observable seconds = 10 | Observable.interval(1, TimeUnit.SECONDS) 11 | .replay() 12 | .autoConnect(); 13 | //Observer 1 14 | seconds.subscribe(l -> System.out.println("Observer 1: " + 15 | l)); 16 | sleep(3000); 17 | //Observer 2 18 | seconds.subscribe(l -> System.out.println("Observer 2: " + 19 | l)); 20 | sleep(3000); 21 | } 22 | 23 | public static void sleep(long millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_17.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch5_17 { 6 | public static void main(String[] args) { 7 | Observable source = 8 | Observable.just("Alpha", "Beta", "Gamma", 9 | "Delta", "Epsilon") 10 | .replay(1) 11 | .autoConnect(); 12 | //Observer 1 13 | source.subscribe(l -> System.out.println("Observer 1: " + 14 | l)); 15 | //Observer 2 16 | source.subscribe(l -> System.out.println("Observer 2: " + 17 | l)); 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_18.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch5_18 { 8 | public static void main(String[] args) { 9 | Observable seconds = 10 | Observable.interval(300, TimeUnit.MILLISECONDS) 11 | .map(l -> (l + 1) * 300) // map to elapsed milliseconds 12 | .replay(1, TimeUnit.SECONDS) 13 | .autoConnect(); 14 | //Observer 1 15 | seconds.subscribe(l -> System.out.println("Observer 1: " + 16 | l)); 17 | sleep(2000); 18 | //Observer 2 19 | seconds.subscribe(l -> System.out.println("Observer 2: " + 20 | l)); 21 | sleep(1000); 22 | } 23 | 24 | public static void sleep(long millis) { 25 | try { 26 | Thread.sleep(millis); 27 | } catch (InterruptedException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_19.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch5_19 { 6 | public static void main(String[] args) { 7 | Observable cachedRollingTotals = 8 | Observable.just(6, 2, 5, 7, 1, 4, 9, 8, 3) 9 | .scan(0, (total, next) -> total + next) 10 | .cache(); 11 | cachedRollingTotals.subscribe(System.out::println); 12 | } 13 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_2.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | public class Ch5_2 { 7 | public static void main(String[] args) { 8 | ConnectableObservable threeIntegers = 9 | Observable.range(1, 3).publish(); 10 | threeIntegers.subscribe(i -> System.out.println("Observer One:" + i)); 11 | threeIntegers.subscribe(i -> System.out.println("Observer Two:" + i)); 12 | threeIntegers.connect(); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_20.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.subjects.PublishSubject; 4 | import io.reactivex.subjects.Subject; 5 | 6 | public class Ch5_20 { 7 | public static void main(String[] args) { 8 | Subject subject = PublishSubject.create(); 9 | subject.map(String::length) 10 | .subscribe(System.out::println); 11 | subject.onNext("Alpha"); 12 | subject.onNext("Beta"); 13 | subject.onNext("Gamma"); 14 | subject.onComplete(); 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_21.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.subjects.PublishSubject; 5 | import io.reactivex.subjects.Subject; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class Ch5_21 { 10 | public static void main(String[] args) { 11 | Observable source1 = 12 | Observable.interval(1, TimeUnit.SECONDS) 13 | .map(l -> (l + 1) + " seconds"); 14 | Observable source2 = 15 | Observable.interval(300, TimeUnit.MILLISECONDS) 16 | .map(l -> ((l + 1) * 300) + " milliseconds"); 17 | Subject subject = PublishSubject.create(); 18 | subject.subscribe(System.out::println); 19 | source1.subscribe(subject); 20 | source2.subscribe(subject); 21 | sleep(3000); 22 | } 23 | 24 | public static void sleep(long millis) { 25 | try { 26 | Thread.sleep(millis); 27 | } catch (InterruptedException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_22.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.subjects.PublishSubject; 4 | import io.reactivex.subjects.Subject; 5 | 6 | public class Ch5_22 { 7 | public static void main(String[] args) { 8 | Subject subject = PublishSubject.create(); 9 | subject.onNext("Alpha"); 10 | subject.onNext("Beta"); 11 | subject.onNext("Gamma"); 12 | subject.onComplete(); 13 | subject.map(String::length) 14 | .subscribe(System.out::println); 15 | } 16 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_23.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.subjects.BehaviorSubject; 4 | import io.reactivex.subjects.Subject; 5 | 6 | public class Ch5_23 { 7 | public static void main(String[] args) { 8 | Subject subject = 9 | BehaviorSubject.create(); 10 | subject.subscribe(s -> System.out.println("Observer 1: " + 11 | s)); 12 | subject.onNext("Alpha"); 13 | subject.onNext("Beta"); 14 | subject.onNext("Gamma"); 15 | subject.subscribe(s -> System.out.println("Observer 2: " + 16 | s)); 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_24.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.subjects.ReplaySubject; 4 | import io.reactivex.subjects.Subject; 5 | 6 | public class Ch5_24 { 7 | public static void main(String[] args) { 8 | Subject subject = 9 | ReplaySubject.create(); 10 | subject.subscribe(s -> System.out.println("Observer 1: " + 11 | s)); 12 | subject.onNext("Alpha"); 13 | subject.onNext("Beta"); 14 | subject.onNext("Gamma"); 15 | subject.onComplete(); 16 | subject.subscribe(s -> System.out.println("Observer 2: " + 17 | s)); 18 | } 19 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_25.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.subjects.AsyncSubject; 4 | import io.reactivex.subjects.Subject; 5 | 6 | public class Ch5_25 { 7 | public static void main(String[] args) { 8 | Subject subject = 9 | AsyncSubject.create(); 10 | subject.subscribe(s -> 11 | System.out.println("Observer 1: " + s), 12 | Throwable::printStackTrace, 13 | () -> System.out.println("Observer 1 done!") 14 | ); 15 | subject.onNext("Alpha"); 16 | subject.onNext("Beta"); 17 | subject.onNext("Gamma"); 18 | subject.onComplete(); 19 | subject.subscribe(s -> 20 | System.out.println("Observer 2: " + s), 21 | Throwable::printStackTrace, 22 | () -> System.out.println("Observer 2 done!") 23 | ); 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_26.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.subjects.ReplaySubject; 5 | import io.reactivex.subjects.Subject; 6 | import io.reactivex.subjects.UnicastSubject; 7 | 8 | import java.util.concurrent.TimeUnit; 9 | 10 | public class Ch5_26 { 11 | public static void main(String[] args) { 12 | Subject subject = 13 | UnicastSubject.create(); 14 | Observable.interval(300, TimeUnit.MILLISECONDS) 15 | .map(l -> ((l + 1) * 300) + " milliseconds") 16 | .subscribe(subject); 17 | sleep(2000); 18 | subject.subscribe(s -> System.out.println("Observer 1: " + 19 | s)); 20 | sleep(2000); 21 | } 22 | 23 | public static void sleep(long millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_27.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.subjects.Subject; 5 | import io.reactivex.subjects.UnicastSubject; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class Ch5_27 { 10 | public static void main(String[] args) { 11 | Subject subject = 12 | UnicastSubject.create(); 13 | Observable.interval(300, TimeUnit.MILLISECONDS) 14 | .map(l -> ((l + 1) * 300) + " milliseconds") 15 | .subscribe(subject); 16 | sleep(2000); 17 | //multicast to support multiple Observers 18 | Observable multicast = 19 | subject.publish().autoConnect(); 20 | //bring in first Observer 21 | multicast.subscribe(s -> System.out.println("Observer 1: " 22 | + s)); 23 | sleep(2000); 24 | //bring in second Observer 25 | multicast.subscribe(s -> System.out.println("Observer 2: " 26 | + s)); 27 | sleep(1000); 28 | } 29 | 30 | public static void sleep(long millis) { 31 | try { 32 | Thread.sleep(millis); 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_3.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | 7 | public class Ch5_3 { 8 | public static void main(String[] args) { 9 | Observable threeRandoms = Observable.range(1, 3) 10 | .map(i -> randomInt()); 11 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 12 | threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i)); 13 | } 14 | 15 | public static int randomInt() { 16 | return ThreadLocalRandom.current().nextInt(100000); 17 | } 18 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_4.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch5_4 { 9 | public static void main(String[] args) { 10 | ConnectableObservable threeInts = 11 | Observable.range(1, 3).publish(); 12 | Observable threeRandoms = threeInts.map(i -> 13 | randomInt()); 14 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 15 | threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i)); 16 | threeInts.connect(); 17 | } 18 | 19 | public static int randomInt() { 20 | return ThreadLocalRandom.current().nextInt(100000); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_5.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch5_5 { 9 | public static void main(String[] args) { 10 | ConnectableObservable threeRandoms = 11 | Observable.range(1, 3) 12 | .map(i -> randomInt()).publish(); 13 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 14 | threeRandoms.subscribe(i -> System.out.println("Observer 2: " + i)); 15 | threeRandoms.connect(); 16 | } 17 | 18 | public static int randomInt() { 19 | return ThreadLocalRandom.current().nextInt(100000); 20 | } 21 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_6.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.observables.ConnectableObservable; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch5_6 { 9 | public static void main(String[] args) { 10 | ConnectableObservable threeRandoms = 11 | Observable.range(1, 3) 12 | .map(i -> randomInt()).publish(); 13 | 14 | //Observer 1 - print each random integer 15 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 16 | 17 | //Observer 2 - sum the random integers, then print 18 | threeRandoms.reduce(0, (total, next) -> total + next) 19 | .subscribe(i -> System.out.println("Observer 2: " + i)); 20 | 21 | threeRandoms.connect(); 22 | } 23 | 24 | public static int randomInt() { 25 | return ThreadLocalRandom.current().nextInt(100000); 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_7.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | 7 | public class Ch5_7 { 8 | public static void main(String[] args) { 9 | Observable threeRandoms = Observable.range(1, 3) 10 | .map(i -> randomInt()) 11 | .publish() 12 | .autoConnect(2); 13 | 14 | //Observer 1 - print each random integer 15 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 16 | 17 | //Observer 2 - sum the random integers, then print 18 | threeRandoms.reduce(0, (total, next) -> total + next) 19 | .subscribe(i -> System.out.println("Observer 2: " + i)); 20 | } 21 | 22 | public static int randomInt() { 23 | return ThreadLocalRandom.current().nextInt(100000); 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_8.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | 7 | public class Ch5_8 { 8 | public static void main(String[] args) { 9 | Observable threeRandoms = Observable.range(1, 3) 10 | .map(i -> randomInt()).publish().autoConnect(2); 11 | //Observer 1 - print each random integer 12 | threeRandoms.subscribe(i -> System.out.println("Observer 1: " + i)); 13 | //Observer 2 - sum the random integers, then print 14 | threeRandoms.reduce(0, (total, next) -> total + next) 15 | .subscribe(i -> System.out.println("Observer 2: " + 16 | i)); 17 | //Observer 3 - receives nothing 18 | threeRandoms.subscribe(i -> System.out.println("Observer 3:" + i)); 19 | } 20 | 21 | public static int randomInt() { 22 | return ThreadLocalRandom.current().nextInt(100000); 23 | } 24 | } -------------------------------------------------------------------------------- /Chapter05/Ch5_9.java: -------------------------------------------------------------------------------- 1 | package ch5; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch5_9 { 8 | public static void main(String[] args) { 9 | Observable seconds = 10 | Observable.interval(1, TimeUnit.SECONDS) 11 | .publish() 12 | .autoConnect(); 13 | //Observer 1 14 | seconds.subscribe(i -> System.out.println("Observer 1: " + i)); 15 | sleep(3000); 16 | //Observer 2 17 | seconds.subscribe(i -> System.out.println("Observer 2: " + i)); 18 | sleep(3000); 19 | } 20 | 21 | public static void sleep(long millis) { 22 | try { 23 | Thread.sleep(millis); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_1.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch6_1 { 8 | public static void main(String[] args) { 9 | Observable.interval(1, TimeUnit.SECONDS) 10 | .map(i -> i + " Mississippi") 11 | .subscribe(System.out::println); 12 | sleep(5000); 13 | } 14 | 15 | public static void sleep(long millis) { 16 | try { 17 | Thread.sleep(millis); 18 | } catch (InterruptedException e) { 19 | e.printStackTrace(); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_10.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.net.URL; 7 | import java.util.Scanner; 8 | 9 | public class Ch6_10 { 10 | public static void main(String[] args) { 11 | Observable.fromCallable(() -> 12 | getResponse("https://api.github.com/users/thomasnield/starred") 13 | ).subscribeOn(Schedulers.io()) 14 | .subscribe(System.out::println); 15 | sleep(10000); 16 | } 17 | 18 | private static String getResponse(String path) { 19 | try { 20 | return new Scanner(new URL(path).openStream(), 21 | "UTF-8").useDelimiter("\\A").next(); 22 | } catch (Exception e) { 23 | return e.getMessage(); 24 | } 25 | } 26 | 27 | public static void sleep(int millis) { 28 | try { 29 | Thread.sleep(millis); 30 | } catch (InterruptedException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_11.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch6_11 { 9 | public static void main(String[] args) { 10 | Observable.interval(1, TimeUnit.SECONDS, 11 | Schedulers.newThread()) 12 | .subscribe(i -> System.out.println("Received " + i 13 | + 14 | " on thread " + 15 | Thread.currentThread().getName())); 16 | sleep(5000); 17 | } 18 | 19 | public static void sleep(int millis) { 20 | try { 21 | Thread.sleep(millis); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_12.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | public class Ch6_12 { 7 | public static void main(String[] args) { 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon") 10 | .subscribeOn(Schedulers.computation()) 11 | .filter(s -> s.length() == 5) 12 | .subscribeOn(Schedulers.io()) 13 | .subscribe(i -> System.out.println("Received " + i 14 | + 15 | " on thread " + 16 | Thread.currentThread().getName())); 17 | sleep(5000); 18 | } 19 | 20 | public static void sleep(int millis) { 21 | try { 22 | Thread.sleep(millis); 23 | } catch (InterruptedException e) { 24 | e.printStackTrace(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_13.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | public class Ch6_13 { 7 | public static void main(String[] args) { 8 | //Happens on IO Scheduler 9 | Observable.just("WHISKEY/27653/TANGO", "6555/BRAVO", 10 | "232352/5675675/FOXTROT") 11 | .subscribeOn(Schedulers.io()) 12 | .flatMap(s -> Observable.fromArray(s.split("/"))) 13 | //Happens on Computation Scheduler 14 | .observeOn(Schedulers.computation()) 15 | .filter(s -> s.matches("[0-9]+")) 16 | .map(Integer::valueOf) 17 | .reduce((total, next) -> total + next) 18 | .subscribe(i -> System.out.println("Received " + i 19 | + " on thread " 20 | + Thread.currentThread().getName())); 21 | sleep(1000); 22 | } 23 | 24 | public static void sleep(int millis) { 25 | try { 26 | Thread.sleep(millis); 27 | } catch (InterruptedException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_16.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.time.LocalTime; 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch6_16 { 9 | public static void main(String[] args) { 10 | Observable.range(1, 10) 11 | .map(i -> intenseCalculation(i)) 12 | .subscribe(i -> System.out.println("Received " + i + 13 | " " 14 | + LocalTime.now())); 15 | } 16 | 17 | public static T intenseCalculation(T value) { 18 | sleep(ThreadLocalRandom.current().nextInt(3000)); 19 | return value; 20 | } 21 | 22 | public static void sleep(long millis) { 23 | try { 24 | Thread.sleep(millis); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_17.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.time.LocalTime; 7 | import java.util.concurrent.ThreadLocalRandom; 8 | 9 | public class Ch6_17 { 10 | public static void main(String[] args) { 11 | Observable.range(1, 10) 12 | .flatMap(i -> Observable.just(i) 13 | .subscribeOn(Schedulers.computation()) 14 | .map(i2 -> intenseCalculation(i2)) 15 | ) 16 | .subscribe(i -> System.out.println("Received " + i + 17 | " " 18 | + LocalTime.now() + " on thread " 19 | + Thread.currentThread().getName())); 20 | sleep(20000); 21 | } 22 | 23 | public static T intenseCalculation(T value) { 24 | sleep(ThreadLocalRandom.current().nextInt(3000)); 25 | return value; 26 | } 27 | 28 | public static void sleep(long millis) { 29 | try { 30 | Thread.sleep(millis); 31 | } catch (InterruptedException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_19.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.disposables.Disposable; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch6_19 { 9 | public static void main(String[] args) { 10 | Disposable d = Observable.interval(1, TimeUnit.SECONDS) 11 | .doOnDispose(() -> System.out.println("Disposing on thread" 12 | + Thread.currentThread().getName())) 13 | .subscribe(i -> System.out.println("Received " + 14 | i)); 15 | sleep(3000); 16 | d.dispose(); 17 | sleep(3000); 18 | } 19 | 20 | public static void sleep(long millis) { 21 | try { 22 | Thread.sleep(millis); 23 | } catch (InterruptedException e) { 24 | e.printStackTrace(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_2.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | 7 | public class Ch6_2 { 8 | public static void main(String[] args) { 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon") 11 | .map(s -> intenseCalculation((s))) 12 | .subscribe(System.out::println); 13 | Observable.range(1, 6) 14 | .map(s -> intenseCalculation((s))) 15 | .subscribe(System.out::println); 16 | } 17 | 18 | public static T intenseCalculation(T value) { 19 | sleep(ThreadLocalRandom.current().nextInt(3000)); 20 | return value; 21 | } 22 | 23 | public static void sleep(long millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_20.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.disposables.Disposable; 5 | import io.reactivex.schedulers.Schedulers; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class Ch6_20 { 10 | public static void main(String[] args) { 11 | Disposable d = Observable.interval(1, TimeUnit.SECONDS) 12 | .doOnDispose(() -> System.out.println("Disposing on thread" 13 | + Thread.currentThread().getName())) 14 | .unsubscribeOn(Schedulers.io()) 15 | .subscribe(i -> System.out.println("Received " + 16 | i)); 17 | sleep(3000); 18 | d.dispose(); 19 | sleep(3000); 20 | } 21 | 22 | public static void sleep(long millis) { 23 | try { 24 | Thread.sleep(millis); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_3.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch6_3 { 9 | public static void main(String[] args) { 10 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 11 | "Epsilon") 12 | .subscribeOn(Schedulers.computation()) 13 | .map(s -> intenseCalculation((s))) 14 | .subscribe(System.out::println); 15 | Observable.range(1, 6) 16 | .subscribeOn(Schedulers.computation()) 17 | .map(s -> intenseCalculation((s))) 18 | .subscribe(System.out::println); 19 | sleep(20000); 20 | } 21 | 22 | public static T intenseCalculation(T value) { 23 | sleep(ThreadLocalRandom.current().nextInt(3000)); 24 | return value; 25 | } 26 | 27 | public static void sleep(long millis) { 28 | try { 29 | Thread.sleep(millis); 30 | } catch (InterruptedException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_4.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch6_4 { 9 | public static void main(String[] args) { 10 | Observable source1 = 11 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 12 | "Epsilon") 13 | .subscribeOn(Schedulers.computation()) 14 | .map(s -> intenseCalculation((s))); 15 | Observable source2 = 16 | Observable.range(1, 6) 17 | .subscribeOn(Schedulers.computation()) 18 | .map(s -> intenseCalculation((s))); 19 | Observable.zip(source1, source2, (s, i) -> s + "-" + i) 20 | .subscribe(System.out::println); 21 | sleep(20000); 22 | } 23 | 24 | public static T intenseCalculation(T value) { 25 | sleep(ThreadLocalRandom.current().nextInt(3000)); 26 | return value; 27 | } 28 | 29 | public static void sleep(long millis) { 30 | try { 31 | Thread.sleep(millis); 32 | } catch (InterruptedException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_5.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch6_5 { 9 | public static void main(String[] args) { 10 | Observable.interval(1, TimeUnit.SECONDS) 11 | .map(l -> intenseCalculation((l))) 12 | .subscribe(System.out::println); 13 | sleep(Long.MAX_VALUE); 14 | } 15 | 16 | public static T intenseCalculation(T value) { 17 | sleep(ThreadLocalRandom.current().nextInt(3000)); 18 | return value; 19 | } 20 | 21 | public static void sleep(long millis) { 22 | try { 23 | Thread.sleep(millis); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_6.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.schedulers.Schedulers; 4 | import io.reactivex.Observable; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch6_6 { 9 | public static void main(String[] args) { 10 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 11 | "Epsilon") 12 | .subscribeOn(Schedulers.computation()) 13 | .map(Ch6_6::intenseCalculation) 14 | .blockingSubscribe(System.out::println, 15 | Throwable::printStackTrace, 16 | () -> System.out.println("Done!")); 17 | } 18 | 19 | public static T intenseCalculation(T value) { 20 | sleep(ThreadLocalRandom.current().nextInt(3000)); 21 | return value; 22 | } 23 | 24 | public static void sleep(int millis) { 25 | try { 26 | Thread.sleep(millis); 27 | } catch (InterruptedException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_7.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.Scheduler; 5 | import io.reactivex.schedulers.Schedulers; 6 | 7 | import java.util.concurrent.ExecutorService; 8 | import java.util.concurrent.Executors; 9 | 10 | public class Ch6_7 { 11 | public static void main(String[] args) { 12 | int numberOfThreads = 20; 13 | ExecutorService executor = 14 | Executors.newFixedThreadPool(numberOfThreads); 15 | Scheduler scheduler = Schedulers.from(executor); 16 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 17 | "Epsilon") 18 | .subscribeOn(scheduler) 19 | .doFinally(executor::shutdown) 20 | .subscribe(System.out::println); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter06/Ch6_8.java: -------------------------------------------------------------------------------- 1 | package ch6; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch6_8 { 9 | public static void main(String[] args) { 10 | Observable lengths = 11 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 12 | "Epsilon") 13 | .subscribeOn(Schedulers.computation()) 14 | .map(Ch6_8::intenseCalculation) 15 | .map(String::length); 16 | lengths.subscribe(i -> 17 | System.out.println("Received " + i + " on thread " + 18 | Thread.currentThread().getName())); 19 | lengths.subscribe(i -> 20 | System.out.println("Received " + i + " on thread " + 21 | Thread.currentThread().getName())); 22 | sleep(10000); 23 | } 24 | 25 | public static T intenseCalculation(T value) { 26 | sleep(ThreadLocalRandom.current().nextInt(3000)); 27 | return value; 28 | } 29 | 30 | public static void sleep(int millis) { 31 | try { 32 | Thread.sleep(millis); 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_1.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch7_1 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 50) 8 | .buffer(8) 9 | .subscribe(System.out::println); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_10.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch7_10 { 8 | public static void main(String[] args) { 9 | Observable.interval(300, TimeUnit.MILLISECONDS) 10 | .map(i -> (i + 1) * 300) // map to elapsed time 11 | .window(1, TimeUnit.SECONDS) 12 | .flatMapSingle(obs -> obs.reduce("", (total, 13 | next) -> total 14 | + (total.equals("") ? "" : "|") + next)) 15 | .subscribe(System.out::println); 16 | sleep(5000); 17 | } 18 | 19 | public static void sleep(int millis) { 20 | try { 21 | Thread.sleep(millis); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_11.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch7_11 { 8 | public static void main(String[] args) { 9 | Observable cutOffs = 10 | Observable.interval(1, TimeUnit.SECONDS); 11 | Observable.interval(300, TimeUnit.MILLISECONDS) 12 | .map(i -> (i + 1) * 300) // map to elapsed time 13 | .window(cutOffs) 14 | .flatMapSingle(obs -> obs.reduce("", (total, next) -> 15 | total 16 | + (total.equals("") ? "" : "|") + next)) 17 | .subscribe(System.out::println); 18 | sleep(5000); 19 | } 20 | 21 | public static void sleep(int millis) { 22 | try { 23 | Thread.sleep(millis); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_12.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch7_12 { 8 | public static void main(String[] args) { 9 | Observable source1 = Observable.interval(100, 10 | TimeUnit.MILLISECONDS) 11 | .map(i -> (i + 1) * 100) // map to elapsed time 12 | .map(i -> "SOURCE 1: " + i) 13 | .take(10); 14 | Observable source2 = Observable.interval(300, 15 | TimeUnit.MILLISECONDS) 16 | .map(i -> (i + 1) * 300) // map to elapsed time 17 | .map(i -> "SOURCE 2: " + i) 18 | .take(3); 19 | Observable source3 = Observable.interval(2000, 20 | TimeUnit.MILLISECONDS) 21 | .map(i -> (i + 1) * 2000) // map to elapsed time 22 | .map(i -> "SOURCE 3: " + i) 23 | .take(2); 24 | Observable.concat(source1, source2, source3) 25 | .subscribe(System.out::println); 26 | sleep(6000); 27 | } 28 | 29 | public static void sleep(int millis) { 30 | try { 31 | Thread.sleep(millis); 32 | } catch (InterruptedException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_13.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch7_13 { 8 | public static void main(String[] args) { 9 | Observable source1 = Observable.interval(100, 10 | TimeUnit.MILLISECONDS) 11 | .map(i -> (i + 1) * 100) // map to elapsed time 12 | .map(i -> "SOURCE 1: " + i) 13 | .take(10); 14 | Observable source2 = Observable.interval(300, 15 | TimeUnit.MILLISECONDS) 16 | .map(i -> (i + 1) * 300) // map to elapsed time 17 | .map(i -> "SOURCE 2: " + i) 18 | .take(3); 19 | Observable source3 = Observable.interval(2000, 20 | TimeUnit.MILLISECONDS) 21 | .map(i -> (i + 1) * 2000) // map to elapsed time 22 | .map(i -> "SOURCE 3: " + i) 23 | .take(2); 24 | Observable.concat(source1, source2, source3) 25 | .throttleWithTimeout(1, TimeUnit.SECONDS) 26 | .subscribe(System.out::println); 27 | sleep(6000); 28 | } 29 | 30 | public static void sleep(int millis) { 31 | try { 32 | Thread.sleep(millis); 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_14.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch7_14 { 9 | public static void main(String[] args) { 10 | Observable items = Observable.just("Alpha", "Beta", 11 | "Gamma", "Delta", "Epsilon", 12 | "Zeta", "Eta", "Theta", "Iota"); 13 | //delay each String to emulate an intense calculation 14 | Observable processStrings = items.concatMap(s -> 15 | Observable.just(s) 16 | .delay(randomSleepTime(), 17 | TimeUnit.MILLISECONDS) 18 | ); 19 | processStrings.subscribe(System.out::println); 20 | //keep application alive for 20 seconds 21 | sleep(20000); 22 | } 23 | 24 | public static int randomSleepTime() { 25 | //returns random sleep time between 0 to 2000 milliseconds 26 | return ThreadLocalRandom.current().nextInt(2000); 27 | } 28 | 29 | public static void sleep(int millis) { 30 | try { 31 | Thread.sleep(millis); 32 | } catch (InterruptedException e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_2.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch7_2 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 10) 8 | .buffer(2, 3) 9 | .subscribe(System.out::println); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_3.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch7_3 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 10) 8 | .buffer(3, 1) 9 | .subscribe(System.out::println); 10 | } 11 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_4.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch7_4 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 10) 8 | .buffer(2, 1) 9 | .filter(c -> c.size() == 2) 10 | .subscribe(System.out::println); 11 | } 12 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_5.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch7_5 { 8 | public static void main(String[] args) { 9 | Observable.interval(300, TimeUnit.MILLISECONDS) 10 | .map(i -> (i + 1) * 300) // map to elapsed time 11 | .buffer(1, TimeUnit.SECONDS) 12 | .subscribe(System.out::println); 13 | sleep(4000); 14 | } 15 | 16 | public static void sleep(int millis) { 17 | try { 18 | Thread.sleep(millis); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_6.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch7_6 { 8 | public static void main(String[] args) { 9 | Observable.interval(300, TimeUnit.MILLISECONDS) 10 | .map(i -> (i + 1) * 300) // map to elapsed time 11 | .buffer(1, TimeUnit.SECONDS, 2) 12 | .subscribe(System.out::println); 13 | sleep(5000); 14 | } 15 | 16 | public static void sleep(int millis) { 17 | try { 18 | Thread.sleep(millis); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_7.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | public class Ch7_7 { 8 | public static void main(String[] args) { 9 | Observable cutOffs = 10 | Observable.interval(1, TimeUnit.SECONDS); 11 | Observable.interval(300, TimeUnit.MILLISECONDS) 12 | .map(i -> (i + 1) * 300) // map to elapsed time 13 | .buffer(cutOffs) 14 | .subscribe(System.out::println); 15 | sleep(5000); 16 | } 17 | 18 | public static void sleep(int millis) { 19 | try { 20 | Thread.sleep(millis); 21 | } catch (InterruptedException e) { 22 | e.printStackTrace(); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_8.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch7_8 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 50) 8 | .window(8) 9 | .flatMapSingle(obs -> obs.reduce("", (total, 10 | next) -> total 11 | + (total.equals("") ? "" : "|") + next)) 12 | .subscribe(System.out::println); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter07/Ch7_9.java: -------------------------------------------------------------------------------- 1 | package ch7; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch7_9 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 50) 8 | .window(2, 3) 9 | .flatMapSingle(obs -> obs.reduce("", (total, 10 | next) -> total 11 | + (total.equals("") ? "" : "|") + next)) 12 | .subscribe(System.out::println); 13 | } 14 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_1.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Observable; 4 | 5 | public class Ch8_1 { 6 | public static void main(String[] args) { 7 | Observable.range(1, 999_999_999) 8 | .map(MyItem::new) 9 | .subscribe(myItem -> { 10 | sleep(50); 11 | System.out.println("Received MyItem " + 12 | myItem.id); 13 | }); 14 | } 15 | 16 | static void sleep(long milliseconds) { 17 | try { 18 | Thread.sleep(milliseconds); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | 24 | static final class MyItem { 25 | final int id; 26 | 27 | MyItem(int id) { 28 | this.id = id; 29 | System.out.println("Constructing MyItem " + id); 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_10.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.BackpressureStrategy; 4 | import io.reactivex.Observable; 5 | import io.reactivex.schedulers.Schedulers; 6 | 7 | public class Ch8_10 { 8 | public static void main(String[] args) { 9 | Observable source = Observable.range(1, 1000); 10 | source.toFlowable(BackpressureStrategy.BUFFER) 11 | .observeOn(Schedulers.io()) 12 | .subscribe(System.out::println); 13 | sleep(10000); 14 | } 15 | 16 | public static void sleep(long millis) { 17 | try { 18 | Thread.sleep(millis); 19 | } catch (InterruptedException e) { 20 | e.printStackTrace(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_11.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.Observable; 5 | import io.reactivex.schedulers.Schedulers; 6 | 7 | public class Ch8_11 { 8 | public static void main(String[] args) { 9 | Flowable integers = 10 | Flowable.range(1, 1000) 11 | .subscribeOn(Schedulers.computation()); 12 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon") 13 | .flatMap(s -> integers.map(i -> i + "-" + 14 | s).toObservable()) 15 | .subscribe(System.out::println); 16 | sleep(5000); 17 | } 18 | 19 | public static void sleep(long millis) { 20 | try { 21 | Thread.sleep(millis); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_12.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch8_12 { 9 | public static void main(String[] args) { 10 | Flowable.interval(1, TimeUnit.MILLISECONDS) 11 | .onBackpressureBuffer() 12 | .observeOn(Schedulers.io()) 13 | .subscribe(i -> { 14 | sleep(5); 15 | System.out.println(i); 16 | }); 17 | sleep(5000); 18 | } 19 | 20 | public static void sleep(long millis) { 21 | try { 22 | Thread.sleep(millis); 23 | } catch (InterruptedException e) { 24 | e.printStackTrace(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_13.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.BackpressureOverflowStrategy; 4 | import io.reactivex.Flowable; 5 | import io.reactivex.schedulers.Schedulers; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | public class Ch8_13 { 10 | public static void main(String[] args) { 11 | Flowable.interval(1, TimeUnit.MILLISECONDS) 12 | .onBackpressureBuffer(10, 13 | () -> System.out.println("overflow!"), 14 | BackpressureOverflowStrategy.DROP_LATEST) 15 | .observeOn(Schedulers.io()) 16 | .subscribe(i -> { 17 | sleep(5); 18 | System.out.println(i); 19 | }); 20 | sleep(5000); 21 | } 22 | 23 | public static void sleep(long millis) { 24 | try { 25 | Thread.sleep(millis); 26 | } catch (InterruptedException e) { 27 | e.printStackTrace(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_14.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch8_14 { 9 | public static void main(String[] args) { 10 | Flowable.interval(1, TimeUnit.MILLISECONDS) 11 | .onBackpressureLatest() 12 | .observeOn(Schedulers.io()) 13 | .subscribe(i -> { 14 | sleep(5); 15 | System.out.println(i); 16 | }); 17 | sleep(5000); 18 | } 19 | 20 | public static void sleep(long millis) { 21 | try { 22 | Thread.sleep(millis); 23 | } catch (InterruptedException e) { 24 | e.printStackTrace(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_15.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.TimeUnit; 7 | 8 | public class Ch8_15 { 9 | public static void main(String[] args) { 10 | Flowable.interval(1, TimeUnit.MILLISECONDS) 11 | .onBackpressureDrop(i -> 12 | System.out.println("Dropping " + i)) 13 | .observeOn(Schedulers.io()) 14 | .subscribe(i -> { 15 | sleep(5); 16 | System.out.println(i); 17 | }); 18 | sleep(5000); 19 | } 20 | 21 | public static void sleep(long millis) { 22 | try { 23 | Thread.sleep(millis); 24 | } catch (InterruptedException e) { 25 | e.printStackTrace(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_16.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch8_16 { 9 | public static void main(String[] args) { 10 | randomGenerator(1, 10000) 11 | .subscribeOn(Schedulers.computation()) 12 | .doOnNext(i -> System.out.println("Emitting " + 13 | i)) 14 | .observeOn(Schedulers.io()) 15 | .subscribe(i -> { 16 | sleep(50); 17 | System.out.println("Received " + i); 18 | }); 19 | sleep(10000); 20 | } 21 | 22 | static Flowable randomGenerator(int min, int max) { 23 | return Flowable.generate(emitter -> 24 | emitter.onNext(ThreadLocalRandom.current().nextInt(min, max)) 25 | ); 26 | } 27 | 28 | public static void sleep(long millis) { 29 | try { 30 | Thread.sleep(millis); 31 | } catch (InterruptedException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_2.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | public class Ch8_2 { 7 | public static void main(String[] args) { 8 | Observable.range(1, 999_999_999) 9 | .map(MyItem::new) 10 | .observeOn(Schedulers.io()) 11 | .subscribe(myItem -> { 12 | sleep(50); 13 | System.out.println("Received MyItem " + 14 | myItem.id); 15 | }); 16 | sleep(Long.MAX_VALUE); 17 | } 18 | 19 | static void sleep(long milliseconds) { 20 | try { 21 | Thread.sleep(milliseconds); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | static final class MyItem { 28 | final int id; 29 | 30 | MyItem(int id) { 31 | this.id = id; 32 | System.out.println("Constructing MyItem " + id); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_3.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | public class Ch8_3 { 7 | public static void main(String[] args) { 8 | Flowable.range(1, 999_999_999) 9 | .map(MyItem::new) 10 | .observeOn(Schedulers.io()) 11 | .subscribe(myItem -> { 12 | sleep(50); 13 | System.out.println("Received MyItem " + 14 | myItem.id); 15 | }); 16 | sleep(Long.MAX_VALUE); 17 | } 18 | 19 | static void sleep(long milliseconds) { 20 | try { 21 | Thread.sleep(milliseconds); 22 | } catch (InterruptedException e) { 23 | e.printStackTrace(); 24 | } 25 | } 26 | 27 | static final class MyItem { 28 | final int id; 29 | 30 | MyItem(int id) { 31 | this.id = id; 32 | System.out.println("Constructing MyItem " + id); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_4.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Flowable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import java.util.concurrent.ThreadLocalRandom; 7 | 8 | public class Ch8_4 { 9 | public static void main(String[] args) { 10 | Flowable.range(1, 1000) 11 | .doOnNext(s -> System.out.println("Source pushed " 12 | + s)) 13 | .observeOn(Schedulers.io()) 14 | .map(i -> intenseCalculation(i)) 15 | .subscribe(s -> System.out.println("Subscriber received " + s), 16 | Throwable::printStackTrace, 17 | () -> System.out.println("Done!") 18 | ); 19 | sleep(20000); 20 | } 21 | 22 | public static T intenseCalculation(T value) { 23 | //sleep up to 200 milliseconds 24 | sleep(ThreadLocalRandom.current().nextInt(200)); 25 | return value; 26 | } 27 | 28 | public static void sleep(long millis) { 29 | try { 30 | Thread.sleep(millis); 31 | } catch (InterruptedException e) { 32 | e.printStackTrace(); 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_8.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.schedulers.Schedulers; 5 | 6 | import static ch5.Ch5_21.sleep; 7 | 8 | public class Ch8_8 { 9 | public static void main(String[] args) { 10 | Observable source = Observable.create(emitter -> { 11 | for (int i = 0; i <= 1000; i++) { 12 | if (emitter.isDisposed()) 13 | return; 14 | emitter.onNext(i); 15 | } 16 | emitter.onComplete(); 17 | }); 18 | source.observeOn(Schedulers.io()) 19 | .subscribe(System.out::println); 20 | sleep(1000); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter08/Ch8_9.java: -------------------------------------------------------------------------------- 1 | package ch8; 2 | 3 | import io.reactivex.BackpressureStrategy; 4 | import io.reactivex.Flowable; 5 | import io.reactivex.schedulers.Schedulers; 6 | 7 | public class Ch8_9 { 8 | public static void main(String[] args) { 9 | Flowable source = Flowable.create(emitter -> { 10 | for (int i = 0; i <= 1000; i++) { 11 | if (emitter.isCancelled()) 12 | return; 13 | emitter.onNext(i); 14 | } 15 | emitter.onComplete(); 16 | }, BackpressureStrategy.BUFFER); 17 | source.observeOn(Schedulers.io()) 18 | .subscribe(System.out::println); 19 | sleep(1000); 20 | } 21 | 22 | public static void sleep(long millis) { 23 | try { 24 | Thread.sleep(millis); 25 | } catch (InterruptedException e) { 26 | e.printStackTrace(); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /Chapter09/Ch9_1.java: -------------------------------------------------------------------------------- 1 | package ch9; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | import io.reactivex.Observable; 5 | import io.reactivex.ObservableTransformer; 6 | 7 | public class Ch9_1 { 8 | public static void main(String[] args) { 9 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon") 11 | .compose(toImmutableList()) 12 | .subscribe(System.out::println); 13 | Observable.range(1, 10) 14 | .compose(toImmutableList()) 15 | .subscribe(System.out::println); 16 | } 17 | 18 | public static ObservableTransformer> 19 | toImmutableList() { 20 | return upstream -> 21 | upstream.collect(ImmutableList::builder, 22 | ImmutableList.Builder::add) 23 | .map(ImmutableList.Builder::build) 24 | .toObservable(); // must turn Single into Observable 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter09/Ch9_10.java: -------------------------------------------------------------------------------- 1 | package ch9; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.SingleTransformer; 5 | 6 | import java.util.Collection; 7 | import java.util.Collections; 8 | 9 | public class Ch9_10 { 10 | public static void main(String[] args) { 11 | Observable.just("Alpha", "Beta", "Gamma", "Delta", "Epsilon") 12 | .toList() 13 | .compose(toUnmodifiable()) 14 | .subscribe(System.out::println); 15 | } 16 | 17 | public static SingleTransformer, 18 | Collection> toUnmodifiable() { 19 | return singleObserver -> 20 | singleObserver.map(Collections::unmodifiableCollection); 21 | } 22 | } -------------------------------------------------------------------------------- /Chapter09/Ch9_2.java: -------------------------------------------------------------------------------- 1 | package ch9; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.ObservableTransformer; 5 | 6 | public class Ch9_2 { 7 | public static void main(String[] args) { 8 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 9 | "Epsilon") 10 | .compose(joinToString("/")) 11 | .subscribe(System.out::println); 12 | } 13 | 14 | public static ObservableTransformer 15 | joinToString(String separator) { 16 | return upstream -> upstream 17 | .collect(StringBuilder::new, (b, s) -> { 18 | if (b.length() == 0) 19 | b.append(s); 20 | else 21 | b.append(separator).append(s); 22 | }) 23 | .map(StringBuilder::toString) 24 | .toObservable(); 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter09/Ch9_3.java: -------------------------------------------------------------------------------- 1 | package ch9; 2 | 3 | import com.google.common.collect.ImmutableList; 4 | import io.reactivex.Flowable; 5 | import io.reactivex.FlowableTransformer; 6 | 7 | public class Ch9_3 { 8 | public static void main(String[] args) { 9 | Flowable.just("Alpha", "Beta", "Gamma", "Delta", 10 | "Epsilon") 11 | .compose(toImmutableList()) 12 | .subscribe(System.out::println); 13 | Flowable.range(1, 10) 14 | .compose(toImmutableList()) 15 | .subscribe(System.out::println); 16 | } 17 | 18 | public static FlowableTransformer> 19 | toImmutableList() { 20 | return upstream -> 21 | upstream.collect(ImmutableList::builder, 22 | ImmutableList.Builder::add) 23 | .map(ImmutableList.Builder::build) 24 | .toFlowable(); // must turn Single into Flowable 25 | } 26 | } -------------------------------------------------------------------------------- /Chapter09/Ch9_4.java: -------------------------------------------------------------------------------- 1 | package ch9; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.ObservableTransformer; 5 | 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | public class Ch9_4 { 9 | public static void main(String[] args) { 10 | Observable> indexedStrings = 11 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 12 | "Epsilon") 13 | .compose(withIndex()); 14 | indexedStrings.subscribe(v -> 15 | System.out.println("Subscriber 1: " + v)); 16 | indexedStrings.subscribe(v -> 17 | System.out.println("Subscriber 2: " + v)); 18 | } 19 | 20 | static ObservableTransformer> 21 | withIndex() { 22 | final AtomicInteger indexer = new AtomicInteger(-1); 23 | return upstream -> upstream.map(v -> new 24 | IndexedValue(indexer.incrementAndGet(), v)); 25 | } 26 | 27 | static final class IndexedValue { 28 | final int index; 29 | final T value; 30 | 31 | IndexedValue(int index, T value) { 32 | this.index = index; 33 | this.value = value; 34 | } 35 | 36 | @Override 37 | public String toString() { 38 | return index + " - " + value; 39 | } 40 | } 41 | } -------------------------------------------------------------------------------- /Chapter09/Ch9_5.java: -------------------------------------------------------------------------------- 1 | package ch9; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.rxjavafx.observers.JavaFxObserver; 5 | import io.reactivex.rxjavafx.schedulers.JavaFxScheduler; 6 | import javafx.application.Application; 7 | import javafx.beans.binding.Binding; 8 | import javafx.scene.Scene; 9 | import javafx.scene.control.Label; 10 | import javafx.scene.layout.VBox; 11 | 12 | import javafx.stage.Stage; 13 | 14 | import java.util.concurrent.TimeUnit; 15 | 16 | public final class Ch9_5 extends Application { 17 | @Override 18 | public void start(Stage stage) throws Exception { 19 | VBox root = new VBox(); 20 | Label label = new Label(""); 21 | // Observable with second timer 22 | Observable seconds = 23 | Observable.interval(1, TimeUnit.SECONDS) 24 | .map(i -> i.toString()) 25 | .observeOn(JavaFxScheduler.platform()); 26 | // Turn Observable into Binding 27 | Binding binding = 28 | JavaFxObserver.toBinding(seconds); 29 | //Bind Label to Binding 30 | label.textProperty().bind(binding); 31 | root.setMinSize(200, 100); 32 | root.getChildren().addAll(label); 33 | Scene scene = new Scene(root); 34 | stage.setScene(scene); 35 | stage.show(); 36 | } 37 | } -------------------------------------------------------------------------------- /Chapter09/Ch9_6.java: -------------------------------------------------------------------------------- 1 | package ch9; 2 | 3 | import io.reactivex.Observable; 4 | import io.reactivex.rxjavafx.observers.JavaFxObserver; 5 | import io.reactivex.rxjavafx.schedulers.JavaFxScheduler; 6 | import javafx.application.Application; 7 | import javafx.beans.binding.Binding; 8 | import javafx.scene.Scene; 9 | import javafx.scene.control.Label; 10 | import javafx.scene.layout.VBox; 11 | import javafx.stage.Stage; 12 | 13 | import java.util.concurrent.TimeUnit; 14 | 15 | public final class Ch9_6 extends Application { 16 | @Override 17 | public void start(Stage stage) throws Exception { 18 | VBox root = new VBox(); 19 | Label label = new Label(""); 20 | // Turn Observable into Binding 21 | Binding binding = 22 | Observable.interval(1, TimeUnit.SECONDS) 23 | .map(i -> i.toString()) 24 | .observeOn(JavaFxScheduler.platform()) 25 | .to(JavaFxObserver::toBinding); 26 | //Bind Label to Binding 27 | label.textProperty().bind(binding); 28 | root.setMinSize(200, 100); 29 | root.getChildren().addAll(label); 30 | Scene scene = new Scene(root); 31 | stage.setScene(scene); 32 | stage.show(); 33 | } 34 | } -------------------------------------------------------------------------------- /Chapter10/build.gradle: -------------------------------------------------------------------------------- 1 | repositories { 2 | mavenCentral() 3 | } 4 | 5 | apply plugin: 'java' 6 | 7 | sourceSets { 8 | test.java.srcDirs += '/src/main/java/' 9 | } 10 | 11 | dependencies { 12 | compile 'io.reactivex.rxjava2:rxjava:2.1.+' 13 | testCompile 'junit:junit:4.12' 14 | } 15 | 16 | -------------------------------------------------------------------------------- /Chapter10/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter10/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter10/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jun 17 17:24:03 CDT 2017 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-3.4.1-bin.zip 7 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_1.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.concurrent.TimeUnit; 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | import static org.junit.Assert.assertTrue; 7 | 8 | public class Ch10_1 { 9 | @Test 10 | public void testBlockingSubscribe() { 11 | AtomicInteger hitCount = new AtomicInteger(); 12 | 13 | Observable source = Observable.interval(1, 14 | TimeUnit.SECONDS) 15 | .take(5); 16 | source.subscribe(i -> hitCount.incrementAndGet()); 17 | assertTrue(hitCount.get() == 5); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_10.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.concurrent.TimeUnit; 5 | 6 | public class Ch10_10 { 7 | @Test 8 | public void testBlockingLatest() { 9 | Observable source = 10 | Observable.interval(1, TimeUnit.MICROSECONDS) 11 | .take(1000); 12 | 13 | Iterable iterable = source.blockingLatest(); 14 | 15 | for (Long i: iterable) { 16 | System.out.println(i); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_11.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.concurrent.TimeUnit; 5 | 6 | public class Ch10_11 { 7 | @Test 8 | public void testBlockingMostRecent() { 9 | Observable source = 10 | Observable.interval(10, TimeUnit.MILLISECONDS) 11 | .take(5); 12 | Iterable iterable = source.blockingMostRecent(-1L); 13 | for (Long i: iterable) { 14 | System.out.println(i); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_14.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import io.reactivex.observers.TestObserver; 3 | import org.junit.Test; 4 | 5 | public class Ch10_14 { 6 | @Test 7 | public void debugWalkthrough() { 8 | //Declare TestObserver 9 | TestObserver testObserver = new TestObserver<>(); 10 | 11 | //Source pushing three strings 12 | Observable items = 13 | Observable.just("521934/2342/Foxtrot", 14 | "Bravo/12112/78886/Tango", 15 | "283242/4542/Whiskey/2348562"); 16 | 17 | //Split and concatMap() on "/" 18 | items.concatMap(s -> 19 | Observable.fromArray(s.split("/")) 20 | ) 21 | //filter for only alphabetic Strings using regex 22 | .filter(s -> s.matches("[A-Z]+")) 23 | //Subscribe the TestObserver 24 | .subscribe(testObserver); 25 | 26 | //Why are no values being emitted? 27 | System.out.println(testObserver.values()); 28 | 29 | //This fails due to no values 30 | testObserver.assertValues("Foxtrot","Bravo","Tango","Whiskey"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_15.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import io.reactivex.observers.TestObserver; 3 | import org.junit.Test; 4 | 5 | public class Ch10_15 { 6 | @Test 7 | public void debugWalkthrough() { 8 | 9 | //Declare TestObserver 10 | TestObserver testObserver = new TestObserver<>(); 11 | 12 | //Source pushing three strings 13 | Observable items = 14 | Observable.just("521934/2342/Foxtrot", 15 | "Bravo/12112/78886/Tango", 16 | "283242/4542/Whiskey/2348562"); 17 | 18 | //Split and concatMap() on "/" 19 | items.concatMap(s -> 20 | Observable.fromArray(s.split("/")) 21 | ) 22 | //filter for only alphabetic Strings using regex 23 | .filter(s -> s.matches("[A-Za-z]+")) 24 | //Subscribe the TestObserver 25 | .subscribe(testObserver); 26 | //This succeeds 27 | testObserver.assertValues("Foxtrot","Bravo","Tango","Whiskey"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_2.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.concurrent.TimeUnit; 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class Ch10_2 { 10 | @Test 11 | public void testBlockingSubscribe() { 12 | AtomicInteger hitCount = new AtomicInteger(); 13 | Observable source = Observable.interval(1, 14 | TimeUnit.SECONDS) 15 | .take(5); 16 | source.subscribe(i -> hitCount.incrementAndGet()); 17 | assertTrue(hitCount.get() == 5); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_3.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.concurrent.TimeUnit; 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class Ch10_3 { 10 | @Test 11 | public void testBlockingSubscribe() { 12 | AtomicInteger hitCount = new AtomicInteger(); 13 | Observable source = Observable.interval(1, 14 | TimeUnit.SECONDS) 15 | .take(5); 16 | source.blockingSubscribe(i -> hitCount.incrementAndGet()); 17 | assertTrue(hitCount.get() == 5); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_4.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.concurrent.TimeUnit; 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class Ch10_4 { 10 | @Test 11 | public void testFirst() { 12 | Observable source = 13 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 14 | "Zeta"); 15 | String firstWithLengthFour = source.filter(s -> s.length() 16 | == 4) 17 | .blockingFirst(); 18 | assertTrue(firstWithLengthFour.equals("Beta")); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_5.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class Ch10_5 { 10 | @Test 11 | public void testSingle() { 12 | Observable source = 13 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 14 | "Zeta"); 15 | List allWithLengthFour = source.filter(s -> 16 | s.length() == 4) 17 | .toList() 18 | .blockingGet(); 19 | assertTrue(allWithLengthFour.equals(Arrays.asList("Beta","Zeta"))); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_6.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | import static org.junit.Assert.assertTrue; 8 | 9 | public class Ch10_6 { 10 | @Test 11 | public void testLast() { 12 | Observable source = 13 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 14 | "Zeta"); 15 | String lastWithLengthFour = source.filter(s -> s.length() 16 | == 4) 17 | .blockingLast(); 18 | assertTrue(lastWithLengthFour.equals("Zeta")); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_7.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import static org.junit.Assert.assertTrue; 5 | 6 | public class Ch10_7 { 7 | @Test 8 | public void testIterable() { 9 | Observable source = 10 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 11 | "Zeta"); 12 | Iterable allWithLengthFive = source.filter(s -> 13 | s.length() == 5) 14 | .blockingIterable(); 15 | for (String s: allWithLengthFive) { 16 | assertTrue(s.length() == 5); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_8.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import static org.junit.Assert.assertTrue; 5 | 6 | public class Ch10_8 { 7 | @Test 8 | public void testBlockingForEach() { 9 | Observable source = 10 | Observable.just("Alpha", "Beta", "Gamma", "Delta", 11 | "Zeta"); 12 | source.filter(s -> s.length() == 5) 13 | .blockingForEach(s -> assertTrue(s.length() == 14 | 5)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Chapter10/src/test/java/Ch10_9.java: -------------------------------------------------------------------------------- 1 | import io.reactivex.Observable; 2 | import org.junit.Test; 3 | 4 | import java.util.concurrent.TimeUnit; 5 | 6 | public class Ch10_9 { 7 | @Test 8 | public void testBlockingNext() { 9 | Observable source = 10 | Observable.interval(1, TimeUnit.MICROSECONDS) 11 | .take(1000); 12 | Iterable iterable = source.blockingNext(); 13 | for (Long i: iterable) { 14 | System.out.println(i); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Chapter11/code1/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/thomas/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/androidTest/java/packtpub/com/rxjavaapp/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("packtpub.com.rxjavaapp", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/java/packtpub/com/rxjavaapp/MainActivity.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | 6 | public class MainActivity extends AppCompatActivity { 7 | 8 | @Override 9 | protected void onCreate(Bundle savedInstanceState) { 10 | super.onCreate(savedInstanceState); 11 | setContentView(R.layout.activity_main); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RxJavaApp 3 | 4 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter11/code1/app/src/test/java/packtpub/com/rxjavaapp/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.Callable; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | /** 10 | * Example local unit test, which will execute on the development machine (host). 11 | * 12 | * @see Testing documentation 13 | */ 14 | public class ExampleUnitTest { 15 | @Test 16 | public void lambdaTest() throws Exception { 17 | Callable callable = () -> 4; 18 | System.out.println(callable.call()); 19 | } 20 | } -------------------------------------------------------------------------------- /Chapter11/code1/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.3.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /Chapter11/code1/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /Chapter11/code1/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code1/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter11/code1/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jun 17 18:05:02 CDT 2017 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-3.3-all.zip 7 | -------------------------------------------------------------------------------- /Chapter11/code1/local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file should *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | sdk.dir=/home/thomas/Android/Sdk -------------------------------------------------------------------------------- /Chapter11/code1/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /Chapter11/code2/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/thomas/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/androidTest/java/packtpub/com/rxjavaapp/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("packtpub.com.rxjavaapp", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/java/packtpub/com/rxjavaapp/MainActivity.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.widget.TextView; 6 | 7 | import java.util.concurrent.TimeUnit; 8 | 9 | import io.reactivex.Observable; 10 | import io.reactivex.android.schedulers.AndroidSchedulers; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | TextView myTextView = (TextView) 19 | findViewById(R.id.my_text_view); 20 | Observable.just("Goodbye World!") 21 | .delay(3, TimeUnit.SECONDS) 22 | .observeOn(AndroidSchedulers.mainThread()) 23 | .subscribe(s -> myTextView.setText(s)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 18 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RxJavaApp 3 | 4 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /Chapter11/code2/app/src/test/java/packtpub/com/rxjavaapp/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.Callable; 6 | 7 | import static org.junit.Assert.*; 8 | 9 | /** 10 | * Example local unit test, which will execute on the development machine (host). 11 | * 12 | * @see Testing documentation 13 | */ 14 | public class ExampleUnitTest { 15 | @Test 16 | public void lambdaTest() throws Exception { 17 | Callable callable = () -> 4; 18 | System.out.println(callable.call()); 19 | } 20 | } -------------------------------------------------------------------------------- /Chapter11/code2/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.3.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /Chapter11/code2/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /Chapter11/code2/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Learning-RxJava/931eb73e7bab1b0e56fa9fcd903ae9346183780b/Chapter11/code2/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /Chapter11/code2/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Sat Jun 17 18:05:02 CDT 2017 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-3.3-all.zip 7 | -------------------------------------------------------------------------------- /Chapter11/code2/local.properties: -------------------------------------------------------------------------------- 1 | ## This file is automatically generated by Android Studio. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file should *NOT* be checked into Version Control Systems, 5 | # as it contains information specific to your local configuration. 6 | # 7 | # Location of the SDK. This is only used by Gradle. 8 | # For customization when using a Version Control System, please read the 9 | # header note. 10 | sdk.dir=/home/thomas/Android/Sdk -------------------------------------------------------------------------------- /Chapter11/code2/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /Chapter11/code3/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/thomas/Android/Sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /Chapter11/code3/app/src/androidTest/java/packtpub/com/rxjavaapp/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("packtpub.com.rxjavaapp", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Chapter11/code3/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /Chapter11/code3/app/src/main/java/packtpub/com/rxjavaapp/MainActivity.java: -------------------------------------------------------------------------------- 1 | package packtpub.com.rxjavaapp; 2 | 3 | import android.support.v7.app.AppCompatActivity; 4 | import android.os.Bundle; 5 | import android.widget.Button; 6 | import android.widget.TextView; 7 | 8 | import com.jakewharton.rxbinding2.view.RxView; 9 | 10 | import java.util.concurrent.TimeUnit; 11 | 12 | import io.reactivex.Observable; 13 | import io.reactivex.android.schedulers.AndroidSchedulers; 14 | 15 | public class MainActivity extends AppCompatActivity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | TextView myTextView = (TextView) 22 | findViewById(R.id.my_text_view); 23 | Button incrementButton = (Button) 24 | findViewById(R.id.increment_button); 25 | //broadcast clicks into a cumulative increment, and display in TextView 26 | RxView.clicks(incrementButton) 27 | .map(o -> 1) 28 | .scan(0,(total, next) -> total + next) 29 | .subscribe(i -> myTextView.setText(i.toString())); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Chapter11/code3/app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 15 |