package fi.reaktor.training.totallylazy.data;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.googlecode.totallylazy.Sequence;

import java.io.IOException;
import java.io.InputStream;

public class Beers {

    private static Sequence<Beer> beers;
    // TODO: static init is nasty and JSON should be read asynchronously e.g. with RxJava
    public static Beers init(InputStream inputStream, ObjectMapper objectMapper) throws IOException {
        beers = objectMapper.readValue(inputStream, new TypeReference<Sequence<Beer>>() {});
        return new Beers();
    }

    public static Sequence<Beer> listAll() {
        return beers;
    }
}

@@ -252,355 +252,954 @@
 m;%0A%0A
-public class Beers %7B%0A%0A    private static Sequence%3CBeer%3E beers;%0A    // TODO: static init is nasty and JSON should be read asynchronously e.g. with RxJava%0A    public static Beers init(InputStream inputStream, ObjectMapper objectMapper) throws IOException %7B%0A        beers = objectMapper.readValue(inputStream, new TypeReference%3CSequence%3CBeer%3E%3E() %7B%7D);
+import rx.Observable;%0Aimport rx.android.schedulers.AndroidSchedulers;%0Aimport rx.schedulers.Schedulers;%0A%0Apublic class Beers %7B%0A%0A    private static Sequence%3CBeer%3E beers;%0A    private static Observable%3CSequence%3CBeer%3E%3E beerObservable;%0A%0A    public static Beers init(InputStream inputStream, ObjectMapper objectMapper) throws IOException %7B%0A        beers = objectMapper.readValue(inputStream, new TypeReference%3CSequence%3CBeer%3E%3E() %7B%7D);%0A%0A        // TODO: not used yet%0A        beerObservable = Observable.%3CSequence%3CBeer%3E%3Ecreate((subscriber) -%3E %7B%0A            try %7B%0A                Sequence%3CBeer%3E beerSeq = objectMapper.readValue(inputStream, new TypeReference%3CSequence%3CBeer%3E%3E() %7B%0A                %7D);%0A                subscriber.onNext(beerSeq);%0A                subscriber.onCompleted();%0A            %7D catch (IOException e) %7B%0A                subscriber.onError(e);%0A            %7D%0A        %7D).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());%0A
 %0A   

Add async json loading with RxJava