dev.vality.woody.api.trace.Metadata Maven / Gradle / Ivy
package dev.vality.woody.api.trace;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class Metadata {
private static final int DEFAULT_INIT_SIZE = 16;
private static final float DEFAULT_LOAD_FACTOR = 0.75f;
private final boolean overrideKeys;
private Map values;
public Metadata() {
this(true);
}
public Metadata(boolean overrideKeys) {
this.overrideKeys = overrideKeys;
this.values = createStore(DEFAULT_INIT_SIZE, DEFAULT_LOAD_FACTOR);
}
protected Metadata(Metadata oldMetadata) {
this.overrideKeys = oldMetadata.overrideKeys;
this.values = cloneStore(oldMetadata.values);
}
public T getValue(String key) {
return (T) values.get(key);
}
public T removeValue(String key) {
if (overrideKeys) {
return (T) values.remove(key);
} else if (values.containsKey(key)) {
throw new IllegalStateException("Value overriding is not allowed");
} else {
return null;
}
}
public T putValue(String key, Object value) {
if (overrideKeys) {
return (T) values.put(key, value);
} else if (values.containsKey(key)) {
throw new IllegalStateException("Value overriding is not allowed");
} else {
return (T) values.put(key, value);
}
}
public boolean isOverrideKeys() {
return overrideKeys;
}
public boolean containsKey(String key) {
return values.containsKey(key);
}
public Collection getKeys() {
return values.keySet();
}
public void reset() {
values = createStore(DEFAULT_INIT_SIZE, DEFAULT_LOAD_FACTOR);
}
public Metadata cloneObject() {
return new Metadata(this);
}
private static HashMap createStore(int size, float loadFactor) {
return new HashMap<>(size, loadFactor);
}
private static Map cloneStore(Map oldMap) {
Map newMap = createStore(DEFAULT_INIT_SIZE, DEFAULT_LOAD_FACTOR);
newMap.putAll(oldMap);
return newMap;
}
}