1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.fjank.jcache;
20
21 import java.io.Serializable;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27 import org.fjank.jcache.collection.CollectionProxy;
28 import org.fjank.jcache.collection.SetProxy;
29 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
30
31
32
33
34
35
36
37 public class CacheGroup implements Serializable {
38
39 protected String name;
40
41 private AttributesImpl attributes = new AttributesImpl();
42
43 private int currentSize;
44
45 private int objectCount;
46
47 protected transient Map weakReferenceObjects = new ConcurrentHashMap();
48 private transient final Map groups = new HashMap();
49
50
51
52
53 final transient Map objects = new ConcurrentHashMap();
54
55 Map getGroups() {
56 return groups;
57 }
58
59
60
61
62
63
64 public CacheGroup(final String name) {
65 this(name, null);
66 }
67
68
69
70
71
72
73
74 public CacheGroup(final String name, final AttributesImpl attributes) {
75 setName(name);
76 setAttributes(attributes);
77 }
78
79
80
81
82
83
84 private void setAttributes(final AttributesImpl attributes) {
85 if (attributes == null) {
86 return;
87 }
88 this.attributes = attributes;
89 }
90
91
92
93
94
95
96 private void setName(final String name) {
97 this.name = name;
98 }
99
100
101
102
103
104
105
106
107
108 public Object get(final Object aName) {
109
110 if ((aName != null) && aName.equals(name)) {
111 return this;
112 }
113 Object obj = weakReferenceObjects.get(aName);
114 if (obj == null) {
115
116 for (Iterator iter = groups.keySet().iterator(); iter.hasNext();) {
117 Object groupName = iter.next();
118 CacheGroup group = (CacheGroup) groups.get(groupName);
119 obj = group.get(aName);
120 if (obj != null) {
121 return obj;
122 }
123 }
124 }
125 return obj;
126 }
127
128
129
130
131
132
133
134
135 public void put(final Object name, final CacheObject object, final Object realObject) {
136 currentSize += object.getAttributes().getSize();
137 increaseObjectCount();
138 weakReferenceObjects.put(name, object);
139 objects.put(name, realObject);
140 }
141
142
143
144
145
146
147
148 public void put(final CacheGroup group) {
149 currentSize += group.getAttributes().getSize();
150 groups.put(group.getName(), group);
151 }
152
153
154
155
156
157
158 AttributesImpl getAttributes() {
159 return this.attributes;
160 }
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177 public CacheObject replace(final Object name, final CacheObject object) {
178 CacheObject oldObj = (CacheObject) weakReferenceObjects.get(name);
179 if (oldObj != null) {
180 if (oldObj.getAttributes() != null) {
181 object.setAttributes(oldObj.getAttributes());
182 }
183 weakReferenceObjects.remove(name);
184 objects.remove(name);
185 } else {
186 increaseObjectCount();
187 }
188 weakReferenceObjects.put(name, object);
189 objects.put(name, object.get());
190 return object;
191 }
192
193
194
195
196 public void invalidate() {
197 Iterator iter = weakReferenceObjects.keySet().iterator();
198 while (iter.hasNext()) {
199 Object key = iter.next();
200 CacheObject obj = (CacheObject) weakReferenceObjects.get(key);
201 obj.invalidate();
202 }
203 objects.clear();
204 iter = groups.keySet().iterator();
205 while (iter.hasNext()) {
206 Object key = iter.next();
207 CacheGroup obj = (CacheGroup) groups.get(key);
208 obj.invalidate();
209 }
210 this.objectCount = 0;
211 }
212
213
214
215
216
217 public void destroy() {
218 this.attributes = null;
219 this.currentSize = 0;
220 this.name = null;
221 for (Iterator iter = weakReferenceObjects.keySet().iterator(); iter.hasNext();) {
222 ((CacheObject) weakReferenceObjects.get(iter.next())).destroy();
223 }
224 for (Iterator iter = groups.keySet().iterator(); iter.hasNext();) {
225 ((CacheGroup) groups.get(iter.next())).destroy();
226 }
227 weakReferenceObjects.clear();
228 objects.clear();
229 groups.clear();
230 weakReferenceObjects = null;
231 }
232
233
234
235
236
237
238
239
240 public boolean contains(final Object aName) {
241 if ((aName != null) && aName.equals(name)) {
242 return true;
243 }
244
245 if (objects.containsKey(aName))
246 return true;
247
248
249
250
251
252
253
254 for (Iterator iter = groups.keySet().iterator(); iter.hasNext();) {
255 Object groupName = iter.next();
256 CacheGroup group = (CacheGroup) groups.get(groupName);
257 if (group.contains(aName)) {
258 return true;
259 }
260 }
261 return false;
262 }
263
264
265
266
267
268
269 public void removeMe(final CacheObject object) {
270 Iterator iter = weakReferenceObjects.keySet().iterator();
271 while (iter.hasNext()) {
272 if (((CacheObject) weakReferenceObjects.get(iter.next())) == object) {
273 iter.remove();
274 objects.remove(object.getKey());
275 decreaseObjectCount();
276 currentSize -= object.getAttributes().getSize();
277 break;
278 }
279 }
280 }
281
282 private void decreaseObjectCount() {
283 this.objectCount = objectCount - 1;
284 }
285
286 private void increaseObjectCount() {
287 this.objectCount = objectCount + 1;
288 }
289
290
291
292
293
294
295 int getCurrentSize() {
296 return currentSize;
297 }
298
299
300
301
302
303
304
305
306
307 public CacheGroup getGroup(final String group) {
308 return (CacheGroup) groups.get(group);
309 }
310
311
312
313
314
315
316 public String getName() {
317 return name;
318 }
319
320
321
322
323 public int getObjectCount() {
324 if (groups.isEmpty()) {
325 return objectCount;
326 }
327 int count = objectCount;
328 for (Iterator iter = groups.keySet().iterator(); iter.hasNext();) {
329 Object key = iter.next();
330 CacheGroup gr = (CacheGroup) groups.get(key);
331 count += gr.getObjectCount();
332 }
333 return count;
334 }
335
336
337
338
339
340
341 public boolean containsValue(Object value) {
342 return objects.containsValue(value);
343 }
344
345 public Set keySet() {
346 return new SetProxy(weakReferenceObjects.keySet(), this);
347 }
348
349 public Collection values() {
350 return new CollectionProxy(weakReferenceObjects.values(), this);
351 }
352
353 public Set entrySet() {
354 return new SetProxy(weakReferenceObjects.entrySet(), this);
355 }
356
357 void removeObjectReference(Object key) {
358
359 objects.remove(key);
360
361
362 }
363
364 Map getObjectReferences() {
365 return objects;
366 }
367 }