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 Detail

      • currentDate

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

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

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

        Instant currentInstant()
        Returns the current Instant in the default time zone.
        Returns:
        the current instant
        See Also:
        Instant.now()
      • 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:
        Instant.now(Clock)
      • currentLocalDate

        LocalDate currentLocalDate()
        Returns the current LocalDate in the default time zone.
        Returns:
        the current local date
        See Also:
        LocalDate
      • 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:
        LocalDate.now(ZoneId)
      • 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:
        LocalTime.now(ZoneId)
      • 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. Use tryGetCurrentProcessId() 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 form pid@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 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:
        Thread.sleep(long, int)
      • 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:
        Thread.sleep(long)
      • 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:
        TimeUnit.sleep(long)
      • 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:
        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 null if 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()
      • 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 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:
        System.getProperty(String, String)