public class Time
extends java.lang.Object
implements java.lang.Comparable
The time value is quantized to the time resolution specified by the timeResolution parameter of the associated director. The reason for this is that without quantization, it is extremely difficult to compare two time values with digit-to-digit accuracy because of the unpredictable numerical errors introduced during computation. In practice, two time values can only be distinguished if their difference can be detected by some measuring instrument, which always has a smallest unit for measurement. This smallest unit measurement gives the physical meaning of the time resolution used for quantization. There are three exceptions that do not really need a notion of time resolution, ZERO, POSITIVE_INFINITY, and NEGATIVE_INFINITY. These are provided as static fields, and normally these should be the only instances of Time constructed without a director.
This implementation of Time does not lose resolution as the magnitude of the time increases (unlike floating point numbers). This is because Time is represented internally as a multiple of the resolution, and the multiple is not constrained to any limited magnitude.
The time value can be retrieved in three ways, the toString()
method
and the getDoubleValue()
method and the getLongValue()
method. The first method returns a string representation while the second
method returns a double value. There are some limitations on both methods.
For the toString() method, we cannot directly do numerical operations on
strings. For the getDoubleValue() method, we can not guarantee that the
returned double value preserves the time resolution because of the limited
digits for double representation. We recommend to operate on time objects
directly instead of the time values of time objects. getLongValue() returns
an integer multiple of the director resolution.
Two operations, add and subtract, can be performed on a time object, where the argument can be a double or a time object. If the argument is not a time object, the argument is quantized before the operations are performed. These operations return a new time object with a quantized result.
The time value of a time object can be infinite. The add and subtract operations on infinite time values follow rules similar to the IEEE Standard 754 for Floating Point Numbers. In particular, adding two positive or negative infinities yield a positive or negative infinity; adding a positive infinity to a negative infinity, however, triggers an ArithmeticException; the negation of a positive or negative infinity is a negative or positive infinity, respectively.
This class implements the Comparable interface, where two time objects can be compared in the following way. If any of the two time objects contains an infinite time value, the rules are: a negative infinity is equal to a negative infinity and less than anything else; a positive infinity is equal to a positive infinity and bigger than anything else. If none of the time objects has an infinite time value, the time values of two time objects are compared. If the time values are the same, the two time objects are treated equal, or they represent the same model time. Otherwise, the time object containing a bigger time value is regarded to happen after the time object with a smaller time value.
All time objects share the same time resolution, which is provided by the top-level director. In some domains, such as CT and DE, users can change the time resolution by configuring the timeResolution parameter. The default value for this parameter "1E-10", which has value 10 -10 . To preserve the consistency of time values, timeResolution cannot be changed when a model is running (attempting to do so will trigger an exception).
Modifier and Type | Field and Description |
---|---|
static Time |
NEGATIVE_INFINITY
A static and final time constant holding a negative infinity.
|
static Time |
POSITIVE_INFINITY
A static and final time constant holding a positive infinity.
|
static Time |
ZERO
A static and final time constant holding a zero.
|
Constructor and Description |
---|
Time(Director director)
Construct a Time object with zero as the time value.
|
Time(Director director,
double timeValue)
Construct a Time object with the specified double value as its
time value.
|
Time(Director director,
long timeValue)
Construct a Time object with the specified long value as its time value.
|
Modifier and Type | Method and Description |
---|---|
protected double |
_timeResolution()
Return the time resolution for this Time object, which in this
class is the time resolution of the director given in the
constructor.
|
Time |
add(double timeValue)
Return a new time object whose time value is increased by the
given double value.
|
Time |
add(Time time)
Return a new time object whose time value is the sum of that of
this time object and of the specified time object.
|
Time |
addUnchecked(double timeValue)
Add the specified double to this time without checking whether the
specified double is too large to ensure the time resolution of the
director.
|
int |
compareTo(java.lang.Object time)
Return -1, 0, or 1 if this time object is less than, equal to, or
greater than the given argument.
|
boolean |
equals(java.lang.Object time)
Return true if this time object has the same time value as
that of the given time object.
|
double |
getDoubleValue()
Return the double representation of the time value of this
time object.
|
long |
getLongValue()
Return the long representation of the time value of this time
object.
|
int |
hashCode()
Return the hash code for the time object.
|
boolean |
isInfinite()
Return true if the current time value is infinite.
|
boolean |
isNegative()
Return true if the current time value is a negative value
(including negative infinity).
|
boolean |
isNegativeInfinite()
Return true if the current time value is a negative infinity.
|
boolean |
isPositive()
Return true if the current time value is a positive value
(including positive infinity).
|
boolean |
isPositiveInfinite()
Return true if the current time value is a positive infinity.
|
boolean |
isZero()
Return true if the current time value is zero.
|
double |
maximumAccurateValueAsDouble()
Return the maximum value of time whose representation as a double
is always accurate to the specified time resolution.
|
static Time |
milliseconds(Director director,
long milliseconds)
Return a new Time object whose value equals the argument,
which is interpreted in milliseconds.
|
Time |
subtract(double timeValue)
Return a new Time object whose time value is decreased by the
given double value.
|
Time |
subtract(Time time)
Return a new time object whose time value is decreased by the
time value of the specified time object.
|
double |
subtractToDouble(Time time)
Subtract the specified time from this time and return the result as
a double.
|
java.lang.String |
toString()
Return the string representation of this time object.
|
public static final Time NEGATIVE_INFINITY
public static final Time POSITIVE_INFINITY
public static final Time ZERO
public Time(Director director)
director
- The director with which this time object is associated.
This must not be null, or subsequent uses of the class will fail.public Time(Director director, double timeValue) throws IllegalActionException
director
- The director with which this time object is associated.timeValue
- A double value as the specified time value.java.lang.ArithmeticException
- If the argument is NaN.IllegalActionException
- If the given double time value does
not match the time resolution.public Time(Director director, long timeValue)
director
- The director with which this time object is associated.timeValue
- A long value as the specified time value, as a multiple
of the resolution.public Time add(double timeValue)
timeValue
- The amount of the time increment.java.lang.ArithmeticException
- If the result is not a valid
number (the argument is NaN or the sum would be), or the given time
value does not match the time resolution.public Time add(Time time)
time
- The time object contains the amount of time increment.java.lang.ArithmeticException
- If the result is not a valid number
(it is the sum of positive and negative infinity).public Time addUnchecked(double timeValue)
That is, two calls to this method could yield the same result even if
the two arguments differ by more than the time resolution.
The add(double)
method, in contrast, will throw an exception
if two such calls could yield the same result.
This method should only be used if the time resolution is not really needed for this addition. For example, when finding the stop time of long simulation, where `timeValue` is a large number, it would be reasonable to use this method if the stop time does not need to be so precise.
timeValue
- The value to add.public int compareTo(java.lang.Object time)
compareTo
in interface java.lang.Comparable
time
- A time object to compare to.public boolean equals(java.lang.Object time)
equals
in class java.lang.Object
time
- The time object that this time object is compared to.public double getDoubleValue()
public long getLongValue()
public int hashCode()
hashCode
in class java.lang.Object
public final boolean isInfinite()
public final boolean isNegative()
public final boolean isNegativeInfinite()
public final boolean isPositive()
public final boolean isPositiveInfinite()
public final boolean isZero()
public double maximumAccurateValueAsDouble()
public static Time milliseconds(Director director, long milliseconds)
director
- The director with which this time object is associated.milliseconds
- The time in ms.public Time subtract(double timeValue)
timeValue
- The amount of time decrement.public Time subtract(Time time)
time
- The time object contains the amount of time decrement.public double subtractToDouble(Time time)
This is equivalent to calling subtract(Time)
and then invoking
getDoubleValue(), but it is more efficient, in that it avoids
unnecessary construction of Time objects.
time
- The time to subtract.public java.lang.String toString()
toString
in class java.lang.Object
protected double _timeResolution()