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 aTimeUnitand 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 TemporaladdTo(Temporal pTemporal)DurationasDuration()longasMillis()Get the value of this TimeSpan as milliseconds.longasNanos()Get the value of this TimeSpan as nanoseconds.longasSeconds()Get the value of this TimeSpan as seconds.intcompareTo(TimeSpan other)static TimeSpandifference(TimeSpan a, TimeSpan b)Create a new time span that is the difference of two time spans.TimeSpandivide(int divisor)Create a new time span that is the current one divided by a non-negative integral value.static TimeSpanempty()booleanequals(@Nullable Object obj)StringformatAs(TimeUnit dest)Return 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.TimeUnitgetUnit()ImmutableList<TemporalUnit>getUnits()Deprecated.inthashCode()booleanisEmpty()Check whether this time span is empty, i.e., represents 0ns (or 0ms or 0s or ...).TimeSpanmultiply(int factor)Create a new time span that is the current one multiplied by a non-negative integral factor.static TimeSpanof(long pSpan, TimeUnit pUnit)static TimeSpanofMillis(long pMillis)static TimeSpanofNanos(long pNanos)static TimeSpanofSeconds(long pSeconds)TemporalsubtractFrom(Temporal pTemporal)static TimeSpansum(Iterable<TimeSpan> timeSpans)Create a new time span that is the sum of several time spans.static TimeSpansum(TimeSpan... t)Create a new time span that is the sum of several time spans.static TimeSpansum(TimeSpan a, TimeSpan b)Create a new time span that is the sum of two time spans.TimeSpantoChecked(TimeUnit dest)Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit.TimeSpantoSaturated(TimeUnit dest)Return a TimeSpan that represents (approximately) the same time span, but whose unit is the given unit.StringtoString()static TimeSpanvalueOf(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- theStringto convert- Returns:
- a
TimeSpanrepresented 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 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
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:
compareToin 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:
getUnitsin 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:
getin interfaceTemporalAmount
-
addTo
public Temporal addTo(Temporal pTemporal)
- Specified by:
addToin interfaceTemporalAmount
-
subtractFrom
public Temporal subtractFrom(Temporal pTemporal)
- Specified by:
subtractFromin interfaceTemporalAmount
-
-