01: package net.suberic.util.cache;
02:
03: /**
04: * This represents a cached object. It stores the object itself, plus
05: * the size and last accessed time.
06: */
07: public class SizedCacheEntry {
08:
09: // the stored object itself. note that this could be a reference to
10: // another object, or a source from which the object may be accessed.
11: protected Object cachedValue;
12:
13: // the last accessed time
14: protected long lastAccessedTime;
15:
16: // the size that the cachedValue occupies in the cache.
17: protected long size = 0;
18:
19: /**
20: * Creates a new, empty SizedCacheEntry.
21: */
22: public SizedCacheEntry() {
23: }
24:
25: /**
26: * Creates a new SizedCacheEntry containing value which has been most
27: * recently accessed now.
28: */
29: public SizedCacheEntry(Object value) {
30: cachedValue = value;
31: // yeah, whatever...
32: size = value.toString().length();
33: touchEntry();
34: }
35:
36: /**
37: * This gets the cached value. Implementations may vary for this.
38: */
39: public Object getCachedValue() {
40: return cachedValue;
41: }
42:
43: /**
44: * Deletes this entry. Should be called in order to clean up entries
45: * for which simple removal from memory is insufficient.
46: *
47: * The default implementation does nothing; subclasses should override
48: * this method if cleanup is required.
49: */
50: public boolean removeFromCache() {
51: return true;
52: }
53:
54: /**
55: * Touches the SizedCacheEntry, making its last accessed time now.
56: */
57: public void touchEntry() {
58: lastAccessedTime = System.currentTimeMillis();
59: }
60:
61: /**
62: * Gets the size of this SizedCacheEntry.
63: */
64: public long getSize() {
65: return size;
66: }
67:
68: /**
69: * Gets the last accessed time for this entry.
70: */
71: public long getLastAccessedTime() {
72: return lastAccessedTime;
73: }
74:
75: /**
76: * Compares the underlying value for equality.
77: */
78: public boolean equals(Object o) {
79: if (o != null) {
80: Object testValue = null;
81: if (o instanceof SizedCacheEntry) {
82: testValue = ((SizedCacheEntry) o).getCachedValue();
83: } else {
84: testValue = o;
85: }
86: return o.equals(cachedValue);
87: }
88:
89: return (cachedValue == null);
90: }
91: }
|