Getter putGetter(Class clazz, String attributeName, Getter getter) { SampleableConcurrentHashMap<String, Getter> cache = getOrPutIfAbsent(getterCache, clazz, getterCacheConstructor); Getter foundGetter = cache.putIfAbsent(attributeName, getter); evictOnPut(cache); return foundGetter == null ? getter : foundGetter; }
/** * It works on best effort basis. If multi-threaded calls involved it may evict all elements, but * it's unlikely. */ private void evictMap( SampleableConcurrentHashMap<?, ?> map, int triggeringEvictionSize, int afterEvictionSize) { int mapSize = map.size(); if (mapSize - triggeringEvictionSize >= 0) { for (Map.Entry entry : map.getRandomSamples(mapSize - afterEvictionSize)) { map.remove(entry.getKey()); } } }
int getGetterPerClassCacheSize(Class clazz) { SampleableConcurrentHashMap cacheForClass = getterCache.get(clazz); return cacheForClass != null ? cacheForClass.size() : -1; }