1 21 package oracle.toplink.essentials.internal.identitymaps; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.helper.linkedlist.*; 26 27 41 public class HardCacheWeakIdentityMap extends WeakIdentityMap { 42 protected ExposedNodeLinkedList referenceCache; 43 44 public HardCacheWeakIdentityMap(int size) { 45 super(size); 46 this.referenceCache = new ExposedNodeLinkedList(); 47 } 48 49 53 public CacheKey createCacheKey(Vector primaryKey, Object object, Object writeLockValue, long readTime) { 54 return new ReferenceCacheKey(primaryKey, object, writeLockValue, readTime); 55 } 56 57 60 public ExposedNodeLinkedList getReferenceCache() { 61 return referenceCache; 62 } 63 64 68 public Object buildReference(Object object) { 69 return object; 70 } 71 72 76 public boolean hasReference(Object reference) { 77 return reference != null; 78 } 79 80 84 protected void put(CacheKey cacheKey) { 85 ReferenceCacheKey referenceCacheKey = (ReferenceCacheKey)cacheKey; 86 LinkedNode node = getReferenceCache().addFirst(buildReference(referenceCacheKey.getObject())); 87 referenceCacheKey.setReferenceCacheNode(node); 88 super.put(cacheKey); 89 } 90 91 94 public Object remove(CacheKey cacheKey) { 95 if (cacheKey == null) { 96 return null; 97 } 98 ReferenceCacheKey referenceCacheKey = (ReferenceCacheKey)cacheKey; 99 synchronized (this){ 100 getReferenceCache().remove(referenceCacheKey.getReferenceCacheNode()); 101 } 102 return super.remove(cacheKey); 103 } 104 105 108 public synchronized void updateMaxSize(int maxSize) { 109 setMaxSize(maxSize); 110 while (getReferenceCache().size() > getMaxSize()) { 112 getReferenceCache().removeLast(); 113 } 114 } 115 116 120 public class ReferenceCacheKey extends WeakCacheKey { 121 protected LinkedNode referenceNode; 122 123 public ReferenceCacheKey(Vector primaryKey, Object object, Object writeLockValue, long readTime) { 124 super(primaryKey, object, writeLockValue, readTime); 125 } 126 127 public LinkedNode getReferenceCacheNode() { 128 return referenceNode; 129 } 130 131 public void setReferenceCacheNode(LinkedNode referenceNode) { 132 this.referenceNode = referenceNode; 133 } 134 135 public ExposedNodeLinkedList getReferenceCache() { 136 return referenceCache; 137 } 138 139 144 public void updateAccess() { 145 synchronized (HardCacheWeakIdentityMap.this) { 147 if (!hasReference(getReferenceCacheNode().getContents())) { 151 getReferenceCacheNode().setContents(buildReference(getObject())); 152 } 153 154 getReferenceCache().moveFirst(getReferenceCacheNode()); 156 while (getReferenceCache().size() > getMaxSize()) { 158 getReferenceCache().removeLast(); 159 } 160 } 161 } 162 } 163 } 164 | Popular Tags |