forked from mockito/mockito
/
Timer.java
47 lines (38 loc) · 1.12 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
46
47
/*
* 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;
import java.time.Instant;
public class Timer {
private final Duration duration;
private Instant startTime = null;
public Timer(Duration duration) {
validateInput(duration);
this.duration = duration;
}
/**
* Informs whether the timer is still counting down.
*/
public boolean isCounting() {
assert startTime != null;
Duration elapsed = Duration.between(startTime, Instant.now());
return elapsed.compareTo(duration) <= 0;
}
/**
* Starts the timer count down.
*/
public void start() {
startTime = Instant.now();
}
private void validateInput(Duration duration) {
if (duration.isNegative()) {
throw cannotCreateTimerWithNegativeDurationTime(duration);
}
}
public Duration duration() {
return duration;
}
}