Interface KiwiEnvironment

All Known Implementing Classes:
DefaultEnvironment

public interface KiwiEnvironment
Interface 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 Details

    • currentDate

      Date currentDate()
      Returns the current date.
      Returns:
      the current date as a Date object
      See Also:
    • currentTime

      Time currentTime()
      Returns the current time. For use with the JDBC API.
      Returns:
      the current time as a Time object
      See Also:
    • currentTimestamp

      Timestamp currentTimestamp()
      Returns the current timestamp. For use with the JDBC API.
      Returns:
      the current timestamp as a Timestamp object
      See Also:
    • currentInstant

      Instant currentInstant()
      Returns the current Instant in the default time zone.
      Returns:
      the current instant
      See Also:
    • currentInstant

      Instant currentInstant(ZoneId zone)
      Returns the current Instant in the specified time zone.
      Parameters:
      zone - the zone ID to use, not null
      Returns:
      the current instant
      See Also:
    • currentLocalDate

      LocalDate currentLocalDate()
      Returns the current LocalDate in the default time zone.
      Returns:
      the current local date
      See Also:
    • currentLocalDate

      LocalDate currentLocalDate(ZoneId zone)
      Returns the current LocalDate in the specified time zone.
      Parameters:
      zone - the zone ID to use, not null
      Returns:
      the current local date
      See Also:
    • currentLocalTime

      LocalTime currentLocalTime()
      Returns the current LocalTime in the default time zone.
      Returns:
      the current local time
      See Also:
    • currentLocalTime

      LocalTime currentLocalTime(ZoneId zone)
      Returns the current LocalTime in the specified time zone.
      Parameters:
      zone - the zone ID to use, not null
      Returns:
      the current local time
      See Also:
    • currentLocalDateTime

      LocalDateTime currentLocalDateTime()
      Returns the current LocalDateTime in the default time zone.
      Returns:
      the current local date/time
      See Also:
    • currentLocalDateTime

      LocalDateTime currentLocalDateTime(ZoneId zone)
      Returns the current LocalDateTime in the specified time zone.
      Parameters:
      zone - the zone ID to use, not null
      Returns:
      the current local date/time
      See Also:
    • currentZonedDateTimeUTC

      ZonedDateTime currentZonedDateTimeUTC()
      Returns the current ZonedDateTime with the ZoneOffset as ZoneOffset.UTC.
      Returns:
      the current date/time in UTC
      See Also:
    • currentZonedDateTime

      ZonedDateTime currentZonedDateTime(ZoneId zone)
      Returns the current ZonedDateTime in the specified time zone.
      Parameters:
      zone - the zone ID to use, not null
      Returns:
      a ZonedDateTime representing the current date/time in the specified zone
      See Also:
    • currentZonedDateTime

      ZonedDateTime currentZonedDateTime()
      Returns the current ZonedDateTime in the default time zone.
      Returns:
      a ZonedDateTime representing the current date/time in the default time zone
      See Also:
    • currentTimeMillis

      long currentTimeMillis()
      Returns the current time in milliseconds since the epoch.
      Returns:
      the current time in milliseconds
      See Also:
    • 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:
    • currentPid

      long currentPid()
      Returns the process ID of the currently executing JVM. This method does not perform any error checking. Use tryGetCurrentPid() for a version that returns an empty optional if it is unable to obtain the pid for any reason.
      Returns:
      the pid of the current process
      See Also:
    • tryGetCurrentPid

      OptionalLong tryGetCurrentPid()
      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:
      an optional containing the pid of the current process, or empty if any problem occurred
      See Also:
    • currentProcessHandle

      ProcessHandle currentProcessHandle()
      Returns a ProcessHandle for the current process.
      Returns:
      a handle to the current process
      See Also:
    • currentThread

      Thread currentThread()
      Returns a reference to the currently executing thread object.
      Returns:
      the currently executing thread
      See Also:
    • processHandleOfPid

      Optional<ProcessHandle> processHandleOfPid(long pid)
      Tries to obtain a ProcessHandle for a process with the given ID. If the process does not exist, then an empty Optional is returned.
      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:
      Implementation Note:
      Implementations may throw the same exceptions as ProcessHandle.of(long), but are not required to do so.
    • sleep

      void sleep(long milliseconds) throws InterruptedException
      Sleep for the given number of milliseconds.
      Parameters:
      milliseconds - the number of milliseconds to sleep
      Throws:
      InterruptedException - if interrupted
      See Also:
    • sleep

      void sleep(long timeout, TimeUnit unit) throws InterruptedException
      Sleep for a specific amount of time given by the specified timeout and TimeUnit.
      Parameters:
      timeout - the value to sleep
      unit - the unit to sleep, e.g. TimeUnit.SECONDS
      Throws:
      InterruptedException - if interrupted
      See Also:
    • sleep

      void sleep(long millis, int nanos) throws InterruptedException
      Sleep for the specified number of milliseconds plus nanoseconds.
      Parameters:
      millis - the number of milliseconds to sleep
      nanos - 0-999999 additional nanoseconds to sleep
      Throws:
      InterruptedException - if interrupted
      See Also:
    • sleepQuietly

      boolean sleepQuietly(long milliseconds)
      Sleep for the given number of milliseconds. Will never throw an InterruptedException.
      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

      boolean sleepQuietly(long timeout, TimeUnit unit)
      Sleep for a specific amount of time given by the specified timeout and TimeUnit. Will never throw an InterruptedException.
      Parameters:
      timeout - the value to sleep
      unit - 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

      boolean sleepQuietly(long millis, int nanos)
      Sleep for the given number of milliseconds plus nanoseconds. Will never throw an InterruptedException.
      Parameters:
      millis - the number of milliseconds to sleep
      nanos - 0-999999 additional nanoseconds to sleep
      Returns:
      false if the sleep was not interrupted, and true if it was interrupted
      See Also:
    • 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 null if the variable is not defined in this environment
      See Also:
    • 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:
    • getProperties

      Properties getProperties()
      Returns the current system properties.
      Returns:
      the system properties
      See Also:
    • 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:
    • getProperty

      String getProperty(String key, String defaultValue)
      Gets the system property indicated by the specified key.
      Parameters:
      key - the name of the system property
      defaultValue - 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: