Package no.digipost.time
Class ControllableClock
- java.lang.Object
-
- java.time.Clock
-
- no.digipost.time.ControllableClock
-
- All Implemented Interfaces:
Serializable,ClockAdjuster
public final class ControllableClock extends Clock implements Serializable
A controllableClock, typically intended for use in testing. AControllableClockmay 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.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Default Methods Modifier and Type Method Description static ControllableClockcontrol(Clock clock)Create a controllable clock based on an existing clock.<X extends Exception>
voiddoWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingConsumer<Instant,X> action)Perform an action with the clock adjusted, and have the clock reset to it's original state after the action has finished.booleanequals(Object obj)default voidfreeze()Freeze the clock at its current instant in time.default voidfreezeAt(Instant instant)Freeze the clock at given instant.default voidfreezeAt(Instant instant, ZoneId zone)Freeze the clock at given instant andzone.default voidfreezeAt(LocalDateTime offsetDateTime)Freeze the clock at given date and time.default voidfreezeAt(OffsetDateTime offsetDateTime)Freeze the clock at given date and time.default voidfreezeAt(ZonedDateTime zonedDateTime)Freeze the clock at given date and time.static ControllableClockfreezedAt(Instant instant)Create a new controllable clock freezed at a given instant, and with thedefault time zone.static ControllableClockfreezedAt(Instant instant, ZoneId zone)static ControllableClockfreezedAt(LocalDateTime dateTime)Create a new controllable clock freezed at the instant resolved from a givenLocalDateTimeand thedefault time zone.static ControllableClockfreezedAt(ZonedDateTime dateTime)Create a new controllable clock freezed at the instant of a givenZonedDateTime.default voidfreezeTruncatedTo(TemporalUnit unit)Freeze the clock at its current instant in time, which will be truncated to the given unit.<T,X extends Exception>
TgetWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingFunction<Instant,T,X> resolveValue)Resolve a value with the clock adjusted, and have the clock reset to it's original state after the operation has finished.ZoneIdgetZone()inthashCode()Instantinstant()default LocalDateTimelocalDateTime()Gets the currentLocalDateTimeresolved for the zone of the clock.default OffsetDateTimeoffsetDateTime()Gets the currentOffsetDateTimeresolved for the zone of the clock.default voidset(Instant instant)Set the time to the given instant.default voidset(Instant instant, ZoneId zone)Set the time of the clock to the given instant and zone.default voidset(LocalDateTime localDateTime)Set the time to the given local date and time.default voidset(OffsetDateTime offsetDateTime)Set the time to the given offset date and time.default voidset(ZonedDateTime zonedDateTime)Set the time to the given zoned date and time.voidset(UnaryOperator<Clock> createNewClock)Set a new clock to resolve the time from.default voidsetToSystemClock()Set the time to freely progressing system time.default voidsetToSystemClock(ZoneId zoneId)Set the time to freely progressing system time with the given zone.default voidtimePasses(Duration amountOfTime)Signal that time is passing a given amount of time.default voidtimePasses(TemporalAmount amountOfTime)Signal that time is passing a given amount of time.StringtoString()ControllableClockwithZone(ZoneId zone)default ZonedDateTimezonedDateTime()Gets the currentZonedDateTimeresolved with the zone of the clock.-
Methods inherited from class java.time.Clock
fixed, millis, offset, system, systemDefaultZone, systemUTC, tick, tickMillis, tickMinutes, tickSeconds
-
-
-
-
Method Detail
-
freezedAt
public static ControllableClock freezedAt(LocalDateTime dateTime)
Create a new controllable clock freezed at the instant resolved from a givenLocalDateTimeand thedefault time zone. The clock will not progress on its own, and will yield theinstantit 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 givenZonedDateTime. The clock will not progress on its own, and will yield theinstantit 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 thedefault time zone. The clock will not progress on its own, and will yield theinstantit 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 givenInstant, and with a givenZoneId. The clock will not progress on its own, and will yield a setinstant.- 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 newControllableClockwill have the same behavior (freezed or progressing) and time as the given clock, but may be mutated to yield anotherinstant, or befreezed. The given clock is of course not altered.- Parameters:
clock- The clock to base the newControllableClockinstance on.- Returns:
- the new
ControllableClock
-
withZone
public ControllableClock withZone(ZoneId zone)
-
doWithTimeAdjusted
public <X extends Exception> void doWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingConsumer<Instant,X> action) throws X extends Exception
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 actionaction- the action to perform, which is given an instant resolved from the adjusted clock- Throws:
X- if the given action throws an exceptionX extends Exception
-
getWithTimeAdjusted
public <T,X extends Exception> T getWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingFunction<Instant,T,X> resolveValue) throws X extends Exception
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 typeX- Exception the may be thrown from the given function- Parameters:
adjustClock- how to adjust the clock before running the actionresolveValue- the operation which resolves the value, which is given an instant resolved from the adjusted clock- Returns:
- the value returned from the given
resolveValuefunction - Throws:
X- if the function throws an exception while resolving the value.X extends Exception
-
set
public void set(UnaryOperator<Clock> createNewClock)
Description copied from interface:ClockAdjusterSet a new clock to resolve the time from.- Specified by:
setin interfaceClockAdjuster- Parameters:
createNewClock- function which is given the current clock, which may be used to create a new clock.
-
timePasses
public default void timePasses(Duration amountOfTime)
Description copied from interface:ClockAdjusterSignal that time is passing a given amount of time.- Specified by:
timePassesin interfaceClockAdjuster- Parameters:
amountOfTime- the amount of time which are passing.
-
timePasses
public default void timePasses(TemporalAmount amountOfTime)
Description copied from interface:ClockAdjusterSignal that time is passing a given amount of time.- Specified by:
timePassesin interfaceClockAdjuster- Parameters:
amountOfTime- the amount of time which are passing.
-
freezeAt
public default void freezeAt(Instant instant, ZoneId zone)
Description copied from interface:ClockAdjusterFreeze the clock at given instant andzone.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
public default void freezeAt(Instant instant)
Description copied from interface:ClockAdjusterFreeze the clock at given instant.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
public default void freezeAt(ZonedDateTime zonedDateTime)
Description copied from interface:ClockAdjusterFreeze the clock at given date and time.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
public default void freezeAt(OffsetDateTime offsetDateTime)
Description copied from interface:ClockAdjusterFreeze the clock at given date and time.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
public default void freezeAt(LocalDateTime offsetDateTime)
Description copied from interface:ClockAdjusterFreeze the clock at given date and time.- Specified by:
freezeAtin interfaceClockAdjuster
-
freeze
public default void freeze()
Description copied from interface:ClockAdjusterFreeze the clock at its current instant in time.- Specified by:
freezein interfaceClockAdjuster
-
freezeTruncatedTo
public default void freezeTruncatedTo(TemporalUnit unit)
Description copied from interface:ClockAdjusterFreeze the clock at its current instant in time, which will be truncated to the given unit.- Specified by:
freezeTruncatedToin interfaceClockAdjuster- Parameters:
unit- the unit the freezed instant will be truncated to- See Also:
ChronoUnit,Instant.truncatedTo(TemporalUnit)
-
setToSystemClock
public default void setToSystemClock(ZoneId zoneId)
Description copied from interface:ClockAdjusterSet the time to freely progressing system time with the given zone.- Specified by:
setToSystemClockin interfaceClockAdjuster- See Also:
Clock.system(ZoneId)
-
setToSystemClock
public default void setToSystemClock()
Description copied from interface:ClockAdjusterSet the time to freely progressing system time.- Specified by:
setToSystemClockin interfaceClockAdjuster- See Also:
Clock.system(ZoneId)
-
set
public default void set(Instant instant, ZoneId zone)
Description copied from interface:ClockAdjusterSet the time of the clock to the given instant and zone.- Specified by:
setin interfaceClockAdjuster- Parameters:
instant- the instant to set.zone- the zone to set.
-
set
public default void set(Instant instant)
Description copied from interface:ClockAdjusterSet the time to the given instant.- Specified by:
setin interfaceClockAdjuster- Parameters:
instant- the instant to set.
-
set
public default void set(ZonedDateTime zonedDateTime)
Description copied from interface:ClockAdjusterSet the time to the given zoned date and time.- Specified by:
setin interfaceClockAdjuster- Parameters:
zonedDateTime- the date and time to set.
-
set
public default void set(OffsetDateTime offsetDateTime)
Description copied from interface:ClockAdjusterSet the time to the given offset date and time.- Specified by:
setin interfaceClockAdjuster- Parameters:
offsetDateTime- the date and time to set.
-
set
public default void set(LocalDateTime localDateTime)
Description copied from interface:ClockAdjusterSet the time to the given local date and time.- Specified by:
setin interfaceClockAdjuster- Parameters:
localDateTime- the date and time to set.
-
zonedDateTime
public default ZonedDateTime zonedDateTime()
Gets the currentZonedDateTimeresolved with the zone of the clock.- Returns:
- the current time as a zoned date and time.
-
localDateTime
public default LocalDateTime localDateTime()
Gets the currentLocalDateTimeresolved for the zone of the clock.- Returns:
- the current time as a local date and time.
-
offsetDateTime
public default OffsetDateTime offsetDateTime()
Gets the currentOffsetDateTimeresolved for the zone of the clock.- Returns:
- the current time as a date and time with
zone offset.
-
-