01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tcclient.cache;
05:
06: import java.util.Date;
07:
08: /**
09: * Tracks timestamp information on when the idle and TTL timers
10: * for a CacheData value will expire. The TTL timer (timeToDie)
11: * cannot be reset, but may not be active. The idle timer is
12: * reset each time the item is used.
13: */
14: public class Timestamp {
15:
16: private long timeToExpireMillis; // Guarded by synchronized lock on "this"
17: private final long timeToDieMillis;
18:
19: public Timestamp(long createTime, long maxIdleMillis,
20: long maxTTLMillis) {
21: this .timeToDieMillis = createTime + maxTTLMillis;
22: if (maxIdleMillis <= 0) {
23: this .timeToExpireMillis = createTime + maxTTLMillis;
24: } else {
25: this .timeToExpireMillis = createTime + maxIdleMillis;
26: }
27: }
28:
29: /**
30: * Get the time at which this timestamp will become invalid
31: */
32: public synchronized long getInvalidatedTimeMillis() {
33: if (timeToDieMillis <= 0) {
34: return timeToExpireMillis;
35: }
36:
37: return (timeToExpireMillis < timeToDieMillis) ? timeToExpireMillis
38: : timeToDieMillis;
39: }
40:
41: /**
42: * Get the maximum time at which this timestamp can be valid,
43: * regardless of usage.
44: */
45: public synchronized long getExpiredTimeMillis() {
46: return timeToExpireMillis;
47: }
48:
49: /**
50: * Modify time at which this timestamp will expire due to idle.
51: * The max TTL limit is not affected.
52: */
53: public synchronized void setExpiredTimeMillis(long millis) {
54: if (timeToDieMillis <= 0 || timeToDieMillis > millis) {
55: this .timeToExpireMillis = millis;
56: }
57: }
58:
59: public String toString() {
60: return new Date(getInvalidatedTimeMillis()).toString();
61: }
62: }
|