Last active
January 13, 2017 08:57
-
-
Save EarlOfEgo/52177d5c9b4f9f91a493686641561756 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.gini; | |
import rx.Observable; | |
import rx.functions.Action0; | |
import rx.functions.Action1; | |
import rx.functions.Func1; | |
import java.util.*; | |
/** | |
* Created by stephan on 03/01/2017. | |
*/ | |
public class ReactiveX { | |
private static List<String> mCache = null; | |
//get cached values | |
public static Observable<List<String>> getCachedUserList() { | |
return Observable.just(mCache); | |
} | |
//https://api.github.com/search/users?q=language:kotlin | |
public static Observable<List<String>> getUserList() { | |
return Observable.just(new ArrayList<>(Arrays.asList("Seymour", "Al", "Ivana", "Hugh", "Yuri", "Kalle", "Hans", "Sepp"))); | |
} | |
//https://api.github.com/users/{username} | |
static Observable<String> getEmail(final String user) { | |
switch (user) { | |
case "Seymour": | |
return Observable.just("[email protected]"); | |
case "Al": | |
return Observable.just("[email protected]"); | |
case "Ivana": | |
return Observable.just("[email protected]"); | |
case "Hugh": | |
return Observable.just("[email protected]"); | |
case "Yuri": | |
return Observable.just("[email protected]"); | |
} | |
return Observable.just(null); | |
} | |
public static void main(String[] args) { | |
Observable.concat(getCachedUserList(), getUserList()) | |
.takeFirst(new Func1<List<String>, Boolean>() { | |
@Override | |
public Boolean call(List<String> strings) { | |
return strings != null; | |
} | |
}) | |
.doOnNext(new Action1<List<String>>() { | |
@Override | |
public void call(List<String> strings) { | |
mCache = strings; | |
} | |
}) | |
.flatMap(new Func1<List<String>, Observable<String>>() { | |
@Override | |
public Observable<String> call(List<String> strings) { | |
return Observable.from(strings); | |
} | |
}) | |
.flatMap(new Func1<String, Observable<String>>() { | |
@Override | |
public Observable<String> call(String s) { | |
return getEmail(s); | |
} | |
}) | |
.filter(new Func1<String, Boolean>() { | |
@Override | |
public Boolean call(String s) { | |
return s != null; | |
} | |
}) | |
.take(5) | |
.subscribe(new Action1<String>() { | |
@Override | |
public void call(String s) { | |
System.out.println(s); | |
} | |
}, new Action1<Throwable>() { | |
@Override | |
public void call(Throwable throwable) { | |
System.err.println(throwable.getLocalizedMessage()); | |
} | |
}, new Action0() { | |
@Override | |
public void call() { | |
System.out.println("onComplete"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment