All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.outbrain.ob1k.concurrent.stream.FutureProviderToStreamHandler Maven / Gradle / Ivy

package com.outbrain.ob1k.concurrent.stream;

import com.outbrain.ob1k.concurrent.ComposableFuture;
import com.outbrain.ob1k.concurrent.Consumer;
import com.outbrain.ob1k.concurrent.Try;
import com.outbrain.ob1k.concurrent.handlers.FutureProvider;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by aronen on 11/9/14.
 * 

* creates a stream from a future provider. * each time a future provides the value, the next future is generated. */ public class FutureProviderToStreamHandler implements Observable.OnSubscribe { private final FutureProvider provider; volatile Subscriber subscriber; public FutureProviderToStreamHandler(final FutureProvider provider) { this.provider = provider; } @Override public void call(final Subscriber subscriber) { this.subscriber = subscriber; final boolean next = provider.moveNext(); if (next) { final ComposableFuture nextFuture = provider.current(); handleNextFuture(nextFuture); } else { subscriber.onCompleted(); } } private void handleNextFuture(final ComposableFuture nextFuture) { nextFuture.consume(new Consumer() { @Override public void consume(final Try result) { if (result.isSuccess()) { subscriber.onNext(result.getValue()); final boolean next = provider.moveNext(); if (next) { handleNextFuture(provider.current()); } else { subscriber.onCompleted(); } } else { subscriber.onError(result.getError()); } } }); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy