This class is designed for testing clock-sensitive components by simulating
the passage of time. This class differs from Clock#fixed(Instant, ZoneId) and Clock#offset(Clock, Duration) in
that it permits arbitrary, unrestricted updates to its instant. This allows
for testing patterns that are not well-supported by the fixed and
offset clocks such as the following pattern:
This class is mutable. The time-zone of the clock is fixed, but the instant may be updated at will.
The instant may be set to any value even if that new value is less than the previous value. Caution should be exercised when moving the clock backwards, since clock-sensitive components are likely to assume that time is monotonically increasing.
Update semantics are expressed in terms of ZonedDateTime. The steps
of each update are as follows:
ZonedDateTime via ZonedDateTime#now(Clock) (or the equivalent thereof)
ZonedDateTime, producing
a new ZonedDateTime
ZonedDateTime is converted to an instant via ZonedDateTime#toInstant() (or the equivalent thereof)
Therefore, whenever there is a question about what argument types, units,
fields, or values an update operation supports, or what the result will be,
refer to the corresponding method of ZonedDateTime. Links are
provided from the documentation of each update operation of this class to the
corresponding method of ZonedDateTime.
While update semantics are expressed in terms of ZonedDateTime, that
imposes no requirements on implementation details. The implementation may
avoid using ZonedDateTime completely or only sometimes, for
convenience, efficiency, or any other reason.
| Modifier and Type | Class and Description |
|---|---|
| private static class | MutableClock.
An identity-having holder object for a mutable instant value. |
| private static class | MutableClock.
The serialized form of a |
| Modifier and Type | Field and Description |
|---|---|
| private final transient MutableClock. | instantHolder
The mutable instant of this clock. |
| private static final long | serialVersionUID
Serialization version. |
| private final transient ZoneId | zone
The fixed time-zone of this clock. |
| Access | Constructor and Description |
|---|---|
| private | MutableClock(MutableClock.
the mutable instant, validated not null instantHolder,the fixed time-zone, validated not null zone)Constructor. |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | add(long
the amount of the specified unit to add, may be negative amountToAdd, TemporalUnit the unit of the amount to add, not null unit)Adds the specified amount to this clock. |
| public static MutableClock | Returns: a newMutableClock, not nullObtains a new |
| public boolean | Returns: true if this is equal to the other clockthe object to check, null returns obj)falseOverrides java. Returns |
| public ZoneId | getZone()
Implements abstract java. Gets the time-zone being used to create dates and times. |
| public int | Returns: a constant hash code for this instanceOverrides java. A hash code for this clock, which is constant for this instance. |
| public Instant | instant()
Implements abstract java. Implements java. Gets the current instant of the clock. |
| public static MutableClock | |
| private void | |
| public void | |
| public void | set(TemporalField
the field to set, not null field, long the new value of the field newValue)Alters the specified field of this clock. |
| public void | setInstant(Instant
the new instant for this clock, not null instant)Overrides the instant of this clock with the specified value. |
| public String | |
| public MutableClock | Returns: a view of this clock in the specified time-zone, not nullthe time-zone to use for the returned clock, not null zone)Overrides default java. Implements abstract java. Returns a |
| private Object | Returns: the serialization proxy, not nullReturns the serialization proxy to replace this |
| instantHolder | back to summary |
|---|---|
| private final transient MutableClock. The mutable instant of this clock. | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version. | |
| zone | back to summary |
|---|---|
| private final transient ZoneId zone The fixed time-zone of this clock. | |
| MutableClock | back to summary |
|---|---|
| private MutableClock(MutableClock. Constructor.
| |
| add | back to summary |
|---|---|
| public void add(TemporalAmount amountToAdd) Adds the specified amount to this clock. Atomically updates this clock to the value of the following expression:
ZonedDateTime.now(thisClock)
.plus(amountToAdd)
.toInstant()
| |
| add | back to summary |
|---|---|
| public void add(long amountToAdd, TemporalUnit unit) Adds the specified amount to this clock. Atomically updates this clock to the value of the following expression:
ZonedDateTime.now(thisClock)
.plus(amountToAdd, unit)
.toInstant()
| |
| epochUTC | back to summary |
|---|---|
| public static MutableClock epochUTC() Obtains a new
Use this method when a
| |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Returns Two clocks with shared updates always have the same instant, and all updates applied to either clock affect both clocks.
A deserialized | |
| getZone | back to summary |
|---|---|
| public ZoneId getZone() Implements abstract java. Doc from java. Gets the time-zone being used to create dates and times. A clock will typically obtain the current instant and then convert that to a date or time using a time-zone. This method returns the time-zone used. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. A hash code for this clock, which is constant for this instance.
| |
| instant | back to summary |
|---|---|
| public Instant instant() Implements abstract java. Implements java. Doc from java. Gets the current instant of the clock. This returns an instant representing the current instant as defined by the clock. | |
| of | back to summary |
|---|---|
| public static MutableClock of(Instant instant, ZoneId zone) Obtains a new
| |
| readObject | back to summary |
|---|---|
| private void readObject(ObjectInputStream s) throws InvalidObjectException Throws
| |
| set | back to summary |
|---|---|
| public void set(TemporalAdjuster adjuster) Adjusts this clock. Atomically updates this clock to the value of the following expression:
ZonedDateTime.now(thisClock)
.with(adjuster)
.toInstant()
| |
| set | back to summary |
|---|---|
| public void set(TemporalField field, long newValue) Alters the specified field of this clock. Atomically updates this clock to the value of the following expression:
ZonedDateTime.now(thisClock)
.with(field, newValue)
.toInstant()
| |
| setInstant | back to summary |
|---|---|
| public void setInstant(Instant instant) Overrides the instant of this clock with the specified value.
| |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| withZone | back to summary |
|---|---|
| public MutableClock withZone(ZoneId zone) Overrides default java. Implements abstract java. Returns a Two clocks with shared updates always have the same instant, and all updates applied to either clock affect both clocks.
| |
| writeReplace | back to summary |
|---|---|
| private Object writeReplace() Returns the serialization proxy to replace this
| |
Clocks have shared updates when they share a holder object. Clocks rely
on the identity of the holder object in their equals and hashCode methods.
Reads of the value are volatile and are never stale. Blind writes to the value are volatile and do not need to synchronize. Atomic read-and-write operations must synchronize on the holder object instance.
| Modifier and Type | Field and Description |
|---|---|
| private volatile Instant | value
The current value. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv Instant | |
| pack-priv void |
| value | back to summary |
|---|---|
| private volatile Instant value The current value. | |
| InstantHolder | back to summary |
|---|---|
| pack-priv InstantHolder(Instant value) Constructor.
| |
| get | back to summary |
|---|---|
| pack-priv Instant get() Reads the value.
| |
| set | back to summary |
|---|---|
| pack-priv void set(Instant value) Writes the value.
| |
MutableClock.
| Modifier and Type | Field and Description |
|---|---|
| private final Instant | instant
A snapshot of the instant of the |
| private static final long | serialVersionUID
Serialization version. |
| private final ZoneId | zone
The time-zone of the |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| private Object | Returns: theMutableClock, not nullReturns the |
| instant | back to summary |
|---|---|
| private final Instant instant A snapshot of the instant of the | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Serialization version. | |
| zone | back to summary |
|---|---|
| private final ZoneId zone The time-zone of the | |
| SerializationProxy | back to summary |
|---|---|
| pack-priv SerializationProxy(MutableClock clock) Constructor.
| |
| readResolve | back to summary |
|---|---|
| private Object readResolve() throws InvalidObjectException Returns the
| |