ucar.nc2.units
Class TimeUnit

java.lang.Object
  extended by ucar.nc2.units.SimpleUnit
      extended by ucar.nc2.units.TimeUnit

public class TimeUnit
extends SimpleUnit

Handles Units that are time durations, eg in seconds, hours, days, years. It keeps track of the original unit name, rather than converting to canonical "seconds". The unit name never changes, but the value may.

This is a wrapper around ucar.units. The underlying ucar.units.Unit always has a value of "1.0", ie is a base unit.

Version:
$Revision: 63 $ $Date: 2006-07-12 15:50:51 -0600 (Wed, 12 Jul 2006) $
Author:
John Caron

Constructor Summary
TimeUnit(double value, java.lang.String unitString)
          Constructor from a value and a unit name.
TimeUnit(java.lang.String text)
          Constructor from a String.
TimeUnit(TimeUnit src)
          Copy Constructor.
 
Method Summary
 java.util.Date add(java.util.Date d)
           
 double convertTo(double value, SimpleUnit outputUnit)
          Convert given value of this unit to the new unit.
 boolean equals(java.lang.Object o)
          TimeUnits with same value and unitString are equal
 double getFactor()
          Get the factor that converts this value to seconds.
 java.lang.String getUnitString()
          Get the "base" unit String, eg "secs" or "days"
 double getValue()
          Get the value.
 double getValueInSeconds()
          Get the time duration in seconds.
 double getValueInSeconds(double value)
          Get the time duration in seconds of the specified value.
 int hashCode()
          Override hashcode to be consistent with equals.
 void setValue(double value)
          Set the value in the original units.
 void setValueInSeconds(double secs)
          Set the value, using the given number of seconds.
 java.lang.String toString()
          String representation.
 
Methods inherited from class ucar.nc2.units.SimpleUnit
factory, factoryWithExceptions, getConversionFactor, getUnit, isCompatible, isCompatible, isCompatibleWithExceptions, isDateUnit, isDateUnit, isTimeUnit, isTimeUnit, isUnknownUnit
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

TimeUnit

public TimeUnit(java.lang.String text)
         throws java.lang.Exception
Constructor from a String.

Parameters:
text - [value]
Throws:
java.lang.Exception - is bad format

TimeUnit

public TimeUnit(double value,
                java.lang.String unitString)
         throws java.lang.Exception
Constructor from a value and a unit name.

Parameters:
value - amount of the unit.
unitString - Time unit string from udunits.
Throws:
java.lang.Exception

TimeUnit

public TimeUnit(TimeUnit src)
Copy Constructor.

Method Detail

getValue

public double getValue()
Get the value.

Overrides:
getValue in class SimpleUnit

getFactor

public double getFactor()
Get the factor that converts this value to seconds. getValueInSeconds = factor * value


setValue

public void setValue(double value)
Set the value in the original units.


getUnitString

public java.lang.String getUnitString()
Get the "base" unit String, eg "secs" or "days"

Overrides:
getUnitString in class SimpleUnit

toString

public java.lang.String toString()
String representation.

Overrides:
toString in class SimpleUnit

getValueInSeconds

public double getValueInSeconds()
Get the time duration in seconds.


getValueInSeconds

public double getValueInSeconds(double value)
Get the time duration in seconds of the specified value.


setValueInSeconds

public void setValueInSeconds(double secs)
Set the value, using the given number of seconds.

Parameters:
secs - : number of seconds; convert this to the units of this TimeUnit.

convertTo

public double convertTo(double value,
                        SimpleUnit outputUnit)
                 throws ucar.units.ConversionException
Convert given value of this unit to the new unit. NOTE: the current value of this unit ignored, the given value is used instead. This is different than ucar.units or SimpleUnit.

Overrides:
convertTo in class SimpleUnit
Parameters:
value - in the units of this "base unit"
outputUnit - convert to this base type, must be convertible to units of "seconds"
Returns:
new value in the units of the "outputUnit
Throws:
ucar.units.ConversionException

add

public java.util.Date add(java.util.Date d)

equals

public boolean equals(java.lang.Object o)
TimeUnits with same value and unitString are equal

Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Override hashcode to be consistent with equals.

Overrides:
hashCode in class java.lang.Object