-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpireable.java
More file actions
92 lines (77 loc) · 1.8 KB
/
Copy pathExpireable.java
File metadata and controls
92 lines (77 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package ru.cwcode.cwutils.datetime;
import java.io.Serializable;
public class Expireable implements Serializable {
private volatile long time = 0;
private volatile long expires = 0;
public Expireable() {
}
/**
* @param ms to expire
* @param lastTime - countdown time expires from
*/
public Expireable(long ms, long lastTime) {
this(ms);
time = lastTime;
}
/**
* @param ms to expire
*/
public Expireable(long ms) {
expires = ms;
}
/**
* isExpired() and reset()
*/
public synchronized boolean isExpiredAndReset() {
if (isExpired()) {
reset();
return true;
}
return false;
}
public boolean isExpired() {
return time + expires < System.currentTimeMillis();
}
public synchronized void reset() {
time = System.currentTimeMillis();
}
public synchronized void expireAfter(long ms) {
expires = ms;
}
/**
* @return countdown time expires from
*/
public long getLastTime() {
return time;
}
/**
* @return ms to expire (absolute)
*/
public long getExpiresTime() {
return expires;
}
/**
* @return ms to expiry (relative to current time)
*/
public long getExpireAfterTime() {
return (time + expires) - System.currentTimeMillis();
}
/**
* @return percent of expiring, >=1 if expired, >0 && <1 if not expired
*/
public double getPercent() {
return (System.currentTimeMillis() - time) / (double) expires;
}
/**
* @return 1 if expired, >0 && <1 if not expired
*/
public double getPercentBounded() {
return Math.min(1.0, (System.currentTimeMillis() - time) / (double) expires);
}
/**
* @return 0 if expired, >0 && <1 if not expired
*/
public double getRevertPercentBounded() {
return 1 - getPercentBounded();
}
}