|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object edu.nrao.sss.measure.TimeDuration
public class TimeDuration
A length of time without a defined starting or ending point.
Contrast this with a TimeInterval
, which is the span
from one instant in time to another.
This duration currently works with only the following units:
TimeUnits.NANOSECOND
TimeUnits.MICROSECOND
TimeUnits.MILLISECOND
TimeUnits.SECOND
TimeUnits.MINUTE
TimeUnits.HOUR
getLegalUnits()
.
Note About Accuracy
This class originally used java's primitive double type
for storage and calculation. Certain transformations, though,
led to results that where not accurate enough for many purposes.
Because of that, the internal references to double
have been replaced with references to BigDecimal
.
Version Info:
$Revision: 1816 $ |
$Date: 2008-12-23 10:21:00 -0700 (Tue, 23 Dec 2008) $ |
$Author: dharland $ |
Constructor Summary | |
---|---|
TimeDuration()
Creates a new duration of zero hours. |
|
TimeDuration(BigDecimal hours)
Creates a new duration of hours hours. |
|
TimeDuration(BigDecimal value,
TimeUnits units)
Creates a new duration of the given length. |
|
TimeDuration(String hours)
Creates a new duration of hours hours. |
|
TimeDuration(String value,
TimeUnits units)
Creates a new duration of the given length. |
Method Summary | |
---|---|
TimeDuration |
add(TimeDuration other)
Adds the given time to this duration. |
TimeDuration |
clone()
Returns a duration that is equal to this one. |
int |
compareTo(TimeDuration otherDur)
Compares this duration with the otherDur for order. |
TimeDuration |
convertTo(TimeUnits newUnits)
Converts this duration to the new units. |
boolean |
equals(Object o)
Returns true if o is equal to this duration. |
long |
getIntegralPart(TimeUnits units)
For a duration that is thought of as HH:MM:SS.xxx, returns the part corresponding to units , truncated to an integral value. |
static Set<TimeUnits> |
getLegalUnits()
Returns a set of TimeUnits that are legal for use with
TimeDuration instances. |
BigDecimal |
getPart(TimeUnits units)
For a duration that is thought of as HH:MM:SS.xxx, returns the part corresponding to units . |
TimeUnits |
getUnits()
Returns the units of this duration. |
BigDecimal |
getValue()
Returns the length of this duration. |
int |
hashCode()
Returns a hash code value for this duration. |
boolean |
isInDefaultState()
Returns true if this duration is in its default state, no matter how it got there. |
boolean |
isInfinite()
Returns true if this duration is infinite. |
TimeDuration |
multiplyBy(BigDecimal multiplier)
Multiplies this duration by multiplier . |
TimeDuration |
multiplyBy(String multiplier)
Multiplies this duration by multiplier . |
TimeDuration |
normalize()
Converts the value and units of this time duration so that the value is normal. |
static TimeDuration |
parse(String timeText)
Creates a time duration based on timeText . |
static TimeDuration |
parse(String timeText,
BigDecimal secondsInOneDay)
Creates a new time duration by parsing timeText . |
void |
reset()
Resets this duration to its initial state. |
void |
set(BigDecimal value,
TimeUnits units)
Sets the length and units of this duration. |
void |
set(long hours,
int minutes,
BigDecimal seconds)
Sets the length of this duration. |
void |
set(long hours,
int minutes,
String seconds)
Sets the length of this duration. |
void |
set(String timeText)
Sets the value and units of this duration based on timeText . |
void |
set(String value,
TimeUnits units)
Sets the length and units of this duration. |
void |
set(TimeDuration other)
Sets this duration to be equal to other . |
void |
setUnits(TimeUnits newUnits)
Sets the units of this duration to newUnits . |
void |
setValue(BigDecimal newValue)
Sets the length of this duration to newValue . |
void |
setValue(String newValue)
Sets the length of this duration to newValue . |
TimeDuration |
subtract(TimeDuration other)
Subtracts other from this duration. |
TimeInterval |
toIntervalCenteredOn(Date center)
Returns a time interval that is centered on center and lasts
as long as this duration. |
TimeInterval |
toIntervalEndingOn(Date end)
Returns a time interval that ends on end and lasts
as long as this duration. |
TimeInterval |
toIntervalStartingOn(Date start)
Returns a time interval that begins on start and lasts
as long as this duration. |
String |
toString()
Returns a text representation of this time duration. |
String |
toString(int minFracDigits,
int maxFracDigits)
Returns a text representation of this duration. |
String |
toString(String separator)
Returns a string where the hours, minutes, and seconds are separated by the given string. |
String |
toString(String separator,
int minFracDigits,
int maxFracDigits)
Returns a string where the hours, minutes, and seconds are separated by the given string. |
String |
toStringHms()
Returns a text representation of this duration in hours, minutes, and seconds. |
String |
toStringHms(int minFracDigits,
int maxFracDigits)
Returns a text representation of this duration in hours, minutes, and seconds. |
BigDecimal |
toUnits(TimeUnits otherUnits)
Returns the length of this duration in otherUnits . |
long |
toWholeUnits(TimeUnits otherUnits)
Returns the length of this duration in a whole number of units . |
Methods inherited from class java.lang.Object |
---|
finalize, getClass, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public TimeDuration()
public TimeDuration(BigDecimal hours)
hours
hours.
See setValue(BigDecimal)
for information
about valid parameter values and exceptions that might
be thrown.
hours
- the length of this duration in hours.public TimeDuration(String hours)
hours
hours.
See setValue(String)
for information
about valid parameter values and exceptions that might
be thrown.
hours
- the length of this duration in hours.public TimeDuration(BigDecimal value, TimeUnits units)
set(BigDecimal, TimeUnits)
for information
about valid parameter values and exceptions that might
be thrown.
value
- the length of this duration.units
- the units in which value
is expressed.
See the class comments
for a list of legal values.public TimeDuration(String value, TimeUnits units)
set(String, TimeUnits)
for information
about valid parameter values and exceptions that might
be thrown.
value
- the length of this duration.units
- the units in which time
is expressed.
See the class comments
for a list of legal values.Method Detail |
---|
public void reset()
public BigDecimal getValue()
public TimeUnits getUnits()
public final void set(BigDecimal value, TimeUnits units)
See setValue(BigDecimal)
for more information on legal
values for value.
value
- the length of this duration.units
- the units in which value
is expressed.public final void set(String value, TimeUnits units)
See setValue(String)
for more information on legal
values for value.
value
- the length of this duration.units
- the units in which time
is expressed.public final void setValue(BigDecimal newValue)
newValue
.
Note that the units of this duration are unaffected by this method.
newValue
- the new magnitude for this duration.
This value may not be negative or null but may be infinite.
NumberFormatException
- if newValue
is null or negative.public final void setValue(String newValue)
newValue
.
Note that the units of this duration are unaffected by this method.
newValue
- the new magnitude for this duration.
This value may not be negative or null but may be infinite.
The allowable representations of infinity are
"infinity", "+infinity", and "-infinity";
these values are not case sensitive.
NumberFormatException
- if newValue
is null or negative.public final void setUnits(TimeUnits newUnits)
newUnits
.
Note that the value of this duration is unaffected by
this method. Contrast this with convertTo(TimeUnits)
.
newUnits
- the new units for this duration.
See the class comments
for a list of legal values.
IllegalArgumentException
- if the parameter is outside the described range.public final void set(long hours, int minutes, BigDecimal seconds)
While there are no restrictions on the values of the individual parameters, their combination must not result in a negative duration.
IllegalArgumentException
- if the combination of parameters results in a negative duration.public final void set(long hours, int minutes, String seconds)
While there are no restrictions on the values of the individual parameters, their combination must not result in a negative duration.
IllegalArgumentException
- if the combination of parameters results in a negative duration.public void set(TimeDuration other)
other
.
The code:
TimeDuration td = new TimeDuration().set(otherDuration);gives the same result as:
TimeDuration td = otherDuration.clone();This method is better suited to those situations where the target duration has already been created.
other
- the duration to which this duration will be made equal.public void set(String timeText)
timeText
.
See parse(String)
for the expected format of timeText
.
If the parsing fails, this duration will be kept in its current state.
timeText
- a string that will be converted into a time duration.
IllegalArgumentException
- if timeText
is not in the expected form.public boolean isInDefaultState()
A duration is in its default state if both its value and
its units are the same as those of a duration newly created via
the no-argument constructor
.
A duration whose most recent update came via the
reset
method is also in its default state.
public boolean isInfinite()
public TimeDuration convertTo(TimeUnits newUnits)
After this method is complete this duration will have units of
units
and its value will have been converted
accordingly.
newUnits
- the new units for this duration.
If newUnits
is null an
IllegalArgumentException
will be thrown.
See the class comments
for
a list of legal values.
BigDecimal hours =
myDuration.convertTo(TimeUnits.HOUR).getValue();
IllegalArgumentException
- if the parameter is outside the
described range.public BigDecimal toUnits(TimeUnits otherUnits)
otherUnits
.
Note that this method does not alter the state of this duration.
Contrast this with convertTo(TimeUnits)
.
Example:
Let this duration be of length 2 hours 13 minutes 45.6789 seconds.
The values returned by this method for the legal values of
unit
are (to 5 decimal places):
Units | Value |
---|---|
MILLISECOND | 8,025,678.90000 |
SECOND | 8,025.67890 |
MINUTE | 133.76132 |
HOUR | 2.22936 |
Contrast this with toWholeUnits(TimeUnits)
.
In addition to working with the legal time units described in the class comments, this method will also convert to:
otherUnits
- the units in which to express this duration's length.
If newUnits
is null, it will be treated as
TimeUnits.HOUR
.
See the class comments
for
a list of legal values.
otherUnits
.
IllegalArgumentException
- if the parameter is outside the
described range.public static Set<TimeUnits> getLegalUnits()
TimeUnits
that are legal for use with
TimeDuration
instances.
TimeUnits
.public TimeDuration normalize()
public long toWholeUnits(TimeUnits otherUnits)
units
.
Example:
Let this duration be of length 2 hours 13 minutes 45.6789 seconds.
The values returned by this method for the legal values of
unit
are:
Units | Value |
---|---|
MILLISECOND | 8,025,678 |
SECOND | 8,025 |
MINUTE | 133 |
HOUR | 2 |
Contrast this with toUnits(TimeUnits)
.
otherUnits
- the units of time in which the return value
is expressed.
See the class comments
for
a list of legal values.
units
.
IllegalArgumentException
- if the parameter is outside the
described range.public BigDecimal getPart(TimeUnits units)
units
.
Range of Returned Value:
The value returned will be in its "natural" range.
The table below gives the natural ranges for each unit. Note that since
hours is the largest legal unit of time, it has no upper bound.
Units | Range |
---|---|
MICROSECOND | 0 <= x < 1000 |
MILLISECOND | 0 <= x < 1000 |
SECOND | 0 <= x < 60 |
MINUTE | 0 <= x < 60 |
HOUR | 0 <= x |
Example:
Let this duration be of length 2 hours 13 minutes 45.6789 seconds.
The values returned by this method for the legal values of
unit
are (to 5 decimal places):
Units | Value |
---|---|
MILLISECOND | 678.90000 |
SECOND | 45.67890 |
MINUTE | 13.76132 |
HOUR | 2.22936 |
units
- the units of time in which the return value is expressed.
See the class comments
for a list of legal values.
units
.
IllegalArgumentException
- if the parameter is outside the described range.getIntegralPart(TimeUnits)
public long getIntegralPart(TimeUnits units)
units
, truncated to an integral value.
Range of Returned Value:
The value returned will be in its "natural" range.
The table below gives the natural ranges for each unit. Note that since
hours is the largest legal unit of time, it has no upper bound.
Units | Range |
---|---|
MICROSECOND | 0 <= x < 1000 |
MILLISECOND | 0 <= x < 1000 |
SECOND | 0 <= x < 60 |
MINUTE | 0 <= x < 60 |
HOUR | 0 <= x |
Example:
Let this duration be of length 2 hours 13 minutes 45.6789 seconds.
The values returned by this method for the legal values of
unit
are:
Units | Value |
---|---|
MILLISECOND | 678 |
SECOND | 45 |
MINUTE | 13 |
HOUR | 2 |
units
- the units of time in which the return value is expressed.
See the class comments
for a list of legal values.
units
.
IllegalArgumentException
- if the parameter is outside the described range.getPart(TimeUnits)
public TimeInterval toIntervalStartingOn(Date start)
start
and lasts
as long as this duration.
start
- the starting time for the returned interval.
start
and lasts
as long as this duration.public TimeInterval toIntervalEndingOn(Date end)
end
and lasts
as long as this duration.
end
- the ending time for the returned interval.
end
and lasts
as long as this duration.public TimeInterval toIntervalCenteredOn(Date center)
center
and lasts
as long as this duration.
If rounding prevents us from locating the endpoints the exact same
distance from center
, this method will have a bias toward
creating an interval whose starting point is closer to
center
than its ending point.
center
- the center time for the returned interval.
center
and
lasts as long as this duration.public TimeDuration add(TimeDuration other)
other
- the duration to be added to this duration.
public TimeDuration subtract(TimeDuration other)
other
from this duration. If the subtraction would
result in a negative interval, this interval is set to a length of zero.
other
- the amount by which to reduce this duration.public TimeDuration multiplyBy(String multiplier)
multiplier
.
multiplier
- the number by which this duration should be multiplied.
This value must not result in a duration that is
negative.
public TimeDuration multiplyBy(BigDecimal multiplier)
multiplier
.
multiplier
- the number by which this duration should be multiplied.
This value must not result in a duration that is
negative.
public String toString()
toString
in class Object
public String toString(int minFracDigits, int maxFracDigits)
minFracDigits
- the minimum number of places after the decimal point.maxFracDigits
- the maximum number of places after the decimal point.
public String toStringHms()
public String toStringHms(int minFracDigits, int maxFracDigits)
minFracDigits
- the minimum number of places after the decimal point
for the seconds field.maxFracDigits
- the maximum number of places after the decimal point
for the seconds field. If this value is less than zero,
no rounding or truncating will be performed.public String toString(String separator)
separator
- the separator to use between the hours and minutes,
and minutes and seconds, fields.
public String toString(String separator, int minFracDigits, int maxFracDigits)
separator
- the separator to use between the hours and minutes,
and minutes and seconds, fields.minFracDigits
- the minimum number of places after the decimal point
for the seconds field.maxFracDigits
- the maximum number of places after the decimal point
for the seconds field.
public static TimeDuration parse(String timeText)
timeText
.
The parsed text can be in many different forms. All of the forms
supported by Longitude.parse(String)
are supported here.
Additionally, the form mm:ss.s (minutes and seconds) is supported.
A naked number will be treated as a number of seconds.
timeText
- the text to be parsed and converted into a time duration.
If this value is null or "" (the empty
string), a new duration of zero length is returned.
timeText
.
IllegalArgumentException
- if timeText
cannot be parsed.public static TimeDuration parse(String timeText, BigDecimal secondsInOneDay)
timeText
.
See parse(String)
for the expected format of timeText
.
timeText
- the text to be parsed and converted into a time duration.
If this value is null or "" (the empty
string), a new duration of zero length is returned.secondsInOneDay
- the length of a day, in seconds. The TimeOfDay
class has two
constants that express time in SI seconds,
TimeOfDay.STANDARD_DAY_LENGTH
and
TimeOfDay.SIDEREAL_DAY_LENGTH
, that my be used here.
timeText
.
IllegalArgumentException
- if timeText
cannot be parsed.public TimeDuration clone()
clone
in class Object
public boolean equals(Object o)
o
is equal to this duration.
equals
in class Object
public int hashCode()
hashCode
in class Object
public int compareTo(TimeDuration otherDur)
otherDur
for order.
compareTo
in interface Comparable<TimeDuration>
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |