1 package org.oxerr.okcoin.websocket.dto; 2 3 import java.math.BigDecimal; 4 import java.text.DateFormat; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.TimeZone; 8 import java.util.stream.Collectors; 9 10 import javax.json.JsonArray; 11 import javax.json.JsonValue; 12 13 import org.oxerr.okcoin.rest.dto.Trade; 14 import org.oxerr.okcoin.rest.dto.Type; 15 16 public class TradesV1 extends BaseObject { 17 18 private static final long serialVersionUID = 2015030501L; 19 20 private static final Trade[] EMPTY_TRADES = new Trade[0]; 21 22 private static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 23 private static final DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 24 25 static { 26 TimeZone timeZone = TimeZone.getTimeZone("Asia/Shanghai"); 27 dateFormat.setTimeZone(timeZone); 28 timeFormat.setTimeZone(timeZone); 29 } 30 31 private final Trade[] trades; 32 33 public TradesV1(JsonValue jsonValue) { 34 this((JsonArray) jsonValue); 35 } 36 37 public TradesV1(JsonArray jsonArray) { 38 String today; 39 synchronized (dateFormat) { 40 today = dateFormat.format(new Date()); 41 } 42 43 this.trades = jsonArray.stream().map(v -> { 44 JsonArray a = (JsonArray) v; 45 long tid = Long.parseLong(a.getString(0)); 46 BigDecimal px = new BigDecimal(a.getString(1)); 47 BigDecimal qty = new BigDecimal(a.getString(2)); 48 Date time; 49 synchronized (timeFormat) { 50 try { 51 time = timeFormat.parse(String.format("%s %s", today, a.getString(3))); 52 } catch (Exception e) { 53 throw new IllegalArgumentException(e); 54 } 55 } 56 String side = a.getString(4); 57 Type type = side.equals("ask") ? Type.SELL : Type.BUY; 58 return new Trade(time.toInstant(), px, qty, tid, type); 59 }).collect(Collectors.toList()).toArray(EMPTY_TRADES); 60 } 61 62 public Trade[] getTrades() { 63 return trades; 64 } 65 66 }