KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > shiftone > cache > adaptor > CacheMap


1 package org.shiftone.cache.adaptor;
2
3
4
5 import org.shiftone.cache.Cache;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11
12
13 /**
14  * Makes a shiftone cache look like a map.
15  *
16  * @author <a HREF="mailto:jeff@shiftone.org">Jeff Drost</a>
17  * @version $Revision: 1.2 $
18  */

19 public class CacheMap implements Map JavaDoc
20 {
21
22     private final Cache cache;
23
24     public CacheMap(Cache cache)
25     {
26         this.cache = cache;
27     }
28
29
30     /**
31      * The size of this cache at this instance in time
32      */

33     public int size()
34     {
35         return cache.size();
36     }
37
38
39     /**
40      * Is the cache empty at this instance in time
41      */

42     public boolean isEmpty()
43     {
44         return size() == 0;
45     }
46
47
48     /**
49      * <b>Warning</b> - just because this method returns true, you can NOT
50      * expect to be able to get the value of the object that the cache contains.
51      * This is because some time will pass between when you test for the elements
52      * existance in the cache, and when you call get(). During that window of time,
53      * the cache element may be evicted.
54      */

55     public boolean containsKey(Object JavaDoc key)
56     {
57         return (cache.getObject(key) != null);
58     }
59
60
61     /**
62      * Gets an object from the cache based on it's key
63      */

64     public Object JavaDoc get(Object JavaDoc key)
65     {
66         return cache.getObject(key);
67     }
68
69
70     /**
71      * Puts an object into the cache and returns the previous
72      * value that was associated with that key.
73      */

74     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
75     {
76
77         Object JavaDoc oldValue = cache.getObject(key);
78
79         cache.addObject(key, value);
80
81         return oldValue;
82     }
83
84
85     public Object JavaDoc remove(Object JavaDoc key)
86     {
87
88         cache.remove(key);
89
90         return null;
91     }
92
93
94     /**
95      * Copies all of the mappings from the specified map to this cache.
96      */

97     public void putAll(Map JavaDoc map)
98     {
99
100         Object JavaDoc key, value;
101         Set JavaDoc keys = map.keySet();
102         Iterator JavaDoc i = keys.iterator();
103
104         while (i.hasNext())
105         {
106             key = i.next();
107             value = map.get(key);
108
109             if (value != null)
110             {
111                 cache.addObject(key, value);
112             }
113         }
114     }
115
116
117     /**
118      * Clears the cache
119      */

120     public void clear()
121     {
122         cache.clear();
123     }
124
125
126     /**
127      * <b>not implemented</b>
128      */

129     public boolean containsValue(Object JavaDoc value)
130     {
131         throw new UnsupportedOperationException JavaDoc("containsValue");
132     }
133
134
135     /**
136      * <b>not implemented</b>
137      */

138     public Set JavaDoc keySet()
139     {
140         throw new UnsupportedOperationException JavaDoc("keySet");
141     }
142
143
144     /**
145      * <b>not implemented</b>
146      */

147     public Collection JavaDoc values()
148     {
149         throw new UnsupportedOperationException JavaDoc("values");
150     }
151
152
153     /**
154      * <b>not implemented</b>
155      */

156     public Set JavaDoc entrySet()
157     {
158         throw new UnsupportedOperationException JavaDoc("entrySet");
159     }
160 }
161
Popular Tags