1 21 package oracle.toplink.essentials.internal.identitymaps; 23 24 import java.io.*; 25 import java.util.Vector ; 26 import oracle.toplink.essentials.internal.helper.*; 27 import oracle.toplink.essentials.sessions.Record; 28 29 37 public class CacheKey implements Serializable, Cloneable { 38 39 40 protected Vector key; 41 42 43 protected int hash; 44 protected Object object; 45 46 protected IdentityMap mapOwner; 50 51 52 protected Object writeLockValue; 53 54 55 protected Object wrapper; 56 57 58 protected ConcurrencyManager mutex; 59 60 61 protected Record record; 62 63 64 65 protected long lastUpdatedQueryId; 68 69 70 protected int invalidationState = CHECK_INVALIDATION_POLICY; 71 72 73 public static final int CHECK_INVALIDATION_POLICY = 0; 74 public static final int CACHE_KEY_INVALID = -1; 75 76 78 protected long readTime = 0; 79 80 public CacheKey(Vector primaryKeys) { 81 this.key = primaryKeys; 82 this.hash = computeHash(primaryKeys); 83 } 84 85 public CacheKey(Vector primaryKey, Object object, Object lockValue) { 86 this(primaryKey); 87 setObject(object); 89 this.writeLockValue = lockValue; 90 } 91 92 public CacheKey(Vector primaryKey, Object object, Object lockValue, long readTime) { 93 this(primaryKey, object, lockValue); 94 this.readTime = readTime; 95 } 96 97 100 public void acquire() { 101 getMutex().acquire(false); 102 } 103 104 108 public void acquire(boolean forMerge) { 109 getMutex().acquire(forMerge); 110 } 111 112 116 public boolean acquireNoWait() { 117 return getMutex().acquireNoWait(false); 118 } 119 120 125 public boolean acquireNoWait(boolean forMerge) { 126 return getMutex().acquireNoWait(forMerge); 127 } 128 129 132 public void acquireDeferredLock() { 133 getMutex().acquireDeferredLock(); 134 } 135 136 141 public void checkReadLock() { 142 getMutex().checkReadLock(); 143 } 144 145 148 public void acquireReadLock() { 149 getMutex().acquireReadLock(); 150 } 151 152 155 public boolean acquireReadLockNoWait() { 156 return getMutex().acquireReadLockNoWait(); 157 } 158 159 163 public Object clone() { 164 Object object = null; 165 166 try { 167 object = super.clone(); 168 } catch (Exception exception) { 169 throw new InternalError (exception.toString()); 170 } 171 172 return object; 173 } 174 175 180 protected int computeHash(Vector primaryKey) { 181 int computedHashValue = 0; 182 183 for (int index = 0; index < primaryKey.size(); index++) { 184 Object value = primaryKey.elementAt(index); 185 if (value != null) { 186 computedHashValue = computedHashValue ^ (value.hashCode()); 187 } 188 } 189 return computedHashValue; 190 } 191 192 197 public boolean equals(Object object) { 198 if (object instanceof CacheKey) { 199 return equals((CacheKey)object); 200 } 201 202 return false; 203 } 204 205 209 public boolean equals(CacheKey key) { 210 if (this == key) { 211 return true; 212 } 213 if (getKey().size() == key.getKey().size()) { 214 for (int index = 0; index < getKey().size(); index++) { 215 Object myValue = getKey().elementAt(index); 216 Object comparisionValue = key.getKey().elementAt(index); 217 218 if (myValue == null) { 219 if (comparisionValue != null) { 220 return false; 221 } 222 } else if (myValue.getClass().isArray()) { 223 if (((myValue.getClass() == ClassConstants.APBYTE) && (comparisionValue.getClass() == ClassConstants.APBYTE)) && (Helper.compareByteArrays((byte[])myValue, (byte[])comparisionValue))) { 224 return false; 225 } else if (((myValue.getClass() == ClassConstants.APCHAR) && (comparisionValue.getClass() == ClassConstants.APCHAR)) && (Helper.compareCharArrays((char[])myValue, (char[])comparisionValue))) { 226 return false; 227 } else { 228 if (Helper.compareArrays((Object [])myValue, (Object [])comparisionValue)) { 229 return false; 230 } 231 } 232 } else { 233 if (!(myValue.equals(comparisionValue))) { 234 return false; 235 } 236 } 237 } 238 return true; 239 } 240 return false; 241 } 242 243 250 public long getLastUpdatedQueryId() { 251 return this.lastUpdatedQueryId; 252 } 253 254 public Vector getKey() { 255 return key; 256 } 257 258 261 public synchronized ConcurrencyManager getMutex() { 262 if (mutex == null) { 263 mutex = new ConcurrencyManager(this); 264 } 265 return mutex; 266 } 267 268 public Object getObject() { 269 return object; 270 } 271 272 public IdentityMap getOwningMap(){ 273 return this.mapOwner; 274 } 275 276 280 public long getReadTime() { 281 return readTime; 282 } 283 284 public Record getRecord() { 285 return record; 286 } 287 288 public Object getWrapper() { 289 return wrapper; 290 } 291 292 public Object getWriteLockValue() { 293 return writeLockValue; 294 } 295 296 299 public int hashCode() { 300 return hash; 301 } 302 303 306 public boolean isAcquired() { 307 return getMutex().isAcquired(); 308 } 309 310 317 public int getInvalidationState() { 318 return invalidationState; 319 } 320 321 324 public void release() { 325 getMutex().release(); 326 } 327 328 331 public void releaseDeferredLock() { 332 getMutex().releaseDeferredLock(); 333 } 334 335 338 public void releaseReadLock() { 339 getMutex().releaseReadLock(); 340 } 341 342 349 public void setInvalidationState(int invalidationState) { 350 this.invalidationState = invalidationState; 351 } 352 353 360 public void setLastUpdatedQueryId(long id) { 361 this.lastUpdatedQueryId = id; 362 } 363 364 public void setKey(Vector key) { 365 this.key = key; 366 this.hash = computeHash(key); 367 } 368 369 372 public void setMutex(ConcurrencyManager mutex) { 373 this.mutex = mutex; 374 } 375 376 public void setObject(Object object) { 377 this.object = object; 378 } 379 380 public void setOwningMap(IdentityMap map){ 381 this.mapOwner = map; 382 } 383 384 388 public void setReadTime(long readTime) { 389 this.readTime = readTime; 390 invalidationState = CHECK_INVALIDATION_POLICY; 391 } 392 393 public void setRecord(Record newRecord) { 394 this.record = newRecord; 395 } 396 397 public void setWrapper(Object wrapper) { 398 this.wrapper = wrapper; 399 } 400 401 public void setWriteLockValue(Object writeLockValue) { 402 this.writeLockValue = writeLockValue; 403 } 404 405 public String toString() { 406 int hashCode = 0; 407 if (getObject() != null) { 408 hashCode = getObject().hashCode(); 409 } 410 411 return "[" + getKey() + ": " + hashCode + ": " + getWriteLockValue() + ": " + getReadTime() + ": " + getObject() + "]"; 412 } 413 414 418 public void updateAccess() { 419 } 421 } 422 | Popular Tags |