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 aTimeUnit
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:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Temporal
addTo(Temporal pTemporal)
Duration
asDuration()
long
asMillis()
Get the value of this TimeSpan as milliseconds.long
asNanos()
Get the value of this TimeSpan as nanoseconds.long
asSeconds()
Get the value of this TimeSpan as seconds.int
compareTo(TimeSpan other)
static TimeSpan
difference(TimeSpan a, TimeSpan b)
Create a new time span that is the difference of two time spans.TimeSpan
divide(int divisor)
Create a new time span that is the current one divided by a non-negative integral value.static TimeSpan
empty()
boolean
equals(@Nullable Object obj)
String
formatAs(TimeUnit dest)
Return a strings that represents (approximately) this time span, in the given unit if possible.long
get(TemporalUnit pUnit)
Deprecated.long
getChecked(TimeUnit dest)
Get the value of this TimeSpan represented in the given unit.long
getSaturated(TimeUnit dest)
Get the value of this TimeSpan represented in the given unit.TimeUnit
getUnit()
ImmutableList<TemporalUnit>
getUnits()
Deprecated.int
hashCode()
boolean
isEmpty()
Check whether this time span is empty, i.e., represents 0ns (or 0ms or 0s or ...).TimeSpan
multiply(int factor)
Create a new time span that is the current one multiplied by a non-negative integral factor.static TimeSpan
of(long pSpan, TimeUnit pUnit)
static TimeSpan
ofMillis(long pMillis)
static TimeSpan
ofNanos(long pNanos)
static TimeSpan
ofSeconds(long pSeconds)
Temporal
subtractFrom(Temporal pTemporal)
static TimeSpan
sum(Iterable<TimeSpan> timeSpans)
Create a new time span that is the sum of several time spans.static TimeSpan
sum(TimeSpan... t)
Create a new time span that is the sum of several time spans.static TimeSpan
sum(TimeSpan a, TimeSpan b)
Create a new time span that is the sum of two time spans.TimeSpan
toChecked(TimeUnit dest)
Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit.TimeSpan
toSaturated(TimeUnit dest)
Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit.String
toString()
static TimeSpan
valueOf(String input)
-
-
-
Method Detail
-
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)
- Parameters:
input
- theString
to convert- Returns:
- a
TimeSpan
represented by the givenString
- Throws:
IllegalArgumentException
- if the input is not a valid string representation of aTimeSpan
.
-
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 aDuration
instance that represents the same amount of time (but it won't be possible to retrieve our unit as fromgetUnit()
). Note that not all possibleTimeSpan
values can be represented by aDuration
.- 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 ...).
-
compareTo
public int compareTo(TimeSpan other)
- Specified by:
compareTo
in interfaceComparable<TimeSpan>
-
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 public ImmutableList<TemporalUnit> getUnits()
Deprecated.Only for implementingTemporalAmount
. UsegetUnit()
instead.- Specified by:
getUnits
in interfaceTemporalAmount
-
get
@Deprecated public long get(TemporalUnit pUnit)
Deprecated.Only for implementingTemporalAmount
. It is not possible to pass arbitrary units here. UsegetChecked(TimeUnit)
instead.- Specified by:
get
in interfaceTemporalAmount
-
addTo
public Temporal addTo(Temporal pTemporal)
- Specified by:
addTo
in interfaceTemporalAmount
-
subtractFrom
public Temporal subtractFrom(Temporal pTemporal)
- Specified by:
subtractFrom
in interfaceTemporalAmount
-
-