io.mstream.trader.simulation.stocks.datafeed.DataFeedStockPriceRepository Maven / Gradle / Ivy
package io.mstream.trader.simulation.stocks.datafeed;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixObservableCommand;
import io.mstream.trader.simulation.stocks.Stock;
import io.mstream.trader.simulation.stocks.StocksPriceRepository;
import io.mstream.trader.simulation.stocks.datafeed.client.DataFeedClient;
import io.mstream.trader.simulation.stocks.datafeed.data.StockPrice;
import io.mstream.trader.simulation.stocks.datafeed.data.StockPriceFactory;
import rx.Observable;
import rx.Single;
import javax.inject.Inject;
import java.time.LocalDate;
public class DataFeedStockPriceRepository
implements StocksPriceRepository {
private final DataFeedClient dataFeedClient;
private final StockPriceFactory stockPriceFactory;
@Inject
public DataFeedStockPriceRepository(
DataFeedClient dataFeedClient,
StockPriceFactory stockPriceFactory
) {
this.dataFeedClient = dataFeedClient;
this.stockPriceFactory = stockPriceFactory;
}
@Override
public Single get(Key key) {
return new GetCommand(
key.getStock(),
key.getDate()
)
.toObservable()
.toSingle();
}
private class GetCommand
extends HystrixObservableCommand {
private final Stock stock;
private final LocalDate date;
private GetCommand(Stock stock, LocalDate date) {
super(HystrixCommandGroupKey.Factory.asKey("DataFeed"));
this.stock = stock;
this.date = date;
}
@Override
protected Observable construct() {
return dataFeedClient
.getPrice(
stock,
date
)
.map(stockPriceFactory::create)
.toObservable();
}
@Override
protected Observable resumeWithFallback() {
return Observable.just(stockPriceFactory.create("100.00"));
}
}
}