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

org.babyfish.jimmer.sql.cache.CacheLoaderWrapper Maven / Gradle / Ivy

The newest version!
package org.babyfish.jimmer.sql.cache;

import org.babyfish.jimmer.runtime.Internal;

import java.util.*;

class CacheLoaderWrapper implements CacheLoader {

    private final CacheLoader raw;

    private final boolean requiresNewDraftContext;

    CacheLoaderWrapper(CacheLoader raw, boolean requiresNewDraftContext) {
        this.raw = raw;
        this.requiresNewDraftContext = requiresNewDraftContext;
    }

    static  CacheLoader wrap(CacheLoader loader, boolean requiresNewDraftContext) {
        if (loader instanceof CacheLoaderWrapper) {
            return loader;
        }
        return new CacheLoaderWrapper<>(loader, requiresNewDraftContext);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Map loadAll(Collection keys) {
        if (keys.isEmpty()) {
            return Collections.emptyMap();
        }
        if (!requiresNewDraftContext) {
            return raw.loadAll(keys);
        }
        return Internal.requiresNewDraftContext(ctx -> {
            Map map = raw.loadAll(keys);
            Map resolvedMap = new HashMap<>((map.size() * 4 + 2) / 3);
            for (Map.Entry e : map.entrySet()) {
                resolvedMap.put(e.getKey(), ctx.resolveObject(e.getValue()));
            }
            return resolvedMap;
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy