com.laamella.parameter_source.CachingParameterSource Maven / Gradle / Ivy
package com.laamella.parameter_source;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
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 storage = new Cache();
private final ParameterSource cachedParameterSource;
public CachingParameterSource(ParameterSource cachedParameterSource) {
this.cachedParameterSource = cachedParameterSource;
}
private static class Cache {
private final Map content = new HashMap<>();
public T get(String key, Function freshValueSource) {
return (T) content.computeIfAbsent(key, freshValueSource);
}
public void flush() {
content.clear();
}
}
@Override
public Optional getOptionalString(String key) {
return storage.get(key, cachedParameterSource::getOptionalString);
}
@Override
public Optional> getOptionalStringList(String key) {
return storage.get(key, cachedParameterSource::getOptionalStringList);
}
@Override
public Optional getOptionalInteger(String key) {
return storage.get(key, cachedParameterSource::getOptionalInteger);
}
@Override
public Optional getOptionalLong(String key) {
return storage.get(key, cachedParameterSource::getOptionalLong);
}
@Override
public Optional getOptionalFloat(String key) {
return storage.get(key, cachedParameterSource::getOptionalFloat);
}
@Override
public Optional getOptionalDouble(String key) {
return storage.get(key, cachedParameterSource::getOptionalDouble);
}
@Override
public Optional getOptionalBoolean(String key) {
return storage.get(key, cachedParameterSource::getOptionalBoolean);
}
@Override
public Optional