1 package cz.cuni.amis.utils;
2
3 import java.util.concurrent.TimeUnit;
4
5
6
7
8
9
10
11
12
13
14 public class Heatup {
15
16
17
18
19 private long heatupMillis;
20
21
22
23
24 private long lastUsedMillis;
25
26 public Heatup(long cooldownMillis) {
27 this(cooldownMillis, TimeUnit.MILLISECONDS);
28 }
29
30 public Heatup(long cooldownTime, TimeUnit timeUnit) {
31 switch(timeUnit) {
32 case DAYS: this.heatupMillis = cooldownTime * 24 * 60 * 60 * 1000; break;
33 case HOURS: this.heatupMillis = cooldownTime * 60 * 60 * 1000; break;
34 case MICROSECONDS: throw new UnsupportedOperationException("Unsupported: MICROSECONDS.");
35 case MILLISECONDS: this.heatupMillis = cooldownTime; break;
36 case MINUTES: this.heatupMillis = cooldownTime * 60 * 1000; break;
37 case NANOSECONDS: throw new UnsupportedOperationException("Unsupported: NANOSECONDS.");
38 case SECONDS: this.heatupMillis = cooldownTime * 1000; break;
39 }
40 }
41
42
43
44
45
46 public boolean isHot() {
47 return (System.currentTimeMillis() - lastUsedMillis < heatupMillis);
48 }
49
50
51
52
53 public void heat() {
54 lastUsedMillis = System.currentTimeMillis();
55 }
56
57
58
59
60
61 public long getRemainingTime() {
62 if (!isHot()) return 0;
63 return heatupMillis - (System.currentTimeMillis() - lastUsedMillis);
64 }
65
66
67
68
69 public void clear() {
70 lastUsedMillis = 0;
71 }
72
73
74
75
76
77 public boolean isCool() {
78 return !isHot();
79 }
80
81 }