01: /*
02: * CacheReference.java February 2001
03: *
04: * Copyright (C) 2001, Niall Gallagher <niallg@users.sf.net>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General
16: * Public License along with this library; if not, write to the
17: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18: * Boston, MA 02111-1307 USA
19: */
20:
21: package simple.util.cache;
22:
23: import java.lang.ref.SoftReference;
24:
25: /**
26: * This is used so that <code>TimeCache</code> can have items removed
27: * when all references to it are gone. The <code>CacheReference</code>
28: * is a special type of reference. Once an object is enqueued into the
29: * <code>TimeCache</code> then that object is wrapped in a
30: * <code>CacheReference</code> and scheduled for removal.
31: * <p>
32: * Once the objects time in the queue has finished then the reference
33: * is obtained. The <code>CacheReference</code> is then searched to see
34: * if it still holds the cached item. If it does not hold the cached
35: * item then that item must have lost all its references because the
36: * semantics of the <code>java.lang.ref.SoftReference</code> says that
37: * it will release an object for garbage collection only when all
38: * references to it are gone. This means that it has already been
39: * removed from the cache. If however the <code>CacheReference</code>
40: * still contains the cached item then the reference can behave as a
41: * key into the <code>TimeCache</code> for the item. The reference
42: * can then be used to access the cached item for removal, or whatever.
43: *
44: * @author Niall Gallagher
45: */
46: final class CacheReference extends SoftReference {
47:
48: /**
49: * Remember the key for the cached object.
50: */
51: private Object key;
52:
53: /**
54: * This will create a reference object that acts like a key. This
55: * keeps both the cached item, which may be a large portion of
56: * memory and the key which indexes into the time cache used.
57: *
58: * @param key this is the key that references the object
59: * @param obj this is the object that is to be stored
60: */
61: public CacheReference(Object key, Object obj) {
62: super (obj);
63: this .key = key;
64: }
65:
66: /**
67: * This allows this reference to act like a key when it is used
68: * by any data arbitrary structures such as <code>Hashtable</code>,
69: * and <code>Vector</code>. This mirrors the keys hash code.
70: *
71: * @return the referents <code>hashCode</code>, i.e. the original
72: * keys <code>hashCode</code>
73: */
74: public int hashCode() {
75: return key.hashCode();
76: }
77:
78: /**
79: * This allows this reference to act like a key. This will allow
80: * the cache reference to remove the item it represents from the
81: * cache by using it with the cache <code>remove</code> method.
82: *
83: * @param obj the objects used in comparison
84: *
85: * @return true if obj this is equal to the key
86: */
87: public boolean equals(Object obj) {
88: return key.equals(obj);
89: }
90: }
|