KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > policy > single > SingleCache


1 package org.shiftone.cache.policy.single;
2
3
4
5 import org.shiftone.cache.util.reaper.ReapableCache;
6
7
8 /**
9  * Class SingleCache
10  *
11  *
12  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
13  * @version $Revision: 1.4 $
14  */

15 class SingleCache implements ReapableCache
16 {
17
18     private long timeoutMilliSeconds = 0;
19     private long expireTime = 0;
20     private Object JavaDoc userKey = null;
21     private Object JavaDoc cacheObject = null;
22
23     /**
24      * Constructor SingleCache
25      *
26      *
27      * @param timeoutMilliSeconds
28      */

29     public SingleCache(long timeoutMilliSeconds)
30     {
31         this.timeoutMilliSeconds = timeoutMilliSeconds;
32     }
33
34
35     /**
36      * Method addObject
37      */

38     public synchronized void addObject(Object JavaDoc userKey, Object JavaDoc cacheObject)
39     {
40
41         this.userKey = userKey;
42         this.cacheObject = cacheObject;
43         expireTime = System.currentTimeMillis() + timeoutMilliSeconds;
44     }
45
46
47     /**
48      * Method getObject
49      */

50     public synchronized Object JavaDoc getObject(Object JavaDoc key)
51     {
52
53         Object JavaDoc value = null;
54
55         if ((this.userKey != null) && (key != null) && (key.equals(this.userKey)))
56         {
57             value = this.cacheObject;
58         }
59
60         return value;
61     }
62
63
64     /**
65      * Method size
66      */

67     public int size()
68     {
69
70         return (userKey == null)
71                ? 0
72                : 1;
73     }
74
75
76     /**
77      * Method remove
78      */

79     public synchronized void remove(Object JavaDoc key)
80     {
81
82         if ((this.userKey != null) && (key != null) && (key.equals(this.userKey)))
83         {
84             userKey = null;
85             cacheObject = null;
86         }
87     }
88
89
90     /**
91      * Method clear
92      */

93     public synchronized void clear()
94     {
95         userKey = null;
96         cacheObject = null;
97     }
98
99
100     /**
101      * Method removeExpiredElements
102      */

103     public synchronized void removeExpiredElements()
104     {
105
106         /// LOG.info("removeExpiredElements");
107
if (System.currentTimeMillis() >= expireTime)
108         {
109             remove(this.userKey);
110         }
111     }
112
113
114     public final String JavaDoc toString()
115     {
116         return "SingleCache" + size();
117     }
118 }
119
Popular Tags