1 19 package org.fjank.jcache; 20 21 import java.io.Serializable ; 22 import javax.util.jcache.Attributes; 23 import javax.util.jcache.CacheEventListener; 24 import javax.util.jcache.CacheLoader; 25 import javax.util.jcache.InvalidArgumentException; 26 27 30 public final class AttributesImpl implements Cloneable , Serializable , Attributes { 31 private static final int ONE_DAY = 24; 32 private static final int ONE_HOUR = 60; 33 private static final int ONE_MINUTE = 60; 34 35 private long createTime = System.currentTimeMillis(); 36 37 private long defaultTTL = -1; 38 43 private long flags; 44 45 private long idleTime; 46 47 private transient CacheEventListener listener; 48 49 private transient CacheLoader loader; 50 51 private int size; 52 53 private long timeToLive = -1; 54 55 private long version; 56 57 60 public AttributesImpl() { 61 } 62 63 public AttributesImpl(Attributes attributes) { 64 this.defaultTTL=attributes.getDefaultTimeToLive(); 65 this.idleTime=attributes.getIdleTime(); 66 67 this.listener=attributes.getListener(); 68 this.loader=attributes.getLoader(); 69 this.size=attributes.getSize(); 70 this.timeToLive=attributes.getTimeToLive(); 71 this.version=attributes.getVersion(); 72 long flags = 0; 73 74 if(attributes.isSet(Attributes.DISTRIBUTE)) { 75 flags |= Attributes.DISTRIBUTE; 76 } 77 if(attributes.isSet(Attributes.NOFLUSH)) { 78 flags |= Attributes.NOFLUSH; 79 } 80 if(attributes.isSet(Attributes.REPLY)) { 81 flags |= Attributes.REPLY; 82 } 83 if(attributes.isSet(Attributes.SYNCHRONIZE)) { 84 flags |= Attributes.SYNCHRONIZE; 85 } 86 if(attributes.isSet(Attributes.SPOOL)) { 87 flags |= Attributes.SPOOL; 88 } 89 if(attributes.isSet(Attributes.GROUP_TTL_DESTROY)) { 90 flags |= Attributes.GROUP_TTL_DESTROY; 91 } 92 if(attributes.isSet(Attributes.ORIGINAL)) { 93 flags |= Attributes.ORIGINAL; 94 } 95 this.flags=flags; 96 } 97 98 public void applyAttributes(Attributes attributes) { 99 this.idleTime = attributes.getIdleTime(); 100 this.timeToLive = attributes.getTimeToLive(); 101 this.defaultTTL = attributes.getDefaultTimeToLive(); 102 CacheEventListener listener = attributes.getListener(); 103 setListener(Attributes.INVALIDATE_EVENT, listener); 104 } 105 106 public boolean equals(Object arg0) { 107 if (arg0 instanceof AttributesImpl) { 108 AttributesImpl obj = (AttributesImpl) arg0; 109 if (defaultTTL != obj.defaultTTL) 110 return false; 111 if (flags != obj.flags) 112 return false; 113 if (listener != obj.listener) 114 return false; 115 if (loader != obj.loader) 116 return false; 117 if (size != obj.size) 118 return false; 119 if (timeToLive != obj.timeToLive) 120 return false; 121 if (version != obj.version) 122 return false; 123 return true; 124 } 125 return super.equals(arg0); 126 } 127 128 135 public long getCreateTime() { 136 return createTime; 137 } 138 139 public long getDefaultTimeToLive() { 140 return defaultTTL; 141 } 142 143 148 public long getIdleTime() { 149 return idleTime; 150 } 151 152 157 public CacheEventListener getListener() { 158 return listener; 159 } 160 161 166 public CacheLoader getLoader() { 167 return loader; 168 } 169 170 177 public int getSize() { 178 return size; 179 } 180 181 186 public long getTimeToLive() { 187 if (defaultTTL != -1 && timeToLive == -1) { 188 return defaultTTL; 189 } 190 return timeToLive; 191 } 192 193 198 public long getVersion() { 199 return version; 200 } 201 202 210 public boolean isSet(final long theFlags) { 211 return ((this.flags | theFlags) ^ this.flags) == 0; 212 } 213 214 223 public void reset() { 224 AttributesImpl def = new AttributesImpl(); 225 this.idleTime = def.idleTime; 226 this.timeToLive = def.timeToLive; 227 this.defaultTTL = def.defaultTTL; 228 this.listener = def.listener; 229 } 230 231 236 public void setCreateTime(final long aCreateTime) { 237 if (aCreateTime < 0) { 238 return; 239 } 240 this.createTime = aCreateTime; 241 } 242 243 262 public void setDefaultTimeToLive(final long ttl) throws InvalidArgumentException { 263 if (ttl < 0) { 264 throw new InvalidArgumentException("Default time to live must be a positive number."); 265 } 266 this.defaultTTL = ttl; 267 } 268 269 279 public void setFlags(final long theFlags) { 280 if (theFlags < 0) { 281 return; 282 } 283 this.flags = theFlags; 284 } 285 286 296 public void setIdleTime(final long idle) throws InvalidArgumentException { 297 if (idle < 0) { 298 throw new InvalidArgumentException("Idle time must be a positive number."); 299 } 300 this.idleTime = idle; 301 } 302 303 317 public void setListener(final int event, final CacheEventListener aListener) { 318 if ((event == INVALIDATE_EVENT) && (aListener != null)) { 319 this.listener = aListener; 320 } 321 } 322 323 328 public void setLoader(final CacheLoader aLoader) { 329 this.loader = aLoader; 330 } 331 332 341 public void setSize(final int aSize) { 342 if (aSize < 0) { 343 return; 344 } 345 this.size = aSize; 346 } 347 348 362 public void setTimeToLive(final long ttl) throws InvalidArgumentException { 363 if (ttl < 0) { 364 throw new InvalidArgumentException("Time to live must be a positive number."); 365 } 366 this.timeToLive = ttl; 367 } 368 369 375 public void setVersion(final long aVersion) { 376 this.version = aVersion; 377 } 378 379 392 public long timeToSeconds(final int days, final int hours, final int minutes, final int seconds) throws InvalidArgumentException { 393 if (days < 0) { 394 throw new InvalidArgumentException("Days must be larger than zero."); 395 } 396 if (hours < 0) { 397 throw new InvalidArgumentException("Hours must be larger than zero."); 398 } 399 if (minutes < 0) { 400 throw new InvalidArgumentException("Minutes must be larger than zero."); 401 } 402 if (seconds < 0) { 403 throw new InvalidArgumentException("Seconds must be larger than zero."); 404 } 405 return seconds + (ONE_MINUTE * minutes) + (ONE_MINUTE * ONE_HOUR * hours) + (ONE_MINUTE * ONE_HOUR * ONE_DAY * days); 406 } 407 408 413 public String toString() { 414 return "Attributes {ttl:" + timeToLive + '}'; } 416 } | Popular Tags |