Enum Time

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<Time>

    public enum Time
    extends java.lang.Enum<Time>
    Utility methods for time travelling and freezing time. This is useful when implementing tests which are dependant upon the passage of time.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static interface  Time.Awaitable  
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
      SINGLETON  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void advance​(long time, java.util.concurrent.TimeUnit unit)  
      static boolean await​(Time.Awaitable obj, long timeout, java.util.concurrent.TimeUnit unit)  
      static boolean await​(java.util.concurrent.CountDownLatch latch, long timeout, java.util.concurrent.TimeUnit unit)  
      static boolean await​(java.util.concurrent.locks.Condition latch, long timeout, java.util.concurrent.TimeUnit unit)  
      static long currentTimeMillis()  
      static void freeze()  
      static void freeze​(long frozenTimeMillis, long frozenNano)  
      static long nanoTime()  
      static void restore()  
      static void sleep​(long milliseconds)  
      static void sleep​(long milliseconds, int nanos)  
      static boolean sleepInterruptable​(long milliseconds)  
      static void sleepUninterruptably​(long milliseconds)  
      static java.time.Clock system​(java.time.ZoneId zone)
      Obtains a clock that returns the current instant using best available system clock.
      static java.time.Clock systemDefaultZone()
      Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.
      static java.time.Clock systemUTC()
      Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the UTC time-zone.
      static Time valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static Time[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Enum Constant Detail

      • SINGLETON

        public static final Time SINGLETON
    • Method Detail

      • values

        public static Time[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (Time c : Time.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static Time valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • currentTimeMillis

        public static long currentTimeMillis()
      • nanoTime

        public static long nanoTime()
      • restore

        public static void restore()
      • freeze

        public static void freeze()
      • freeze

        public static void freeze​(long frozenTimeMillis,
                                  long frozenNano)
      • advance

        public static void advance​(long time,
                                   java.util.concurrent.TimeUnit unit)
      • systemUTC

        public static java.time.Clock systemUTC()
        Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the UTC time-zone.

        This clock, rather than systemDefaultZone(), should be used when you need the current instant without the date or time.

        This clock is based on the best available system clock. This may use System.currentTimeMillis(), or a higher resolution clock if one is available.

        Conversion from instant to date or time uses the UTC time-zone.

        The returned implementation is immutable, thread-safe and Serializable. It is equivalent to system(ZoneOffset.UTC).

        Returns:
        a clock that uses the best available system clock in the UTC zone, not null
      • systemDefaultZone

        public static java.time.Clock systemDefaultZone()
        Obtains a clock that returns the current instant using the best available system clock, converting to date and time using the default time-zone.

        This clock is based on the best available system clock. This may use System.currentTimeMillis(), or a higher resolution clock if one is available.

        Using this method hard codes a dependency to the default time-zone into your application. It is recommended to avoid this and use a specific time-zone whenever possible. The UTC clock should be used when you need the current instant without the date or time.

        The returned implementation is immutable, thread-safe and Serializable. It is equivalent to system(ZoneId.systemDefault()).

        Returns:
        a clock that uses the best available system clock in the default zone, not null
        See Also:
        ZoneId.systemDefault()
      • system

        public static java.time.Clock system​(java.time.ZoneId zone)
        Obtains a clock that returns the current instant using best available system clock.

        This clock is based on the best available system clock. This may use System.currentTimeMillis(), or a higher resolution clock if one is available.

        Conversion from instant to date or time uses the specified time-zone.

        The returned implementation is immutable, thread-safe and Serializable.

        Parameters:
        zone - the time-zone to use to convert the instant to date-time, not null
        Returns:
        a clock that uses the best available system clock in the specified zone, not null
      • sleep

        public static void sleep​(long milliseconds)
                          throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • sleep

        public static void sleep​(long milliseconds,
                                 int nanos)
                          throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • sleepUninterruptably

        public static void sleepUninterruptably​(long milliseconds)
      • sleepInterruptable

        public static boolean sleepInterruptable​(long milliseconds)
      • await

        public static boolean await​(java.util.concurrent.CountDownLatch latch,
                                    long timeout,
                                    java.util.concurrent.TimeUnit unit)
                             throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • await

        public static boolean await​(java.util.concurrent.locks.Condition latch,
                                    long timeout,
                                    java.util.concurrent.TimeUnit unit)
                             throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • await

        public static boolean await​(Time.Awaitable obj,
                                    long timeout,
                                    java.util.concurrent.TimeUnit unit)
                             throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException