Record Class ImmutableMeasure<U extends Unit>

java.lang.Object
java.lang.Record
edu.wpi.first.units.ImmutableMeasure<U>
Type Parameters:
U - the unit type of the measure
Record Components:
magnitude - the magnitude of the measure in terms of its unit
baseUnitMagnitude - the magnitude of the measure in terms of its base unit
unit - the unit of the measurement
All Implemented Interfaces:
Measure<U>, Comparable<Measure<U>>

public record ImmutableMeasure<U extends Unit>(double magnitude, double baseUnitMagnitude, U extends Unit unit) extends Record implements Measure<U>
A measure holds the magnitude and unit of some dimension, such as distance, time, or speed. An immutable measure is immutable and type safe, making it easy to use in concurrent situations and gives compile-time safety. Two measures with the same unit and magnitude are effectively equivalent objects.
  • Constructor Details

    • ImmutableMeasure

      public ImmutableMeasure(double magnitude, double baseUnitMagnitude, U unit)
      Creates an instance of a ImmutableMeasure record class.
      Parameters:
      magnitude - the value for the magnitude record component
      baseUnitMagnitude - the value for the baseUnitMagnitude record component
      unit - the value for the unit record component
  • Method Details

    • ofBaseUnits

      public static <U extends Unit> ImmutableMeasure<U> ofBaseUnits(double baseUnitMagnitude, U unit)
      Creates a new 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 measure
      unit - the unit of measure
      Returns:
      a new measure
    • ofRelativeUnits

      public static <U extends Unit> ImmutableMeasure<U> ofRelativeUnits(double relativeMagnitude, U unit)
      Creates a new 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 measure
      unit - the unit of measure
      Returns:
      a new measure
    • copy

      public Measure<U> 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.
      Specified by:
      copy in interface Measure<U extends Unit>
      Returns:
      the copied measure
    • mutableCopy

      public MutableMeasure<U,?,?> mutableCopy()
      Description copied from interface: Measure
      Returns a mutable copy of this measure. It will be initialized to the current state of this measure, but can be changed over time without needing to allocate new measurement objects.
      Specified by:
      mutableCopy in interface Measure<U extends Unit>
      Returns:
      the copied measure
    • unaryMinus

      public Measure<U> unaryMinus()
      Description copied from interface: Measure
      Returns a measure equivalent to this one equal to zero minus its current value. For non-linear unit types like temperature, the zero point is treated as the zero value of the base unit (eg Kelvin). In effect, this means code like Celsius.of(10).unaryMinus() returns a value equivalent to -10 Kelvin, and not -10° Celsius.
      Specified by:
      unaryMinus in interface Measure<U extends Unit>
      Returns:
      a measure equal to zero minus this measure
    • plus

      public Measure<U> plus(Measure<? extends U> other)
      Description copied from interface: Measure
      Adds another measure of the same unit type to this one.
      Specified by:
      plus in interface Measure<U extends Unit>
      Parameters:
      other - the measurement to add
      Returns:
      a measure of the sum of both measures
    • minus

      public Measure<U> minus(Measure<? extends U> other)
      Description copied from interface: Measure
      Subtracts another measure of the same unit type from this one.
      Specified by:
      minus in interface Measure<U extends Unit>
      Parameters:
      other - the measurement to subtract
      Returns:
      a measure of the difference between the measures
    • times

      public Measure<U> times(double multiplier)
      Description copied from interface: Measure
      Multiplies this measure by a scalar unitless multiplier.
      Specified by:
      times in interface Measure<U extends Unit>
      Parameters:
      multiplier - the scalar multiplication factor
      Returns:
      the scaled result
    • times

      public Measure<U> times(Dimensionless multiplier)
      Description copied from interface: Measure
      Multiplies this measure by a scalar dimensionless multiplier.
      Specified by:
      times in interface Measure<U extends Unit>
      Parameters:
      multiplier - the scalar multiplication factor
      Returns:
      the scaled result
    • div

      public Measure<U> div(double divisor)
      Description copied from interface: Measure
      Divides this measure by a unitless scalar and returns the result.
      Specified by:
      div in interface Measure<U extends Unit>
      Parameters:
      divisor - the measurement to divide by.
      Returns:
      the division result
    • div

      public Measure<U> div(Dimensionless divisor)
      Description copied from interface: Measure
      Divides this measure by a dimensionless scalar and returns the result.
      Specified by:
      div in interface Measure<U extends Unit>
      Parameters:
      divisor - the measurement to divide by.
      Returns:
      the division result
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • magnitude

      public double magnitude()
      Returns the value of the magnitude record component.
      Specified by:
      magnitude in interface Measure<U extends Unit>
      Returns:
      the value of the magnitude record component
    • baseUnitMagnitude

      public double baseUnitMagnitude()
      Returns the value of the baseUnitMagnitude record component.
      Specified by:
      baseUnitMagnitude in interface Measure<U extends Unit>
      Returns:
      the value of the baseUnitMagnitude record component
    • unit

      public U unit()
      Returns the value of the unit record component.
      Specified by:
      unit in interface Measure<U extends Unit>
      Returns:
      the value of the unit record component