1 21 package oracle.toplink.essentials.internal.identitymaps; 23 24 import java.util.*; 25 26 import oracle.toplink.essentials.exceptions.*; 27 28 39 public class CacheIdentityMap extends FullIdentityMap { 40 41 42 protected LinkedCacheKey first; 43 44 45 protected LinkedCacheKey last; 46 47 51 public CacheIdentityMap(int size) { 52 super(size); 53 this.first = new LinkedCacheKey(new Vector(2), null, null, 0); 54 this.last = new LinkedCacheKey(new Vector(2), null, null, 0); 55 this.first.setNext(this.last); 56 this.last.setPrevious(this.first); 57 } 58 59 public CacheKey createCacheKey(Vector primaryKey, Object object, Object writeLockValue, long readTime) { 60 return new LinkedCacheKey(primaryKey, object, writeLockValue, readTime); 61 } 62 63 67 protected void ensureFixedSize() { 68 synchronized(this.first) { 71 while (getMaxSize() > 0 && getSize() > getMaxSize()) { 72 remove(last.getPrevious()); 73 } 74 } 75 } 76 77 83 protected CacheKey getCacheKey(Vector primaryKeys) { 84 LinkedCacheKey cacheKey = (LinkedCacheKey)super.getCacheKey(primaryKeys); 85 86 if (cacheKey != null) { 87 synchronized (this.first) { 88 removeLink(cacheKey); 89 insertLink(cacheKey); 90 } 91 } 92 93 return cacheKey; 94 } 95 96 101 protected LinkedCacheKey insertLink(LinkedCacheKey key) { 102 if (key == null){ 103 return key; 104 } 105 synchronized (this.first){ 107 this.first.getNext().setPrevious(key); 108 key.setNext(this.first.getNext()); 109 key.setPrevious(this.first); 110 this.first.setNext(key); 111 } 112 return key; 113 } 114 115 118 protected void put(CacheKey cacheKey) { 119 super.put(cacheKey); 120 insertLink((LinkedCacheKey)cacheKey); 121 ensureFixedSize(); 122 } 123 124 128 public Object remove(CacheKey key) { 129 super.remove(key); 130 if (key == null) { 132 Class cacheItemClass = null; 133 134 if (!getCacheKeys().isEmpty()) { 138 CacheKey aKey = (CacheKey)getCacheKeys().keys().nextElement(); 139 if ((aKey != null) && (aKey.getObject() != null)) { 140 cacheItemClass = aKey.getObject().getClass(); 141 } 142 } 143 throw ValidationException.nullCacheKeyFoundOnRemoval(this, cacheItemClass); 144 } 145 return removeLink((LinkedCacheKey)key).getObject(); 146 } 147 148 152 protected LinkedCacheKey removeLink(LinkedCacheKey key) { 153 if (key == null){ 154 return key; 155 } 156 synchronized (this.first) { 157 if (key.getPrevious() == null || key.getNext() == null){ 158 return key; 160 } 161 key.getPrevious().setNext(key.getNext()); 162 key.getNext().setPrevious(key.getPrevious()); 163 key.setNext(null); 164 key.setPrevious(null); 165 } 166 return key; 167 } 168 169 175 public synchronized void updateMaxSize(int maxSize) { 176 setMaxSize(maxSize); 177 ensureFixedSize(); 178 } 179 } 180 | Popular Tags |