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
public class Unit<U extends Unit<U>> extends Object
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
Constructors Modifier Constructor Description protected
Unit(Class<? extends U> baseType, double baseUnitEquivalent, String name, String symbol)
Creates a new unit with the given name and multiplier to the base unit.protected
Unit(Class<? extends U> baseType, UnaryFunction toBaseConverter, UnaryFunction fromBaseConverter, String name, String symbol)
Creates a new unit defined by its relationship to some base unit. -
Method Summary
Modifier and Type Method Description double
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
equals(Object o)
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.UnaryFunction
getConverterFromBase()
Gets the conversion function used to convert values to terms of this unit.UnaryFunction
getConverterToBase()
Gets the conversion function used to convert values to base unit terms.int
hashCode()
<U2 extends Unit<U2>>
Mult<U,U2>mult(U2 other)
Takes this unit and creates a new combinatory unit equivalent to this unit multiplied by another.String
name()
Gets the name of this unit.Measure<U>
of(double magnitude)
Creates a new measure of this unit with the given value.Measure<U>
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.Measure<U>
one()
Gets a measure with a magnitude of 1 in terms of this unit.<D extends Unit<D>>
Per<U,D>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.Velocity<U>
per(Time period)
Creates a velocity unit derived from this one.String
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.String
toString()
Measure<U>
zero()
Gets a measure with a magnitude of 0 in terms of this unit.
-
Constructor Details
-
Unit
protected Unit(Class<? extends U> baseType, UnaryFunction toBaseConverter, UnaryFunction fromBaseConverter, String name, String symbol)Creates a new unit defined by its relationship to some base unit.- Parameters:
baseType
- the base type of the unit, e.g. Distance.class for the distance 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:
baseType
- the base type of the unit, e.g. Distance.class for the distance unitbaseUnitEquivalent
- 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
-
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
-