Package ucar.units
Class UnitImpl
- java.lang.Object
-
- ucar.units.UnitImpl
-
- All Implemented Interfaces:
Serializable
,Unit
- Direct Known Subclasses:
DerivedUnitImpl
,LogarithmicUnit
,OffsetUnit
,ScaledUnit
,TimeScaleUnit
@Immutable public abstract class UnitImpl extends Object implements Unit, Serializable
Provides support for classes that implement units.- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static class
UnitImpl.MyConverter
Provides support for converting numeric values from this unit to another unit.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description 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(double origin)
Returns a unit identical to this instance but whose origin (i.e., zero value) has been shifted to the given value.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.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 interfaceUnit
- Returns:
- The ID of this unit. May be
null
.
-
getName
public final String getName()
Gets the name of the unit.
-
getPlural
public final String getPlural()
Gets the plural form of the name of the unit.
-
getSymbol
public final String getSymbol()
Gets the symbol for the unit.
-
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, ifdegK
is a Kelvin unit, thendegK.shiftTo(273.15)
is a Celsius unit.- Specified by:
shiftTo
in interfaceUnit
- 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, ifsec
is a second unit, thensec.shiftTo(new Date(0L)
is the unit corresponding to seconds since the epoch (1970-01-01 00:00:00 UTC).- Specified by:
shiftTo
in interfaceUnit
- 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 interfaceUnit
- 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, ifm
is a meter unit, thenm.multiplyBy(1e-2)
returns a centimeter unit.- Specified by:
multiplyBy
in interfaceUnit
- Parameters:
scale
- The scale factor.- Returns:
- The result of multiplying this unit by the scale factor.
- Throws:
MultiplyException
- ifscale
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 interfaceUnit
- 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 interfaceUnit
- 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 interfaceUnit
- 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, ifmW
is a milliwatt unit, thenmW.log(10.)
returns a base-ten logarithmic unit with a milliwatt reference level.
-
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 interfaceUnit
- 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 interfaceUnit
- 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 interfaceUnit
- 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 interfaceUnit
- 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 interfaceUnit
- 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 interfaceUnit
- 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 interfaceUnit
- 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 interfaceUnit
- 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.
-
toString
public String toString()
Returns the string representation of this unit.
-
-