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()
      • 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:
        ProcessHandle.current(), ProcessHandle.pid()
      • 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:
        ProcessHandle.current(), ProcessHandle.pid()
      • tryGetCurrentProcessId

        @Deprecated(since="1.1.0",
                    forRemoval=true)
        @KiwiDeprecated(since="1.1.0",
                        removeAt="2.0.0",
                        replacedBy="tryGetCurrentPid()",
                        reference="https://github.com/kiwiproject/kiwi/issues/642")
        Optional<Integer> tryGetCurrentProcessId()
        Deprecated, for removal: This API element is subject to removal in a future version.
        replaced by 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:
        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)