1 19 package org.fjank.jcache; 20 21 import java.lang.ref.ReferenceQueue ; 22 import java.lang.ref.WeakReference ; 23 import javax.util.jcache.Attributes; 24 import javax.util.jcache.CacheEvent; 25 import javax.util.jcache.CacheEventListener; 26 27 28 33 public class CacheObject extends WeakReference { 34 37 public boolean equals(Object arg0) { 38 if(arg0 instanceof CacheObject) { 39 CacheObject obj = (CacheObject) arg0; 40 if(!attributes.equals(obj.attributes)) return false; 41 if(!group.equals(obj.group))return false; 42 if(!key.equals(obj.key))return false; 43 if(!region.equals(obj.region))return false; 44 if(valid!=obj.valid)return false; 45 46 return true; 47 } 48 return super.equals(arg0); 49 50 } 51 55 private int refCount; 56 57 58 private boolean valid; 59 60 61 private final Object key; 62 63 64 private int accesses; 65 66 67 private final CacheGroup group; 68 69 private final CacheRegion region; 70 71 72 private AttributesImpl attributes; 73 74 83 public CacheObject(final Object key, final Object referent, 84 final CacheGroup group, final CacheRegion region, final ReferenceQueue q) { 85 super(((referent instanceof String ) ? new String (((String ) referent)) 86 : referent), q); 87 this.key = key; 88 this.group = group; 89 this.region = region; 90 if(group!=null) { 91 setAttributes(group.getAttributes()); 92 } 93 } 94 95 100 public boolean needsLoading() { 101 return needsLoading(NullObject.getInstance()); 102 } 103 boolean needsLoading(final Object arguments) { 104 if(arguments!=null && arguments !=NullObject.getInstance()) { 105 return true; 106 } 107 if ((super.get() == NullObject.getInstance()) 108 && (attributes.getLoader() != null)) { 109 return true; 110 } 111 return false; 112 } 113 116 public void destroy() { 117 this.attributes = null; 118 this.valid = false; 119 } 120 121 124 public void invalidate() { 125 this.valid = false; 126 if ((attributes.getLoader() == null) && (group != null)) { 127 group.removeMe(this); 128 } 129 130 CacheEventListener listener = attributes.getListener(); 132 if (listener != null) { 133 listener.handleEvent(new CacheEventImpl(CacheEvent.OBJECT_INVALIDATED, getKey())); 134 } 135 } 136 137 142 public Object get() { 143 accesses++; 144 refCount++; 145 return super.get(); 146 } 147 148 153 public String toString() { 154 return "CacheObject {accesses:"+ 155 accesses 156 +", attributes:"+ 157 attributes 158 +", group:"+ 159 group.getName() 160 +", key:"+ 161 key 162 +", refCount:"+ 163 refCount 164 +", region:"+ 165 region.getName() 166 +", valid:"+ 167 valid 168 +"}"; 169 } 170 171 176 public Object getKey() { 177 return key; 178 } 179 180 185 public void setAttributes(final AttributesImpl attributes) { 186 if (attributes != null) { 187 this.attributes = attributes; 188 } 189 } 190 191 public void setAttributes(Attributes attributes) { 192 if(attributes!=null) { 193 this.attributes=new AttributesImpl(attributes); 194 } 195 } 196 197 202 public AttributesImpl getAttributes() { 203 return attributes; 204 } 205 206 211 public CacheGroup getGroup() { 212 return group; 213 } 214 215 220 public int getAccesses() { 221 return accesses; 222 } 223 224 232 public int getRefCount() { 233 return refCount; 234 } 235 236 239 public void resetRefCount() { 240 refCount = 0; 241 } 242 243 244 245 248 public CacheRegion getRegion() { 249 return region; 250 } 251 252 255 public void clear() { 256 System.out.println("cleared:"+key); 258 } 259 260 261 } 262 | Popular Tags |