package com.datastax.oss.driver.api.core;
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Map;
public enum DefaultConsistencyLevel implements ConsistencyLevel {
ANY(ProtocolConstants.ConsistencyLevel.ANY),
ONE(ProtocolConstants.ConsistencyLevel.ONE),
TWO(ProtocolConstants.ConsistencyLevel.TWO),
THREE(ProtocolConstants.ConsistencyLevel.THREE),
QUORUM(ProtocolConstants.ConsistencyLevel.QUORUM),
ALL(ProtocolConstants.ConsistencyLevel.ALL),
LOCAL_ONE(ProtocolConstants.ConsistencyLevel.LOCAL_ONE),
LOCAL_QUORUM(ProtocolConstants.ConsistencyLevel.LOCAL_QUORUM),
EACH_QUORUM(ProtocolConstants.ConsistencyLevel.EACH_QUORUM),
SERIAL(ProtocolConstants.ConsistencyLevel.SERIAL),
LOCAL_SERIAL(ProtocolConstants.ConsistencyLevel.LOCAL_SERIAL),
;
private final int protocolCode;
DefaultConsistencyLevel(int protocolCode) {
this.protocolCode = protocolCode;
}
@Override
public int getProtocolCode() {
return protocolCode;
}
@NonNull
public static DefaultConsistencyLevel fromCode(int code) {
DefaultConsistencyLevel level = BY_CODE.get(code);
if (level == null) {
throw new IllegalArgumentException("Unknown code: " + code);
}
return level;
}
@Override
public boolean isDcLocal() {
return this == LOCAL_ONE || this == LOCAL_QUORUM || this == LOCAL_SERIAL;
}
@Override
public boolean isSerial() {
return this == SERIAL || this == LOCAL_SERIAL;
}
private static final Map<Integer, DefaultConsistencyLevel> BY_CODE = mapByCode(values());
private static Map<Integer, DefaultConsistencyLevel> mapByCode(DefaultConsistencyLevel[] levels) {
ImmutableMap.Builder<Integer, DefaultConsistencyLevel> builder = ImmutableMap.builder();
for (DefaultConsistencyLevel level : levels) {
builder.put(level.protocolCode, level);
}
return builder.build();
}
}