Package edu.wpi.first.units
Class UnitBuilder<U extends Unit<U>>
java.lang.Object
edu.wpi.first.units.UnitBuilder<U>
- Type Parameters:
U
- the type of the unit
public final class UnitBuilder<U extends Unit<U>> extends Object
Builder used for easily deriving new units from existing ones.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
UnitBuilder.MappingBuilder
Helper class used for safely chaining mapping builder calls. -
Constructor Summary
Constructors Constructor Description UnitBuilder(U base)
Creates a new unit builder object, building off of a base unit. -
Method Summary
Modifier and Type Method Description UnitBuilder<U>
aggregate(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).UnitBuilder<U>
fromBase(UnaryFunction fromBase)
Sets the conversion function to transform values in the base unit to values in the derived unit.U
make()
Creates the new unit based off of the builder methods called prior.UnitBuilder.MappingBuilder
mappingInputRange(double minBase, double maxBase)
Defines a mapping for values within the given input range.UnitBuilder<U>
named(String name)
Sets the name of the new unit.UnitBuilder<U>
offset(double offset)
Sets the unit conversions based on a simple offset.UnitBuilder<U>
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).UnitBuilder<U>
symbol(String symbol)
Sets the symbol of the new unit.UnitBuilder<U>
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.- 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
-