org.snapscript.common.HashCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of snap-all Show documentation
Show all versions of snap-all Show documentation
Dynamic scripting for the JVM
package org.snapscript.common;
import static java.util.Collections.EMPTY_MAP;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class HashCache implements Cache {
private volatile Map map;
public HashCache() {
this.map = EMPTY_MAP;
}
@Override
public Set keySet() {
return map.keySet();
}
@Override
public V take(K key) {
return map.remove(key);
}
@Override
public V fetch(K key) {
return map.get(key);
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean contains(K key) {
return map.containsKey(key);
}
@Override
public void cache(K key, V value) {
if(map == EMPTY_MAP) {
map = new HashMap();
}
map.put(key, value);
}
@Override
public void clear() {
map.clear();
}
@Override
public int size() {
return map.size();
}
@Override
public String toString() {
return String.valueOf(map);
}
}