Class TimeValue

  • All Implemented Interfaces:
    java.lang.Comparable<TimeValue>
    Direct Known Subclasses:
    Timeout

    @Contract(threading=IMMUTABLE)
    public class TimeValue
    extends java.lang.Object
    implements java.lang.Comparable<TimeValue>
    Represents a time value as a long time and a TimeUnit.
    Since:
    5.0
    • Field Detail

      • MAX_VALUE

        public static final TimeValue MAX_VALUE
        A constant holding the maximum value a TimeValue can have: Long.MAX_VALUE days.
      • NEG_ONE_MILLISECOND

        public static final TimeValue NEG_ONE_MILLISECOND
        A negative one millisecond TimeValue.
      • NEG_ONE_SECOND

        public static final TimeValue NEG_ONE_SECOND
        A negative one second TimeValue.
      • ZERO_MILLISECONDS

        public static final TimeValue ZERO_MILLISECONDS
        A zero milliseconds TimeValue.
      • duration

        private final long duration
      • timeUnit

        private final java.util.concurrent.TimeUnit timeUnit
    • Constructor Detail

      • TimeValue

        TimeValue​(long duration,
                  java.util.concurrent.TimeUnit timeUnit)
    • Method Detail

      • asBoundInt

        public static int asBoundInt​(long value)
        Returns the given long value as an int where long values out of int range are returned as Integer.MIN_VALUE and Integer.MAX_VALUE.

        For example: TimeValue.asBoundInt(Long.MAX_VALUE) returns Integer.MAX_VALUE.

        Parameters:
        value - a long value to convert
        Returns:
        an int value bound within Integer.MIN_VALUE and Integer.MAX_VALUE.
      • defaultsTo

        public static <T extends TimeValue> T defaultsTo​(T timeValue,
                                                         T defaultValue)
        Returns the given timeValue if it is not null, if null then returns the given defaultValue.
        Parameters:
        timeValue - may be null
        defaultValue - may be null
        Returns:
        timeValue or defaultValue
      • defaultsToNegativeOneMillisecond

        public static TimeValue defaultsToNegativeOneMillisecond​(TimeValue timeValue)
        Returns the given timeValue if it is not null, if null then returns NEG_ONE_SECOND.
        Parameters:
        timeValue - may be null
        Returns:
        timeValue or NEG_ONE_SECOND
      • defaultsToNegativeOneSecond

        public static TimeValue defaultsToNegativeOneSecond​(TimeValue timeValue)
        Returns the given timeValue if it is not null, if null then returns NEG_ONE_SECOND.
        Parameters:
        timeValue - may be null
        Returns:
        timeValue or NEG_ONE_SECOND
      • defaultsToZeroMilliseconds

        public static TimeValue defaultsToZeroMilliseconds​(TimeValue timeValue)
        Returns the given timeValue if it is not null, if null then returns ZERO_MILLISECONDS.
        Parameters:
        timeValue - may be null
        Returns:
        timeValue or ZERO_MILLISECONDS
      • isNonNegative

        public static boolean isNonNegative​(TimeValue timeValue)
      • isPositive

        public static boolean isPositive​(TimeValue timeValue)
      • of

        public static TimeValue of​(long duration,
                                   java.util.concurrent.TimeUnit timeUnit)
        Creates a TimeValue.
        Parameters:
        duration - the time duration in the given timeUnit.
        timeUnit - the time unit for the given duration.
        Returns:
        a Timeout.
      • of

        public static TimeValue of​(java.time.Duration duration)
        Creates a TimeValue from a Duration.
        Parameters:
        duration - the time duration.
        Returns:
        a Timeout
        Since:
        5.2
      • ofDays

        public static TimeValue ofDays​(long days)
      • ofHours

        public static TimeValue ofHours​(long hours)
      • ofMicroseconds

        public static TimeValue ofMicroseconds​(long microseconds)
      • ofMilliseconds

        public static TimeValue ofMilliseconds​(long millis)
      • ofMinutes

        public static TimeValue ofMinutes​(long minutes)
      • ofNanoseconds

        public static TimeValue ofNanoseconds​(long nanoseconds)
      • ofSeconds

        public static TimeValue ofSeconds​(long seconds)
      • toChronoUnit

        static java.time.temporal.ChronoUnit toChronoUnit​(java.util.concurrent.TimeUnit timeUnit)
        Converts a TimeUnit to the equivalent ChronoUnit.
        Returns:
        the converted equivalent ChronoUnit
      • parse

        public static TimeValue parse​(java.lang.String value)
                               throws java.text.ParseException
        Parses a TimeValue in the format <Long><SPACE><TimeUnit>, for example "1200 MILLISECONDS".

        Parses:

        • "1200 MILLISECONDS".
        • " 1200 MILLISECONDS ", spaces are ignored.
        • "1 MINUTE", singular units.
        Parameters:
        value - the TimeValue to parse
        Returns:
        a new TimeValue
        Throws:
        java.text.ParseException - if the number cannot be parsed
      • convert

        public long convert​(java.util.concurrent.TimeUnit targetTimeUnit)
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • divide

        public TimeValue divide​(long divisor)
        Returns a TimeValue whose value is (this / divisor).
        Parameters:
        divisor - value by which this TimeValue is to be divided.
        Returns:
        this / divisor
        Throws:
        java.lang.ArithmeticException - if divisor is zero.
      • divide

        public TimeValue divide​(long divisor,
                                java.util.concurrent.TimeUnit targetTimeUnit)
        Returns a TimeValue whose value is (this / divisor).
        Parameters:
        divisor - value by which this TimeValue is to be divided.
        targetTimeUnit - the target TimeUnit
        Returns:
        this / divisor
        Throws:
        java.lang.ArithmeticException - if divisor is zero.
      • getDuration

        public long getDuration()
      • getTimeUnit

        public java.util.concurrent.TimeUnit getTimeUnit()
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • min

        private java.util.concurrent.TimeUnit min​(java.util.concurrent.TimeUnit other)
      • scale

        private int scale()
      • scale

        private int scale​(java.util.concurrent.TimeUnit tUnit)
        Returns a made up scale for TimeUnits.
        Parameters:
        tUnit - a TimeUnit
        Returns:
        a number from 1 to 7, where 1 is NANOSECONDS and 7 DAYS.
      • sleep

        public void sleep()
                   throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • timedJoin

        public void timedJoin​(java.lang.Thread thread)
                       throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • timedWait

        public void timedWait​(java.lang.Object obj)
                       throws java.lang.InterruptedException
        Throws:
        java.lang.InterruptedException
      • toDays

        public long toDays()
      • toDuration

        public java.time.Duration toDuration()
        Converts this instance of to a Duration.
        Returns:
        a Duration.
        Since:
        5.2
      • toHours

        public long toHours()
      • toMicroseconds

        public long toMicroseconds()
      • toMilliseconds

        public long toMilliseconds()
      • toMillisecondsIntBound

        public int toMillisecondsIntBound()
      • toMinutes

        public long toMinutes()
      • toNanoseconds

        public long toNanoseconds()
      • toSeconds

        public long toSeconds()
      • toSecondsIntBound

        public int toSecondsIntBound()
      • compareTo

        public int compareTo​(TimeValue other)
        Specified by:
        compareTo in interface java.lang.Comparable<TimeValue>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • toTimeout

        public Timeout toTimeout()