forked from mockito/mockito
/
Timer.java
43 lines (35 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
/*
* 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;
public class Timer {
private final long durationMillis;
private long startTime = -1;
public Timer(long durationMillis) {
validateInput(durationMillis);
this.durationMillis = durationMillis;
}
/**
* Informs whether the timer is still counting down.
*/
public boolean isCounting() {
assert startTime != -1;
return System.currentTimeMillis() - startTime <= durationMillis;
}
/**
* Starts the timer count down.
*/
public void start() {
startTime = System.currentTimeMillis();
}
private void validateInput(long durationMillis) {
if (durationMillis < 0) {
throw cannotCreateTimerWithNegativeDurationTime(durationMillis);
}
}
public long duration() {
return durationMillis;
}
}