public class SetTimeProvider extends AtomicLong implements TimeProvider
TimeProvider implementation that allows explicit setting and manipulation of time values
for testing purposes. This class supports time values in milliseconds, microseconds, and nanoseconds.
It can be set to a specific time and can be auto-incremented at each call, which is useful for
simulating time progression in a controlled testing environment.| Constructor and Description |
|---|
SetTimeProvider()
Constructs a time provider initialized to 0 nanoseconds.
|
SetTimeProvider(Instant instant)
Constructs a time provider starting at a given
Instant. |
SetTimeProvider(long initialNanos)
Constructs a time provider starting at a specific time in nanoseconds.
|
SetTimeProvider(String timestamp)
Constructs a time provider starting at a time specified in ISO8601 format.
|
| Modifier and Type | Method and Description |
|---|---|
SetTimeProvider |
advanceMicros(long micros)
Advances the current time by the specified duration in microseconds.
|
SetTimeProvider |
advanceMillis(long millis)
Advances the current time by the specified duration in milliseconds.
|
SetTimeProvider |
advanceNanos(long nanos)
Advances the current time by the specified duration in nanoseconds.
|
SetTimeProvider |
autoIncrement(long autoIncrement,
TimeUnit timeUnit)
Configures this time provider to auto-increment the time after each invocation.
|
long |
currentTime(TimeUnit unit)
Converts and retrieves the current time in the specified time unit.
|
long |
currentTimeMicros()
Retrieves the current time in microseconds.
|
void |
currentTimeMicros(long micros)
Explicitly sets the current time in microseconds.
|
long |
currentTimeMillis()
Retrieves the current time in milliseconds.
|
void |
currentTimeMillis(long millis)
Explicitly sets the current time in milliseconds.
|
long |
currentTimeNanos()
Retrieves the current time in nanoseconds.
|
void |
currentTimeNanos(long nanos)
Explicitly sets the current time in nanoseconds.
|
SetTimeProvider |
now()
Constructs a time provider that starts now, using the current nanosecond time.
|
String |
toString()
Provides a string representation of the
SetTimeProvider state. |
accumulateAndGet, addAndGet, compareAndSet, decrementAndGet, doubleValue, floatValue, get, getAndAccumulate, getAndAdd, getAndDecrement, getAndIncrement, getAndSet, getAndUpdate, incrementAndGet, intValue, lazySet, longValue, set, updateAndGet, weakCompareAndSetbyteValue, shortValuepublic SetTimeProvider()
public SetTimeProvider(long initialNanos)
initialNanos - Initial time in nanoseconds since the epoch.public SetTimeProvider(String timestamp)
timestamp - The initial timestamp in ISO8601 format.public SetTimeProvider now()
SetTimeProvider initialized to the current time.public SetTimeProvider autoIncrement(long autoIncrement, TimeUnit timeUnit)
autoIncrement - The amount of time to auto-increment after each time retrieval.timeUnit - The time unit of the autoIncrement value.SetTimeProvider instance for fluent method chaining.public void currentTimeMillis(long millis)
throws IllegalArgumentException
millis - New time value in milliseconds since the epoch. It must not be less than the previous value.IllegalArgumentException - if the time is set to a value earlier than the current time.public long currentTimeMillis()
currentTimeMillis in interface TimeProviderpublic void currentTimeMicros(long micros)
throws IllegalArgumentException
micros - New time value in microseconds since the epoch. It must not be less than the previous value.IllegalArgumentException - if the time is set to a value earlier than the current time.public long currentTimeMicros()
currentTimeMicros in interface TimeProviderpublic void currentTimeNanos(long nanos)
throws IllegalArgumentException
nanos - New time value in nanoseconds since the epoch. It must not be less than the previous value.IllegalArgumentException - if the time is set to a value earlier than the current time.public long currentTimeNanos()
currentTimeNanos in interface TimeProviderpublic long currentTime(TimeUnit unit)
unit - The time unit to return the current time in.public SetTimeProvider advanceMillis(long millis)
millis - The duration in milliseconds to advance the time.SetTimeProvider instance for fluent method chaining.public SetTimeProvider advanceMicros(long micros)
micros - The duration in microseconds to advance the time.SetTimeProvider instance for fluent method chaining.public SetTimeProvider advanceNanos(long nanos)
nanos - The duration in nanoseconds to advance the time.SetTimeProvider instance for fluent method chaining.public String toString()
SetTimeProvider state.toString in class AtomicLongCopyright © 2024. All rights reserved.