Package org.kiwiproject.base
Class DefaultEnvironment
java.lang.Object
org.kiwiproject.base.DefaultEnvironment
- All Implemented Interfaces:
KiwiEnvironment
A default implementation of the
KiwiEnvironment interface. Normal usage is to define a private
KiwiEnvironment and initialize it to a new instance of this class, for example in a constructor:
Then wherever you would normally call things like System.currentTimeMillis(), use the corresponding method
from KiwiEnvironment instead, e.g. env.currentTimeMillis().
For testing environment-related code, inject a mock instance via a constructor. A common pattern is to provide
a separate constructor that accepts a KiwiEnvironment specifically for test code to use; often this should
be made package-private (default scope). Other constructors will generally call this constructor. For example:
private KiwiEnvironment env;
public Foo() {
this(new DefaultKiwiEnvironment());
}
Foo(KiwiEnvironment env) {
this.env = env;
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the current date.Returns the currentInstantin the default time zone.currentInstant(ZoneId zone) Returns the currentInstantin the specified time zone.Returns the currentLocalDatein the default time zone.currentLocalDate(ZoneId zone) Returns the currentLocalDatein the specified time zone.Returns the currentLocalDateTimein the default time zone.currentLocalDateTime(ZoneId zone) Returns the currentLocalDateTimein the specified time zone.Returns the currentLocalTimein the default time zone.currentLocalTime(ZoneId zone) Returns the currentLocalTimein the specified time zone.longReturns the process ID of the currently executing JVM.Returns a ProcessHandle for the current process.Returns a reference to the currently executing thread object.Returns the current time.longReturns the current time in milliseconds since the epoch.Returns the current timestamp.Returns the currentZonedDateTimein the default time zone.currentZonedDateTime(ZoneId zone) Returns the currentZonedDateTimein the specified time zone.getenv()Returns an unmodifiable string map view of the environment.Gets the value of the specified environment variable.Returns the current system properties.getProperty(String key) Gets the system property indicated by the specified key.getProperty(String key, String defaultValue) Gets the system property indicated by the specified key.longnanoTime()Returns the current value of the running JVM's time source, in nanoseconds.processHandleOfPid(long pid) Tries to obtain aProcessHandlefor a process with the given ID.voidsleep(long milliseconds) Sleep for the given number of milliseconds.voidsleep(long millis, int nanos) Sleep for the specified number of milliseconds plus nanoseconds.voidSleep for a specific amount of time given by the specifiedtimeoutandTimeUnit.booleansleepQuietly(long milliseconds) Sleep for the given number of milliseconds.booleansleepQuietly(long millis, int nanos) Sleep for the given number of milliseconds plus nanoseconds.booleansleepQuietly(long timeout, TimeUnit unit) Sleep for a specific amount of time given by the specifiedtimeoutandTimeUnit.Tries to obtain the process ID of the currently executing JVM.
-
Constructor Details
-
DefaultEnvironment
public DefaultEnvironment()
-
-
Method Details
-
currentDate
Description copied from interface:KiwiEnvironmentReturns the current date.- Specified by:
currentDatein interfaceKiwiEnvironment- Returns:
- the current date as a
Dateobject - See Also:
-
currentTime
Description copied from interface:KiwiEnvironmentReturns the current time. For use with the JDBC API.- Specified by:
currentTimein interfaceKiwiEnvironment- Returns:
- the current time as a
Timeobject - See Also:
-
currentTimestamp
Description copied from interface:KiwiEnvironmentReturns the current timestamp. For use with the JDBC API.- Specified by:
currentTimestampin interfaceKiwiEnvironment- Returns:
- the current timestamp as a
Timestampobject - See Also:
-
currentInstant
Description copied from interface:KiwiEnvironmentReturns the currentInstantin the default time zone.- Specified by:
currentInstantin interfaceKiwiEnvironment- Returns:
- the current instant
- See Also:
-
currentInstant
Description copied from interface:KiwiEnvironmentReturns the currentInstantin the specified time zone.- Specified by:
currentInstantin interfaceKiwiEnvironment- Parameters:
zone- the zone ID to use, not null- Returns:
- the current instant
- See Also:
-
currentLocalDate
Description copied from interface:KiwiEnvironmentReturns the currentLocalDatein the default time zone.- Specified by:
currentLocalDatein interfaceKiwiEnvironment- Returns:
- the current local date
- See Also:
-
currentLocalDate
Description copied from interface:KiwiEnvironmentReturns the currentLocalDatein the specified time zone.- Specified by:
currentLocalDatein interfaceKiwiEnvironment- Parameters:
zone- the zone ID to use, not null- Returns:
- the current local date
- See Also:
-
currentLocalTime
Description copied from interface:KiwiEnvironmentReturns the currentLocalTimein the default time zone.- Specified by:
currentLocalTimein interfaceKiwiEnvironment- Returns:
- the current local time
- See Also:
-
currentLocalTime
Description copied from interface:KiwiEnvironmentReturns the currentLocalTimein the specified time zone.- Specified by:
currentLocalTimein interfaceKiwiEnvironment- Parameters:
zone- the zone ID to use, not null- Returns:
- the current local time
- See Also:
-
currentLocalDateTime
Description copied from interface:KiwiEnvironmentReturns the currentLocalDateTimein the default time zone.- Specified by:
currentLocalDateTimein interfaceKiwiEnvironment- Returns:
- the current local date/time
- See Also:
-
currentLocalDateTime
Description copied from interface:KiwiEnvironmentReturns the currentLocalDateTimein the specified time zone.- Specified by:
currentLocalDateTimein interfaceKiwiEnvironment- Parameters:
zone- the zone ID to use, not null- Returns:
- the current local date/time
- See Also:
-
currentZonedDateTimeUTC
Description copied from interface:KiwiEnvironment- Specified by:
currentZonedDateTimeUTCin interfaceKiwiEnvironment- Returns:
- the current date/time in UTC
- See Also:
-
currentZonedDateTime
Description copied from interface:KiwiEnvironmentReturns the currentZonedDateTimein the specified time zone.- Specified by:
currentZonedDateTimein interfaceKiwiEnvironment- Parameters:
zone- the zone ID to use, not null- Returns:
- a
ZonedDateTimerepresenting the current date/time in the specified zone - See Also:
-
currentZonedDateTime
Description copied from interface:KiwiEnvironmentReturns the currentZonedDateTimein the default time zone.- Specified by:
currentZonedDateTimein interfaceKiwiEnvironment- Returns:
- a
ZonedDateTimerepresenting the current date/time in the default time zone - See Also:
-
currentTimeMillis
public long currentTimeMillis()Description copied from interface:KiwiEnvironmentReturns the current time in milliseconds since the epoch.- Specified by:
currentTimeMillisin interfaceKiwiEnvironment- Returns:
- the current time in milliseconds
- See Also:
-
nanoTime
public long nanoTime()Description copied from interface:KiwiEnvironmentReturns the current value of the running JVM's time source, in nanoseconds.Only used to measure elapsed time, per
System.nanoTime().- Specified by:
nanoTimein interfaceKiwiEnvironment- Returns:
- the current time in nanoseconds
- See Also:
-
currentPid
public long currentPid()Description copied from interface:KiwiEnvironmentReturns the process ID of the currently executing JVM. This method does not perform any error checking. UseKiwiEnvironment.tryGetCurrentPid()for a version that returns an empty optional if it is unable to obtain the pid for any reason.- Specified by:
currentPidin interfaceKiwiEnvironment- Returns:
- the pid of the current process
- See Also:
-
tryGetCurrentPid
Description copied from interface:KiwiEnvironmentTries to obtain the process ID of the currently executing JVM. If any problem occurs, it is caught and an empty optional is returned.- Specified by:
tryGetCurrentPidin interfaceKiwiEnvironment- Returns:
- an optional containing the pid of the current process, or empty if any problem occurred
- See Also:
-
currentProcessHandle
Description copied from interface:KiwiEnvironmentReturns a ProcessHandle for the current process.- Specified by:
currentProcessHandlein interfaceKiwiEnvironment- Returns:
- a handle to the current process
- See Also:
-
processHandleOfPid
Description copied from interface:KiwiEnvironmentTries to obtain aProcessHandlefor a process with the given ID. If the process does not exist, then an empty Optional is returned.- Specified by:
processHandleOfPidin interfaceKiwiEnvironment- Parameters:
pid- the process ID- Returns:
- an Optional containing a ProcessHandle for the given process ID, or an empty Optional if the process does not exist
- See Also:
-
currentThread
Description copied from interface:KiwiEnvironmentReturns a reference to the currently executing thread object.- Specified by:
currentThreadin interfaceKiwiEnvironment- Returns:
- the currently executing thread
- See Also:
-
sleep
Description copied from interface:KiwiEnvironmentSleep for the given number of milliseconds.- Specified by:
sleepin interfaceKiwiEnvironment- Parameters:
milliseconds- the number of milliseconds to sleep- Throws:
InterruptedException- if interrupted- See Also:
-
sleep
Description copied from interface:KiwiEnvironmentSleep for a specific amount of time given by the specifiedtimeoutandTimeUnit.- Specified by:
sleepin interfaceKiwiEnvironment- Parameters:
timeout- the value to sleepunit- the unit to sleep, e.g.TimeUnit.SECONDS- Throws:
InterruptedException- if interrupted- See Also:
-
sleep
Description copied from interface:KiwiEnvironmentSleep for the specified number of milliseconds plus nanoseconds.- Specified by:
sleepin interfaceKiwiEnvironment- Parameters:
millis- the number of milliseconds to sleepnanos-0-999999additional nanoseconds to sleep- Throws:
InterruptedException- if interrupted- See Also:
-
sleepQuietly
public boolean sleepQuietly(long milliseconds) Description copied from interface:KiwiEnvironmentSleep for the given number of milliseconds. Will never throw anInterruptedException.- Specified by:
sleepQuietlyin interfaceKiwiEnvironment- Parameters:
milliseconds- the number of milliseconds to sleep- Returns:
- false if the sleep was not interrupted, and true if it was interrupted
- See Also:
-
sleepQuietly
Description copied from interface:KiwiEnvironmentSleep for a specific amount of time given by the specifiedtimeoutandTimeUnit. Will never throw anInterruptedException.- Specified by:
sleepQuietlyin interfaceKiwiEnvironment- Parameters:
timeout- the value to sleepunit- the unit to sleep, e.g.TimeUnit.SECONDS- Returns:
- false if the sleep was not interrupted, and true if it was interrupted
- See Also:
-
sleepQuietly
public boolean sleepQuietly(long millis, int nanos) Description copied from interface:KiwiEnvironmentSleep for the given number of milliseconds plus nanoseconds. Will never throw anInterruptedException.- Specified by:
sleepQuietlyin interfaceKiwiEnvironment- Parameters:
millis- the number of milliseconds to sleepnanos-0-999999additional nanoseconds to sleep- Returns:
- false if the sleep was not interrupted, and true if it was interrupted
- See Also:
-
getenv
Description copied from interface:KiwiEnvironmentGets the value of the specified environment variable.- Specified by:
getenvin interfaceKiwiEnvironment- Parameters:
name- the name of the environment variable- Returns:
- the string value of the variable, or
nullif the variable is not defined in this environment - See Also:
-
getenv
Description copied from interface:KiwiEnvironmentReturns an unmodifiable string map view of the environment.- Specified by:
getenvin interfaceKiwiEnvironment- Returns:
- the environment as a map of variable names to values
- See Also:
-
getProperties
Description copied from interface:KiwiEnvironmentReturns the current system properties.- Specified by:
getPropertiesin interfaceKiwiEnvironment- Returns:
- the system properties
- See Also:
-
getProperty
Description copied from interface:KiwiEnvironmentGets the system property indicated by the specified key.- Specified by:
getPropertyin interfaceKiwiEnvironment- Parameters:
key- the name of the system property- Returns:
- the string value of the system property
- See Also:
-
getProperty
Description copied from interface:KiwiEnvironmentGets the system property indicated by the specified key.- Specified by:
getPropertyin interfaceKiwiEnvironment- Parameters:
key- the name of the system propertydefaultValue- a default value- Returns:
- the string value of the system property, or the given default value if there is no property with that key
- See Also:
-