1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.fjank.jcache.collection;
20
21 import java.util.Collection;
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.Set;
25
26 import javax.util.jcache.Attributes;
27 import javax.util.jcache.CacheException;
28 import javax.util.jcache.CacheFullException;
29 import javax.util.jcache.CacheMap;
30 import javax.util.jcache.NotARetrievableObjectException;
31
32 import org.fjank.jcache.CacheAccessImpl2;
33 import org.fjank.jcache.CacheGroup;
34 import org.fjank.jcache.CacheObject;
35 import org.fjank.jcache.CacheRegion;
36
37
38
39
40
41
42
43 public class MapAdapter implements CacheMap {
44 private final CacheAccessImpl2 acc;
45
46 public MapAdapter(CacheAccessImpl2 originalAccess) {
47 this.acc = originalAccess;
48 }
49
50
51
52
53
54
55
56
57 public int size() {
58 return acc.getRegion().getObjectCount();
59 }
60
61 public boolean isEmpty() {
62 return acc.getRegion().getObjectCount() == 0;
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 public boolean containsKey(Object key) {
80 if (key == null)
81 throw new NullPointerException("This Map does not permit null keys.");
82 return acc.isPresent(key);
83 }
84
85 public boolean containsValue(Object value) {
86 CacheRegion region = acc.getRegion();
87 return region.containsValue(value);
88 }
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111 public Object get(Object key) {
112 if (key == null)
113 throw new NullPointerException("This Map does not permit null keys.");
114 Object object = acc.get(key);
115 if (object == null) {
116 CacheException ex = acc.getException(true);
117 if (ex instanceof NotARetrievableObjectException) {
118 throw new ClassCastException(ex.getMessage());
119 }
120 if (ex != null) {
121 throw new IllegalStateException(ex.getMessage());
122 }
123
124 return null;
125 }
126 return object;
127 }
128
129
130
131
132
133
134
135
136
137 public Object put(Object key, Object value) {
138 Object prev = null;
139 if (acc.isPresent(key)) {
140 prev = acc.get(key);
141 }
142 boolean success = acc.put(key, value);
143 if (!success) {
144 CacheException ex = acc.getException(true);
145 if (ex instanceof CacheFullException) {
146 throw new IllegalArgumentException(ex.getMessage());
147 }
148 if (ex != null) {
149 throw new IllegalStateException(ex.getMessage());
150 }
151 throw new IllegalStateException("Unknown error.");
152 }
153 return prev;
154 }
155
156
157
158
159
160
161
162
163 public Object put(Object key, String group, Object value) {
164 Object prev = null;
165
166 if (!acc.isPresent("group")) {
167 acc.defineGroup(group);
168 }
169 if (acc.isPresent(key)) {
170 prev = acc.get(key, group, null);
171 }
172 acc.put(key, group, value);
173
174 return prev;
175 }
176
177
178
179
180
181
182
183
184
185
186
187 public Object remove(Object key) {
188 if (key == null) {
189 throw new NullPointerException("This Map does not permit null keys.");
190 }
191 CacheObject obj = (CacheObject) acc.getRegion().get(key);
192 if (obj == null) {
193 return null;
194 }
195 obj.invalidate();
196 return obj.get();
197 }
198
199 public Object remove(Object key, String group) {
200 if (key == null || group == null) {
201 throw new NullPointerException("This Map does not permit null keys.");
202 }
203 CacheRegion region = acc.getRegion();
204 CacheGroup group2 = region.getGroup(group);
205 if (group2 == null) {
206 return null;
207 }
208 CacheObject object = (CacheObject) group2.get(key);
209 if (object == null) {
210 return null;
211 }
212 object.invalidate();
213 return object.get();
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 public void putAll(Map t) {
230
231
232 for (Iterator iter = t.keySet().iterator(); iter.hasNext();) {
233 Object key = iter.next();
234 acc.put(key, t.get(key));
235 }
236 }
237
238 public void clear() {
239 acc.invalidate();
240 }
241
242 public Set keySet() {
243 return acc.getRegion().keySet();
244 }
245
246 public Collection values() {
247 return acc.getRegion().values();
248 }
249
250 public Set entrySet() {
251 return acc.getRegion().entrySet();
252 }
253
254
255
256
257
258
259 public Attributes getAttributes() {
260 return acc.getAttributes();
261 }
262
263
264
265
266
267
268 public Attributes getAttributes(Object name) throws CacheException {
269 return acc.getAttributes(name);
270 }
271
272
273
274
275
276
277 public Object get(Object name, Object arguments) {
278 return acc.get(name, arguments);
279 }
280
281
282
283
284
285
286
287 public void defineObject(Object name, Attributes attributes) {
288 acc.defineObject(name, attributes);
289 }
290 }