All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.transferwise.common.baseutils.clock.TestClock Maven / Gradle / Ivy

package com.transferwise.common.baseutils.clock;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;

public class TestClock extends Clock {

  private Instant instant;
  private ZoneId zoneId;

  public static void reset() {
    ClockHolder.setClock(Clock.systemDefaultZone());
  }

  public static TestClock createAndRegister() {
    return createAndRegister(Instant.now(ClockHolder.getClock()));
  }

  public static TestClock createAndRegister(Instant instant) {
    return createAndRegister(instant, ZoneId.systemDefault());
  }

  public static TestClock createAndRegister(Instant instant, ZoneId zoneId) {
    TestClock testClock = new TestClock(instant, zoneId);
    ClockHolder.setClock(testClock);
    return testClock;
  }

  public TestClock() {
    this(Instant.now(ClockHolder.getClock()));
  }

  public TestClock(Instant instant) {
    this(instant, ZoneId.systemDefault());
  }

  public TestClock(Instant instant, ZoneId zoneId) {
    this.instant = instant;
    this.zoneId = zoneId;
  }

  @Override
  public ZoneId getZone() {
    return zoneId;
  }

  @Override
  public Clock withZone(ZoneId zoneId) {
    return new TestClock(instant, zoneId);
  }

  @Override
  public Instant instant() {
    return instant;
  }

  public void set(Instant instant) {
    this.instant = instant;
  }

  public void tick(Duration duration) {
    instant = instant.plus(duration);
  }

  /**
   * Advances current test clock by duration represented by string.
   *
   * @param duration a string representing a Duration, for example "P1D" for 1 day or "PT1M" for 1 minute
   * @see java.time.Duration#parse(CharSequence)
   */
  public void plus(String duration) {
    tick(Duration.parse(duration));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy