1 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 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 53 final transient Map objects = new ConcurrentHashMap(); 54 55 Map getGroups() { 56 return groups; 57 } 58 59 64 public CacheGroup(final String name) { 65 this(name, null); 66 } 67 68 74 public CacheGroup(final String name, final AttributesImpl attributes) { 75 setName(name); 76 setAttributes(attributes); 77 } 78 79 84 private void setAttributes(final AttributesImpl attributes) { 85 if (attributes == null) { 86 return; 87 } 88 this.attributes = attributes; 89 } 90 91 96 private void setName(final String name) { 97 this.name = name; 98 } 99 100 108 public Object get(final Object aName) { 109 if ((aName != null) && aName.equals(name)) { 111 return this; 112 } 113 Object obj = weakReferenceObjects.get(aName); 114 if (obj == null) { 115 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 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 148 public void put(final CacheGroup group) { 149 currentSize += group.getAttributes().getSize(); 150 groups.put(group.getName(), group); 151 } 152 153 158 AttributesImpl getAttributes() { 159 return this.attributes; 160 } 161 162 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 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 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 240 public boolean contains(final Object aName) { 241 if ((aName != null) && aName.equals(name)) { 242 return true; 243 } 244 if (objects.containsKey(aName)) 246 return true; 247 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 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 295 int getCurrentSize() { 296 return currentSize; 297 } 298 299 307 public CacheGroup getGroup(final String group) { 308 return (CacheGroup) groups.get(group); 309 } 310 311 316 public String getName() { 317 return name; 318 } 319 320 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 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 objects.remove(key); 360 } 363 364 Map getObjectReferences() { 365 return objects; 366 } 367 } | Popular Tags |