View Javadoc
1   package org.oxerr.peatio.websocket.dto;
2   
3   import java.math.BigDecimal;
4   import java.time.Instant;
5   import java.util.Date;
6   
7   import javax.json.JsonObject;
8   
9   public class Trade extends org.oxerr.peatio.rest.dto.Trade {
10  
11  	private final Order bid, ask;
12  
13  	public Trade(long id, BigDecimal price, BigDecimal volume,
14  			BigDecimal funds, String market, Date createdAt, String side,
15  			Order bid, Order ask) {
16  		super(id, price, volume, funds, market, createdAt, side, getOrderId(side, bid, ask));
17  		this.bid = bid;
18  		this.ask = ask;
19  	}
20  
21  	public Trade (JsonObject jsonObject) {
22  		this(
23  			jsonObject.getJsonNumber("id").longValue(),
24  			new BigDecimal(jsonObject.getString("price")),
25  			new BigDecimal(jsonObject.getString("volume")),
26  			new BigDecimal(jsonObject.getString("funds")),
27  			jsonObject.getString("market"),
28  			Date.from(Instant.parse(jsonObject.getString("created_at"))),
29  			jsonObject.getString("side"),
30  			Order.from(jsonObject.getJsonObject("bid")),
31  			Order.from(jsonObject.getJsonObject("ask"))
32  		);
33  	}
34  
35  	private static Long getOrderId(String side, Order bid, Order ask) {
36  		return side.equals("bid") ? bid.getId() : ask.getId();
37  	}
38  
39  	public Order getOrder() {
40  		return getSide().equals("bid") ? bid : ask;
41  	}
42  
43  }