Package org.kiwiproject.base
Interface KiwiEnvironment
-
- All Known Implementing Classes:
DefaultEnvironment
public interface KiwiEnvironmentInterface that defines methods related to the external environment, for example getting the current time in milliseconds, obtaining the process ID, and sleeping quietly for a specified time.The main advantage of this over simply using things like
System.currentTimeMillis()is to allow for easier testing. Since this is an interface, it can easily be replaced with a mock object in unit tests that deal with time-based or external environment-based code.
-
-
Method Summary
All Methods Instance Methods Abstract 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
Date currentDate()
Returns the current date.
-
currentTime
Time currentTime()
Returns the current time. For use with the JDBC API.
-
currentTimestamp
Timestamp currentTimestamp()
Returns the current timestamp. For use with the JDBC API.
-
currentInstant
Instant currentInstant()
Returns the currentInstantin the default time zone.- Returns:
- the current instant
- See Also:
Instant.now()
-
currentInstant
Instant currentInstant(ZoneId zone)
Returns the currentInstantin the specified time zone.- Parameters:
zone- the zone ID to use, not null- Returns:
- the current instant
- See Also:
Instant.now(Clock)
-
currentLocalDate
LocalDate currentLocalDate()
Returns the currentLocalDatein the default time zone.- Returns:
- the current local date
- See Also:
LocalDate
-
currentLocalDate
LocalDate currentLocalDate(ZoneId zone)
Returns the currentLocalDatein the specified time zone.- Parameters:
zone- the zone ID to use, not null- Returns:
- the current local date
- See Also:
LocalDate.now(ZoneId)
-
currentLocalTime
LocalTime currentLocalTime()
Returns the currentLocalTimein the default time zone.- Returns:
- the current local time
- See Also:
LocalTime.now()
-
currentLocalTime
LocalTime currentLocalTime(ZoneId zone)
Returns the currentLocalTimein the specified time zone.- Parameters:
zone- the zone ID to use, not null- Returns:
- the current local time
- See Also:
LocalTime.now(ZoneId)
-
currentLocalDateTime
LocalDateTime currentLocalDateTime()
Returns the currentLocalDateTimein the default time zone.- Returns:
- the current local date/time
- See Also:
LocalDateTime.now()
-
currentLocalDateTime
LocalDateTime currentLocalDateTime(ZoneId zone)
Returns the currentLocalDateTimein the specified time zone.- Parameters:
zone- the zone ID to use, not null- Returns:
- the current local date/time
- See Also:
LocalDateTime.now(ZoneId)
-
currentZonedDateTimeUTC
ZonedDateTime currentZonedDateTimeUTC()
- Returns:
- the current date/time in UTC
- See Also:
ZonedDateTime.now(ZoneId)
-
currentZonedDateTime
ZonedDateTime currentZonedDateTime(ZoneId zone)
Returns the currentZonedDateTimein the specified time zone.- 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
ZonedDateTime currentZonedDateTime()
Returns the currentZonedDateTimein the default time zone.- Returns:
- a
ZonedDateTimerepresenting the current date/time in the default time zone - See Also:
ZonedDateTime.now()
-
currentTimeMillis
long currentTimeMillis()
Returns the current time in milliseconds since the epoch.- Returns:
- the current time in milliseconds
- See Also:
System.currentTimeMillis()
-
nanoTime
long nanoTime()
Returns the current value of the running JVM's time source, in nanoseconds.Only used to measure elapsed time, per
System.nanoTime().- Returns:
- the current time in nanoseconds
- See Also:
System.nanoTime()
-
currentProcessId
String currentProcessId()
Returns the process ID of the currently executing JVM. This method does not perform any error checking. UsetryGetCurrentProcessId()for a safer version, which returns the result as an integer (wrapped in an optional).- Returns:
- process ID
- API Note:
- Originally defined to return a string due to the implementation using
ManagementFactory.getRuntimeMXBean()to get the process information in the formpid@hostname
-
tryGetCurrentProcessId
Optional<Integer> tryGetCurrentProcessId()
Tries to obtain the process ID of the currently executing JVM. If any problem occurs, it is caught and an empty optional is returned.- Returns:
- am optional containing the process ID, or empty if any problem occurred
-
sleep
void sleep(long milliseconds) throws InterruptedExceptionSleep for the given number of milliseconds.- Parameters:
milliseconds- the number of milliseconds to sleep- Throws:
InterruptedException- if interrupted- See Also:
Thread.sleep(long)
-
sleep
void sleep(long timeout, TimeUnit unit) throws InterruptedExceptionSleep for a specific amount of time given by the specifiedtimeoutandTimeUnit.- Parameters:
timeout- the value to sleepunit- the unit to sleep, e.g.TimeUnit.SECONDS- Throws:
InterruptedException- if interrupted- See Also:
TimeUnit.sleep(long)
-
sleep
void sleep(long millis, int nanos) throws InterruptedExceptionSleep for the specified number of milliseconds plus nanoseconds.- Parameters:
millis- the number of milliseconds to sleepnanos-0-999999additional nanoseconds to sleep- Throws:
InterruptedException- if interrupted- See Also:
Thread.sleep(long, int)
-
sleepQuietly
boolean sleepQuietly(long milliseconds)
Sleep for the given number of milliseconds. Will never throw anInterruptedException.- 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
boolean sleepQuietly(long timeout, TimeUnit unit)Sleep for a specific amount of time given by the specifiedtimeoutandTimeUnit. Will never throw anInterruptedException.- 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
boolean sleepQuietly(long millis, int nanos)Sleep for the given number of milliseconds plus nanoseconds. Will never throw anInterruptedException.- 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
String getenv(String name)
Gets the value of the specified environment variable.- 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
Map<String,String> getenv()
Returns an unmodifiable string map view of the environment.- Returns:
- the environment as a map of variable names to values
- See Also:
System.getenv()
-
getProperties
Properties getProperties()
Returns the current system properties.- Returns:
- the system properties
- See Also:
System.getProperties()
-
getProperty
String getProperty(String key)
Gets the system property indicated by the specified key.- Parameters:
key- the name of the system property- Returns:
- the string value of the system property
- See Also:
System.getProperty(String)
-
getProperty
String getProperty(String key, String defaultValue)
Gets the system property indicated by the specified key.- 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)
-
-