-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1fc755
commit 88a0355
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package main.util; | ||
|
||
import java.time.Duration; | ||
|
||
public class StopWatch { | ||
private long startMillis; | ||
private long pauseMillis; | ||
private boolean paused; | ||
|
||
private StopWatch(long startMillis) { | ||
this.startMillis = startMillis; | ||
this.paused = false; | ||
} | ||
|
||
public static StopWatch start() { | ||
return new StopWatch(System.currentTimeMillis()); | ||
} | ||
|
||
public void pauseWatch() { | ||
this.paused = true; | ||
this.pauseMillis = System.currentTimeMillis(); | ||
} | ||
|
||
public void continueWatch() { | ||
if (this.paused) { | ||
long currentMillis = System.currentTimeMillis(); | ||
this.startMillis += currentMillis - this.pauseMillis; | ||
this.paused = false; | ||
} | ||
} | ||
|
||
public Duration stop() { | ||
if (this.paused) { | ||
return Duration.ofMillis(this.pauseMillis - this.startMillis); | ||
} else { | ||
return Duration.ofMillis(System.currentTimeMillis() - this.startMillis); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package main.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.locks.LockSupport; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class StopWatchTest { | ||
@Test | ||
public void test_stopwatch() { | ||
StopWatch stopWatch = StopWatch.start(); | ||
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); | ||
assertEquals(stopWatch.stop().toMillis() - 100, 0, 5); | ||
} | ||
|
||
@Test | ||
public void test_pause() { | ||
StopWatch stopWatch = StopWatch.start(); | ||
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); | ||
stopWatch.pauseWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // does not matter as the watch is paused | ||
assertEquals(stopWatch.stop().toMillis() - 100, 0, 5); | ||
} | ||
|
||
@Test | ||
public void test_pause_continue() { | ||
StopWatch stopWatch = StopWatch.start(); | ||
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); // run | ||
stopWatch.pauseWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // pause for this much time | ||
stopWatch.continueWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(25).toNanos()); // again run for this much time | ||
assertEquals(stopWatch.stop().toMillis() - (100 + 25), 0, 5); | ||
} | ||
|
||
@Test | ||
public void test_pause_continue_pause() { | ||
StopWatch stopWatch = StopWatch.start(); | ||
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); // run | ||
|
||
stopWatch.pauseWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // pause for this much time | ||
|
||
stopWatch.continueWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(25).toNanos()); // again run for this much time | ||
|
||
stopWatch.pauseWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(75).toNanos()); // pause | ||
|
||
assertEquals(stopWatch.stop().toMillis() - (100 + 25), 0, 5); | ||
} | ||
|
||
@Test | ||
public void test_pause_continue_pause_continue() { | ||
StopWatch stopWatch = StopWatch.start(); | ||
LockSupport.parkNanos(Duration.ofMillis(100).toNanos()); // run | ||
|
||
stopWatch.pauseWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(50).toNanos()); // pause for this much time | ||
|
||
stopWatch.continueWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(25).toNanos()); // again run for this much time | ||
|
||
stopWatch.pauseWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(75).toNanos()); // pause | ||
|
||
stopWatch.continueWatch(); | ||
LockSupport.parkNanos(Duration.ofMillis(111).toNanos()); // again run for this much time | ||
|
||
assertEquals(stopWatch.stop().toMillis() - (100 + 25 + 111), 0, 5); | ||
} | ||
} |