Posted on

Loading data from few sources by rxjava

Observable<Data> memory = ...;  
Observable<Data> disk = ...;  
Observable<Data> network = ...;
// Retrieve the first source with data
Observable<Data> source = Observable  
  .concat(memory, disk, network)
  .first();

Save data to cache

Observable<Data> networkWithSave = network.doOnNext(data -> {  
  saveToDisk(data);
  cacheInMemory(data);
});
Observable<Data> diskWithCache = disk.doOnNext(data -> {  
  cacheInMemory(data);
});

Update data

Observable<Data> source = Observable  
  .concat(memory, diskWithCache, networkWithSave)
  .first(data -> data.isUpToDate());
Posted on

Java feature

  1. double brace
  2. ThreadLocal
  3. Instance Initializers
    public class Foo {
      public Foo() {
        System.out.println("constructor called");
      }
     
      static {
        System.out.println("static initializer called");
      }
     
      {
        System.out.println("instance initializer called");
      }
    }
    
  4. Enum – is a class
  5. try, finally, exception
    public static int f() {
        try {
          throw new RuntimeException();
        } finally {
          return 0;
        }
      }
    
  6. URL
    new URL("http://www.yahoo.com").equals(new URL("http://209.191.93.52"))
    

    =true