package com.moparisthebest.jdbc.util; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; /** * Created by mopar on 6/19/17. */ public abstract class CacheUtil { public static Map getCache(final int maxEntries) { if (maxEntries > 0) { // we want a limited cache final float loadFactor = 0.75f; // default for HashMaps // if we set the initialCapacity this way, nothing should ever need re-sized final int initialCapacity = ((int) Math.ceil(maxEntries / loadFactor)) + 1; return new LinkedHashMap(initialCapacity, loadFactor, true) { @Override protected boolean removeEldestEntry(final Map.Entry eldest) { return size() > maxEntries; } }; } else return new HashMap(); } public static Map getCache(final boolean threadSafe) { return threadSafe ? new ConcurrentHashMap() : new HashMap(); } }