Class DefaultEnvironment
- java.lang.Object
-
- org.kiwiproject.base.DefaultEnvironment
-
- All Implemented Interfaces:
KiwiEnvironment
public class DefaultEnvironment extends Object implements KiwiEnvironment
A default implementation of theKiwiEnvironmentinterface. Normal usage is to define a privateKiwiEnvironmentand 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 fromKiwiEnvironmentinstead, 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
KiwiEnvironmentspecifically 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 Constructor Description DefaultEnvironment()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description DatecurrentDate()Returns the current date.InstantcurrentInstant()Returns the currentInstantin the default time zone.InstantcurrentInstant(ZoneId zone)Returns the currentInstantin the specified time zone.LocalDatecurrentLocalDate()Returns the currentLocalDatein the default time zone.LocalDatecurrentLocalDate(ZoneId zone)Returns the currentLocalDatein the specified time zone.LocalDateTimecurrentLocalDateTime()Returns the currentLocalDateTimein the default time zone.LocalDateTimecurrentLocalDateTime(ZoneId zone)Returns the currentLocalDateTimein the specified time zone.LocalTimecurrentLocalTime()Returns the currentLocalTimein the default time zone.LocalTimecurrentLocalTime(ZoneId zone)Returns the currentLocalTimein the specified time zone.StringcurrentProcessId()Returns the process ID of the currently executing JVM.TimecurrentTime()Returns the current time.longcurrentTimeMillis()Returns the current time in milliseconds since the epoch.TimestampcurrentTimestamp()Returns the current timestamp.ZonedDateTimecurrentZonedDateTime()Returns the currentZonedDateTimein the default time zone.ZonedDateTimecurrentZonedDateTime(ZoneId zone)Returns the currentZonedDateTimein the specified time zone.ZonedDateTimecurrentZonedDateTimeUTC()Map<String,String>getenv()Returns an unmodifiable string map view of the environment.Stringgetenv(String name)Gets the value of the specified environment variable.PropertiesgetProperties()Returns the current system properties.StringgetProperty(String key)Gets the system property indicated by the specified key.StringgetProperty(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.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(long timeout, TimeUnit unit)Sleep 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.Optional<Integer>tryGetCurrentProcessId()Tries to obtain the process ID of the currently executing JVM.
-
-
-
Method Detail
-
currentDate
public Date currentDate()
Description copied from interface:KiwiEnvironmentReturns the current date.- Specified by:
currentDatein interfaceKiwiEnvironment- Returns:
- the current date as a
Dateobject - See Also:
Date()
-
currentTime
public Time 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:
Time
-
currentTimestamp
public Timestamp 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:
Timestamp
-
currentInstant
public Instant currentInstant()
Description copied from interface:KiwiEnvironmentReturns the currentInstantin the default time zone.- Specified by:
currentInstantin interfaceKiwiEnvironment- Returns:
- the current instant
- See Also:
Instant.now()
-
currentInstant
public Instant currentInstant(ZoneId zone)
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:
Instant.now(Clock)
-
currentLocalDate
public LocalDate currentLocalDate()
Description copied from interface:KiwiEnvironmentReturns the currentLocalDatein the default time zone.- Specified by:
currentLocalDatein interfaceKiwiEnvironment- Returns:
- the current local date
- See Also:
LocalDate
-
currentLocalDate
public LocalDate currentLocalDate(ZoneId zone)
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:
LocalDate.now(ZoneId)
-
currentLocalTime
public LocalTime currentLocalTime()
Description copied from interface:KiwiEnvironmentReturns the currentLocalTimein the default time zone.- Specified by:
currentLocalTimein interfaceKiwiEnvironment- Returns:
- the current local time
- See Also:
LocalTime.now()
-
currentLocalTime
public LocalTime currentLocalTime(ZoneId zone)
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:
LocalTime.now(ZoneId)
-
currentLocalDateTime
public LocalDateTime currentLocalDateTime()
Description copied from interface:KiwiEnvironmentReturns the currentLocalDateTimein the default time zone.- Specified by:
currentLocalDateTimein interfaceKiwiEnvironment- Returns:
- the current local date/time
- See Also:
LocalDateTime.now()
-
currentLocalDateTime
public LocalDateTime currentLocalDateTime(ZoneId zone)
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:
LocalDateTime.now(ZoneId)
-
currentZonedDateTimeUTC
public ZonedDateTime currentZonedDateTimeUTC()
Description copied from interface:KiwiEnvironment- Specified by:
currentZonedDateTimeUTCin interfaceKiwiEnvironment- Returns:
- the current date/time in UTC
- See Also:
ZonedDateTime.now(ZoneId)
-
currentZonedDateTime
public ZonedDateTime currentZonedDateTime(ZoneId zone)
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:
ZonedDateTime.now(ZoneId)
-
currentZonedDateTime
public ZonedDateTime 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:
ZonedDateTime.now()
-
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:
System.currentTimeMillis()
-
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:
System.nanoTime()
-
currentProcessId
public String currentProcessId()
Returns the process ID of the currently executing JVM. This method does not perform any error checking. UseKiwiEnvironment.tryGetCurrentProcessId()for a safer version, which returns the result as an integer (wrapped in an optional).This default implementation uses the JMX
ManagementFactory.getRuntimeMXBean()to get the process information in the formpid@hostname. It then simply splits the string and returns thepid.- Specified by:
currentProcessIdin interfaceKiwiEnvironment- Returns:
- process ID
-
tryGetCurrentProcessId
public Optional<Integer> tryGetCurrentProcessId()
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:
tryGetCurrentProcessIdin interfaceKiwiEnvironment- Returns:
- am optional containing the process ID, or empty if any problem occurred
-
sleep
public void sleep(long milliseconds) throws InterruptedExceptionDescription 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:
Thread.sleep(long)
-
sleep
public void sleep(long timeout, TimeUnit unit) throws InterruptedExceptionDescription 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:
TimeUnit.sleep(long)
-
sleep
public void sleep(long millis, int nanos) throws InterruptedExceptionDescription 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:
Thread.sleep(long, int)
-
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:
Thread.sleep(long)
-
sleepQuietly
public boolean sleepQuietly(long timeout, TimeUnit unit)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:
TimeUnit.sleep(long)
-
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:
Thread.sleep(long, int)
-
getenv
public String getenv(String name)
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:
System.getenv(String)
-
getenv
public Map<String,String> 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:
System.getenv()
-
getProperties
public Properties getProperties()
Description copied from interface:KiwiEnvironmentReturns the current system properties.- Specified by:
getPropertiesin interfaceKiwiEnvironment- Returns:
- the system properties
- See Also:
System.getProperties()
-
getProperty
public String getProperty(String key)
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:
System.getProperty(String)
-
getProperty
public String getProperty(String key, String defaultValue)
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:
System.getProperty(String, String)
-
-