ucar.units
Class UnitImpl

java.lang.Object
  extended by ucar.units.UnitImpl
All Implemented Interfaces:
Serializable, Unit
Direct Known Subclasses:
DerivedUnitImpl, LogarithmicUnit, OffsetUnit, ScaledUnit, TimeScaleUnit

public abstract class UnitImpl
extends Object
implements Unit, Serializable

Provides support for classes that implement units.

Version:
$Id: UnitImpl.java 64 2006-07-12 22:30:50Z edavis $
Author:
Steven R. Emmerson
See Also:
Serialized Form

Nested Class Summary
protected static class UnitImpl.MyConverter
          Provides support for converting numeric values from this unit to another unit.
 
Constructor Summary
protected UnitImpl()
          Constructs with no ID.
protected UnitImpl(UnitName id)
          Constructs with the given ID.
 
Method Summary
 double[] convertTo(double[] amounts, Unit outputUnit)
          Converts numeric values from this unit to another unit.
 double[] convertTo(double[] input, Unit outputUnit, double[] output)
          Converts numeric values from this unit to another unit.
 double convertTo(double amount, Unit outputUnit)
          Converts a numeric value from this unit to another unit.
 float[] convertTo(float[] amounts, Unit outputUnit)
          Converts numeric values from this unit to another unit.
 float[] convertTo(float[] input, Unit outputUnit, float[] output)
          Converts numeric values from this unit to another unit.
 float convertTo(float amount, Unit outputUnit)
          Converts a numeric value from this unit to another unit.
 Unit divideBy(Unit that)
          Divides this unit by another.
 Unit divideInto(Unit that)
          Divides this unit into another.
 Converter getConverterTo(Unit outputUnit)
          Gets a Converter for converting numeric values from this unit to another, compatible unit.
 String getName()
          Gets the name of the unit.
 String getPlural()
          Gets the plural form of the name of the unit.
 String getSymbol()
          Gets the symbol for the unit.
 UnitName getUnitName()
          Gets the identifier of this unit.
abstract  int hashCode()
          Returns the hash code of this instance.
 boolean isCompatible(Unit that)
          Indicates if numeric values in this unit are convertible with another unit.
 Unit log(double base)
          Returns a logarithmic unit whose reference level is equal to this unit.
 String makeLabel(String quantityID)
          Returns a label for a quantity in this unit.
 Unit multiplyBy(double scale)
          Multiplies this unit by a scale factor.
 Unit multiplyBy(Unit that)
          Multiplies this unit by another.
protected abstract  Unit myDivideBy(Unit unit)
          Divides this unit by another.
protected abstract  Unit myDivideInto(Unit unit)
          Divides this unit into another.
protected abstract  Unit myMultiplyBy(Unit that)
          Multiplies this unit by another.
protected abstract  Unit myRaiseTo(int power)
          Raises this unit to a power.
 Unit raiseTo(int power)
          Raises this unit to a power.
 Unit shiftTo(Date origin)
          Returns a unit identical to this instance but whose origin (i.e., zero value) has been shifted to the given time.
 Unit shiftTo(double origin)
          Returns a unit identical to this instance but whose origin (i.e., zero value) has been shifted to the given value.
 String toString()
          Returns the string representation of this unit.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface ucar.units.Unit
clone, equals, getCanonicalString, getDerivedUnit, isDimensionless
 

Constructor Detail

UnitImpl

protected UnitImpl()
Constructs with no ID.


UnitImpl

protected UnitImpl(UnitName id)
Constructs with the given ID.

Parameters:
id - The id of the unit (e.g. "foot"). May be null.
Method Detail

getUnitName

public final UnitName getUnitName()
Gets the identifier of this unit.

Specified by:
getUnitName in interface Unit
Returns:
The ID of this unit. May be null.

getName

public final String getName()
Gets the name of the unit.

Specified by:
getName in interface Unit
Returns:
The name of the unit. May be null.

getPlural

public final String getPlural()
Gets the plural form of the name of the unit.

Specified by:
getPlural in interface Unit
Returns:
The plural of the name of the unit. May be null.

getSymbol

public final String getSymbol()
Gets the symbol for the unit.

Specified by:
getSymbol in interface Unit
Returns:
The symbol of the unit. May be null.

shiftTo

public Unit shiftTo(double origin)
             throws ShiftException
Description copied from interface: Unit
Returns a unit identical to this instance but whose origin (i.e., zero value) has been shifted to the given value. For example, if degK is a Kelvin unit, then degK.shiftTo(273.15) is a Celsius unit.

Specified by:
shiftTo in interface Unit
Parameters:
origin - The new origin in units of this instance.
Returns:
A unit convertible with this instance but whose zero value is equal to the value origin of this instance.
Throws:
ShiftException - if the corresponding new unit can't be created.

shiftTo

public Unit shiftTo(Date origin)
             throws ShiftException
Description copied from interface: Unit
Returns a unit identical to this instance but whose origin (i.e., zero value) has been shifted to the given time. For example, if sec is a second unit, then sec.shiftTo(new Date(0L) is the unit corresponding to seconds since the epoch (1970-01-01 00:00:00 UTC).

Specified by:
shiftTo in interface Unit
Parameters:
origin - The new origin.
Returns:
A unit whose zero value is the time given by origin.
Throws:
ShiftException - if the corresponding new unit can't be created. For example, if this instance isn't a unit of time.

multiplyBy

public final Unit multiplyBy(Unit that)
                      throws MultiplyException
Multiplies this unit by another.

Specified by:
multiplyBy in interface Unit
Parameters:
that - The other unit.
Returns:
The product of this unit multiplied by the other unit.
Throws:
MultiplyException - Can't multiply these units.

multiplyBy

public Unit multiplyBy(double scale)
                throws MultiplyException
Description copied from interface: Unit
Multiplies this unit by a scale factor. For example, if m is a meter unit, then m.multiplyBy(1e-2) returns a centimeter unit.

Specified by:
multiplyBy in interface Unit
Parameters:
scale - The scale factor.
Returns:
The result of multiplying this unit by the scale factor.
Throws:
MultiplyException - if scale is zero.

myMultiplyBy

protected abstract Unit myMultiplyBy(Unit that)
                              throws MultiplyException
Multiplies this unit by another.

Parameters:
that - The other unit.
Returns:
The product of this unit multiplied by the other unit.
Throws:
MultiplyException - Can't multiply these units.

divideBy

public final Unit divideBy(Unit that)
                    throws OperationException
Divides this unit by another.

Specified by:
divideBy in interface Unit
Parameters:
that - The other unit.
Returns:
The quotient of this unit divided by the other unit.
Throws:
OperationException - Can't divide these units.

myDivideBy

protected abstract Unit myDivideBy(Unit unit)
                            throws OperationException
Divides this unit by another.

Parameters:
unit - The other unit.
Returns:
The quotient of this unit divided by the other unit.
Throws:
OperationException - Can't divide these units.

divideInto

public final Unit divideInto(Unit that)
                      throws OperationException
Divides this unit into another.

Specified by:
divideInto in interface Unit
Parameters:
that - The other unit.
Returns:
The quotient of this unit divided into the other unit.
Throws:
OperationException - Can't divide these units.

myDivideInto

protected abstract Unit myDivideInto(Unit unit)
                              throws OperationException
Divides this unit into another.

Parameters:
unit - The other unit.
Returns:
The quotient of this unit divided into the other unit.
Throws:
OperationException - Can't divide these units.

raiseTo

public final Unit raiseTo(int power)
                   throws RaiseException
Raises this unit to a power.

Specified by:
raiseTo in interface Unit
Parameters:
power - The power.
Returns:
The result of raising this unit to the power.
Throws:
RaiseException - Can't raise this unit to a power.

myRaiseTo

protected abstract Unit myRaiseTo(int power)
                           throws RaiseException
Raises this unit to a power.

Parameters:
power - The power.
Returns:
The result of raising this unit to the power.
Throws:
RaiseException - Can't raise this unit to a power.

log

public Unit log(double base)
Description copied from interface: Unit
Returns a logarithmic unit whose reference level is equal to this unit. For example, if mW is a milliwatt unit, then mW.log(10.) returns a base-ten logarithmic unit with a milliwatt reference level.

Specified by:
log in interface Unit
Parameters:
base - The logarithmic base. Must be one of 2, Math.E , or 10.

getConverterTo

public Converter getConverterTo(Unit outputUnit)
                         throws ConversionException
Gets a Converter for converting numeric values from this unit to another, compatible unit.

Specified by:
getConverterTo in interface Unit
Parameters:
outputUnit - The unit to which to convert the numeric values.
Returns:
A converter of values from this unit to the other unit.
Throws:
ConversionException - The units aren't convertible.

convertTo

public float convertTo(float amount,
                       Unit outputUnit)
                throws ConversionException
Converts a numeric value from this unit to another unit.

Specified by:
convertTo in interface Unit
Parameters:
amount - The numeric value.
outputUnit - The unit to which to convert the numeric value.
Returns:
The numeric value in the output unit.
Throws:
ConversionException - The units aren't convertible.

convertTo

public double convertTo(double amount,
                        Unit outputUnit)
                 throws ConversionException
Converts a numeric value from this unit to another unit.

Specified by:
convertTo in interface Unit
Parameters:
amount - The numeric value.
outputUnit - The unit to which to convert the numeric value.
Returns:
The numeric value in the output unit.
Throws:
ConversionException - The units aren't convertible.

convertTo

public float[] convertTo(float[] amounts,
                         Unit outputUnit)
                  throws ConversionException
Converts numeric values from this unit to another unit.

Specified by:
convertTo in interface Unit
Parameters:
amounts - The numeric values.
outputUnit - The unit to which to convert the numeric values.
Returns:
The numeric values in the output unit in allocated space.
Throws:
ConversionException - The units aren't convertible.

convertTo

public double[] convertTo(double[] amounts,
                          Unit outputUnit)
                   throws ConversionException
Converts numeric values from this unit to another unit.

Specified by:
convertTo in interface Unit
Parameters:
amounts - The numeric values.
outputUnit - The unit to which to convert the numeric values.
Returns:
The numeric values in the output unit in allocated space.
Throws:
ConversionException - The units aren't convertible.

convertTo

public float[] convertTo(float[] input,
                         Unit outputUnit,
                         float[] output)
                  throws ConversionException
Converts numeric values from this unit to another unit.

Specified by:
convertTo in interface Unit
Parameters:
input - The input numeric values.
outputUnit - The unit to which to convert the numeric values.
output - The output numeric values. May be the same array as the input values.
Returns:
The numeric values in the output unit.
Throws:
ConversionException - The units aren't convertible.

convertTo

public double[] convertTo(double[] input,
                          Unit outputUnit,
                          double[] output)
                   throws ConversionException
Converts numeric values from this unit to another unit.

Specified by:
convertTo in interface Unit
Parameters:
input - The input numeric values.
outputUnit - The unit to which to convert the numeric values.
output - The output numeric values. May be the same array as the input values.
Returns:
The numeric values in the output unit.
Throws:
ConversionException - The units aren't convertible.

isCompatible

public boolean isCompatible(Unit that)
Indicates if numeric values in this unit are convertible with another unit.

Specified by:
isCompatible in interface Unit
Parameters:
that - The other unit.
Returns:
true if and only if numeric values in this unit are convertible the other unit.

hashCode

public abstract int hashCode()
Returns the hash code of this instance.

Overrides:
hashCode in class Object
Returns:
The hash code of this instance.

toString

public String toString()
Returns the string representation of this unit.

Specified by:
toString in interface Unit
Overrides:
toString in class Object
Returns:
The string representation of this unit.

makeLabel

public String makeLabel(String quantityID)
Returns a label for a quantity in this unit.

Specified by:
makeLabel in interface Unit
Parameters:
quantityID - The identifier for the quantity (e.g. "altitude").
Returns:
The appropriate label (e.g. "altitude/m").


Copyright © 1999-2011 UCAR/Unidata. All Rights Reserved.