public abstract class Unit<Q extends Quantity> extends java.lang.Object implements java.io.Serializable
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.
Modifier and Type | Field and Description |
---|---|
static Unit<Dimensionless> |
ONE
Holds the dimensionless unit
ONE . |
Modifier and Type | Method and Description |
---|---|
<A extends Quantity> |
alternate(java.lang.String symbol)
Returns a unit equivalent to this unit but used in expressions to
distinguish between quantities of a different nature but of the same
dimensions.
|
<T extends Quantity> |
asType(java.lang.Class<T> type)
Casts this unit to a parameterized unit of specified nature or
throw a
ClassCastException if the dimension of the
specified quantity and this unit's dimension do not match. |
CompoundUnit<Q> |
compound(Unit<Q> subunit)
Returns the combination of this unit with the specified sub-unit.
|
Unit<Q> |
divide(double divisor)
Returns the result of dividing this unit by an approximate divisor.
|
Unit<Q> |
divide(long divisor)
Returns the result of dividing this unit by an exact divisor.
|
Unit<? extends Quantity> |
divide(Unit<?> that)
Returns the quotient of this unit with the one specified.
|
abstract boolean |
equals(java.lang.Object that)
Indicates if the specified unit can be considered equals to
the one specified.
|
UnitConverter |
getConverterTo(Unit<?> that)
Returns a converter of numeric values from this unit to another unit.
|
Dimension |
getDimension()
Returns the dimension of this unit (depends upon the current
dimensional
model ). |
abstract Unit<? super Q> |
getStandardUnit()
Returns the
base 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.
|
Unit<? extends Quantity> |
inverse()
Returns the inverse of this unit.
|
boolean |
isCompatible(Unit<?> that)
Indicates if this unit is compatible with the unit specified.
|
boolean |
isStandardUnit()
Indicates if this unit is a standard unit (base units and
alternate units are standard units).
|
Unit<Q> |
plus(double offset)
Returns the result of adding an offset to this unit.
|
Unit<? extends Quantity> |
pow(int n)
Returns a unit equals to this unit raised to an exponent.
|
Unit<? extends Quantity> |
root(int n)
Returns a unit equals to the given root of this unit.
|
Unit<Q> |
times(double factor)
Returns the result of multiplying this unit by a an approximate factor
|
Unit<Q> |
times(long factor)
Returns the result of multiplying this unit by an exact factor.
|
Unit<? extends Quantity> |
times(Unit<?> that)
Returns the product of this unit with the one specified.
|
abstract UnitConverter |
toStandardUnit()
Returns the converter from this unit to its system unit.
|
java.lang.String |
toString()
Returns the standard
String representation of this unit. |
Unit<Q> |
transform(UnitConverter operation)
Returns the unit derived from this unit using the specified converter.
|
static Unit<? extends Quantity> |
valueOf(java.lang.CharSequence csq)
Returns a unit instance that is defined from the specified
character sequence using the
standard unit format . |
public static final Unit<Dimensionless> ONE
ONE
.public abstract Unit<? super Q> getStandardUnit()
base unit
, alternate
unit
or product of base units and alternate units this unit is derived
from. The standard unit identifies the "type" of
quantity
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).
public abstract UnitConverter toStandardUnit()
this.getConverterTo(this.getSystemUnit())
public abstract int hashCode()
hashCode
in class java.lang.Object
public abstract boolean equals(java.lang.Object that)
equals
in class java.lang.Object
that
- the object to compare to.true
if this unit is considered equal to
that unit; false
otherwise.public boolean isStandardUnit()
quantity
for
which the unit is employed.getStandardUnit().equals(this)
public final boolean isCompatible(Unit<?> that)
that
- the other unit.this.getDimension().equals(that.getDimension())
getDimension()
public final <T extends Quantity> Unit<T> asType(java.lang.Class<T> type) throws java.lang.ClassCastException
ClassCastException
if the dimension of the
specified quantity and this unit's dimension do not match.
For example:[code]
Unittype
- the quantity class identifying the nature of the unit.java.lang.ClassCastException
- if the dimension of this unit is different
from the specified quantity dimension.java.lang.UnsupportedOperationException
- if the specified quantity class
does not have a public static field named "UNIT" holding the
standard unit for the quantity.public final Dimension getDimension()
model
).public final UnitConverter getConverterTo(Unit<?> that) throws ConversionException
that
- the unit to which to convert the numeric values.that
unit.ConversionException
- if the conveter cannot be constructed
(e.g. !this.isCompatible(that)
).public final <A extends Quantity> AlternateUnit<A> alternate(java.lang.String symbol)
Examples of alternate units:[code]
Unit
symbol
- the new symbol for the alternate unit.java.lang.UnsupportedOperationException
- if this unit is not a standard unit.java.lang.IllegalArgumentException
- if the specified symbol is already
associated to a different unit.public final CompoundUnit<Q> compound(Unit<Q> subunit)
subunit
- the sub-unit to combine with this unit.public final Unit<Q> transform(UnitConverter operation)
operation
- the converter from the transformed unit to this unit.public final Unit<Q> plus(double offset)
offset
- the offset added (expressed in this unit,
e.g. CELSIUS = KELVIN.plus(273.15)
).this.transform(new AddConverter(offset))
public final Unit<Q> times(long factor)
factor
- the exact scale factor
(e.g. KILOMETER = METER.times(1000)
).this.transform(new RationalConverter(factor, 1))
public final Unit<Q> times(double factor)
factor
- the approximate factor (e.g.
ELECTRON_MASS = KILOGRAM.times(9.10938188e-31)
).this.transform(new MultiplyConverter(factor))
public final Unit<? extends Quantity> times(Unit<?> that)
that
- the unit multiplicand.this * that
public final Unit<? extends Quantity> inverse()
1 / this
public final Unit<Q> divide(long divisor)
divisor
- the exact divisor.
(e.g. QUART = GALLON_LIQUID_US.divide(4)
).this.transform(new RationalConverter(1 , divisor))
public final Unit<Q> divide(double divisor)
divisor
- the approximate divisor.this.transform(new MultiplyConverter(1.0 / divisor))
public final Unit<? extends Quantity> divide(Unit<?> that)
that
- the unit divisor.this / that
public final Unit<? extends Quantity> root(int n)
n
- the root's order.java.lang.ArithmeticException
- if n == 0
.public final Unit<? extends Quantity> pow(int n)
n
- the exponent.public static Unit<? extends Quantity> valueOf(java.lang.CharSequence csq)
standard unit format
.
Examples of valid entries (all for meters per second squared) are:
csq
- the character sequence to parse.UnitFormat.getStandardInstance().parse(csq, new ParsePosition(0))
java.lang.IllegalArgumentException
- if the specified character sequence
cannot be correctly parsed (e.g. symbol unknown).public final java.lang.String toString()
String
representation of this unit.
This representation is not affected by locale. Locale-sensitive
unit formatting and parsing is handled by the MeasureFormat
class and its subclasses.toString
in class java.lang.Object
UnitFormat.getStandardInstance().format(this)