public abstract class NotifyMessage {
private ChannelEnum channelEnum;
private String symbol;
public Map<String, Object> result = new HashMap<>();
public String toJson(){
if(symbol == null)
result.put("channel", channelEnum.getType());
else
result.put("channel", symbol + "_" + channelEnum.getType());
result.put("date", System.currentTimeMillis());
convertJson();
return JSONObject.toJSONString(result);
}
public abstract void convertJson();
public ChannelEnum getChannelEnum() {
return channelEnum;
}
public void setChannelEnum(ChannelEnum channelEnum) {
this.channelEnum = channelEnum;
}
public String getSymbol() {
return symbol;
}
public void setSymbol(String symbol) {
this.symbol = symbol;
}
}
public class RealTimeMessage extends NotifyMessage{
List<RealTimeTradeDto> tradeDtoList;
public RealTimeMessage(String symbol, List<RealTimeTradeDto> tradeLogDTOS){
this.setChannelEnum(ChannelEnum.MSG_REALTIME);
this.setSymbol(symbol);
this.tradeDtoList = tradeLogDTOS;
}
@Override
public void convertJson() {
result.put("data", tradeDtoList);
}
}
public class OrderMessage extends NotifyMessage {
private Map<String, List<DepthDTO>> orderData;
public OrderMessage(String symbol, Map<String, List<DepthDTO>> orderData) {
this.setChannelEnum(ChannelEnum.MSG_ORDER);
this.setSymbol(symbol);
this.orderData = orderData;
}
@Override
public void convertJson() {
List<DepthDTO> buys = orderData.get(OrderType.BUY.getCode());
List<DepthDTO> sells = orderData.get(OrderType.SELL.getCode());
JSONArray b = new JSONArray();
buys.stream().forEach(item -> {
JSONArray bb = new JSONArray();
bb.add(item.getPrice().toPlainString());
bb.add(item.getQuantity().toPlainString());
b.add(bb);
});
result.put("b", b);
JSONArray s = new JSONArray();
sells.stream().forEach(item -> {
JSONArray ss = new JSONArray();
ss.add(item.getPrice().toPlainString());
ss.add(item.getQuantity().toPlainString());
s.add(ss);
});
result.put("s", s);
}
}
AllTickerMessage allTickerMessage = new AllTickerMessage(marketTickers);
//推送所有行情
WSData.getInstance().pushClientMsg(allTickerMessage);
标签:JSONArray,symbol,channelEnum,result,put,抽象类,public
From: https://www.cnblogs.com/youkiko/p/18050818