Class TimeValue

java.lang.Object
org.apache.hc.core5.util.TimeValue
All Implemented Interfaces:
Comparable<TimeValue>
Direct Known Subclasses:
Timeout

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

    • INT_UNDEFINED

      static final int INT_UNDEFINED
      See Also:
    • 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 TimeUnit timeUnit
  • Constructor Details

    • TimeValue

      TimeValue(long duration, TimeUnit timeUnit)
  • Method Details

    • 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, 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(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 ChronoUnit toChronoUnit(TimeUnit timeUnit)
      Converts a TimeUnit to the equivalent ChronoUnit.
      Returns:
      the converted equivalent ChronoUnit
    • parse

      public static TimeValue parse(String value) throws 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:
      ParseException - if the number cannot be parsed
    • convert

      public long convert(TimeUnit targetTimeUnit)
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class 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:
      ArithmeticException - if divisor is zero.
    • divide

      public TimeValue divide(long divisor, 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:
      ArithmeticException - if divisor is zero.
    • getDuration

      public long getDuration()
    • getTimeUnit

      public TimeUnit getTimeUnit()
    • hashCode

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

      public TimeValue min(TimeValue other)
    • min

      private TimeUnit min(TimeUnit other)
    • scale

      private int scale()
    • scale

      private int scale(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 InterruptedException
      Throws:
      InterruptedException
    • timedJoin

      public void timedJoin(Thread thread) throws InterruptedException
      Throws:
      InterruptedException
    • timedWait

      public void timedWait(Object obj) throws InterruptedException
      Throws:
      InterruptedException
    • toDays

      public long toDays()
    • toDuration

      public 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 Comparable<TimeValue>
    • toString

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

      public Timeout toTimeout()