Class Unit<Q extends Quantity>
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
BaseUnit
,DerivedUnit
This class represents a determinate quantity
(as of length, time, heat, or value) adopted as a standard
of measurement.
It is helpful to think of instances of this class as recording the
history by which they are created. Thus, for example, the string
"g/kg" (which is a dimensionless unit) would result from invoking
the method toString() on a unit that was created by dividing a
gram unit by a kilogram unit. Yet, "kg" divided by "kg" returns
ONE
and not "kg/kg" due to automatic unit factorization.
This class supports the multiplication of offsets units. The result is
usually a unit not convertible to its standard unit
.
Such units may appear in derivative quantities. For example °C/m is an
unit of gradient, which is common in atmospheric and oceanographic
research.
Units raised at rational powers are also supported. For example the cubic root of "liter" is a unit compatible with meter.
Instances of this class are immutable.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final Unit<Dimensionless>
Holds the dimensionless unitONE
. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal <A extends Quantity>
AlternateUnit<A>Returns a unit equivalent to this unit but used in expressions to distinguish between quantities of a different nature but of the same dimensions.Casts this unit to a parameterized unit of specified nature or throw aClassCastException
if the dimension of the specified quantity and this unit's dimension do not match.final CompoundUnit<Q>
Returns the combination of this unit with the specified sub-unit.divide
(double divisor) Returns the result of dividing this unit by an approximate divisor.divide
(long divisor) Returns the result of dividing this unit by an exact divisor.Returns the quotient of this unit with the one specified.abstract boolean
Indicates if the specified unit can be considered equals to the one specified.final UnitConverter
getConverterTo
(Unit<?> that) Returns a converter of numeric values from this unit to another unit.final Dimension
Returns the dimension of this unit (depends upon the current dimensionalmodel
).Returns thebase unit
,alternate unit
or product of base units and alternate units this unit is derived from.abstract int
hashCode()
Returns the hash code for this unit.inverse()
Returns the inverse of this unit.final boolean
isCompatible
(Unit<?> that) Indicates if this unit is compatible with the unit specified.boolean
Indicates if this unit is a standard unit (base units and alternate units are standard units).plus
(double offset) Returns the result of adding an offset to this unit.pow
(int n) Returns a unit equals to this unit raised to an exponent.root
(int n) Returns a unit equals to the given root of this unit.times
(double factor) Returns the result of multiplying this unit by a an approximate factortimes
(long factor) Returns the result of multiplying this unit by an exact factor.Returns the product of this unit with the one specified.abstract UnitConverter
Returns the converter from this unit to its system unit.final String
toString()
Returns the standardString
representation of this unit.transform
(UnitConverter operation) Returns the unit derived from this unit using the specified converter.valueOf
(CharSequence csq) Returns a unit instance that is defined from the specified character sequence using thestandard unit format
.
-
Field Details
-
ONE
Holds the dimensionless unitONE
.
-
-
Constructor Details
-
Unit
protected Unit()Default constructor.
-
-
Method Details
-
getStandardUnit
Returns thebase unit
,alternate unit
or product of base units and alternate units this unit is derived from. The standard unit identifies the "type" ofquantity
for which this unit is employed. For example:[code] boolean isAngularVelocity(Unit<?> u) { return u.getStandardUnit().equals(RADIAN.divide(SECOND)); } assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); [/code]Note: Having the same system unit is not sufficient to ensure that a converter exists between the two units (e.g. °C/m and K/m).
- Returns:
- the system unit this unit is derived from.
-
toStandardUnit
Returns the converter from this unit to its system unit.- Returns:
this.getConverterTo(this.getSystemUnit())
-
hashCode
public abstract int hashCode()Returns the hash code for this unit. -
equals
Indicates if the specified unit can be considered equals to the one specified. -
isStandardUnit
public boolean isStandardUnit()Indicates if this unit is a standard unit (base units and alternate units are standard units). The standard unit identifies the "type" ofquantity
for which the unit is employed.- Returns:
getStandardUnit().equals(this)
-
isCompatible
Indicates if this unit is compatible with the unit specified. Units don't need to be equals to be compatible. For example:[code] RADIAN.equals(ONE) == false RADIAN.isCompatible(ONE) == true [/code]- Parameters:
that
- the other unit.- Returns:
this.getDimension().equals(that.getDimension())
- See Also:
-
asType
Casts this unit to a parameterized unit of specified nature or throw aClassCastException
if the dimension of the specified quantity and this unit's dimension do not match. For example:[code] UnitLIGHT_YEAR = NonSI.C.times(NonSI.YEAR).asType(Length.class); [/code] - Parameters:
type
- the quantity class identifying the nature of the unit.- Returns:
- this unit parameterized with the specified type.
- Throws:
ClassCastException
- if the dimension of this unit is different from the specified quantity dimension.UnsupportedOperationException
- if the specified quantity class does not have a public static field named "UNIT" holding the standard unit for the quantity.
-
getDimension
Returns the dimension of this unit (depends upon the current dimensionalmodel
).- Returns:
- the dimension of this unit for the current model.
-
getConverterTo
Returns a converter of numeric values from this unit to another unit.- Parameters:
that
- the unit to which to convert the numeric values.- Returns:
- the converter from this unit to
that
unit. - Throws:
ConversionException
- if the conveter cannot be constructed (e.g.!this.isCompatible(that)
).
-
alternate
Returns a unit equivalent to this unit but used in expressions to distinguish between quantities of a different nature but of the same dimensions.Examples of alternate units:[code] Unit
RADIAN = ONE.alternate("rad"); Unit NEWTON = METER.times(KILOGRAM).divide(SECOND.pow(2)).alternate("N"); Unit PASCAL = NEWTON.divide(METER.pow(2)).alternate("Pa"); [/code] - Parameters:
symbol
- the new symbol for the alternate unit.- Returns:
- the alternate unit.
- Throws:
UnsupportedOperationException
- if this unit is not a standard unit.IllegalArgumentException
- if the specified symbol is already associated to a different unit.
-
compound
Returns the combination of this unit with the specified sub-unit. Compound units are typically used for formatting purpose. Examples of compound units:[code] HOUR_MINUTE = NonSI.HOUR.compound(NonSI.MINUTE); DEGREE_MINUTE_SECOND_ANGLE = NonSI.DEGREE_ANGLE.compound( NonSI.DEGREE_MINUTE).compound(NonSI.SECOND_ANGLE); [/code]- Parameters:
subunit
- the sub-unit to combine with this unit.- Returns:
- the corresponding compound unit.
-
transform
Returns the unit derived from this unit using the specified converter. The converter does not need to be linear. For example:[code] UnitDECIBEL = Unit.ONE.transform( new LogConverter(10).inverse().concatenate( new RationalConverter(1, 10)));[/code] - Parameters:
operation
- the converter from the transformed unit to this unit.- Returns:
- the unit after the specified transformation.
-
plus
Returns the result of adding an offset to this unit. The returned unit is convertible with all units that are convertible with this unit.- Parameters:
offset
- the offset added (expressed in this unit, e.g.CELSIUS = KELVIN.plus(273.15)
).- Returns:
this.transform(new AddConverter(offset))
-
times
Returns the result of multiplying this unit by an exact factor.- Parameters:
factor
- the exact scale factor (e.g.KILOMETER = METER.times(1000)
).- Returns:
this.transform(new RationalConverter(factor, 1))
-
times
Returns the result of multiplying this unit by a an approximate factor- Parameters:
factor
- the approximate factor (e.g.ELECTRON_MASS = KILOGRAM.times(9.10938188e-31)
).- Returns:
this.transform(new MultiplyConverter(factor))
-
times
Returns the product of this unit with the one specified.- Parameters:
that
- the unit multiplicand.- Returns:
this * that
-
inverse
Returns the inverse of this unit.- Returns:
1 / this
-
divide
Returns the result of dividing this unit by an exact divisor.- Parameters:
divisor
- the exact divisor. (e.g.QUART = GALLON_LIQUID_US.divide(4)
).- Returns:
this.transform(new RationalConverter(1 , divisor))
-
divide
Returns the result of dividing this unit by an approximate divisor.- Parameters:
divisor
- the approximate divisor.- Returns:
this.transform(new MultiplyConverter(1.0 / divisor))
-
divide
Returns the quotient of this unit with the one specified.- Parameters:
that
- the unit divisor.- Returns:
this / that
-
root
Returns a unit equals to the given root of this unit.- Parameters:
n
- the root's order.- Returns:
- the result of taking the given root of this unit.
- Throws:
ArithmeticException
- ifn == 0
.
-
pow
Returns a unit equals to this unit raised to an exponent.- Parameters:
n
- the exponent.- Returns:
- the result of raising this unit to the exponent.
-
valueOf
Returns a unit instance that is defined from the specified character sequence using thestandard unit format
.Examples of valid entries (all for meters per second squared) are:
- m*s-2
- m/s²
- m·s-²
- m*s**-2
- m^+1 s^-2
- Parameters:
csq
- the character sequence to parse.- Returns:
UnitFormat.getStandardInstance().parse(csq, new ParsePosition(0))
- Throws:
IllegalArgumentException
- if the specified character sequence cannot be correctly parsed (e.g. symbol unknown).
-
toString
Returns the standardString
representation of this unit. This representation is not affected by locale. Locale-sensitive unit formatting and parsing is handled by theMeasureFormat
class and its subclasses.
-