Package edu.wpi.first.units
Class Unit
java.lang.Object
edu.wpi.first.units.Unit
- Direct Known Subclasses:
AngleUnit
,CurrentUnit
,DimensionlessUnit
,DistanceUnit
,EnergyUnit
,MassUnit
,MultUnit
,PerUnit
,TemperatureUnit
,TimeUnit
,VoltageUnit
Unit of measurement that defines a quantity, such as grams, meters, or seconds.
This is the base class for units. Actual units (such as Units.Grams
and Units.Meters
) can be found in the Units
class.
-
Constructor Summary
ModifierConstructorDescriptionprotected
Creates a new unit with the given name and multiplier to the base unit.protected
Unit
(Unit baseUnit, UnaryFunction toBaseConverter, UnaryFunction fromBaseConverter, String name, String symbol) Creates a new unit defined by its relationship to some base unit. -
Method Summary
Modifier and TypeMethodDescriptionboolean
boolean
equivalent
(Unit other) Checks if this unit is equivalent to another one.double
fromBaseUnits
(double valueInBaseUnits) Converts a value in terms of base units to a value in terms of this unit.Gets the base unit of measurement that this unit is derived from.Gets the conversion function used to convert values to terms of this unit.Gets the conversion function used to convert values to base unit terms.int
hashCode()
boolean
Checks if this unit is the base unit for its own system of measurement.abstract MutableMeasure<?,
?, ?> mutable
(double initialMagnitude) Creates a new mutable measurement that is initialized to the given magnitude in terms of this unit.name()
Gets the name of this unit.abstract Measure<?>
of
(double magnitude) Creates a new immutable measurement of the given magnitude in terms of this unit.abstract Measure<?>
ofBaseUnits
(double baseUnitMagnitude) Creates a new immutable measurement of the given magnitude in terms of this unit's base unit.Measure<?>
one()
Gets a measure with a magnitude of 1.0 in terms of this unit.abstract Unit
Combines this unit with a unit of time.symbol()
Gets the symbol of this unit.double
toBaseUnits
(double valueInNativeUnits) Converts a value in terms of this unit to a value in terms of the base unit.toString()
Measure<?>
zero()
Gets a measure of zero magnitude in terms of this unit.
-
Constructor Details
-
Unit
protected Unit(Unit baseUnit, UnaryFunction toBaseConverter, UnaryFunction fromBaseConverter, String name, String symbol) Creates a new unit defined by its relationship to some base unit.- Parameters:
baseUnit
- the base unit, e.g. Meters for distances. Set this tonull
if the unit being constructed is its own base unittoBaseConverter
- a function for converting units of this type to the base unitfromBaseConverter
- a function for converting units of the base unit to this onename
- the name of the unit. This should be a singular noun (so "Meter", not "Meters")symbol
- the short symbol for the unit, such as "m" for meters or "lb." for pounds
-
Unit
Creates a new unit with the given name and multiplier to the base unit.- Parameters:
baseUnit
- the base unit, e.g. Meters for distancesbaseUnitEquivalent
- the multiplier to convert this unit to the base unit of this type. For example, meters has a multiplier of 1, mm has a multiplier of 1e3, and km has multiplier of 1e-3.name
- the name of the unit. This should be a singular noun (so "Meter", not "Meters")symbol
- the short symbol for the unit, such as "m" for meters or "lb." for pounds
-
-
Method Details
-
of
Creates a new immutable measurement of the given magnitude in terms of this unit. Implementations are strongly recommended to sharpen the return type to a unit-specific measurement implementation.- Parameters:
magnitude
- the magnitude of the measurement.- Returns:
- the measurement object
-
ofBaseUnits
Creates a new immutable measurement of the given magnitude in terms of this unit's base unit. Implementations are strongly recommended to sharpen the return type to a unit-specific measurement implementation.- Parameters:
baseUnitMagnitude
- the magnitude in terms of the base unit- Returns:
- the measurement object
-
mutable
Creates a new mutable measurement that is initialized to the given magnitude in terms of this unit. Implementations are strongly recommended to sharpen the return type to a unit-specific measurement implementation.- Parameters:
initialMagnitude
- the initial magnitude of the mutable measurement- Returns:
- the mutable measurement object
-
zero
Gets a measure of zero magnitude in terms of this unit. The returned object is guaranteed to be of the same type returned byof(double)
. Subclasses are encouraged to override this method to sharpen the return type.- Returns:
- a zero-magnitude measure of this unit
-
one
Gets a measure with a magnitude of 1.0 in terms of this unit. The returned object is guaranteed to be of the same type returned byof(double)
. Subclasses are encouraged to override this method to sharpen the return type.- Returns:
- a measure of magnitude 1.0 in terms of this unit
-
per
Combines this unit with a unit of time. This often - but not always - results in a velocity. Subclasses should sharpen the return type to be unit-specific.- Parameters:
time
- the unit of time- Returns:
- the combined unit
-
getBaseUnit
Gets the base unit of measurement that this unit is derived from. If the unit is the base unit, the unit will be returned.NOTE: Subclasses must override this method to provide the correct return type. Failing to do say will make unit combinations that use it break at runtime!
Unit baseUnit = new Unit(null, ...); baseUnit.getBaseUnit(); // returns baseUnit Unit derivedUnit = new Unit(baseUnit, ...); derivedUnit.getBaseUnit(); // returns baseUnit
- Returns:
- the base unit
-
isBaseUnit
Checks if this unit is the base unit for its own system of measurement.- Returns:
- true if this is the base unit, false if not
-
fromBaseUnits
Converts a value in terms of base units to a value in terms of this unit.- Parameters:
valueInBaseUnits
- the value in base units to convert- Returns:
- the equivalent value in terms of this unit
-
toBaseUnits
Converts a value in terms of this unit to a value in terms of the base unit.- Parameters:
valueInNativeUnits
- the value in terms of this unit to convert- Returns:
- the equivalent value in terms of the base unit
-
getConverterToBase
Gets the conversion function used to convert values to base unit terms. This generally shouldn't need to be used directly; prefertoBaseUnits(double)
instead.- Returns:
- the conversion function
-
getConverterFromBase
Gets the conversion function used to convert values to terms of this unit. This generally shouldn't need to be used directly; preferfromBaseUnits(double)
instead.- Returns:
- the conversion function
-
equivalent
Checks if this unit is equivalent to another one. Equivalence is determined by both units having the same base type and treat the same base unit magnitude as the same magnitude in their own units, to withinMeasure.EQUIVALENCE_THRESHOLD
.- Parameters:
other
- the unit to compare to.- Returns:
- true if both units are equivalent, false if not
-
equals
-
hashCode
-
name
Gets the name of this unit.- Returns:
- the unit's name
-
symbol
Gets the symbol of this unit.- Returns:
- the unit's symbol
-
toString
-