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

io.virtdata.docsys.metafs.fs.renderfs.api.versioning.VersionedObjectCache Maven / Gradle / Ivy

package io.virtdata.docsys.metafs.fs.renderfs.api.versioning;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class VersionedObjectCache {
    public final static Logger logger = LoggerFactory.getLogger(VersionedObjectCache.class);

    public static VersionedObjectCache INSTANCE = new VersionedObjectCache();
    private ConcurrentHashMap cache = new ConcurrentHashMap<>();
    private ConcurrentHashMap, ConcurrentHashMap> typeMap = new ConcurrentHashMap<>();

    private VersionedObjectCache() {
    }

    public  T getOrCreate(
            Class clazz,
            String key,
            Supplier supplier
    ) {
        ConcurrentHashMap typedMap = typeMap.computeIfAbsent(clazz, (Class c) -> new ConcurrentHashMap<>());

        Versioned versioned = typedMap.get(key);
        if (versioned == null || !versioned.isValid()) {
            versioned = supplier.get();
        }
        if (typedMap.containsKey(key)) {
            typedMap.put(key, versioned);
        }
        return clazz.cast(versioned);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy