@MinDoc(copyright="Copyright 2016 A. Weinert", author="Albrecht Weinert", version="V.33", lastModified="27.03.2021", usage="make read update read .... switch read ..", purpose="a versatile Clock for real time applications") public class SwiClock extends Clock implements Serializable, AClock, SClock
java.time.Clock
for real time applications. It
provides readings of the time in different forms: as long (ms since
1.1.1070), Instant
,
ZonedDateTime
and else. All readings are
consistent with the last update()
.SwiClock
is like SynClock
. With
SynClock
the underlying Clock
(mechanism) and the
zone
are set finally at construction.SynClock
, with SwiClock
both Clock
and zone
are "switchable". Additionally, SwiClock
is an observer
and can be switched to the observed time
events as source.observer state
observed time setting events can
be determined by hasObserved()
and used / set by
updateObserved()
or as usual by just update()
.SwiClock
provides the information on its actual time source and
methods to see the abbreviations / differences to system time.SwiClock
is also a base for providing a simulated Clock for
tests and demos as well as to read the time from a controlled system,
like, e.g., a Simatic PLC system, for real time automation
applications. Those process control applications may (and do) extend
SwiClock for their special time keeping requirements. That's why
SwiClock
is not final.TimeHelper
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected Instant |
actTime |
protected long |
actTimeMS |
protected ZonedDateTime |
actTimeZd |
protected Clock |
clock
The underlying clock.
|
protected boolean |
gotMs |
protected ZonedDateTime |
observedDateTime |
protected Instant |
observedInstant |
protected long |
observedMs |
protected boolean |
observer |
protected ZoneId |
zone
This Clock's / SwiClocks's time-zone.
|
Modifier | Constructor and Description |
---|---|
protected |
SwiClock(Clock clock,
boolean observer)
Constructor for inheritors.
|
Modifier and Type | Method and Description |
---|---|
Duration |
between()
Difference to the underlying system's clock as Duration.
|
long |
difToMs(long ms)
Difference to a time in ms.
|
long |
difToSystemClock()
Difference to the underlying system's clock.
|
FixClock |
fix()
Provide another AClock with a fixed time according to current state.
|
ZonedDateTime |
getActTime()
The current zone related time of this SynClock.
|
ZonedDateTime |
getActTime(ZoneId zone)
The current zone related time of this SynClock.
|
Clock |
getClock()
The SwiClock's actually used Clock.
|
ZoneId |
getZone()
The SwiClock's actually used time-zone.
|
boolean |
hasObserved()
The observed time source set new values.
|
Instant |
instant()
The current instant of this SynClock.
|
void |
instantChange(Instant instant)
Set the time by an Instant.
|
void |
instantChange(ZonedDateTime actTimeZD)
Set the time by a ZonedDateTime.
|
boolean |
isObserver()
Use as observer.
|
boolean |
isSystemClockBased()
Based on the underlying system's clock.
|
long |
millis()
The current time of this SynClock.
|
void |
msChange(long ms)
Set the time by ms since 1.1.1970.
|
static SwiClock |
ofClock(Clock clock,
boolean observer)
Obtain a SwiClock.
|
void |
setClock(Clock clock)
Set the SwiClock's actually used Clock.
|
void |
setObserver(boolean observer)
Use as observer.
|
boolean |
setSystemClockBased()
Set based on the underlying system's clock.
|
String |
toString()
The state as String.
|
boolean |
update()
Update this SynClock to its current time.
|
boolean |
updateObserved()
Update this SynClock to its current time.
|
SwiClock |
withZone(ZoneId zone)
Switch this SwiClocks time-zone.
|
equals, fixed, hashCode, offset, system, systemDefaultZone, systemUTC, tick, tickMinutes, tickSeconds
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
format, formatDIN, getDayOfMonth, getHour, getMilli, getMinute, getMonth, getMonthOfYear, getNano, getSecond, getYear, of, setActTime, setActTimeMs, setInstant
protected Clock clock
protected ZoneId zone
protected Instant actTime
protected long actTimeMS
protected transient ZonedDateTime actTimeZd
protected boolean observer
protected transient volatile Instant observedInstant
protected transient volatile ZonedDateTime observedDateTime
protected transient volatile long observedMs
protected transient volatile boolean gotMs
protected SwiClock(Clock clock, boolean observer)
clock
- the underlying Clockobserver
- see ofClock(Clock, boolean)
public static SwiClock ofClock(Clock clock, boolean observer)
clock
- the underlying Clockobserver
- true: the time source will be the object where this new
SwiClock will (in future) be registered to as
observer
. Nevertheless, the first setting at
construction will be to the provided clock. This can, of course, be
inconsistent to the observed subject.updated
to clock's actual timepublic final ZoneId getZone()
ZonedDateTime
.public final Clock getClock()
public final void setClock(Clock clock)
observer
this
SwiClock will be updated
.clock
- the new time source; null: no actionpublic SwiClock withZone(ZoneId zone)
internal Clock
object.observer
status nor does it update
.public final boolean isSystemClockBased()
clock
) the system clock
and is
not an observer
.public final long difToSystemClock()
on the system
clock this method will seldom
return >= 0L, as it will mostly be behind between
updates
.observer
.public final long difToMs(long ms)
public final Duration between()
difToSystemClock()
public final boolean setSystemClockBased()
observer
status nor does it update
. update()
is
recommended.clock
was already the system clock;
no actionpublic final Instant instant()
public final long millis()
public final ZonedDateTime getActTime()
getActTime
in interface AClock
zone
public final ZonedDateTime getActTime(ZoneId zone)
getActTime
in interface AClock
zone
- the zone to relate to; null will be this SynClock's zone
public boolean update()
public boolean updateObserved()
hasObserved()
&& update()
;
efficiently and in one synchronisation step.public String toString()
public final FixClock fix()
AClock
public boolean hasObserved()
observer
state when
instantChange()
or msChange(long)
have occurred since last update()
. if(hasObserved()
) update()
;
respectively
hasObserved()
&& update()
use
updateObserved()
;
.public void setObserver(boolean observer)
public final boolean isObserver()
public final void instantChange(Instant instant)
SClock
instantChange
in interface SClock
instant
- the changed time providedpublic final void instantChange(ZonedDateTime actTimeZD)
SClock
instantChange
in interface SClock
actTimeZD
- the changed time providedpublic final void msChange(long ms)
SClock