
com.laamella.parameter_source.CachingParameterSource Maven / Gradle / Ivy
package com.laamella.parameter_source;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
/**
* Basic caching for any parameter source.
* Keeps values in cache until flush() is called.
*/
public class CachingParameterSource implements ParameterSource {
private final Cache> stringValueCache;
private final Cache> integerValueCache;
private final Cache> objectValueCache;
public CachingParameterSource(ParameterSource cachedParameterSource) {
stringValueCache = new Cache<>(cachedParameterSource::getOptionalString);
integerValueCache = new Cache<>(cachedParameterSource::getOptionalInteger);
objectValueCache = new Cache<>(cachedParameterSource::getOptionalObject);
}
private static class Cache {
private final Map content = new HashMap();
private final Function freshValueSource;
public Cache(Function freshValueSource) {
this.freshValueSource = freshValueSource;
}
public V get(K key) {
return content.computeIfAbsent(key, freshValueSource);
}
public void flush() {
content.clear();
}
}
@Override
public Optional getOptionalString(String key) {
return stringValueCache.get(key);
}
@Override
public Optional getOptionalInteger(String key) {
return integerValueCache.get(key);
}
@Override
public Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy