RxJava: using flatMap method
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Fri, 2 Dec 2016 22:44:05 +0000 (23:44 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 4 Dec 2016 22:29:05 +0000 (23:29 +0100)
Allgemeines/RxJava/src/main/java/de/rxjava/tests/MainRxJava.java
Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/AsyncHTTPClient.java
Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/FlatMapTestImpl.java [new file with mode: 0644]

index 09c0d70..1999493 100644 (file)
@@ -1,12 +1,12 @@
 package de.rxjava.tests;
 
 import de.rxjava.tests.service.impl.AsyncHTTPClient;
+import de.rxjava.tests.service.impl.FlatMapTestImpl;
 
 public class MainRxJava {
 
        public static void main(String[] args) {
-               AsyncHTTPClient asyncHTTPClient = new AsyncHTTPClient();
-               
-               asyncHTTPClient.getPages();
+               new AsyncHTTPClient().getPages();
+               new FlatMapTestImpl().getWords();
        }
 }
index 331356d..0a0f11e 100644 (file)
@@ -1,7 +1,6 @@
 package de.rxjava.tests.service.impl;
 
 import java.io.IOException;
-import java.net.MalformedURLException;
 import java.net.URL;
 
 import de.rxjava.tests.httpclient.CustomHTTPClient;
@@ -27,7 +26,7 @@ public class AsyncHTTPClient {
                });
                
                
-               System.out.println("YOU SEE ME FIRST!!!!");
+               System.out.println("AsyncHTTPClient: YOU SEE ME FIRST!!!!");
                
                
        try {   
@@ -42,7 +41,7 @@ public class AsyncHTTPClient {
        private Observable<String> getDataAsync(String uri) {
         return getDataSync(uri)
                        .subscribeOn(Schedulers.io());  // Creates a pool of threads for us which will run the code implemented below :)
-                                                // THIE METHOD DOES NOT START TO RUN MY CODE!!! IT IS DONE BY subscribe METHOD!!!
+                                                // THIS METHOD DOES NOT START TO RUN MY CODE!!! IT IS DONE BY subscribe METHOD!!!
     }
        
        private Observable<String> getDataSync(String uri) {
@@ -52,7 +51,7 @@ public class AsyncHTTPClient {
                try {
                                data = CustomHTTPClient.newInstance("RxJavaTest").retrieveDataAsString(new URL(uri));
                                
-                               // Making it slower as if having a bad connection :)
+                               // Making it slower as if I had a bad connection :)
                                Thread.sleep(2000);
                        } catch (InterruptedException exception) {
                                // Do not forget good patterns when dealing with InterruptedException :(
diff --git a/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/FlatMapTestImpl.java b/Allgemeines/RxJava/src/main/java/de/rxjava/tests/service/impl/FlatMapTestImpl.java
new file mode 100644 (file)
index 0000000..a06bf33
--- /dev/null
@@ -0,0 +1,61 @@
+package de.rxjava.tests.service.impl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import rx.Observable;
+import rx.schedulers.Schedulers;
+
+public class FlatMapTestImpl {
+
+       
+       public void getWords() {
+               
+               getWordsAsync()
+               .flatMap(words -> {                // flatmap method enables us to return one Observable. The subscribe method does not have such feature.
+                       return Observable.from(words);
+               })
+               .subscribe(word -> {               //Unlike JavaScript Promises, we can call many times the same promise without resolving it.
+                                                      //This stuff would be impossible in JavaScript :)
+                       System.out.println(word);
+               });
+               
+               System.out.println("FlatMapTestImpl: YOU SEE ME FIRST!!!!");
+               
+       try {   
+                       Thread.sleep(30000);
+               } catch (InterruptedException exception) {
+                       // Do not forget good patterns when dealing with InterruptedException :(
+                       Thread.currentThread().interrupt();
+               }
+
+       }
+       
+       private Observable<List<String>> getWordsAsync() {
+        return getWordsSync()
+                       .subscribeOn(Schedulers.io());  // Creates a pool of threads for us which will run the code implemented below :)
+                                                // THIS METHOD DOES NOT START TO RUN MY CODE!!! IT IS DONE BY subscribe METHOD!!!
+    }
+       
+       private Observable<List<String>> getWordsSync() {
+        return Observable.create(observer -> {
+               System.out.println(Thread.currentThread().getName());
+               
+               String[] words = { "gus", "stuff", "given", "no", "way" };
+               try {
+                               // Making it slower as if having a bad connection :)
+                               Thread.sleep(2000);
+                       } catch (InterruptedException exception) {
+                               // Do not forget good patterns when dealing with InterruptedException :(
+                               Thread.currentThread().interrupt();
+                               
+                               observer.onError(exception);
+                       }
+
+               // When do you use this stuff?
+               // observer.onCompleted();
+               
+            observer.onNext(Arrays.asList(words));    
+        });
+    }
+}