-
Notifications
You must be signed in to change notification settings - Fork 615
/
ControlledTimeService.java
70 lines (55 loc) · 1.76 KB
/
ControlledTimeService.java
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
package org.infinispan.commons.time;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import org.infinispan.commons.logging.Log;
import org.infinispan.commons.logging.LogFactory;
/**
* TimeService that allows for wall clock time to be adjust manually.
*/
public class ControlledTimeService extends DefaultTimeService {
private static final Log log = LogFactory.getLog(ControlledTimeService.class);
private final Object id;
private TimeService actualTimeService;
protected volatile long currentMillis;
public ControlledTimeService() {
this(null);
}
public ControlledTimeService(Object id) {
this(id, 1_000_000L);
}
public ControlledTimeService(Object id, ControlledTimeService other) {
this(id, other.currentMillis);
}
public ControlledTimeService(Object id, long currentMillis) {
this.id = id;
this.currentMillis = currentMillis;
}
@Override
public long wallClockTime() {
return currentMillis;
}
@Override
public long time() {
return TimeUnit.MILLISECONDS.toNanos(currentMillis);
}
@Override
public Instant instant() {
return Instant.ofEpochMilli(currentMillis);
}
public void advance(long delta, TimeUnit timeUnit) {
advance(timeUnit.toMillis(delta));
}
public TimeService getActualTimeService() {
return actualTimeService;
}
public void setActualTimeService(TimeService actualTimeService) {
this.actualTimeService = actualTimeService;
}
public synchronized void advance(long deltaMillis) {
if (deltaMillis <= 0) {
throw new IllegalArgumentException("Argument must be greater than 0");
}
currentMillis += deltaMillis;
log.tracef("Current time for %s is now %d", id, currentMillis);
}
}