1 21 package oracle.toplink.essentials.internal.identitymaps; 23 24 import java.util.*; 25 26 34 public class FullIdentityMap extends IdentityMap { 35 36 37 protected Hashtable cacheKeys; 38 39 public FullIdentityMap(int size) { 40 super(size); 41 cacheKeys = new Hashtable(size); 42 } 43 44 48 public Object clone() { 49 FullIdentityMap clone = (FullIdentityMap)super.clone(); 50 clone.setCacheKeys(new Hashtable(getCacheKeys().size())); 51 52 for (Enumeration cacheKeysEnum = getCacheKeys().elements(); 53 cacheKeysEnum.hasMoreElements();) { 54 CacheKey key = (CacheKey)((CacheKey)cacheKeysEnum.nextElement()).clone(); 55 clone.getCacheKeys().put(key, key); 56 } 57 58 return clone; 59 } 60 61 66 public void collectLocks(HashMap threadList) { 67 Iterator cacheKeyIterator = this.cacheKeys.values().iterator(); 68 while (cacheKeyIterator.hasNext()) { 69 CacheKey cacheKey = (CacheKey)cacheKeyIterator.next(); 70 if (cacheKey.isAcquired()) { 71 Thread activeThread = cacheKey.getMutex().getActiveThread(); 72 Set set = (Set)threadList.get(activeThread); 73 if (set == null) { 74 set = new HashSet(); 75 threadList.put(activeThread, set); 76 } 77 set.add(cacheKey); 78 } 79 } 80 } 81 82 85 public Enumeration elements() { 86 return new IdentityMapEnumeration(this); 87 } 88 89 94 protected synchronized CacheKey getCacheKey(CacheKey searchKey) { 95 return (CacheKey)getCacheKeys().get(searchKey); 96 } 97 98 public Hashtable getCacheKeys() { 99 return cacheKeys; 100 } 101 102 105 public int getSize() { 106 return cacheKeys.size(); 107 } 108 109 113 public int getSize(Class myClass, boolean recurse) { 114 int i = 0; 115 Enumeration keys = getCacheKeys().keys(); 116 117 while (keys.hasMoreElements()) { 118 CacheKey key = (CacheKey)keys.nextElement(); 119 Object obj = key.getObject(); 120 121 if (obj != null) { 122 if (recurse && myClass.isInstance(obj)) { 123 i++; 124 } else if (obj.getClass().equals(myClass)) { 125 i++; 126 } 127 } 128 } 129 130 return i; 131 } 132 133 136 public Enumeration keys() { 137 return new IdentityMapKeyEnumeration(this); 138 } 139 140 146 public CacheKey put(Vector primaryKey, Object object, Object writeLockValue, long readTime) { 147 CacheKey cacheKey = getCacheKey(primaryKey); 148 149 if (cacheKey != null) { 151 resetCacheKey(cacheKey, object, writeLockValue); 153 154 put(cacheKey); 156 } else { 157 cacheKey = createCacheKey(primaryKey, object, writeLockValue, readTime); 159 160 put(cacheKey); 161 } 162 163 return cacheKey; 164 } 165 166 169 protected void put(CacheKey cacheKey) { 170 synchronized(this){ 172 getCacheKeys().put(cacheKey, cacheKey); 173 } 174 cacheKey.setOwningMap(this); 175 } 176 177 181 public Object remove(CacheKey cacheKey) { 182 if (cacheKey != null) { 183 cacheKey.acquire(); 185 186 synchronized (this) { 188 getCacheKeys().remove(cacheKey); 189 } 190 191 cacheKey.release(); 193 } else { 194 return null; 195 } 196 197 return cacheKey.getObject(); 198 } 199 200 public void resetCacheKey(CacheKey key, Object object, Object writeLockValue) { 201 resetCacheKey(key, object, writeLockValue, 0); 202 } 203 204 public void resetCacheKey(CacheKey key, Object object, Object writeLockValue, long readTime) { 205 key.acquire(); 206 key.setObject(object); 207 key.setWriteLockValue(writeLockValue); 208 key.setReadTime(readTime); 209 key.release(); 210 } 211 212 protected void setCacheKeys(Hashtable cacheKeys) { 213 this.cacheKeys = cacheKeys; 214 } 215 } 216 | Popular Tags |