public final class ControllableClock extends Clock implements Serializable
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.| Modifier and Type | Method and Description |
|---|---|
static ControllableClock |
control(Clock clock)
Create a controllable clock based on an existing clock.
|
<X extends Exception> |
doWithTimeAdjusted(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.
|
boolean |
equals(Object obj) |
default void |
freeze()
Signal that the clock should freeze at the instant it is currently at.
|
default void |
freezeAt(Instant instant)
Freeze the clock at given instant.
|
default void |
freezeAt(Instant instant,
ZoneId zone)
Freeze the clock at given instant and
zone. |
default void |
freezeAt(LocalDateTime offsetDateTime)
Freeze the clock at given date and time.
|
default void |
freezeAt(OffsetDateTime offsetDateTime)
Freeze the clock at given date and time.
|
default void |
freezeAt(ZonedDateTime zonedDateTime)
Freeze the clock at given date and time.
|
static ControllableClock |
freezedAt(Instant instant)
Create a new controllable clock freezed at a given instant,
and with the
default time zone. |
static ControllableClock |
freezedAt(Instant instant,
ZoneId zone)
|
static ControllableClock |
freezedAt(LocalDateTime dateTime)
Create a new controllable clock freezed at the instant resolved from a given
LocalDateTime and the default time zone. |
static ControllableClock |
freezedAt(ZonedDateTime dateTime)
Create a new controllable clock freezed at the instant of a given
ZonedDateTime. |
<T,X extends Exception> |
getWithTimeAdjusted(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.
|
ZoneId |
getZone() |
int |
hashCode() |
Instant |
instant() |
default LocalDateTime |
localDateTime()
Gets the current
LocalDateTime resolved for the zone of the clock. |
default OffsetDateTime |
offsetDateTime()
Gets the current
OffsetDateTime resolved for the zone of the clock. |
default void |
set(Instant instant)
Set the time to the given instant.
|
default void |
set(Instant instant,
ZoneId zone)
Set the time of the clock to the given instant and zone.
|
default void |
set(LocalDateTime localDateTime)
Set the time to the given local date and time.
|
default void |
set(OffsetDateTime offsetDateTime)
Set the time to the given offset date and time.
|
void |
set(UnaryOperator<Clock> createNewClock)
Set a new clock to resolve the time from.
|
default void |
set(ZonedDateTime zonedDateTime)
Set the time to the given zoned date and time.
|
default void |
setToSystemClock()
Set the time to freely progressing system time.
|
default void |
setToSystemClock(ZoneId zoneId)
Set the time to freely progressing system time with
the given zone.
|
default void |
timePasses(Duration amountOfTime)
Signal that time is passing a given amount of time.
|
default void |
timePasses(TemporalAmount amountOfTime)
Signal that time is passing a given amount of time.
|
String |
toString() |
ControllableClock |
withZone(ZoneId zone) |
default ZonedDateTime |
zonedDateTime()
Gets the current
ZonedDateTime resolved with the zone of the clock. |
fixed, millis, offset, system, systemDefaultZone, systemUTC, tick, tickMinutes, tickSecondspublic static ControllableClock freezedAt(LocalDateTime dateTime)
LocalDateTime and the default time zone.
The clock will not progress on its own, and will yield the instant
it is set to.dateTime - The date and time to set for the new freezed clock.ControllableClockpublic static ControllableClock freezedAt(ZonedDateTime dateTime)
ZonedDateTime.
The clock will not progress on its own, and will yield the instant
it is set to.dateTime - The date and time to set for the new freezed clock.ControllableClockpublic static ControllableClock freezedAt(Instant instant)
default time zone.
The clock will not progress on its own, and will yield the instant
it is set to.instant - The instant to set for the new freezed clock.ControllableClockpublic static ControllableClock freezedAt(Instant instant, ZoneId zone)
Instant, and with a given ZoneId.
The clock will not progress on its own, and will yield a set instant.instant - The instant to set for the new freezed clock.zone - The time zone of the new freezed clock.ControllableClockpublic static ControllableClock control(Clock clock)
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.clock - The clock to base the new ControllableClock instance on.ControllableClockpublic ControllableClock withZone(ZoneId zone)
public <X extends Exception> void doWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingConsumer<Instant,X> action) throws X extends Exception
X - Exception the may be thrown from the given actionadjustClock - how to adjust the clock before running the actionaction - the action to perform, which is given an instant resolved from the adjusted clockX - if the given action throws an exceptionX extends Exceptionpublic <T,X extends Exception> T getWithTimeAdjusted(Consumer<ClockAdjuster> adjustClock, ThrowingFunction<Instant,T,X> resolveValue) throws X extends Exception
T - The returned typeX - Exception the may be thrown from the given functionadjustClock - how to adjust the clock before running the actionresolveValue - the operation which resolves the value, which is given an instant resolved from the adjusted clockresolveValue functionX - if the function throws an exception while resolving the value.X extends Exceptionpublic void set(UnaryOperator<Clock> createNewClock)
ClockAdjustercreateNewClock - function which is given the current clock, which may
be used to create a new clock.public void timePasses(Duration amountOfTime)
ClockAdjustertimePasses in interface ClockAdjusteramountOfTime - the amount of time which are passing.public void timePasses(TemporalAmount amountOfTime)
ClockAdjustertimePasses in interface ClockAdjusteramountOfTime - the amount of time which are passing.public void freezeAt(Instant instant, ZoneId zone)
ClockAdjusterzone.freezeAt in interface ClockAdjusterpublic void freezeAt(Instant instant)
ClockAdjusterfreezeAt in interface ClockAdjusterpublic void freezeAt(ZonedDateTime zonedDateTime)
ClockAdjusterfreezeAt in interface ClockAdjusterpublic void freezeAt(OffsetDateTime offsetDateTime)
ClockAdjusterfreezeAt in interface ClockAdjusterpublic void freezeAt(LocalDateTime offsetDateTime)
ClockAdjusterfreezeAt in interface ClockAdjusterpublic void freeze()
ClockAdjusterfreeze in interface ClockAdjusterpublic void setToSystemClock(ZoneId zoneId)
ClockAdjustersetToSystemClock in interface ClockAdjusterClock.system(ZoneId)public void setToSystemClock()
ClockAdjustersetToSystemClock in interface ClockAdjusterClock.system(ZoneId)public void set(Instant instant, ZoneId zone)
ClockAdjusterset in interface ClockAdjusterinstant - the instant to set.zone - the zone to set.public void set(Instant instant)
ClockAdjusterset in interface ClockAdjusterinstant - the instant to set.public void set(ZonedDateTime zonedDateTime)
ClockAdjusterset in interface ClockAdjusterzonedDateTime - the date and time to set.public void set(OffsetDateTime offsetDateTime)
ClockAdjusterset in interface ClockAdjusteroffsetDateTime - the date and time to set.public void set(LocalDateTime localDateTime)
ClockAdjusterset in interface ClockAdjusterlocalDateTime - the date and time to set.public ZonedDateTime zonedDateTime()
ZonedDateTime resolved with the zone of the clock.public LocalDateTime localDateTime()
LocalDateTime resolved for the zone of the clock.public OffsetDateTime offsetDateTime()
OffsetDateTime resolved for the zone of the clock.zone offset.Copyright © 2019 Digipost. All rights reserved.