Package edu.wpi.first.units
Class UnitBuilder<U extends Unit>
java.lang.Object
edu.wpi.first.units.UnitBuilder<U>
- Type Parameters:
U
- the type of the unit
Builder used for easily deriving new units from existing ones. When deriving a new unit, the base
unit class must redeclare the constructor
(U, UnaryFunction, UnaryFunction, String, String)
. The unit
builder class will invoke this constructor automatically and build the new unit. Alternatively,
new units can be derived by passing an explicit constructor function to make(UnitConstructorFunction)
.-
Nested Class Summary
Modifier and TypeClassDescriptionfinal class
Helper class used for safely chaining mapping builder calls.static interface
UnitBuilder.UnitConstructorFunction<U extends Unit>
A functional interface for constructing new units without relying on reflection. -
Constructor Summary
ConstructorDescriptionUnitBuilder
(U base) Creates a new unit builder object, building off of a base unit. -
Method Summary
Modifier and TypeMethodDescriptionaggregate
(double aggregation) Helper for defining units that are a scalar multiple of the base unit, such as kilometers being 1000x of the base unit (meters).fromBase
(UnaryFunction fromBase) Sets the conversion function to transform values in the base unit to values in the derived unit.make()
Creates the new unit based off of the builder methods called prior.make
(UnitBuilder.UnitConstructorFunction<U> constructor) Creates the new unit based off of the builder methods called prior, passing them to a provided constructor function.mappingInputRange
(double minBase, double maxBase) Defines a mapping for values within the given input range.Sets the name of the new unit.offset
(double offset) Sets the unit conversions based on a simple offset.splitInto
(double fraction) Helper for defining units that are a scalar fraction of the base unit, such as centimeters being 1/100th of the base unit (meters).Sets the symbol of the new unit.toBase
(UnaryFunction toBase) Sets the conversion function to transform values in the derived unit to values in the base unit.
-
Constructor Details
-
UnitBuilder
Creates a new unit builder object, building off of a base unit. The base unit does not have to be the base unit of its unit system; furlongs work just as well here as meters.- Parameters:
base
- the unit to base the new unit off of
-
-
Method Details
-
offset
Sets the unit conversions based on a simple offset. The new unit will have its values equal to (base value - offset).- Parameters:
offset
- the offset- Returns:
- this builder
-
mappingInputRange
Defines a mapping for values within the given input range. This method call should be immediately followed by.toOutputRange
, egmappingInputRange(1, 2).toOutputRange(3, 4)
, which will return the unit builder for continued chaining.- Parameters:
minBase
- the minimum input value (does not have to be absolute)maxBase
- the maximum output value (does not have to be absolute)- Returns:
- a builder object used to define the output range
-
fromBase
Sets the conversion function to transform values in the base unit to values in the derived unit.- Parameters:
fromBase
- the conversion function- Returns:
- the unit builder, for continued chaining
-
toBase
Sets the conversion function to transform values in the derived unit to values in the base unit.- Parameters:
toBase
- the conversion function- Returns:
- the unit builder, for continued chaining
-
named
Sets the name of the new unit.- Parameters:
name
- the new name- Returns:
- the unit builder, for continued chaining
-
symbol
Sets the symbol of the new unit.- Parameters:
symbol
- the new symbol- Returns:
- the unit builder, for continued chaining
-
splitInto
Helper for defining units that are a scalar fraction of the base unit, such as centimeters being 1/100th of the base unit (meters). The fraction value is specified as the denominator of the fraction, so a centimeter definition would usesplitInto(100)
instead ofsplitInto(1/100.0)
.- Parameters:
fraction
- the denominator portion of the fraction of the base unit that a value of 1 in the derived unit corresponds to- Returns:
- the unit builder, for continued chaining
-
aggregate
Helper for defining units that are a scalar multiple of the base unit, such as kilometers being 1000x of the base unit (meters).- Parameters:
aggregation
- the magnitude required for a measure in the base unit to equal a magnitude of 1 in the derived unit- Returns:
- the unit builder, for continued chaining
-
make
Creates the new unit based off of the builder methods called prior, passing them to a provided constructor function.- Parameters:
constructor
- the function to use to create the new derived unit- Returns:
- the new derived unit
- Throws:
NullPointerException
- if the unit conversions, unit name, or unit symbol were not set
-
make
Creates the new unit based off of the builder methods called prior.- Returns:
- the new derived unit
- Throws:
NullPointerException
- if the unit conversions, unit name, or unit symbol were not setRuntimeException
- if the base unit does not define a constructor accepting the conversion functions, unit name, and unit symbol - in that order
-