1 21 package oracle.toplink.essentials.internal.identitymaps; 23 24 import java.util.*; 25 26 36 public class WeakIdentityMap extends FullIdentityMap { 37 38 39 protected int cleanupCount; 40 41 42 protected int cleanupSize; 43 44 public WeakIdentityMap(int size) { 45 super(size); 46 this.cleanupCount = 0; 47 this.cleanupSize = size; 48 } 49 50 57 protected void cleanupDeadCacheKeys() { 58 for (Enumeration keysEnum = getCacheKeys().elements(); keysEnum.hasMoreElements();) { 59 CacheKey key = (CacheKey)keysEnum.nextElement(); 60 if (key.getObject() == null) { 61 if (key.acquireNoWait()) { 64 try { 65 if (key.getObject() == null) { 66 getCacheKeys().remove(key); 67 } 68 } finally { 69 key.release(); 70 } 71 } 72 73 } 75 } 76 } 77 78 public CacheKey createCacheKey(Vector primaryKey, Object object, Object writeLockValue, long readTime) { 79 return new WeakCacheKey(primaryKey, object, writeLockValue, readTime); 80 } 81 82 85 protected int getCleanupCount() { 86 return cleanupCount; 87 } 88 89 protected void setCleanupCount(int cleanupCount) { 90 this.cleanupCount = cleanupCount; 91 } 92 93 96 protected int getCleanupSize() { 97 return cleanupSize; 98 } 99 100 protected void setCleanupSize(int cleanupSize) { 101 this.cleanupSize = cleanupSize; 102 } 103 104 107 protected void put(CacheKey cacheKey) { 108 synchronized (this) { 110 if (getCleanupCount() > getCleanupSize()) { 111 cleanupDeadCacheKeys(); 112 setCleanupCount(0); 113 if (getSize() > getCleanupSize()) { 115 setCleanupSize(getSize()); 116 } 117 } 118 setCleanupCount(getCleanupCount() + 1); 119 } 120 super.put(cacheKey); 121 } 122 } 123 | Popular Tags |