Package edu.wpi.first.units
Class MutableMeasure<U extends Unit<U>>
java.lang.Object
edu.wpi.first.units.MutableMeasure<U>
- Type Parameters:
U
- the type of the unit of measure
- All Implemented Interfaces:
Measure<U>
,Comparable<Measure<U>>
public final class MutableMeasure<U extends Unit<U>> extends Object implements Measure<U>
A specialization of
Measure
that allows for mutability. This is intended to be used for
memory use reasons (such as on the memory-restricted roboRIO 1 or 2 or SBC coprocessors) and
should NOT be exposed in the public API for a class that uses it.
The advantage of using this class is to reuse one instance of a measurement object, as opposed to instantiating a new immutable instance every time an operation is performed. This will reduce memory pressure, but comes at the cost of increased code complexity and sensitivity to race conditions if misused.
Any unsafe methods are prefixed with mut_*
, such as mut_plus(Measure)
or
mut_replace(Measure)
. These methods will change the internal state of the measurement
object, and as such can be dangerous to use. They are primarily intended for use to track
internal state of things like sensors
-
Field Summary
-
Method Summary
Modifier and Type Method Description double
baseUnitMagnitude()
Gets the magnitude of this measure in terms of the base unit.Measure<U>
copy()
Returns an immutable copy of this measure.boolean
equals(Object o)
int
hashCode()
double
magnitude()
Gets the unitless magnitude of this measure.MutableMeasure<U>
mut_acc(double raw)
Increments the current magnitude of the measure by the given value.MutableMeasure<U>
mut_acc(Measure<U> other)
Increments the current magnitude of the measure by the amount of the given measure.MutableMeasure<U>
mut_divide(double divisor)
Divides this measurement by some constant value.MutableMeasure<U>
mut_divide(Measure<? extends Dimensionless> divisor)
Divides this measurement by some constant value.MutableMeasure<U>
mut_minus(double magnitude, U unit)
Subtracts another measurement to this one.MutableMeasure<U>
mut_minus(Measure<U> other)
Subtracts another measurement to this one.MutableMeasure<U>
mut_plus(double magnitude, U unit)
Adds another measurement to this one.MutableMeasure<U>
mut_plus(Measure<U> other)
Adds another measurement to this one.MutableMeasure<U>
mut_replace(double magnitude, U unit)
Overwrites the state of this measure with new values.MutableMeasure<U>
mut_replace(Measure<U> other)
Overwrites the state of this measure and replaces it with values from the given one.void
mut_setBaseUnitMagnitude(double baseUnitMagnitude)
Sets the new magnitude of the measurement.void
mut_setMagnitude(double magnitude)
Sets the new magnitude of the measurement.MutableMeasure<U>
mut_times(double multiplier)
Multiplies this measurement by some constant value.MutableMeasure<U>
mut_times(Measure<? extends Dimensionless> multiplier)
Multiplies this measurement by some constant value.static <U extends Unit<U>>
MutableMeasure<U>mutable(Measure<U> measure)
Creates a new mutable measure that is a copy of the given one.static <U extends Unit<U>>
MutableMeasure<U>ofBaseUnits(double baseUnitMagnitude, U unit)
Creates a new mutable measure in the given unit with a magnitude equal to the given one in base units.static <U extends Unit<U>>
MutableMeasure<U>ofRelativeUnits(double relativeMagnitude, U unit)
Creates a new mutable measure in the given unit with a magnitude in terms of that unit.String
toString()
U
unit()
Gets the units of this measure.static <U extends Unit<U>>
MutableMeasure<U>zero(U unit)
Creates a new mutable measure with a magnitude of 0 in the given unit.
-
Method Details
-
mutable
Creates a new mutable measure that is a copy of the given one.- Type Parameters:
U
- the type of the units of measure- Parameters:
measure
- the measure to create a mutable copy of- Returns:
- a new mutable measure with an initial state equal to the given measure
-
zero
Creates a new mutable measure with a magnitude of 0 in the given unit.- Type Parameters:
U
- the type of the units of measure- Parameters:
unit
- the unit of measure- Returns:
- a new mutable measure
-
ofBaseUnits
Creates a new mutable measure in the given unit with a magnitude equal to the given one in base units.- Type Parameters:
U
- the type of the units of measure- Parameters:
baseUnitMagnitude
- the magnitude of the measure, in terms of the base unit of measureunit
- the unit of measure- Returns:
- a new mutable measure
-
ofRelativeUnits
public static <U extends Unit<U>> MutableMeasure<U> ofRelativeUnits(double relativeMagnitude, U unit)Creates a new mutable measure in the given unit with a magnitude in terms of that unit.- Type Parameters:
U
- the type of the units of measure- Parameters:
relativeMagnitude
- the magnitude of the measureunit
- the unit of measure- Returns:
- a new mutable measure
-
magnitude
Description copied from interface:Measure
Gets the unitless magnitude of this measure. -
baseUnitMagnitude
Description copied from interface:Measure
Gets the magnitude of this measure in terms of the base unit. If the unit is the base unit for its system of measure, then the value will be equivalent toMeasure.magnitude()
.- Specified by:
baseUnitMagnitude
in interfaceMeasure<U extends Unit<U>>
- Returns:
- the magnitude in terms of the base unit
-
unit
Description copied from interface:Measure
Gets the units of this measure. -
mut_setMagnitude
Sets the new magnitude of the measurement. The magnitude must be in terms of theunit()
.- Parameters:
magnitude
- the new magnitude of the measurement
-
mut_setBaseUnitMagnitude
Sets the new magnitude of the measurement. The magnitude must be in terms of the base unit of the current unit.- Parameters:
baseUnitMagnitude
- the new magnitude of the measurement
-
mut_replace
Overwrites the state of this measure and replaces it with values from the given one.- Parameters:
other
- the other measure to copy values from- Returns:
- this measure
-
mut_replace
Overwrites the state of this measure with new values.- Parameters:
magnitude
- the new magnitude in terms of the new unitunit
- the new unit- Returns:
- this measure
-
mut_acc
Increments the current magnitude of the measure by the given value. The value must be in terms of the currentunit
.- Parameters:
raw
- the raw value to accumulate by- Returns:
- the measure
-
mut_acc
Increments the current magnitude of the measure by the amount of the given measure.- Parameters:
other
- the measure whose value should be added to this one- Returns:
- the measure
-
mut_plus
Adds another measurement to this one. This will mutate the object instead of generating a new measurement object.- Parameters:
other
- the measurement to add- Returns:
- this measure
-
mut_plus
Adds another measurement to this one. This will mutate the object instead of generating a new measurement object. This is a denormalized version ofmut_plus(Measure)
to avoid having to wrap raw numbers in aMeasure
object and pay for an object allocation.- Parameters:
magnitude
- the magnitude of the other measurement.unit
- the unit of the other measurement- Returns:
- this measure
-
mut_minus
Subtracts another measurement to this one. This will mutate the object instead of generating a new measurement object.- Parameters:
other
- the measurement to add- Returns:
- this measure
-
mut_minus
Subtracts another measurement to this one. This will mutate the object instead of generating a new measurement object. This is a denormalized version ofmut_minus(Measure)
to avoid having to wrap raw numbers in aMeasure
object and pay for an object allocation.- Parameters:
magnitude
- the magnitude of the other measurement.unit
- the unit of the other measurement- Returns:
- this measure
-
mut_times
Multiplies this measurement by some constant value. This will mutate the object instead of generating a new measurement object.- Parameters:
multiplier
- the multiplier to scale the measurement by- Returns:
- this measure
-
mut_times
Multiplies this measurement by some constant value. This will mutate the object instead of generating a new measurement object.- Parameters:
multiplier
- the multiplier to scale the measurement by- Returns:
- this measure
-
mut_divide
Divides this measurement by some constant value. This will mutate the object instead of generating a new measurement object.- Parameters:
divisor
- the divisor to scale the measurement by- Returns:
- this measure
-
mut_divide
Divides this measurement by some constant value. This will mutate the object instead of generating a new measurement object.- Parameters:
divisor
- the divisor to scale the measurement by- Returns:
- this measure
-
copy
Description copied from interface:Measure
Returns an immutable copy of this measure. The copy can be used freely and is guaranteed never to change. -
toString
-
equals
-
hashCode
-