main.java.com.nutanix.dp1.net.models.NetAbstractBoundedMap Maven / Gradle / Ivy
package com.nutanix.dp1.net.models;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SuppressWarnings({"unused", "SameParameterValue"})
@Slf4j
@Data
public abstract class NetAbstractBoundedMap {
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
protected Map boundedMap;
@JsonIgnore
protected final Class valueClass;
@JsonIgnore
@Setter(AccessLevel.NONE)
@Getter(AccessLevel.NONE)
protected Map unknownKeysMap = new LinkedHashMap<>();
protected NetAbstractBoundedMap(Class valueClass) {
this.valueClass = valueClass;
}
public T get(String key) {
if(key == null || boundedMap == null || !boundedMap.containsKey(key)) {
log.debug("Null or invalid key {} ", key);
throw new IllegalArgumentException("key:" + key + " not recognized");
}
return boundedMap.get(key);
}
public void put(String key, T value) {
if(boundedMap == null) {
throw new IllegalStateException("BoundedMap is not initialized");
}
if(key == null) {
throw new IllegalArgumentException("Null key is not supported");
}
if(!boundedMap.containsKey(key)) {
log.debug("Putting unrecognized key {} into unknown map", key);
unknownKeysMap.put(key, value);
return;
}
boundedMap.replace(key, value);
}
@JsonIgnore
public Set getKeys() {
return boundedMap.keySet();
}
protected final void init(List allowedKeys, T defaultValue) {
if(boundedMap == null) {
boundedMap = new LinkedHashMap<>();
allowedKeys.forEach(k -> boundedMap.put(k, defaultValue));
}
}
}