Class MultUnit<A extends Unit,B extends Unit>

java.lang.Object
edu.wpi.first.units.Unit
edu.wpi.first.units.MultUnit<A,B>
Type Parameters:
A - the type of the first unit in the result
B - the type of the second unit in the result
Direct Known Subclasses:
AngularMomentumUnit, ForceUnit, LinearMomentumUnit, TorqueUnit

public class MultUnit<A extends Unit,B extends Unit> extends Unit
A combinatory unit type that is equivalent to the product of two other others. Note that algebraic reduction is not possible in Java's generic type system, so MultUnit<A, B> is not type-compatible with MultUnit<B, A>!
  • Constructor Details

    • MultUnit

      protected MultUnit(MultUnit<A,B> baseUnit, A a, B b)
      Creates a new product unit. Subclasses of MultUnit should use this constructor.
      Parameters:
      baseUnit - the base unit. Set this to null if the unit being constructed is its own base unit
      a - the first unit of the product
      b - the second unit of the product
  • Method Details

    • combine

      public static <A extends Unit, B extends Unit> MultUnit<A,B> combine(A a, B b)
      Creates a new MultUnit unit derived from two arbitrary units multiplied together.
         MultUnit.combine(Volts, Meters) // Volt-Meters
       
      Type Parameters:
      A - the type of the first unit
      B - the type of the second unit
      Parameters:
      a - the first unit
      b - the second unit
      Returns:
      the combined unit
    • of

      public Measure<? extends MultUnit<A,B>> of(double magnitude)
      Creates a new immutable measurement of the given magnitude in terms of this unit. Implementations are strongly recommended to sharpen the return type to a unit-specific measurement implementation.

      Note: When called on an object of type MultUnit (and not a subclass!), this method will always return a Mult instance. If you want to avoid casting, use ofNativeBaseUnits(double) that returns a Per instance directly.

      Specified by:
      of in class Unit
      Parameters:
      magnitude - the magnitude of the measure in terms of its base units.
      Returns:
      the measurement object
    • ofBaseUnits

      public Measure<? extends MultUnit<A,B>> ofBaseUnits(double baseUnitMagnitude)
      Creates a new immutable measurement of the given magnitude in terms of this unit's base unit. Implementations are strongly recommended to sharpen the return type to a unit-specific measurement implementation.

      Note: When called on an object of type MultUnit (and not a subclass!), this method will always return a Mult instance. If you want to avoid casting, use ofNativeBaseUnits(double) that returns a Per instance directly.

      Specified by:
      ofBaseUnits in class Unit
      Parameters:
      baseUnitMagnitude - the magnitude of the measure in terms of its base units.
      Returns:
      the measurement object
    • ofNative

      public final Mult<A,B> ofNative(double magnitude)
      Creates a new immutable measurement of the given magnitude in terms of this unit. This will always return a Mult object and cannot be overridden by subclasses.
      Parameters:
      magnitude - the magnitude of the measurement.
      Returns:
      the measurement object
      See Also:
    • ofNativeBaseUnits

      public final Mult<A,B> ofNativeBaseUnits(double baseUnitMagnitude)
      Creates a new immutable measurement of the given magnitude in terms of the unit's base unit. This will always return a Mult object and cannot be overridden by subclasses.
      Parameters:
      baseUnitMagnitude - the magnitude of the measure in terms of its base units.
      Returns:
      the measurement object
      See Also:
    • zero

      public Measure<? extends MultUnit<A,B>> zero()
      Description copied from class: Unit
      Gets a measure of zero magnitude in terms of this unit. The returned object is guaranteed to be of the same type returned by Unit.of(double). Subclasses are encouraged to override this method to sharpen the return type.
      Overrides:
      zero in class Unit
      Returns:
      a zero-magnitude measure of this unit
    • one

      public Measure<? extends MultUnit<A,B>> one()
      Description copied from class: Unit
      Gets a measure with a magnitude of 1.0 in terms of this unit. The returned object is guaranteed to be of the same type returned by Unit.of(double). Subclasses are encouraged to override this method to sharpen the return type.
      Overrides:
      one in class Unit
      Returns:
      a measure of magnitude 1.0 in terms of this unit
    • mutable

      public MutableMeasure<? extends MultUnit<A,B>,?,?> mutable(double initialMagnitude)
      Description copied from class: Unit
      Creates a new mutable measurement that is initialized to the given magnitude in terms of this unit. Implementations are strongly recommended to sharpen the return type to a unit-specific measurement implementation.
      Specified by:
      mutable in class Unit
      Parameters:
      initialMagnitude - the initial magnitude of the mutable measurement
      Returns:
      the mutable measurement object
    • per

      public Unit per(TimeUnit time)
      Description copied from class: Unit
      Combines this unit with a unit of time. This often - but not always - results in a velocity. Subclasses should sharpen the return type to be unit-specific.
      Specified by:
      per in class Unit
      Parameters:
      time - the unit of time
      Returns:
      the combined unit
    • convertFrom

      public double convertFrom(double magnitude, MultUnit<A,B> otherUnit)
      Converts a measurement value in terms of another unit to this unit.
      Parameters:
      magnitude - the magnitude of the measurement in terms of the other unit
      otherUnit - the other unit
      Returns:
      the value of the measurement in terms of this unit
    • getBaseUnit

      public MultUnit<A,B> getBaseUnit()
      Description copied from class: Unit
      Gets the base unit of measurement that this unit is derived from. If the unit is the base unit, the unit will be returned.

      NOTE: Subclasses must override this method to provide the correct return type. Failing to do say will make unit combinations that use it break at runtime!

      
         Unit baseUnit = new Unit(null, ...);
         baseUnit.getBaseUnit(); // returns baseUnit
      
         Unit derivedUnit = new Unit(baseUnit, ...);
         derivedUnit.getBaseUnit(); // returns baseUnit
       
      Overrides:
      getBaseUnit in class Unit
      Returns:
      the base unit
    • unitA

      public A unitA()
      Gets the first unit of the product.
      Returns:
      the first unit
    • unitB

      public B unitB()
      Gets the second unit of the product.
      Returns:
      the second unit
    • toString

      public String toString()
      Overrides:
      toString in class Unit
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Unit
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Unit