Interface Unit<Q extends Quantity<Q>>
- All Known Implementing Classes:
Currency, StandardUnit
Units define how quantities are measured and provide conversion between different units of the same dimension. Units are immutable and can be combined through multiplication and division to form derived units.
Example Usage:
- Since:
- 1.0
- Author:
- Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
-
Method Summary
Modifier and TypeMethodDescriptionCasts this unit to a specific quantity type.divide(double factor) Returns a unit equal to this unit divided by the specified factor.Unit<?> Returns the quotient of this unit by another unit.default UnitConvertergetConverterFrom(Unit<Q> sourceUnit) Returns a converter from the target unit to this unit.getConverterTo(Unit<Q> targetUnit) Returns a converter to convert values from this unit to the target unit.Returns the dimension of this unit.getName()Returns the human-readable name for this unit (e.g., "meter", "kilogram").Returns the symbol for this unit (e.g., "m", "kg", "s").default Unit<?> inverse()Returns the inverse of this unit.booleanChecks if this is a base SI unit (meter, kilogram, second, etc.).default booleanisCompatible(Unit<?> other) Checks if this unit is compatible with another unit.multiply(double factor) Returns a unit equal to this unit multiplied by the specified factor.Unit<?> Returns the product of this unit with another unit.Unit<?> pow(int exponent) Returns this unit raised to the specified power.Unit<?> sqrt()Returns the square root of this unit.
-
Method Details
-
getSymbol
String getSymbol()Returns the symbol for this unit (e.g., "m", "kg", "s").Symbols follow UCUM (Unified Code for Units of Measure) conventions where possible.
- Returns:
- the unit symbol
-
getName
String getName()Returns the human-readable name for this unit (e.g., "meter", "kilogram").- Returns:
- the unit name
-
getDimension
Dimension getDimension()Returns the dimension of this unit.The dimension describes the physical nature of the quantity (e.g., length, mass, time) as a combination of base SI dimensions.
- Returns:
- the dimension
-
getConverterTo
Returns a converter to convert values from this unit to the target unit.Both units must have compatible dimensions.
- Parameters:
targetUnit- the target unit- Returns:
- a converter from this unit to the target
- Throws:
IllegalArgumentException- if units are incompatible
-
getConverterFrom
Returns a converter from the target unit to this unit.This is the inverse of
getConverterTo(Unit).- Parameters:
sourceUnit- the source unit- Returns:
- a converter from the source unit to this unit
-
multiply
-
divide
-
multiply
-
divide
-
pow
Returns this unit raised to the specified power.For example,
METER.pow(2)gives square meters (area).- Parameters:
exponent- the exponent- Returns:
- the unit raised to the power
-
asType
Casts this unit to a specific quantity type.This is primarily used for type safety when creating derived units. For example, casting J/K to Entropy type.
- Type Parameters:
R- the target quantity type- Parameters:
type- the target quantity class- Returns:
- this unit cast to the target type
-
inverse
Returns the inverse of this unit.For example, the inverse of Time is Frequency (1/s = Hz).
- Returns:
- the inverse unit
-
sqrt
Unit<?> sqrt()Returns the square root of this unit.The dimension exponents must all be even.
- Returns:
- the square root unit
- Throws:
IllegalArgumentException- if square root is undefined
-
isCompatible
Checks if this unit is compatible with another unit.Units are compatible if they have the same dimension, allowing conversion between them.
- Parameters:
other- the other unit- Returns:
- true if units are compatible
-
isBaseUnit
boolean isBaseUnit()Checks if this is a base SI unit (meter, kilogram, second, etc.).- Returns:
- true if this is a base unit
-