Package ucar.units
Class OffsetUnit
- java.lang.Object
-
- ucar.units.UnitImpl
-
- ucar.units.OffsetUnit
-
- All Implemented Interfaces:
Serializable
,DerivableUnit
,Unit
@Immutable public final class OffsetUnit extends UnitImpl implements DerivableUnit
Provides support for units that are offset from reference units (ex: as the unit "degree Celsius" is offset from the reference unit "kelvin"). Instances of this class are immutable.- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class ucar.units.UnitImpl
UnitImpl.MyConverter
-
-
Constructor Summary
Constructors Constructor Description OffsetUnit(Unit unit, double offset)
Constructs from a reference unit and an offset.OffsetUnit(Unit unit, double offset, UnitName id)
Constructs from a reference unit, and offset, and a unit identifier.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Unit
clone(UnitName id)
Clones this unit, changing the identifier.boolean
equals(Object object)
Indicates if this unit is semantically identical to an object.double
fromDerivedUnit(double amount)
Converts a value in the convertible derived unit to the equivalent value in this unit.double[]
fromDerivedUnit(double[] input, double[] output)
Converts values in the convertible derived unit to the equivalent values in this unit.float
fromDerivedUnit(float amount)
Converts a value in the convertible derived unit to the equivalent value in this unit.float[]
fromDerivedUnit(float[] input, float[] output)
Converts values in the convertible derived unit to the equivalent values in this unit.String
getCanonicalString()
Returns the canonical string representation of the unit.DerivedUnit
getDerivedUnit()
Returns the derived unit that is convertible with this unit.double
getOffset()
Returns the offset.Unit
getUnit()
Returns the reference unit.int
hashCode()
Returns the hash code of this instance.boolean
isDimensionless()
Indicates if this unit is dimensionless.Unit
multiplyBy(double scale)
Multiplies this unit by a scale factor.protected Unit
myDivideBy(Unit that)
Divide this unit by another unit.protected Unit
myDivideInto(Unit that)
Divide this unit into another unit.protected Unit
myMultiplyBy(Unit that)
Multiply this unit by another unit.protected Unit
myRaiseTo(int power)
Raise 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.double
toDerivedUnit(double amount)
Converts a value in this unit to the equivalent value in the convertible derived unit.double[]
toDerivedUnit(double[] input, double[] output)
Converts values in this unit to the equivalent values in the convertible derived unit.float
toDerivedUnit(float amount)
Converts a value in this unit to the equivalent value in the convertible derived unit.float[]
toDerivedUnit(float[] input, float[] output)
Converts values in this unit to the equivalent values in the convertible derived unit.String
toString()
Returns the string representation of this unit.-
Methods inherited from class ucar.units.UnitImpl
convertTo, convertTo, convertTo, convertTo, convertTo, convertTo, divideBy, divideInto, getConverterTo, getName, getPlural, getSymbol, getUnitName, isCompatible, log, makeLabel, multiplyBy, raiseTo, shiftTo
-
-
-
-
Constructor Detail
-
OffsetUnit
public OffsetUnit(Unit unit, double offset)
Constructs from a reference unit and an offset.- Parameters:
unit
- The reference unit.offset
- The origin of this unit in terms of the reference unit. For example, a degree Celsius unit would be created as "new OffsetUnit(kelvin, 273.15)
.
-
OffsetUnit
public OffsetUnit(Unit unit, double offset, UnitName id)
Constructs from a reference unit, and offset, and a unit identifier.- Parameters:
unit
- The reference unit.offset
- The origin of this unit in terms of the reference unit. For example, a degree Celsius unit would be created as "new OffsetUnit(kelvin, 273.15)
.id
- The identifier for the new unit.
-
-
Method Detail
-
getUnit
public Unit getUnit()
Returns the reference unit.- Returns:
- The reference unit.
-
getOffset
public double getOffset()
Returns the offset. The offset is the location of the origin of this unit in terms of the reference unit.- Returns:
- The origin of this unit in terms of the reference unit.
-
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
- Overrides:
multiplyBy
in classUnitImpl
- Parameters:
scale
- The scale factor.- Returns:
- The result of multiplying this unit by the scale factor.
- Throws:
MultiplyException
- ifscale
is zero.
-
shiftTo
public Unit shiftTo(double origin)
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.
-
myMultiplyBy
protected Unit myMultiplyBy(Unit that) throws MultiplyException
Multiply this unit by another unit.- Specified by:
myMultiplyBy
in classUnitImpl
- Parameters:
that
- The unit to multiply this unit by.- Returns:
- The product of this unit and
that
. The offset of this unit will be ignored; thus, for example "celsius.myMultiplyBy(day)" is equivalent to "kelvin.myMultiplyBy(day)". - Throws:
MultiplyException
- Can't multiply these units together.
-
myDivideBy
protected Unit myDivideBy(Unit that) throws OperationException
Divide this unit by another unit.- Specified by:
myDivideBy
in classUnitImpl
- Parameters:
that
- The unit to divide this unit by.- Returns:
- The quotient of this unit and
that
. The offset of this unit will be ignored; thus, for example "celsius.myDivideBy(day)" is equivalent to "kelvin.myDivideBy(day)". - Throws:
OperationException
- Can't divide these units.
-
myDivideInto
protected Unit myDivideInto(Unit that) throws OperationException
Divide this unit into another unit.- Specified by:
myDivideInto
in classUnitImpl
- Parameters:
that
- The unit to divide this unit into.- Returns:
- The quotient of
that
unit and this unit. The offset of this unit will be ignored; thus, for example "celsius.myDivideInto(day)" is equivalent to "kelvin.myDivideInto(day)". - Throws:
OperationException
- Can't divide these units.
-
myRaiseTo
protected Unit myRaiseTo(int power) throws RaiseException
Raise this unit to a power.- Specified by:
myRaiseTo
in classUnitImpl
- Parameters:
power
- The power to raise this unit by.- Returns:
- The result of raising this unit by the power
power
. The offset of this unit will be ignored; thus, for example "celsius.myRaiseTo(2)" is equivalent to "kelvin.myRaiseTo(2)". - Throws:
RaiseException
- Can't raise this unit to a power.
-
getDerivedUnit
public DerivedUnit getDerivedUnit()
Returns the derived unit that is convertible with this unit.- Specified by:
getDerivedUnit
in interfaceDerivableUnit
- Specified by:
getDerivedUnit
in interfaceUnit
- Returns:
- The derived unit that is convertible with this unit.
-
toDerivedUnit
public float toDerivedUnit(float amount) throws ConversionException
Converts a value in this unit to the equivalent value in the convertible derived unit.- Specified by:
toDerivedUnit
in interfaceDerivableUnit
- Parameters:
amount
- The value in this unit.- Returns:
- The equivalent value in the convertible derived unit.
- Throws:
ConversionException
- Can't convert between units.
-
toDerivedUnit
public double toDerivedUnit(double amount) throws ConversionException
Converts a value in this unit to the equivalent value in the convertible derived unit.- Specified by:
toDerivedUnit
in interfaceDerivableUnit
- Parameters:
amount
- The value in this unit.- Returns:
- The equivalent value in the convertible derived unit.
- Throws:
ConversionException
- Can't convert between units.
-
toDerivedUnit
public float[] toDerivedUnit(float[] input, float[] output) throws ConversionException
Converts values in this unit to the equivalent values in the convertible derived unit.- Specified by:
toDerivedUnit
in interfaceDerivableUnit
- Parameters:
input
- The values in this unit.output
- The equivalent values in the convertible derived unit. May be the same array asinput
.- Returns:
output
.- Throws:
ConversionException
- Can't convert between units.
-
toDerivedUnit
public double[] toDerivedUnit(double[] input, double[] output) throws ConversionException
Converts values in this unit to the equivalent values in the convertible derived unit.- Specified by:
toDerivedUnit
in interfaceDerivableUnit
- Parameters:
input
- The values in this unit.output
- The equivalent values in the convertible derived unit. May be the same array asinput
.- Returns:
output
.- Throws:
ConversionException
- Can't convert between units.
-
fromDerivedUnit
public float fromDerivedUnit(float amount) throws ConversionException
Converts a value in the convertible derived unit to the equivalent value in this unit.- Specified by:
fromDerivedUnit
in interfaceDerivableUnit
- Parameters:
amount
- The value in the convertible derived unit.- Returns:
- The equivalent value in this unit.
- Throws:
ConversionException
- Can't convert between units.
-
fromDerivedUnit
public double fromDerivedUnit(double amount) throws ConversionException
Converts a value in the convertible derived unit to the equivalent value in this unit.- Specified by:
fromDerivedUnit
in interfaceDerivableUnit
- Parameters:
amount
- The value in the convertible derived unit.- Returns:
- The equivalent value in this unit.
- Throws:
ConversionException
- Can't convert between units.
-
fromDerivedUnit
public float[] fromDerivedUnit(float[] input, float[] output) throws ConversionException
Converts values in the convertible derived unit to the equivalent values in this unit.- Specified by:
fromDerivedUnit
in interfaceDerivableUnit
- Parameters:
input
- The values in the convertible derived unit.output
- The equivalent values in this unit. May be the same array asinput
.- Returns:
output
.- Throws:
ConversionException
- Can't convert between units.
-
fromDerivedUnit
public double[] fromDerivedUnit(double[] input, double[] output) throws ConversionException
Converts values in the convertible derived unit to the equivalent values in this unit.- Specified by:
fromDerivedUnit
in interfaceDerivableUnit
- Parameters:
input
- The values in the convertible derived unit.output
- The equivalent values in this unit. May be the same array asinput
.- Returns:
output
.- Throws:
ConversionException
- Can't convert between units.
-
equals
public boolean equals(Object object)
Indicates if this unit is semantically identical to an object.
-
hashCode
public int hashCode()
Returns the hash code of this instance.
-
isDimensionless
public boolean isDimensionless()
Indicates if this unit is dimensionless.- Specified by:
isDimensionless
in interfaceUnit
- Returns:
true
if and only if this unit is dimensionless.
-
toString
public String toString()
Returns the string representation of this unit.
-
getCanonicalString
public String getCanonicalString()
Returns the canonical string representation of the unit.- Specified by:
getCanonicalString
in interfaceUnit
- Returns:
- The canonical string representation.
-
-