1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model;
17
18 import java.util.Date;
19
20
21
22
23 public class ExpirationDetails {
24
25 public static final int UNLIMITED_AGE = 0;
26
27 private int maxAgeSeconds = UNLIMITED_AGE;
28 private long lastRefreshTimestampSeconds = getCurrentTimestampSeconds();
29
30 public ExpirationDetails() {
31 }
32
33 public ExpirationDetails(int maxAgeSeconds) {
34 this.maxAgeSeconds = maxAgeSeconds;
35 }
36
37 public int getMaxAgeSeconds() {
38 return maxAgeSeconds;
39 }
40
41 public long getLastRefreshTimestampSeconds() {
42 return lastRefreshTimestampSeconds;
43 }
44
45 public void setLastRefreshTimestampSeconds(long lastRefreshTimestampSeconds) {
46 this.lastRefreshTimestampSeconds = lastRefreshTimestampSeconds;
47 }
48
49 public void stampLastRefresh() {
50 setLastRefreshTimestampSeconds(getCurrentTimestampSeconds());
51 }
52
53 public boolean hasExpired() {
54 return hasExpired(false);
55 }
56
57
58
59
60
61 public boolean hasExpired(boolean halfTime) {
62
63 return maxAgeSeconds != UNLIMITED_AGE &&
64 (lastRefreshTimestampSeconds + (maxAgeSeconds/(halfTime ? 2 : 1))) < getCurrentTimestampSeconds();
65 }
66
67 public long getSecondsUntilExpiration() {
68
69 return maxAgeSeconds == UNLIMITED_AGE
70 ? Integer.MAX_VALUE
71 : (lastRefreshTimestampSeconds + maxAgeSeconds) - getCurrentTimestampSeconds();
72 }
73
74 protected long getCurrentTimestampSeconds() {
75 return new Date().getTime()/1000;
76 }
77
78
79 private static String simpleName = ExpirationDetails.class.getSimpleName();
80 @Override
81 public String toString() {
82 return "(" + simpleName + ")" + " MAX AGE: " + maxAgeSeconds;
83 }
84 }