All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.laamella.parameter_source.CachingParameterSource Maven / Gradle / Ivy

There is a newer version: 1.0
Show newest version
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 getOptionalObject(String key) {
        return storage.get(key, cachedParameterSource::getOptionalObject);
    }

    @Override
    public Optional> getOptionalClass(String key) {
        return storage.get(key, cachedParameterSource::getOptionalClass);
    }

    @Override
    public > Optional getOptionalEnum(String key, Class enumType) {
        return storage.get(key, k -> cachedParameterSource.getOptionalEnum(k, enumType));
    }

    @Override
    public Optional getOptionalUrl(String key) {
        return storage.get(key, cachedParameterSource::getOptionalUrl);
    }

    @Override
    public Optional getOptionalUri(String key) {
        return storage.get(key, cachedParameterSource::getOptionalUri);
    }

    public void flush() {
        storage.flush();
    }
}