forked from mockito/mockito
/
Timer.java
45 lines (36 loc) · 1.06 KB
/
Timer.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
/*
* Copyright (c) 2016 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.util;
import static org.mockito.internal.exceptions.Reporter.cannotCreateTimerWithNegativeDurationTime;
import java.time.Duration;
public class Timer {
private final Duration duration;
private long startTime = -1;
public Timer(Duration duration) {
validateInput(duration);
this.duration = duration;
}
/**
* Informs whether the timer is still counting down.
*/
public boolean isCounting() {
assert startTime != -1;
return System.currentTimeMillis() - startTime <= duration.toMillis();
}
/**
* Starts the timer count down.
*/
public void start() {
startTime = System.currentTimeMillis();
}
private void validateInput(Duration duration) {
if (duration.isNegative()) {
throw cannotCreateTimerWithNegativeDurationTime(duration);
}
}
public Duration duration() {
return duration;
}
}