org.babyfish.jimmer.sql.cache.CacheLoaderWrapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-sql Show documentation
Show all versions of jimmer-sql Show documentation
A revolutionary ORM framework for both java and kotlin
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;
});
}
}