Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Commit

Permalink
gh-10: Added nanoseconds support for the WallClock classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Pitzeruse committed Aug 15, 2017
1 parent 34ce7fd commit 1af4a82
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
Expand Up @@ -11,4 +11,9 @@ public class DefaultWallClock implements WallClock {
public long currentTimeMillis() {
return System.currentTimeMillis();
}

@Override
public long nanoTime() {
return System.nanoTime();
}
}
Expand Up @@ -10,13 +10,13 @@
* @author matts
*/
public final class StoppedClock implements WallClock {
@Nonnull private final AtomicLong millis;
@Nonnull private final AtomicLong nanos;

/**
* Creates a new stopped clock frozen at the current moment in time.
*/
public StoppedClock() {
this(System.currentTimeMillis());
this(System.nanoTime(), TimeUnit.NANOSECONDS);
}

/**
Expand All @@ -25,7 +25,18 @@ public StoppedClock() {
* @param millis The current time to use for the stopped clock.
*/
public StoppedClock(final long millis) {
this.millis = new AtomicLong(millis);
this(millis, TimeUnit.MILLISECONDS);
}

/**
* Creates a new stopped clock frozen at the given moment in time with the
* specified time unit.
*
* @param time The time to use for the stopped clock.
* @param timeUnit The time unit that {@code time} is measured in.
*/
public StoppedClock(final long time, final TimeUnit timeUnit) {
nanos = new AtomicLong(timeUnit.toNanos(time));
}

/**
Expand All @@ -34,23 +45,38 @@ public StoppedClock(final long millis) {
* @param millis The new time to set the stopped clock to.
*/
public final void set(final long millis) {
this.millis.set(millis);
set(millis, TimeUnit.MILLISECONDS);
}

/**
* Reset this stopped clock to the given moment in time.
*
* @param time The new time to set the stopped clock to.
* @param timeUnit The time unit that {@code time} is measured in.
*/
public final void set(final long time, final TimeUnit timeUnit) {
this.nanos.set(timeUnit.toNanos(time));
}

/**
* Add the specified amount of time to the current clock.
*
* @param value The numeric value to add to the clock after converting
* @param time The numeric value to add to the clock after converting
* based on the provided {@code timeUnit}.
* @param timeUnit The time unit that {@code value} is measured in.
* @param timeUnit The time unit that {@code time} is measured in.
* @return The time after being adjusted by the provided offset.
*/
public final long plus(final long value, @Nonnull final TimeUnit timeUnit) {
return this.millis.addAndGet(timeUnit.toMillis(value));
public final long plus(final long time, @Nonnull final TimeUnit timeUnit) {
return this.nanos.addAndGet(timeUnit.toNanos(time));
}

@Override
public long currentTimeMillis () {
return millis.get();
return TimeUnit.NANOSECONDS.toMillis(nanos.get());
}

@Override
public long nanoTime() {
return nanos.get();
}
}
Expand Up @@ -7,4 +7,6 @@
*/
public interface WallClock {
long currentTimeMillis();

long nanoTime();
}

0 comments on commit 1af4a82

Please sign in to comment.