Skip to content

Commit

Permalink
Use nano time instead of Instant
Browse files Browse the repository at this point in the history
  • Loading branch information
iTitus committed Dec 1, 2020
1 parent a4310ad commit 88509f9
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/main/java/io/github/ititus/math/time/StopWatch.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package io.github.ititus.math.time;

import java.time.Duration;
import java.time.Instant;

public class StopWatch {

private Instant time;
private long time;
private boolean running;

private StopWatch() {
this.time = null;
this.time = 0;
this.running = false;
}

Expand All @@ -26,8 +25,8 @@ public StopWatch start() {
throw new IllegalStateException();
}

time = Instant.now();
running = true;
time = System.nanoTime();
return this;
}

Expand All @@ -36,19 +35,13 @@ public Duration stop() {
throw new IllegalStateException();
}

long duration = System.nanoTime() - time;
running = false;
return Duration.between(time, Instant.now());
time = 0;
return Duration.ofNanos(duration);
}

public boolean isRunning() {
return running;
}

public Instant getStartTime() {
if (!running) {
throw new IllegalStateException();
}

return time;
}
}

0 comments on commit 88509f9

Please sign in to comment.