Class Timeout

java.lang.Object
com.peterphi.std.threading.Timeout
All Implemented Interfaces:
Serializable, Comparable<Timeout>

public final class Timeout extends Object implements Comparable<Timeout>, Serializable
Represents an immutable Timeout (ie. a duration). Timeouts are distinct from Deadlines in that they do not have a start time.
This class can be used to represent a duration without needing a dependency on Joda time
See Also:
  • Field Details

    • ZERO

      public static final Timeout ZERO
      A zero-length timeout
    • ONE_QUARTER_SECOND

      public static final Timeout ONE_QUARTER_SECOND
      0.25 seconds (250 milliseconds)
    • ONE_SECOND

      public static final Timeout ONE_SECOND
      One second
    • TEN_SECONDS

      public static final Timeout TEN_SECONDS
      Ten seconds
    • THIRTY_SECONDS

      public static final Timeout THIRTY_SECONDS
      Thirty seconds
    • ONE_MINUTE

      public static final Timeout ONE_MINUTE
      One minute
    • FIVE_MINUTES

      public static final Timeout FIVE_MINUTES
      Five minutes
    • THIRTY_MINUTES

      public static final Timeout THIRTY_MINUTES
      30 minutes
    • MAX_VALUE

      public static final Timeout MAX_VALUE
      The maximum possible timeout
  • Constructor Details

    • Timeout

      public Timeout(org.joda.time.Duration duration)
    • Timeout

      public Timeout(long milliseconds)
      Constructs a deadline using a given period of time
      Parameters:
      milliseconds - period in milliseconds
    • Timeout

      public Timeout(long period, TimeUnit unit)
      Constructs a deadline using a given period of time
      Parameters:
      period - the number of units
      unit - the unit of time being used
  • Method Details

    • start

      public Deadline start()
      Create a new deadline which is the given period of time in the future
      Returns:
      a deadline the given period of time in the future
    • start

      public Deadline start(Deadline max)
      Create a new deadline within a given maximum timeframe; this prevents
      Parameters:
      max - the maximum deadline
      Returns:
      a new deadline which is at most the provided maximum
    • getMilliseconds

      public long getMilliseconds()
      Get the number of milliseconds this timeout represents
      Returns:
    • get

      public long get(TimeUnit toUnit)
      Get this timeout represented in a different unit
      Parameters:
      toUnit -
      Returns:
    • hashCode

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

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • multiply

      public Timeout multiply(long by)
      Create a new Timeout which is equal to this timeout multiplied by some value
      Parameters:
      by - the amount to multiply the current timeout by
      Returns:
      a new Timeout which is equal to this timeout multiplied by some value
    • multiply

      public Timeout multiply(double by)
      Create a new Timeout which is equal to this timeout multiplied by some value
      The resulting Timeout will be in milliseconds to maximise precision.
      Parameters:
      by - the amount to multiply the current timeout by
      Returns:
      a new Timeout which is equal to this timeout multiplied by some value (with the unit now set to Milliseconds)
    • toString

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

      public String toEnglishString()
    • compareTo

      public int compareTo(Timeout that)
      Specified by:
      compareTo in interface Comparable<Timeout>
    • sleep

      public void sleep()
      Sleep for the duration of this timeout (or until the thread is interrupted)
    • sleep

      public void sleep(Deadline deadline)
      Sleep for the duration of this timeout (or until the thread is interrupted, or until the provided deadline expires)
    • toDuration

      public org.joda.time.Duration toDuration()
    • max

      public static Timeout max(Timeout... timeouts)
      Filter through a number of timeouts to find the one with the longest period
      Parameters:
      timeouts - a list of timeouts (nulls are ignored)
      Returns:
      the timeout with the longest period
      Throws:
      IllegalArgumentException - if no non-null timeouts are presented
    • min

      public static Timeout min(Timeout... timeouts)
      Filter through a number of timeouts to find the one with the shortest period
      Parameters:
      timeouts - a list of timeouts (nulls are ignored)
      Returns:
      the timeout with the shortest period
      Throws:
      IllegalArgumentException - if no non-null timeouts are presented
    • sum

      public static Timeout sum(Timeout... timeouts)
      Adds together all the supplied timeouts
      Parameters:
      timeouts - a list of timeouts (nulls are ignored)
      Returns:
      the sum of all the timeouts specified