All Downloads are FREE. Search and download functionalities are using the official Maven repository.

estonlabs.cxtl.exchanges.binance.fapi.domain.stream.BinanceOutboundMessage Maven / Gradle / Ivy

There is a newer version: 1.4.14
Show newest version
package estonlabs.cxtl.exchanges.binance.fapi.domain.stream;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import estonlabs.cxtl.common.stream.managed.OutboundMessage;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;
import lombok.ToString;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;

@EqualsAndHashCode()
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@ToString(callSuper = true)
public class BinanceOutboundMessage implements OutboundMessage {
    private static final AtomicInteger ID_GENERATOR = new AtomicInteger(0);
    @NonNull
    @JsonIgnore
    protected MessageType messageType;

    private int id;
    private String method;
    private List params;

    public BinanceOutboundMessage(@NonNull MessageType messageType) {
        this.messageType=messageType;
        if(messageType == MessageType.SUBSCRIPTION){
            method="SUBSCRIBE";
        }else if(messageType == MessageType.UNSUBSCRIBE){
            method="UNSUBSCRIBE";
        }
        this.id = ID_GENERATOR.getAndIncrement();
    }

    public static BinanceOutboundMessage subscribe(String topic) {
        return request(MessageType.SUBSCRIPTION, List.of(topic));
    }

    public static BinanceOutboundMessage subscribe(Object ... topics) {
        return request(MessageType.SUBSCRIPTION, Arrays.stream(topics).toList());
    }

    public static BinanceOutboundMessage unsubscribe(String topic) {
        return request(MessageType.UNSUBSCRIBE, List.of(topic));
    }

    public static BinanceOutboundMessage request(MessageType type, String ... topics) {
        BinanceOutboundMessage request = new BinanceOutboundMessage(type);
        request.setParams(List.of(topics));
        return request;
    }

    public static BinanceOutboundMessage request(MessageType type, List topics) {
        BinanceOutboundMessage request = new BinanceOutboundMessage(type);
        request.setParams(topics);
        return request;
    }
}