private void initializeMaps() { ConcurrentReferenceHashMap statsMap = new ConcurrentReferenceHashMap(ReferenceMap.HARD, ReferenceMap.HARD, CONCURRENCY, LOAD_FACTOR); statsMap.setMaxSize(FIXED_SIZE); stats = statsMap; ConcurrentReferenceHashMap aStatsMap = new ConcurrentReferenceHashMap(ReferenceMap.HARD, ReferenceMap.HARD, CONCURRENCY, LOAD_FACTOR); aStatsMap.setMaxSize(FIXED_SIZE); astats = aStatsMap; }
protected boolean eq(Object x, Object y) { // the Entries in ConcurrentReferenceHashMap delegate back to // eq() in their equals() impls if (x instanceof Map.Entry) return super.eq(x, y); else return x == y; }
protected int hc(Object o) { // the Entries in ConcurrentReferenceHashMap delegate back to // hc() in their hashCode() impls if (o instanceof Map.Entry) return super.hc(o); else return System.identityHashCode(o); } };
public Map.Entry removeRandom() { synchronized (this) { expungeStaleEntries(); if (count == 0) return null; int random = randomEntryIndex(); int index = findEntry(random, random % 2 == 0, false); if (index == -1) return null; Entry rem = table[index]; table[index] = rem.getNext(); count--; return rem; } }
/** * Generate the hash code for this Id. Cache the type's generated hash code * so that it doesn't have to be generated each time. */ public int hashCode() { if (_typeHash == 0) { Integer typeHashInt = (Integer) _typeCache.get(type); if (typeHashInt == null) { Class base = type; Class superclass = base.getSuperclass(); while (superclass != null && superclass != Object.class) { base = base.getSuperclass(); superclass = base.getSuperclass(); } _typeHash = base.getName().hashCode(); _typeCache.put(type, Integer.valueOf(_typeHash)); } else { _typeHash = typeHashInt.intValue(); } } return _typeHash ^ idHash(); }
public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry entry = (Map.Entry) o; Object key = entry.getKey(); Entry[] tab = table; int hash = hc(key); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry e = tab[index]; e != null; e = e.getNext()) if (e.getHash() == hash && eq(e, entry)) return true; return false; }
return null; int hash = hc(key); synchronized (this) { expungeStaleEntries(); for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.getNext()) { if ((e.getHash() == hash) && eq(key, e.getKey())) { if (prev != null) prev.setNext(e.getNext());
/** * Loads the given class name by the given loader. * For efficiency, a cache per class loader is maintained of classes already loader. * @param clsName * @param loader */ static Class<?> loadClass(String clsName, ClassLoader loader) { Class<?> cls = null; Object key = loader == null ? NULL_LOADER : loader; Map<String,Class<?>> loaderCache = (Map<String,Class<?>>) _loaders.get(key); if (loaderCache == null) { // We don't have a cache for this loader. //OPENJPA-2636: Changed to HARD/WEAK to avoid Classloader leak: loaderCache = new ConcurrentReferenceHashMap(ConcurrentReferenceHashMap.HARD, ConcurrentReferenceHashMap.WEAK); _loaders.put(key, loaderCache); } else { // We have a cache for this loader. cls = (Class<?>) loaderCache.get(clsName); } if (cls == null) { try { cls = ClassUtil.toClass(clsName, loader); loaderCache.put(clsName, cls); } catch (RuntimeException re) { // TODO, empty block is never good } } return cls; }
private static void setSetterMethod(Class<?> cls, String prop, Method method) { Map<String, Method> clsMap = setterMethodCache.get(cls); if (clsMap == null) { clsMap = new ConcurrentReferenceHashMap(ReferenceMap.HARD, ReferenceMap.WEAK); setterMethodCache.put(cls, clsMap); } clsMap.put(prop, method); }
public void clear() { ConcurrentReferenceHashMap.this.clear(); } };
/** * Copies all of the mappings from the specified Map to this HashMap * These mappings will replace any mappings that this HashMap had for any * of the keys currently in the specified Map. * * @param t Mappings to be stored in this Map. */ public void putAll(Map t) { Iterator i = t.entrySet().iterator(); while (i.hasNext()) { Map.Entry e = (Map.Entry) i.next(); put(e.getKey(), e.getValue()); } }
public void removeExpired() { synchronized (this) { expungeStaleEntries(); } }
public boolean contains(Object o) { return containsKey(o); }
public boolean contains(Object o) { return containsValue(o); }
/** * Find the index of the entry nearest the given index, starting in the * given direction. */ private int findEntry(int start, boolean forward, boolean searchedOther) { if (forward) { for (int i = start; i < table.length; i++) if (table[i] != null) return i; return (searchedOther || start == 0) ? -1 : findEntry(start - 1, false, true); } else { for (int i = start; i >= 0; i--) if (table[i] != null) return i; return (searchedOther || start == table.length - 1) ? -1 : findEntry(start + 1, true, true); } }
/** * Generate the hash code for this Id. Cache the type's generated hash code * so that it doesn't have to be generated each time. */ public int hashCode() { if (_typeHash == 0) { Integer typeHashInt = (Integer) _typeCache.get(type); if (typeHashInt == null) { Class base = type; Class superclass = base.getSuperclass(); while (superclass != null && superclass != Object.class) { base = base.getSuperclass(); superclass = base.getSuperclass(); } _typeHash = base.getName().hashCode(); _typeCache.put(type, Integer.valueOf(_typeHash)); } else { _typeHash = typeHashInt.intValue(); } } return _typeHash ^ idHash(); }
public boolean contains(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry entry = (Map.Entry) o; Object key = entry.getKey(); Entry[] tab = table; int hash = hc(key); int index = (hash & 0x7FFFFFFF) % tab.length; for (Entry e = tab[index]; e != null; e = e.getNext()) if (e.getHash() == hash && eq(e, entry)) return true; return false; }
return null; int hash = hc(key); synchronized (this) { expungeStaleEntries(); for (Entry e = tab[index], prev = null; e != null; prev = e, e = e.getNext()) { if ((e.getHash() == hash) && eq(key, e.getKey())) { if (prev != null) prev.setNext(e.getNext());
/** * Loads the given class name by the given loader. * For efficiency, a cache per class loader is maintained of classes already loader. * @param clsName * @param loader */ static Class<?> loadClass(String clsName, ClassLoader loader) { Class<?> cls = null; Object key = loader == null ? NULL_LOADER : loader; Map<String,Class<?>> loaderCache = (Map<String,Class<?>>) _loaders.get(key); if (loaderCache == null) { // We don't have a cache for this loader. //OPENJPA-2636: Changed to HARD/WEAK to avoid Classloader leak: loaderCache = new ConcurrentReferenceHashMap(ConcurrentReferenceHashMap.HARD, ConcurrentReferenceHashMap.WEAK); _loaders.put(key, loaderCache); } else { // We have a cache for this loader. cls = (Class<?>) loaderCache.get(clsName); } if (cls == null) { try { cls = ClassUtil.toClass(clsName, loader); loaderCache.put(clsName, cls); } catch (RuntimeException re) { // TODO, empty block is never good } } return cls; }
public Map.Entry removeRandom() { synchronized (this) { expungeStaleEntries(); if (count == 0) return null; int random = randomEntryIndex(); int index = findEntry(random, random % 2 == 0, false); if (index == -1) return null; Entry rem = table[index]; table[index] = rem.getNext(); count--; return rem; } }