Skip to content

Commit

Permalink
Simple implementation of StopWatch
Browse files Browse the repository at this point in the history
  • Loading branch information
heySourabh committed Feb 1, 2024
1 parent f1fc755 commit 88a0355
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/util/StopWatch.java
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);
}
}
}
74 changes: 74 additions & 0 deletions test/main/util/StopWatchTest.java
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);
}
}

0 comments on commit 88a0355

Please sign in to comment.