1 package org.oxerr.okcoin.websocket.dto;
2
3 import java.math.BigDecimal;
4 import java.time.Instant;
5 import java.util.stream.Collectors;
6
7 import javax.json.JsonArray;
8 import javax.json.JsonObject;
9 import javax.json.JsonValue;
10
11 public class Depth extends org.oxerr.okcoin.rest.dto.Depth {
12
13 private static final long serialVersionUID = 2015022801L;
14
15 private static final BigDecimal[][] EMPTY_DEPTH = new BigDecimal[2][];
16
17 private final Instant timestamp;
18
19 public Depth(JsonValue jsonValue) {
20 this((JsonObject) jsonValue);
21 }
22
23 public Depth(JsonObject jsonObject) {
24 this(jsonObject.getJsonArray("asks"), jsonObject.getJsonArray("bids"),
25 Instant.ofEpochMilli(Long.parseLong(jsonObject.getString("timestamp"))));
26 }
27
28 public Depth(JsonArray asks, JsonArray bids, Instant timestamp) {
29 super(asks.stream().map(v -> {
30 JsonArray a = (JsonArray) v;
31 BigDecimal px = a.getJsonNumber(0).bigDecimalValue();
32 BigDecimal qty = a.getJsonNumber(1).bigDecimalValue();
33 return new BigDecimal[] { px, qty };
34 }).collect(Collectors.toList()).toArray(EMPTY_DEPTH),
35
36 bids.stream().map(v -> {
37 JsonArray a = (JsonArray) v;
38 BigDecimal px = a.getJsonNumber(0).bigDecimalValue();
39 BigDecimal qty = a.getJsonNumber(1).bigDecimalValue();
40 return new BigDecimal[] { px, qty };
41 }).collect(Collectors.toList()).toArray(EMPTY_DEPTH));
42
43 this.timestamp = timestamp;
44 }
45
46 public Instant getTimestamp() {
47 return timestamp;
48 }
49
50 }