Package no.digipost.time
Class ControllableClock
java.lang.Object
java.time.Clock
no.digipost.time.ControllableClock
- All Implemented Interfaces:
Serializable,InstantSource,ClockAdjuster
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.-
Method Summary
Modifier and TypeMethodDescriptionstatic ControllableClockCreate 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.booleandefault voidfreeze()Freeze the clock at its current instant in time.default voidFreeze the clock at given instant.default voidFreeze 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 ControllableClockCreate a new controllable clock freezed at a given instant, and with thedefault time zone.static ControllableClockstatic 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 voidFreeze 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.getZone()inthashCode()instant()default LocalDateTimeGets the currentLocalDateTimeresolved for the zone of the clock.default OffsetDateTimeGets the currentOffsetDateTimeresolved for the zone of the clock.default voidSet the time to the given instant.default voidSet 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 voidSet 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.toString()default ZonedDateTimeGets 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 Details
-
freezedAt
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
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
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
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
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
- Specified by:
withZonein interfaceInstantSource- Specified by:
withZonein classClock
-
instant
- Specified by:
instantin interfaceInstantSource- Specified by:
instantin classClock
-
getZone
-
doWithTimeAdjusted
public <X extends Exception> void doWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingConsumer<Instant, X> action) throws XPerform 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 exception
-
getWithTimeAdjusted
public <T,X extends Exception> T getWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingFunction<Instant, T, throws XX> resolveValue) 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.
-
set
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.
-
toString
-
equals
-
hashCode
public int hashCode() -
timePasses
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
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
Description copied from interface:ClockAdjusterFreeze the clock at given instant andzone.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
Description copied from interface:ClockAdjusterFreeze the clock at given instant.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
Description copied from interface:ClockAdjusterFreeze the clock at given date and time.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
Description copied from interface:ClockAdjusterFreeze the clock at given date and time.- Specified by:
freezeAtin interfaceClockAdjuster
-
freezeAt
Description copied from interface:ClockAdjusterFreeze the clock at given date and time.- Specified by:
freezeAtin interfaceClockAdjuster
-
freeze
default void freeze()Description copied from interface:ClockAdjusterFreeze the clock at its current instant in time.- Specified by:
freezein interfaceClockAdjuster
-
freezeTruncatedTo
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:
-
setToSystemClock
Description copied from interface:ClockAdjusterSet the time to freely progressing system time with the given zone.- Specified by:
setToSystemClockin interfaceClockAdjuster- See Also:
-
setToSystemClock
default void setToSystemClock()Description copied from interface:ClockAdjusterSet the time to freely progressing system time.- Specified by:
setToSystemClockin interfaceClockAdjuster- See Also:
-
set
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
Description copied from interface:ClockAdjusterSet the time to the given instant.- Specified by:
setin interfaceClockAdjuster- Parameters:
instant- the instant to set.
-
set
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
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
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
Gets the currentZonedDateTimeresolved with the zone of the clock.- Returns:
- the current time as a zoned date and time.
-
localDateTime
Gets the currentLocalDateTimeresolved for the zone of the clock.- Returns:
- the current time as a local date and time.
-
offsetDateTime
Gets the currentOffsetDateTimeresolved for the zone of the clock.- Returns:
- the current time as a date and time with
zone offset.
-