Class TimeSpan

java.lang.Object
org.sosy_lab.common.time.TimeSpan
All Implemented Interfaces:
Serializable, Comparable<TimeSpan>, TemporalAmount

@Immutable public final class TimeSpan extends Object implements Comparable<TimeSpan>, Serializable, TemporalAmount
This is an immutable representation of some time span, using a TimeUnit and a value.

The value may be positive or negative. All operations check for overflows and underflows, the behavior on overflow and underflow differs and is documented for each method.

Two instances are considered equal if they represent the exact same time span regardless of their unit, for example, 60s and 1min are considered equal.

See Also:
  • Method Details

    • of

      public static TimeSpan of(long pSpan, TimeUnit pUnit)
    • ofSeconds

      public static TimeSpan ofSeconds(long pSeconds)
    • ofMillis

      public static TimeSpan ofMillis(long pMillis)
    • ofNanos

      public static TimeSpan ofNanos(long pNanos)
    • empty

      public static TimeSpan empty()
    • valueOf

      public static TimeSpan valueOf(String input)
      Converts the given String into a TimeSpan object. Supported units are day, hour, minute and second.
      Parameters:
      input - the String to convert
      Returns:
      a TimeSpan represented by the given String
      Throws:
      IllegalArgumentException - if the input is not a valid string representation of a TimeSpan.
    • getChecked

      public long getChecked(TimeUnit dest)
      Get the value of this TimeSpan represented in the given unit. If the given unit is larger than the current unit, precision may be lost.
      Throws:
      ArithmeticException - If the value cannot be represented in the given unit due to overflow.
    • getSaturated

      public long getSaturated(TimeUnit dest)
      Get the value of this TimeSpan represented in the given unit. If the given unit is larger than the current unit, precision may be lost. If the value cannot be represented in the given unit due to overflow, Long.MAX_VALUE/Long.MIN_VALUE is returned.
    • toChecked

      public TimeSpan toChecked(TimeUnit dest)
      Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit. If the given unit is larger than the current unit, precision may be lost.
      Throws:
      ArithmeticException - If the value cannot be represented in the given unit
    • toSaturated

      public TimeSpan toSaturated(TimeUnit dest)
      Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit. If the given unit is larger than the current unit, precision may be lost. If the value cannot be represented in the given unit due to overflow, Long.MAX_VALUE/Long.MIN_VALUE is returned.
    • asSeconds

      public long asSeconds()
      Get the value of this TimeSpan as seconds. If the current unit is smaller than seconds, precision may be lost.
      Throws:
      ArithmeticException - If the value cannot be represented as seconds due to overflow.
    • asMillis

      public long asMillis()
      Get the value of this TimeSpan as milliseconds. If the current unit is smaller than milliseconds, precision may be lost.
      Throws:
      ArithmeticException - If the value cannot be represented as milliseconds due to overflow.
    • asNanos

      public long asNanos()
      Get the value of this TimeSpan as nanoseconds.
      Throws:
      ArithmeticException - If the value cannot be represented as nanoseconds due to overflow.
    • getUnit

      public TimeUnit getUnit()
    • asDuration

      public Duration asDuration()
      Return a Duration instance that represents the same amount of time (but it won't be possible to retrieve our unit as from getUnit()). Note that not all possible TimeSpan values can be represented by a Duration.
      Throws:
      ArithmeticException - If an overflow occurs.
    • formatAs

      public String formatAs(TimeUnit dest)
      Return a strings that represents (approximately) this time span, in the given unit if possible. If the given unit is larger than the current unit, precision may be lost. If the value cannot be represented in the given unit due to overflow, the result does not use the given unit, but the closest unit one that still allows to hold the exact value.
    • isEmpty

      public boolean isEmpty()
      Check whether this time span is empty, i.e., represents 0ns (or 0ms or 0s or ...).
    • equals

      public boolean equals(@Nullable Object obj)
      Overrides:
      equals in class Object
    • hashCode

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

      public int compareTo(TimeSpan other)
      Specified by:
      compareTo in interface Comparable<TimeSpan>
    • toString

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

      public static TimeSpan sum(TimeSpan a, TimeSpan b)
      Create a new time span that is the sum of two time spans. The unit of the returned time span is the more precise one if possible, otherwise the closest unit that still allows to hold both input values and the result. Note that this can loose precision when adding a very large and a very small value.
      Throws:
      ArithmeticException - If no unit is large enough to represent the result value.
    • sum

      public static TimeSpan sum(Iterable<TimeSpan> timeSpans)
      Create a new time span that is the sum of several time spans. The unit of the returned time span is the most precise one if possible, otherwise the closest unit that still allows to hold input values and the result. Note that this can loose precision when adding very large and very small values.
      Throws:
      ArithmeticException - If no unit is large enough to represent the result value.
    • sum

      public static TimeSpan sum(TimeSpan... t)
      Create a new time span that is the sum of several time spans. The unit of the returned time span is the most precise one.
    • difference

      public static TimeSpan difference(TimeSpan a, TimeSpan b)
      Create a new time span that is the difference of two time spans. The unit of the returned time span is the more precise one if possible, otherwise the closest unit that still allows to hold both input values and the result. Note that this can loose precision when subtracting a very large and a very small value.
    • multiply

      @CheckReturnValue public TimeSpan multiply(int factor)
      Create a new time span that is the current one multiplied by a non-negative integral factor. The unit of the returned time span is the same as the current one if possible, otherwise the closest unit that still allows to the result. Note that this can loose precision.
    • divide

      @CheckReturnValue public TimeSpan divide(int divisor)
      Create a new time span that is the current one divided by a non-negative integral value. The result of the division is rounded down (integer division). The unit of the returned time span is the same as the current one.
    • getUnits

      Deprecated.
      Only for implementing TemporalAmount. Use getUnit() instead.
      Specified by:
      getUnits in interface TemporalAmount
    • get

      @Deprecated public long get(TemporalUnit pUnit)
      Deprecated.
      Only for implementing TemporalAmount. It is not possible to pass arbitrary units here. Use getChecked(TimeUnit) instead.
      Specified by:
      get in interface TemporalAmount
    • addTo

      public Temporal addTo(Temporal pTemporal)
      Specified by:
      addTo in interface TemporalAmount
    • subtractFrom

      public Temporal subtractFrom(Temporal pTemporal)
      Specified by:
      subtractFrom in interface TemporalAmount