Guest User

Untitled

a guest
Nov 23rd, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. // Type could be something else
  2. Callable<String> callHello = new Callable<String>() {
  3. @Override
  4. public String call() throws Exception {
  5. // Could be a db query, api call...
  6. return "Hello";
  7. }
  8. };
  9.  
  10. Callable<String> callWorld = new Callable<String>() {
  11. @Override
  12. public String call() throws Exception {
  13. return "World";
  14. }
  15. };
  16.  
  17. Observable.just(callHello, callWorld)
  18. .map(new Function<Callable<String>, String>() {
  19. @Override
  20. public String apply(Callable<String> callable) throws Exception {
  21. return callable.call();
  22. }
  23. })
  24. .subscribe(new Consumer<String>() {
  25. @Override
  26. public void accept(String x) throws Exception {
  27. System.out.println(x);
  28. }
  29. });
  30.  
  31. interface ModelRepository<T extends Model> {
  32. List<T> listModels();
  33. }
  34.  
  35. class UserRepository implements ModelRepository<User> {
  36.  
  37. @Override
  38. public List<User> listModels() {
  39. return userDAO.list();
  40. }
  41. }
  42.  
  43. class EventRepository implements ModelRepository<Event> {
  44.  
  45. @Override
  46. public List<Event> listModels() {
  47. return eventDAO.list();
  48. }
  49. }
  50.  
  51. Observable.just(userRepository, eventRepository)
  52. .map(ModelRepository::listModels)
  53. .subscribe(listOfModels -> {/* do something with List<Model> */});
  54.  
  55. Observable.fromCallable(() -> blockingGetUserFromRemote())
  56. .flatMap(user -> Observable.fromCallable(() -> blockingSaveUserToLocal(user)))
  57. .subscribe(saveResult -> { /* Do something with save result */ });
Add Comment
Please, Sign In to add comment