Package org.sosy_lab.common.time
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 Summary
Modifier and TypeMethodDescriptionlongasMillis()Get the value of this TimeSpan as milliseconds.longasNanos()Get the value of this TimeSpan as nanoseconds.longGet the value of this TimeSpan as seconds.intstatic TimeSpandifference(TimeSpan a, TimeSpan b) Create a new time span that is the difference of two time spans.divide(int divisor) Create a new time span that is the current one divided by a non-negative integral value.static TimeSpanempty()booleanReturn a strings that represents (approximately) this time span, in the given unit if possible.longget(TemporalUnit pUnit) Deprecated.longgetChecked(TimeUnit dest) Get the value of this TimeSpan represented in the given unit.longgetSaturated(TimeUnit dest) Get the value of this TimeSpan represented in the given unit.getUnit()getUnits()Deprecated.inthashCode()booleanisEmpty()Check whether this time span is empty, i.e., represents 0ns (or 0ms or 0s or ...).multiply(int factor) Create a new time span that is the current one multiplied by a non-negative integral factor.static TimeSpanstatic TimeSpanofMillis(long pMillis) static TimeSpanofNanos(long pNanos) static TimeSpanofSeconds(long pSeconds) subtractFrom(Temporal pTemporal) static TimeSpanCreate a new time span that is the sum of several time spans.static TimeSpanCreate a new time span that is the sum of several time spans.static TimeSpanCreate a new time span that is the sum of two time spans.Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit.toSaturated(TimeUnit dest) Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit.toString()static TimeSpan
-
Method Details
-
of
-
ofSeconds
-
ofMillis
-
ofNanos
-
empty
-
valueOf
- Parameters:
input- theStringto convert- Returns:
- a
TimeSpanrepresented by the givenString - Throws:
IllegalArgumentException- if the input is not a valid string representation of aTimeSpan.
-
getChecked
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
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
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
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
-
asDuration
Return aDurationinstance that represents the same amount of time (but it won't be possible to retrieve our unit as fromgetUnit()). Note that not all possibleTimeSpanvalues can be represented by aDuration.- Throws:
ArithmeticException- If an overflow occurs.
-
formatAs
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
-
hashCode
public int hashCode() -
compareTo
- Specified by:
compareToin interfaceComparable<TimeSpan>
-
toString
-
sum
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
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
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
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
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
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 implementingTemporalAmount. UsegetUnit()instead.- Specified by:
getUnitsin interfaceTemporalAmount
-
get
Deprecated.Only for implementingTemporalAmount. It is not possible to pass arbitrary units here. UsegetChecked(TimeUnit)instead.- Specified by:
getin interfaceTemporalAmount
-
addTo
- Specified by:
addToin interfaceTemporalAmount
-
subtractFrom
- Specified by:
subtractFromin interfaceTemporalAmount
-