Add null checks

This commit is contained in:
Manfred Karrer 2017-03-18 17:17:54 -05:00
parent f2c9ff247b
commit eb6360dffa
5 changed files with 28 additions and 16 deletions

View file

@ -23,13 +23,13 @@ public class PriceData {
public final double a; // ask;
public final double b; // bid
public final double l; // last
public final long e; // epochSec
public final long ts; // timestamp
public PriceData(String currencyCode, double ask, double bid, double last, long epochSec) {
public PriceData(String currencyCode, double ask, double bid, double last, long ts) {
this.c = currencyCode;
this.a = ask;
this.b = bid;
this.l = last;
this.e = epochSec;
this.ts = ts;
}
}

View file

@ -155,7 +155,10 @@ public class PriceRequestService {
.filter(e -> poloniexMap == null || !poloniexMap.containsKey(e.getKey()))
.forEach(e -> allPricesMap.put(e.getKey(), e.getValue()));
coinmarketcapTs = Instant.now().getEpochSecond();
log.info("Coinmarketcap LTC (last): " + map.get("LTC").l);
if (map.get("LTC") != null)
log.info("Coinmarketcap LTC (last): " + map.get("LTC").l);
writeToJson();
}
@ -167,15 +170,21 @@ public class PriceRequestService {
removeOutdatedPrices(allPricesMap);
allPricesMap.putAll(poloniexMap);
poloniexTs = Instant.now().getEpochSecond();
log.info("Poloniex LTC (last): " + poloniexMap.get("LTC").l);
if (poloniexMap.get("LTC") != null)
log.info("Poloniex LTC (last): " + poloniexMap.get("LTC").l);
writeToJson();
}
private void requestBtcAverageLocalPrices() throws NoSuchAlgorithmException, InvalidKeyException, IOException, HttpException {
long ts = System.currentTimeMillis();
btcAverageLocalMap = btcAverageProvider.getLocal();
log.info("BTCAverage local USD (last):" + btcAverageLocalMap.get("USD").l);
if (btcAverageLocalMap.get("USD") != null)
log.info("BTCAverage local USD (last):" + btcAverageLocalMap.get("USD").l);
log.info("requestBtcAverageLocalPrices took {} ms.", (System.currentTimeMillis() - ts));
removeOutdatedPrices(allPricesMap);
allPricesMap.putAll(btcAverageLocalMap);
btcAverageTs = Instant.now().getEpochSecond();
@ -185,8 +194,11 @@ public class PriceRequestService {
private void requestBtcAverageGlobalPrices() throws NoSuchAlgorithmException, InvalidKeyException, IOException, HttpException {
long ts = System.currentTimeMillis();
Map<String, PriceData> map = btcAverageProvider.getGlobal();
log.info("BTCAverage global USD (last):" + map.get("USD").l);
if (map.get("USD") != null)
log.info("BTCAverage global USD (last):" + map.get("USD").l);
log.info("requestBtcAverageGlobalPrices took {} ms.", (System.currentTimeMillis() - ts));
removeOutdatedPrices(btcAverageLocalMap);
removeOutdatedPrices(allPricesMap);
// we don't replace prices which we got form the local request, just in case the global data are received
@ -208,10 +220,10 @@ public class PriceRequestService {
}
private void removeOutdatedPrices(Map<String, PriceData> map) {
long epochSecond = Instant.now().getEpochSecond();
long limit = epochSecond - MARKET_PRICE_TTL_SEC;
long now = Instant.now().getEpochSecond();
long limit = now - MARKET_PRICE_TTL_SEC;
Map<String, PriceData> filtered = map.entrySet().stream()
.filter(e -> e.getValue().e > limit)
.filter(e -> e.getValue().ts > limit)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
map.clear();
map.putAll(filtered);

View file

@ -67,7 +67,7 @@ public class BtcAverageProvider {
private Map<String, PriceData> getMap(String json) {
Map<String, PriceData> marketPriceMap = new HashMap<>();
LinkedTreeMap<String, Object> treeMap = new Gson().<LinkedTreeMap<String, Object>>fromJson(json, LinkedTreeMap.class);
long epochSec = Instant.now().getEpochSecond();
long ts = Instant.now().getEpochSecond();
treeMap.entrySet().stream().forEach(e -> {
Object value = e.getValue();
// We need to check the type as we get an unexpected "timestamp" object at the end:
@ -79,7 +79,7 @@ public class BtcAverageProvider {
(double) data.get("ask"),
(double) data.get("bid"),
(double) data.get("last"),
epochSec));
ts));
}
});
return marketPriceMap;

View file

@ -34,7 +34,7 @@ public class CoinmarketcapProvider {
Map<String, PriceData> marketPriceMap = new HashMap<>();
String response = httpClient.requestWithGET("v1/ticker/?limit=200", "User-Agent", "");
List<LinkedTreeMap<String, Object>> list = new Gson().fromJson(response, ArrayList.class);
long epochSec = Instant.now().getEpochSecond();
long ts = Instant.now().getEpochSecond();
list.stream().forEach(treeMap -> {
String code = (String) treeMap.get("symbol");
if (supportedAltcoins.contains(code)) {
@ -43,7 +43,7 @@ public class CoinmarketcapProvider {
price_btc,
price_btc,
price_btc,
epochSec));
ts));
}
});
return marketPriceMap;

View file

@ -37,7 +37,7 @@ public class PoloniexProvider {
Map<String, PriceData> marketPriceMap = new HashMap<>();
String response = httpClient.requestWithGET("?command=returnTicker", "User-Agent", "");
LinkedTreeMap<String, Object> treeMap = new Gson().fromJson(response, LinkedTreeMap.class);
long epochSec = Instant.now().getEpochSecond();
long ts = Instant.now().getEpochSecond();
treeMap.entrySet().stream().forEach(e -> {
Object value = e.getValue();
String invertedCurrencyPair = e.getKey();
@ -54,7 +54,7 @@ public class PoloniexProvider {
parseDouble((String) data.get("lowestAsk")),
parseDouble((String) data.get("highestBid")),
parseDouble((String) data.get("last")),
epochSec)
ts)
);
}
}