Skip to content

trickl/clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Trickl Clock

Maven Central build_status Maintainability Test Coverage License: MIT

A set of java.time.Clock implementations with more useful functionality.

  • MutableClock - A version of FixedClock with a mutable time.
  • RelativeClock - An adjustable clock that time keeps using a relative clock, but can be stopped, started and run at variable speeds.

Installation

To install from Maven Central:

<dependency>
  <groupId>com.github.trickl</groupId>
  <artifactId>clock</artifactId>
  <version>0.1.1</version>
</dependency>

Usage

See the Junit tests for usage

Building

To download the library into a folder called "clock" run

git clone https://github.com/trickl/clock.git

To build the library run

mvn clean build

Examples

Mutable Clock

        MutableClock mutableClock = MutableClock.at(clockStart);
        Duration adjustment = Duration.ofMinutes(5);
        mutableClock.advance(adjustment);
        assertEquals(clockStart.plus(adjustment), mutableClock.instant());

Relative Clock

        RelativeClock relativeClock = RelativeClock.standard()
            .withReferenceClock(referenceClock);

        relativeClock.setSpeed(3);
        referenceClock.advance(Duration.ofMinutes(3));
        relativeClock.setSpeed(5);
        referenceClock.advance(Duration.ofMinutes(5));
        relativeClock.setSpeed(7);
        referenceClock.advance(Duration.ofMinutes(7));

        Instant expectedTime = referenceClockStart.plus(Duration.ofMinutes(83));

        assertEquals(expectedTime, relativeClock.instant());