Class ControllableClock

java.lang.Object
java.time.Clock
no.digipost.time.ControllableClock
All Implemented Interfaces:
Serializable, InstantSource, ClockAdjuster

public final class ControllableClock extends Clock implements Serializable
A controllable Clock, typically intended for use in testing. A ControllableClock may either be freely running (as e.g. the system clock) or freezed, and can in all cases be set to a new point in time.
See Also:
  • Method Details

    • freezedAt

      public static ControllableClock freezedAt(LocalDateTime dateTime)
      Create a new controllable clock freezed at the instant resolved from a given LocalDateTime and the default time zone. The clock will not progress on its own, and will yield the instant it is set to.
      Parameters:
      dateTime - The date and time to set for the new freezed clock.
      Returns:
      the new ControllableClock
    • freezedAt

      public static ControllableClock freezedAt(ZonedDateTime dateTime)
      Create a new controllable clock freezed at the instant of a given ZonedDateTime. The clock will not progress on its own, and will yield the instant it is set to.
      Parameters:
      dateTime - The date and time to set for the new freezed clock.
      Returns:
      the new ControllableClock
    • freezedAt

      public static ControllableClock freezedAt(Instant instant)
      Create a new controllable clock freezed at a given instant, and with the default time zone. The clock will not progress on its own, and will yield the instant it is set to.
      Parameters:
      instant - The instant to set for the new freezed clock.
      Returns:
      the new ControllableClock
    • freezedAt

      public static ControllableClock freezedAt(Instant instant, ZoneId zone)
      Create a new controllable clock freezed at a given Instant, and with a given ZoneId. The clock will not progress on its own, and will yield a set instant.
      Parameters:
      instant - The instant to set for the new freezed clock.
      zone - The time zone of the new freezed clock.
      Returns:
      the new ControllableClock
    • control

      public static ControllableClock control(Clock clock)
      Create a controllable clock based on an existing clock. The new ControllableClock will have the same behavior (freezed or progressing) and time as the given clock, but may be mutated to yield another instant, or be freezed. The given clock is of course not altered.
      Parameters:
      clock - The clock to base the new ControllableClock instance on.
      Returns:
      the new ControllableClock
    • withZone

      public ControllableClock withZone(ZoneId zone)
      Specified by:
      withZone in interface InstantSource
      Specified by:
      withZone in class Clock
    • instant

      public Instant instant()
      Specified by:
      instant in interface InstantSource
      Specified by:
      instant in class Clock
    • getZone

      public ZoneId getZone()
      Specified by:
      getZone in class Clock
    • doWithTimeAdjusted

      public <X extends Exception> void doWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingConsumer<Instant,X> action) throws X
      Perform an action with the clock adjusted, and have the clock reset to it's original state after the action has finished.
      Type Parameters:
      X - Exception the may be thrown from the given action
      Parameters:
      adjustClock - how to adjust the clock before running the action
      action - the action to perform, which is given an instant resolved from the adjusted clock
      Throws:
      X - if the given action throws an exception
    • getWithTimeAdjusted

      public <T, X extends Exception> T getWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingFunction<Instant,T,X> resolveValue) throws X
      Resolve a value with the clock adjusted, and have the clock reset to it's original state after the operation has finished.
      Type Parameters:
      T - The returned type
      X - Exception the may be thrown from the given function
      Parameters:
      adjustClock - how to adjust the clock before running the action
      resolveValue - the operation which resolves the value, which is given an instant resolved from the adjusted clock
      Returns:
      the value returned from the given resolveValue function
      Throws:
      X - if the function throws an exception while resolving the value.
    • set

      public void set(UnaryOperator<Clock> createNewClock)
      Description copied from interface: ClockAdjuster
      Set a new clock to resolve the time from.
      Specified by:
      set in interface ClockAdjuster
      Parameters:
      createNewClock - function which is given the current clock, which may be used to create a new clock.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Clock
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Clock
    • timePasses

      default void timePasses(Duration amountOfTime)
      Description copied from interface: ClockAdjuster
      Signal that time is passing a given amount of time.
      Specified by:
      timePasses in interface ClockAdjuster
      Parameters:
      amountOfTime - the amount of time which are passing.
    • timePasses

      default void timePasses(TemporalAmount amountOfTime)
      Description copied from interface: ClockAdjuster
      Signal that time is passing a given amount of time.
      Specified by:
      timePasses in interface ClockAdjuster
      Parameters:
      amountOfTime - the amount of time which are passing.
    • freezeAt

      default void freezeAt(Instant instant, ZoneId zone)
      Description copied from interface: ClockAdjuster
      Freeze the clock at given instant and zone.
      Specified by:
      freezeAt in interface ClockAdjuster
    • freezeAt

      default void freezeAt(Instant instant)
      Description copied from interface: ClockAdjuster
      Freeze the clock at given instant.
      Specified by:
      freezeAt in interface ClockAdjuster
    • freezeAt

      default void freezeAt(ZonedDateTime zonedDateTime)
      Description copied from interface: ClockAdjuster
      Freeze the clock at given date and time.
      Specified by:
      freezeAt in interface ClockAdjuster
    • freezeAt

      default void freezeAt(OffsetDateTime offsetDateTime)
      Description copied from interface: ClockAdjuster
      Freeze the clock at given date and time.
      Specified by:
      freezeAt in interface ClockAdjuster
    • freezeAt

      default void freezeAt(LocalDateTime offsetDateTime)
      Description copied from interface: ClockAdjuster
      Freeze the clock at given date and time.
      Specified by:
      freezeAt in interface ClockAdjuster
    • freeze

      default void freeze()
      Description copied from interface: ClockAdjuster
      Freeze the clock at its current instant in time.
      Specified by:
      freeze in interface ClockAdjuster
    • freezeTruncatedTo

      default void freezeTruncatedTo(TemporalUnit unit)
      Description copied from interface: ClockAdjuster
      Freeze the clock at its current instant in time, which will be truncated to the given unit.
      Specified by:
      freezeTruncatedTo in interface ClockAdjuster
      Parameters:
      unit - the unit the freezed instant will be truncated to
      See Also:
    • setToSystemClock

      default void setToSystemClock(ZoneId zoneId)
      Description copied from interface: ClockAdjuster
      Set the time to freely progressing system time with the given zone.
      Specified by:
      setToSystemClock in interface ClockAdjuster
      See Also:
    • setToSystemClock

      default void setToSystemClock()
      Description copied from interface: ClockAdjuster
      Set the time to freely progressing system time.
      Specified by:
      setToSystemClock in interface ClockAdjuster
      See Also:
    • set

      default void set(Instant instant, ZoneId zone)
      Description copied from interface: ClockAdjuster
      Set the time of the clock to the given instant and zone.
      Specified by:
      set in interface ClockAdjuster
      Parameters:
      instant - the instant to set.
      zone - the zone to set.
    • set

      default void set(Instant instant)
      Description copied from interface: ClockAdjuster
      Set the time to the given instant.
      Specified by:
      set in interface ClockAdjuster
      Parameters:
      instant - the instant to set.
    • set

      default void set(ZonedDateTime zonedDateTime)
      Description copied from interface: ClockAdjuster
      Set the time to the given zoned date and time.
      Specified by:
      set in interface ClockAdjuster
      Parameters:
      zonedDateTime - the date and time to set.
    • set

      default void set(OffsetDateTime offsetDateTime)
      Description copied from interface: ClockAdjuster
      Set the time to the given offset date and time.
      Specified by:
      set in interface ClockAdjuster
      Parameters:
      offsetDateTime - the date and time to set.
    • set

      default void set(LocalDateTime localDateTime)
      Description copied from interface: ClockAdjuster
      Set the time to the given local date and time.
      Specified by:
      set in interface ClockAdjuster
      Parameters:
      localDateTime - the date and time to set.
    • zonedDateTime

      default ZonedDateTime zonedDateTime()
      Gets the current ZonedDateTime resolved with the zone of the clock.
      Returns:
      the current time as a zoned date and time.
    • localDateTime

      default LocalDateTime localDateTime()
      Gets the current LocalDateTime resolved for the zone of the clock.
      Returns:
      the current time as a local date and time.
    • offsetDateTime

      default OffsetDateTime offsetDateTime()
      Gets the current OffsetDateTime resolved for the zone of the clock.
      Returns:
      the current time as a date and time with zone offset.