KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > suberic > util > cache > SizedCacheEntry


1 package net.suberic.util.cache;
2
3 /**
4  * This represents a cached object. It stores the object itself, plus
5  * the size and last accessed time.
6  */

7 public class SizedCacheEntry {
8
9   // 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc o) {
79     if (o != null) {
80       Object JavaDoc 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 }
92
93
Popular Tags