io.contek.zeus.OrderKey Maven / Gradle / Ivy
The newest version!
package io.contek.zeus;
import io.contek.zeus.common.OrderFilter;
import io.contek.zeus.common.OrderSnapshot;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.Objects;
import static com.google.common.base.MoreObjects.firstNonNull;
import static io.contek.zeus.Constants.JOINER;
@Immutable
public final class OrderKey {
private final MarketSymbol marketSymbol;
private final ServerOrderLabel serverOrderLabel;
private final ClientOrderLabel clientOrderLabel;
private OrderKey(
MarketSymbol marketSymbol,
@Nullable ServerOrderLabel serverOrderLabel,
@Nullable ClientOrderLabel clientOrderLabel) {
this.marketSymbol = marketSymbol;
this.serverOrderLabel = serverOrderLabel;
this.clientOrderLabel = clientOrderLabel;
}
public static OrderKey of(OrderSnapshot snapshot) {
MarketSymbol marketSymbol = MarketSymbol.of(snapshot);
ServerOrderLabel serverOrderLabel = null;
if (!snapshot.getServerOrderLabel().isEmpty()) {
serverOrderLabel = ServerOrderLabel.of(snapshot.getServerOrderLabel());
}
ClientOrderLabel clientOrderLabel = null;
if (!snapshot.getClientOrderLabel().isEmpty()) {
clientOrderLabel = ClientOrderLabel.of(snapshot.getClientOrderLabel());
}
if (serverOrderLabel == null && clientOrderLabel == null) {
throw new IllegalArgumentException();
}
return of(marketSymbol, serverOrderLabel, clientOrderLabel);
}
public static OrderKey of(OrderFilter filter) {
return of(
MarketSymbol.of(filter.getMarketSymbol()),
ServerOrderLabel.ofNullable(filter.getServerOrderLabel()),
ClientOrderLabel.ofNullable(filter.getClientOrderLabel()));
}
public static OrderKey of(
MarketSymbol marketSymbol,
@Nullable ServerOrderLabel serverOrderLabel,
@Nullable ClientOrderLabel clientOrderLabel) {
return new OrderKey(marketSymbol, serverOrderLabel, clientOrderLabel);
}
public MarketSymbol getMarketSymbol() {
return marketSymbol;
}
@Nullable
public ClientOrderLabel getClientOrderLabel() {
return clientOrderLabel;
}
@Nullable
public ServerOrderLabel getServerOrderLabel() {
return serverOrderLabel;
}
public OrderFilter toOrderFilter() {
OrderFilter.Builder builder =
OrderFilter.newBuilder().setMarketSymbol(marketSymbol.getStringValue());
if (serverOrderLabel != null) {
builder.setServerOrderLabel(serverOrderLabel.getStringValue());
}
if (clientOrderLabel != null) {
builder.setClientOrderLabel(clientOrderLabel.getStringValue());
}
return builder.build();
}
@Override
public boolean equals(@Nullable Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OrderKey orderKey = (OrderKey) o;
return Objects.equals(marketSymbol, orderKey.marketSymbol)
&& Objects.equals(serverOrderLabel, orderKey.serverOrderLabel)
&& Objects.equals(clientOrderLabel, orderKey.clientOrderLabel);
}
@Override
public int hashCode() {
return Objects.hash(marketSymbol, serverOrderLabel, clientOrderLabel);
}
@Override
public String toString() {
return JOINER.join(
marketSymbol, firstNonNull(serverOrderLabel, ""), firstNonNull(clientOrderLabel, ""));
}
}