Class AbstractUnit<Q extends Quantity<Q>>
- java.lang.Object
-
- tech.units.indriya.AbstractUnit<Q>
-
- All Implemented Interfaces:
Serializable,Comparable<Unit<Q>>,Unit<Q>,tech.uom.lib.common.function.Nameable,tech.uom.lib.common.function.PrefixOperator<Q>,tech.uom.lib.common.function.SymbolSupplier
- Direct Known Subclasses:
AlternateUnit,AnnotatedUnit,BaseUnit,ProductUnit,TransformedUnit
public abstract class AbstractUnit<Q extends Quantity<Q>> extends Object implements Unit<Q>, Comparable<Unit<Q>>, tech.uom.lib.common.function.PrefixOperator<Q>, tech.uom.lib.common.function.Nameable, Serializable, tech.uom.lib.common.function.SymbolSupplier
The class represents units founded on the seven SI base units for seven base quantities assumed to be mutually independent.
For all physics units, unit conversions are symmetrical:
u1.getConverterTo(u2).equals(u2.getConverterTo(u1).inverse()). Non-physical units (e.g. currency units) for which conversion is not symmetrical should have their own separate class hierarchy and are considered distinct (e.g. financial units), although they can always be combined with physics units (e.g. "€/Kg", "$/h").- Since:
- 1.0
- Version:
- 4.1, October 4, 2024
- Author:
- Jean-Marie Dautelle, Werner Keil
- See Also:
- Wikipedia: International System of Units, Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAbstractUnit.EqualizerUtility class for number comparison and equality
-
Field Summary
Fields Modifier and Type Field Description static Unit<Dimensionless>ONEHolds the dimensionless unitONE.protected Quantity.ScalescaleHolds the measurement scaleprotected static Map<String,Unit<?>>SYMBOL_TO_UNITHolds the unique symbols collection (base units or alternate units).
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractUnit()Default constructor.protectedAbstractUnit(String symbol)Constructor setting a symbol.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description Unit<Q>alternate(String newSymbol)Unit<Q>annotate(String annotation)Annotates the specified unit.<T extends Quantity<T>>
Unit<T>asType(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 (regardless whether or not the dimensions are independent or not).intcompareTo(Unit<Q> that)Compares this unit to the specified unit.Unit<Q>divide(double divisor)Unit<Q>divide(Number divisor)Returns the result of dividing this unit by the specified divisor.Unit<?>divide(Unit<?> that)Returns the quotient of this unit with the one specified.abstract booleanequals(Object obj)protected TypegetActualType()abstract Map<? extends Unit<?>,Integer>getBaseUnits()UnitConvertergetConverterTo(Unit<Q> that)UnitConvertergetConverterTo(Unit<Q> that, Quantity.Scale scale)Returns a converter of numeric values from this unit to another unit of same type.UnitConvertergetConverterToAny(Unit<?> that)UnitConvertergetConverterToAny(Unit<?> that, Quantity.Scale scale)Returns a converter from this unit to the specified unit of type unknown in the given scale.abstract DimensiongetDimension()StringgetName()StringgetSymbol()abstract UnitConvertergetSystemConverter()Returns the converter from this unit to its unscaledSystem Unitunit.Unit<Q>getSystemUnit()Returns the system unit (unscaled SI unit) from which this unit is derived.abstract inthashCode()protected UnitConverterinternalGetConverterTo(Unit<Q> that, boolean useEquals)Unit<?>inverse()Returns the inverse of this physical unit.booleanisCompatible(Unit<?> that)Indicates if this unit is compatible with the unit specified.booleanisEquivalentTo(Unit<Q> that)booleanisSystemUnit()Indicates if this unit belongs to the set of coherent SI units (unscaled SI units).Unit<Q>multiply(double multiplier)Unit<Q>multiply(Number factor)Unit<?>multiply(Unit<?> that)Returns the product of this physical unit with the one specified.static Unit<?>parse(CharSequence charSequence)Returns the abstract unit represented by the specified characters as per default format.Unit<?>pow(int n)Returns a unit equals to this unit raised to an exponent.Unit<Q>prefix(Prefix prefix)Unit<?>root(int n)Returns a unit equals to the given root of this unit.protected voidsetName(String name)protected voidsetSymbol(String s)Unit<Q>shift(double offset)Unit<Q>shift(Number offset)StringtoString()Returns the standard representation of this physics unit.protected abstract Unit<Q>toSystemUnit()Returns the unscaledSIunit from which this unit is derived.Unit<Q>transform(UnitConverter operation)
-
-
-
Field Detail
-
ONE
public static final Unit<Dimensionless> ONE
Holds the dimensionless unitONE.
-
scale
protected Quantity.Scale scale
Holds the measurement scale
-
SYMBOL_TO_UNIT
protected static final transient Map<String,Unit<?>> SYMBOL_TO_UNIT
Holds the unique symbols collection (base units or alternate units).
-
-
Constructor Detail
-
AbstractUnit
protected AbstractUnit()
Default constructor.
-
AbstractUnit
protected AbstractUnit(String symbol)
Constructor setting a symbol.- Parameters:
symbol- the unit symbol.
-
-
Method Detail
-
getActualType
protected Type getActualType()
-
isSystemUnit
public boolean isSystemUnit()
Indicates if this unit belongs to the set of coherent SI units (unscaled SI units). The base and coherent derived units of the SI form a coherent set, designated the set of coherent SI units. The word coherent is used here in the following sense: when coherent units are used, equations between the numerical values of quantities take exactly the same form as the equations between the quantities themselves. Thus if only units from a coherent set are used, conversion factors between units are never required.- Returns:
equals(toSystemUnit())
-
getSystemConverter
public abstract UnitConverter getSystemConverter()
Returns the converter from this unit to its unscaledSystem Unitunit.- Returns:
getConverterTo(this.toSystemUnit())- See Also:
toSystemUnit()
-
toSystemUnit
protected abstract Unit<Q> toSystemUnit()
Returns the unscaledSIunit from which this unit is derived. The SI unit can be be used to identify a quantity given the unit. For example:static boolean isAngularVelocity(AbstractUnit> unit) { return unit.toSystemUnit().equals(RADIAN.divide(SECOND)); } assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); // Returns true.- Returns:
- the unscaled metric unit from which this unit is derived.
-
annotate
public final Unit<Q> annotate(String annotation)
Annotates the specified unit. Annotation does not change the unit semantic. Annotations are often written between curly braces behind units. For example:
UnitNote: Annotation of system units are not considered themselves as system units.PERCENT_VOL = ((AbstractUnit)Units.PERCENT).annotate("vol"); // "%{vol}" Unit KG_TOTAL = ((AbstractUnit)Units.KILOGRAM).annotate("total"); // "kg{total}" Unit RED_BLOOD_CELLS = ((AbstractUnit)Units.ONE).annotate("RBC"); // "{RBC}" - Parameters:
annotation- the unit annotation.- Returns:
- the annotated unit.
-
parse
public static Unit<?> parse(CharSequence charSequence)
Returns the abstract unit represented by the specified characters as per default format. Locale-sensitive unit parsing could be handled usingLocalUnitFormatin subclasses of AbstractUnit.Note: The standard format supports dimensionless units.
AbstractUnitPERCENT = AbstractUnit.parse("100").inverse().asType(Dimensionless.class); - Parameters:
charSequence- the character sequence to parse.- Returns:
SimpleUnitFormat.getInstance().parse(csq)- Throws:
MeasurementParseException- if the specified character sequence cannot be correctly parsed (e.g. not UCUM compliant).
-
toString
public String toString()
Returns the standard representation of this physics unit. The string produced for a given unit is always the same; it is not affected by the locale. It can be used as a canonical string representation for exchanging units, or as a key for a Hashtable, etc. Locale-sensitive unit parsing could be handled usingLocalUnitFormatin subclasses of AbstractUnit.
-
getSystemUnit
public final Unit<Q> getSystemUnit()
Returns the system unit (unscaled SI unit) from which this unit is derived. They can be be used to identify a quantity given the unit. For example:
static boolean isAngularVelocity(AbstractUnit> unit) {
return unit.getSystemUnit().equals(RADIAN.divide(SECOND));
}
assert(REVOLUTION.divide(MINUTE).isAngularVelocity()); // Returns true.- Specified by:
getSystemUnitin interfaceUnit<Q extends Quantity<Q>>- Returns:
- the unscaled metric unit from which this unit is derived.
-
isCompatible
public final boolean isCompatible(Unit<?> that)
Indicates if this unit is compatible with the unit specified. To be compatible both units must be physics units having the same fundamental dimension.- Specified by:
isCompatiblein interfaceUnit<Q extends Quantity<Q>>- Parameters:
that- the other unit.- Returns:
trueif this unit and that unit have the same fundamental dimension according to the current dimensional model;falseotherwise.
-
asType
public final <T extends Quantity<T>> Unit<T> asType(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 (regardless whether or not the dimensions are independent or not).- Specified by:
asTypein interfaceUnit<Q extends Quantity<Q>>- Parameters:
type- the quantity class identifying the nature of the unit.- Throws:
ClassCastException- if the dimension of this unit is different from the SI dimension of the specified type.- See Also:
AbstractSystemOfUnits.getUnit(Class)
-
getBaseUnits
public abstract Map<? extends Unit<?>,Integer> getBaseUnits()
- Specified by:
getBaseUnitsin interfaceUnit<Q extends Quantity<Q>>
-
getDimension
public abstract Dimension getDimension()
- Specified by:
getDimensionin interfaceUnit<Q extends Quantity<Q>>
-
getConverterTo
public final UnitConverter getConverterTo(Unit<Q> that) throws UnconvertibleException
- Specified by:
getConverterToin interfaceUnit<Q extends Quantity<Q>>- Throws:
UnconvertibleException
-
getConverterToAny
public final UnitConverter getConverterToAny(Unit<?> that) throws IncommensurableException, UnconvertibleException
- Specified by:
getConverterToAnyin interfaceUnit<Q extends Quantity<Q>>- Throws:
IncommensurableExceptionUnconvertibleException
-
getConverterTo
@API(status=EXPERIMENTAL) public final UnitConverter getConverterTo(Unit<Q> that, Quantity.Scale scale) throws UnconvertibleException
Returns a converter of numeric values from this unit to another unit of same type. This method performs the same work asgetConverterToAny(Unit)without raising checked exception.- Parameters:
that- the unit of same type to which to convert the numeric values.scale- the measurement scale.- Returns:
- the converter from this unit to
thatunit in the givenscale. - Throws:
UnconvertibleException- if a converter cannot be constructed.- See Also:
getConverterToAny(Unit)
-
getConverterToAny
@API(status=EXPERIMENTAL) public final UnitConverter getConverterToAny(Unit<?> that, Quantity.Scale scale) throws IncommensurableException, UnconvertibleException
Returns a converter from this unit to the specified unit of type unknown in the given scale. This method can be used when the quantity type of the specified unit is unknown at compile-time or when dimensional analysis allows for conversion between units of different type.To convert to a unit having the same parameterized type,
getConverterTo(Unit, Scale)is preferred (no checked exception raised).- Parameters:
that- the unit to which to convert the numeric values.scale- the measurement scale.- Returns:
- the converter from
thisunit tothatunit using the givenscale. - Throws:
IncommensurableException- if this unit is not compatible withthat.UnconvertibleException- if a converter cannot be constructed.- See Also:
getConverterTo(Unit),isCompatible(Unit)
-
transform
public final Unit<Q> transform(UnitConverter operation)
-
internalGetConverterTo
protected final UnitConverter internalGetConverterTo(Unit<Q> that, boolean useEquals) throws UnconvertibleException
- Throws:
UnconvertibleException
-
multiply
public final Unit<?> multiply(Unit<?> that)
Returns the product of this physical unit with the one specified.
-
divide
public final Unit<Q> divide(Number divisor)
Returns the result of dividing this unit by the specified divisor. If the factor is an integer value, the division is exact. For example:QUART = GALLON_LIQUID_US.divide(4); // Exact definition.
-
divide
public final Unit<?> divide(Unit<?> that)
Returns the quotient of this unit with the one specified.
-
compareTo
public int compareTo(Unit<Q> that)
Compares this unit to the specified unit. The default implementation compares the name and symbol of both this unit and the specified unit, giving precedence to the symbol.- Specified by:
compareToin interfaceComparable<Q extends Quantity<Q>>- Returns:
- a negative integer, zero, or a positive integer as this unit is less than, equal to, or greater than the specified unit.
-
isEquivalentTo
public boolean isEquivalentTo(Unit<Q> that)
- Specified by:
isEquivalentToin interfaceUnit<Q extends Quantity<Q>>
-
-