Package edu.wpi.first.units
Class Unit<U extends Unit<U>>
java.lang.Object
edu.wpi.first.units.Unit<U>
- Type Parameters:
U
- the self type, e.g.class SomeUnit extends Unit<SomeUnit>
- Direct Known Subclasses:
Angle
,Current
,Dimensionless
,Distance
,Energy
,Mass
,Mult
,Per
,Power
,Temperature
,Time
,Velocity
,Voltage
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
ConstructorsModifierConstructorDescriptionprotected
Creates a new unit with the given name and multiplier to the base unit.protected
Unit
(U 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 TypeMethodDescriptiondouble
convertFrom
(double magnitude, Unit<U> otherUnit) Converts a magnitude in terms of another unit of the same dimension to a magnitude in terms of this unit.boolean
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.mult
(U2 other) Takes this unit and creates a new combinatory unit equivalent to this unit multiplied by another.name()
Gets the name of this unit.of
(double magnitude) Creates a new measure of this unit with the given value.ofBaseUnits
(double baseUnitMagnitude) Creates a new measure with a magnitude equal to the given base unit magnitude, converted to be in terms of this unit.one()
Gets a measure with a magnitude of 1 in terms of this unit.per
(D denominator) Takes this unit and creates a new proportional unit where this unit is the numerator and the given denominator is the denominator.Creates a velocity unit derived from this one.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()
zero()
Gets a measure with a magnitude of 0 in terms of this unit.
-
Constructor Details
-
Unit
protected Unit(U 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
-
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.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
-
convertFrom
Converts a magnitude in terms of another unit of the same dimension to a magnitude in terms of this unit.Inches.convertFrom(12, Feet) // 144.0 Kilograms.convertFrom(2.2, Pounds) // 0.9979024
- Parameters:
magnitude
- a magnitude measured in another unitotherUnit
- the unit to convert the magnitude to- Returns:
- the corresponding value in terms of this 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
-
of
Creates a new measure of this unit with the given value. The resulting measure is immutable and cannot have its value modified.- Parameters:
magnitude
- the magnitude of the measure to create- Returns:
- the measure
-
ofBaseUnits
Creates a new measure with a magnitude equal to the given base unit magnitude, converted to be in terms of this unit.- Parameters:
baseUnitMagnitude
- the magnitude of the measure in terms of the base unit- Returns:
- the measure
-
zero
Gets a measure with a magnitude of 0 in terms of this unit.- Returns:
- the zero-valued measure
-
one
Gets a measure with a magnitude of 1 in terms of this unit.- Returns:
- the 1-valued measure
-
per
Creates a velocity unit derived from this one. Can be chained to denote velocity, acceleration, jerk, etc.Meters.per(Second) // linear velocity Kilograms.per(Second) // mass flow Feet.per(Second).per(Second).of(32) // roughly 1G of acceleration
- Parameters:
period
- the time period of the velocity, such as seconds or milliseconds- Returns:
- a velocity unit corresponding to the rate of change of this unit over time
-
per
Takes this unit and creates a new proportional unit where this unit is the numerator and the given denominator is the denominator.Volts.per(Meter) // V/m
- Type Parameters:
D
- the type of the denominator units- Parameters:
denominator
- the denominator of the proportional unit- Returns:
- a combined proportional unit
-
mult
Takes this unit and creates a new combinatory unit equivalent to this unit multiplied by another.Volts.mult(Meter) // V*m
- Type Parameters:
U2
- the type of the unit to multiply by- Parameters:
other
- the unit to multiply by- Returns:
- a combined unit equivalent to this unit multiplied by the other
-
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
-