public abstract class TimeSource extends Object
NOTE: depending on implementation returned values may not be related in any way to the current Epoch or calendar time, and they may even be negative - but the API guarantees that they are always monotonically increasing.
Modifier and Type | Class and Description |
---|---|
static class |
TimeSource.CurrentTimeSource
Implementation that uses
System.currentTimeMillis() . |
static class |
TimeSource.NanoTimeSource
Implementation that uses
System.nanoTime() . |
static class |
TimeSource.SimTimeSource
Implementation that uses
NANO_TIME accelerated by a double multiplier. |
Modifier and Type | Field and Description |
---|---|
static TimeSource |
CURRENT_TIME
This instance uses
TimeSource.CurrentTimeSource for generating timestamps. |
static TimeSource |
NANO_TIME
This instance uses
TimeSource.NanoTimeSource for generating timestamps. |
Constructor and Description |
---|
TimeSource() |
Modifier and Type | Method and Description |
---|---|
abstract long |
convertDelay(TimeUnit fromUnit,
long value,
TimeUnit toUnit)
This method allows using TimeSource with APIs that require providing just plain time intervals,
eg.
|
static TimeSource |
get(String type)
Obtain an instance of time source.
|
abstract long |
getEpochTimeNs()
Return Epoch time.
|
abstract long[] |
getTimeAndEpochNs()
Return both the source's time value and the corresponding epoch time
value.
|
abstract long |
getTimeNs()
Return a time value, in nanosecond units.
|
abstract void |
sleep(long ms)
Sleep according to this source's notion of time.
|
String |
toString() |
public static final TimeSource CURRENT_TIME
TimeSource.CurrentTimeSource
for generating timestamps.public static final TimeSource NANO_TIME
TimeSource.NanoTimeSource
for generating timestamps.public static TimeSource get(String type)
type
- supported types: currentTime
, nanoTime
and accelerated
time with a double factor in the form of simTime:FACTOR
, eg.
simTime:2.5
public abstract long getTimeNs()
public abstract long getEpochTimeNs()
public abstract long[] getTimeAndEpochNs()
getTimeNs()
.getTimeNs()
and the
second element is getEpochTimeNs()
.public abstract void sleep(long ms) throws InterruptedException
TimeSource.SimTimeSource
will sleep proportionally shorter, according to its multiplier.ms
- number of milliseconds to sleepInterruptedException
- when the current thread is interruptedpublic abstract long convertDelay(TimeUnit fromUnit, long value, TimeUnit toUnit)
Object.wait(long)
. Values returned by this method are adjusted according to the
time source's notion of time - eg. accelerated time source provided by TimeSource.SimTimeSource
will return intervals that are proportionally shortened by the multiplier.
NOTE: converting small values may significantly affect precision of the returned values due to rounding, especially for accelerated time source, so care should be taken to use time units that result in relatively large values. For example, converting a value of 1 expressed in seconds would result in less precision than converting a value of 1000 expressed in milliseconds.
fromUnit
- source unitvalue
- original valuetoUnit
- target unitTimeSource.SimTimeSource
)Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.