org.snapscript.common.LazyCache 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 java.util.Set;
public class LazyCache implements Cache {
private final LazyBuilder builder;
private final Cache cache;
public LazyCache(LazyBuilder builder) {
this(builder, 16);
}
public LazyCache(LazyBuilder builder, int size) {
this.cache = new CopyOnWriteCache(size);
this.builder = builder;
}
@Override
public void clear() {
cache.clear();
}
@Override
public int size() {
return cache.size();
}
@Override
public Set keySet() {
return cache.keySet();
}
@Override
public V fetch(K key) {
V value = cache.fetch(key);
if(value == null) {
value = builder.create(key);
cache.cache(key, value);
}
return value;
}
@Override
public void cache(K key, V value) {
cache.cache(key, value);
}
@Override
public V take(K key) {
return cache.take(key);
}
@Override
public boolean contains(K key) {
return cache.contains(key);
}
@Override
public boolean isEmpty() {
return cache.isEmpty();
}
@Override
public String toString() {
return String.valueOf(cache);
}
}