Interface Measure<U extends Unit<U>>

Type Parameters:
U - the unit type of the measure
All Superinterfaces:
Comparable<Measure<U>>
All Known Implementing Classes:
ImmutableMeasure, MutableMeasure

public interface Measure<U extends Unit<U>>
extends Comparable<Measure<U>>
A measure holds the magnitude and unit of some dimension, such as distance, time, or speed. Two measures with the same unit and magnitude are effectively equivalent objects.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static double EQUIVALENCE_THRESHOLD
    The threshold for two measures to be considered equivalent if converted to the same unit.
  • Method Summary

    Modifier and Type Method Description
    double baseUnitMagnitude()
    Gets the magnitude of this measure in terms of the base unit.
    default int compareTo​(Measure<U> o)
    Measure<U> copy()
    Returns an immutable copy of this measure.
    default Measure<U> divide​(double divisor)
    Divides this measurement by some constant divisor and returns the result.
    default Measure<U> divide​(Measure<Dimensionless> divisor)
    Divides this measurement by some constant divisor and returns the result.
    default boolean gt​(Measure<U> o)
    Checks if this measure is greater than another measure of the same unit.
    default boolean gte​(Measure<U> o)
    Checks if this measure is greater than or equivalent to another measure of the same unit.
    default double in​(Unit<U> unit)
    Converts this measure to a measure with a different unit of the same type, eg minutes to seconds.
    default boolean isEquivalent​(Measure<?> other)
    Checks if this measure is equivalent to another measure of the same unit.
    default boolean isNear​(Measure<?> other, double varianceThreshold)
    Checks if this measure is near another measure of the same unit.
    default boolean lt​(Measure<U> o)
    Checks if this measure is less than another measure of the same unit.
    default boolean lte​(Measure<U> o)
    Checks if this measure is less than or equivalent to another measure of the same unit.
    double magnitude()
    Gets the unitless magnitude of this measure.
    static <U extends Unit<U>>
    Measure<U>
    max​(Measure<U>... measures)
    Returns the measure with the absolute value closest to positive infinity.
    static <U extends Unit<U>>
    Measure<U>
    min​(Measure<U>... measures)
    Returns the measure with the absolute value closest to negative infinity.
    default Measure<U> minus​(Measure<U> other)
    Subtracts another measure from this one.
    default MutableMeasure<U> mutableCopy()
    Creates a new mutable copy of this measure.
    default Measure<U> negate()
    Negates this measure and returns the result.
    default Measure<Velocity<U>> per​(Measure<Time> period)
    Creates a velocity measure by dividing this one by a time period measure.
    default Measure<Velocity<U>> per​(Time time)
    Creates a velocity measure equivalent to this one per a unit of time.
    default <U2 extends Unit<U2>>
    Measure<Per<U,​U2>>
    per​(U2 denominator)
    Creates a relational measure equivalent to this one per some other unit.
    default Measure<U> plus​(Measure<U> other)
    Adds another measure to this one.
    default Measure<U> times​(double multiplier)
    Multiplies this measurement by some constant multiplier and returns the result.
    default <U2 extends Unit<U2>>
    Measure<?>
    times​(Measure<U2> other)
    Generates a new measure that is equal to this measure multiplied by another.
    default String toLongString()
    Returns a string representation of this measurement in a longhand form.
    default String toShortString()
    Returns a string representation of this measurement in a shorthand form.
    U unit()
    Gets the units of this measure.
  • Field Details

  • Method Details

    • magnitude

      double magnitude()
      Gets the unitless magnitude of this measure.
      Returns:
      the magnitude in terms of the unit.
    • baseUnitMagnitude

      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 to magnitude().
      Returns:
      the magnitude in terms of the base unit
    • unit

      U unit()
      Gets the units of this measure.
      Returns:
      the unit
    • in

      default double in​(Unit<U> unit)
      Converts this measure to a measure with a different unit of the same type, eg minutes to seconds. Converting to the same unit is equivalent to calling magnitude().
         Meters.of(12).in(Feet) // 39.3701
         Seconds.of(15).in(Minutes) // 0.25
       
      Parameters:
      unit - the unit to convert this measure to
      Returns:
      the value of this measure in the given unit
    • times

      default Measure<U> times​(double multiplier)
      Multiplies this measurement by some constant multiplier and returns the result. The magnitude of the result will be the base magnitude multiplied by the scalar value. If the measure uses a unit with a non-linear relation to its base unit (such as Fahrenheit for temperature), then the result will only be a multiple in terms of the base unit.
      Parameters:
      multiplier - the constant to multiply by
      Returns:
      the resulting measure
    • times

      default <U2 extends Unit<U2>> Measure<?> times​(Measure<U2> other)
      Generates a new measure that is equal to this measure multiplied by another. Some dimensional analysis is performed to reduce the units down somewhat; for example, multiplying a Measure<Time> by a Measure<Velocity<Distance>> will return just a Measure<Distance> instead of the naive Measure<Mult<Time, Velocity<Distance>>. This is not guaranteed to perform perfect dimensional analysis.
      Type Parameters:
      U2 - the type of the other measure to multiply by
      Parameters:
      other - the unit to multiply by
      Returns:
      the multiplicative unit
    • divide

      default Measure<U> divide​(double divisor)
      Divides this measurement by some constant divisor and returns the result. This is equivalent to times(1 / divisor)
      Parameters:
      divisor - the constant to divide by
      Returns:
      the resulting measure
      See Also:
      times(double)
    • divide

      default Measure<U> divide​(Measure<Dimensionless> divisor)
      Divides this measurement by some constant divisor and returns the result. This is equivalent to divide(divisor.baseUnitMagnitude())
      Parameters:
      divisor - the dimensionless measure to divide by
      Returns:
      the resulting measure
      See Also:
      divide(double), times(double)
    • per

      default Measure<Velocity<U>> per​(Measure<Time> period)
      Creates a velocity measure by dividing this one by a time period measure.
         Meters.of(1).per(Second) // Measure<Velocity<Distance>>
       
      Parameters:
      period - the time period to divide by.
      Returns:
      the velocity result
    • per

      default <U2 extends Unit<U2>> Measure<Per<U,​U2>> per​(U2 denominator)
      Creates a relational measure equivalent to this one per some other unit.
         Volts.of(1.05).per(Meter) // V/m, potential PID constant
       
      Type Parameters:
      U2 - the type of the denominator unit
      Parameters:
      denominator - the denominator unit being divided by
      Returns:
      the relational measure
    • per

      default Measure<Velocity<U>> per​(Time time)
      Creates a velocity measure equivalent to this one per a unit of time.
         Radians.of(3.14).per(Second) // Velocity<Angle> equivalent to RadiansPerSecond.of(3.14)
       
      Parameters:
      time - the unit of time
      Returns:
      the velocity measure
    • plus

      default Measure<U> plus​(Measure<U> other)
      Adds another measure to this one. The resulting measure has the same unit as this one.
      Parameters:
      other - the measure to add to this one
      Returns:
      a new measure containing the result
    • minus

      default Measure<U> minus​(Measure<U> other)
      Subtracts another measure from this one. The resulting measure has the same unit as this one.
      Parameters:
      other - the measure to subtract from this one
      Returns:
      a new measure containing the result
    • negate

      default Measure<U> negate()
      Negates this measure and returns the result.
      Returns:
      the resulting measure
    • copy

      Returns an immutable copy of this measure. The copy can be used freely and is guaranteed never to change.
      Returns:
      the copied measure
    • mutableCopy

      Creates a new mutable copy of this measure.
      Returns:
      a mutable measure initialized to be identical to this measure
    • isNear

      default boolean isNear​(Measure<?> other, double varianceThreshold)
      Checks if this measure is near another measure of the same unit. Provide a variance threshold for use for a +/- scalar, such as 0.05 for +/- 5%.
         Inches.of(11).isNear(Inches.of(10), 0.1) // true
         Inches.of(12).isNear(Inches.of(10), 0.1) // false
       
      Parameters:
      other - the other measurement to compare against
      varianceThreshold - the acceptable variance threshold, in terms of an acceptable +/- error range multiplier. Checking if a value is within 10% means a value of 0.1 should be passed; checking if a value is within 1% means a value of 0.01 should be passed, and so on.
      Returns:
      true if this unit is near the other measure, otherwise false
    • isEquivalent

      default boolean isEquivalent​(Measure<?> other)
      Checks if this measure is equivalent to another measure of the same unit.
      Parameters:
      other - the measure to compare to
      Returns:
      true if this measure is equivalent, false otherwise
    • compareTo

      default int compareTo​(Measure<U> o)
      Specified by:
      compareTo in interface Comparable<U extends Unit<U>>
    • gt

      default boolean gt​(Measure<U> o)
      Checks if this measure is greater than another measure of the same unit.
      Parameters:
      o - the other measure to compare to
      Returns:
      true if this measure has a greater equivalent magnitude, false otherwise
    • gte

      default boolean gte​(Measure<U> o)
      Checks if this measure is greater than or equivalent to another measure of the same unit.
      Parameters:
      o - the other measure to compare to
      Returns:
      true if this measure has an equal or greater equivalent magnitude, false otherwise
    • lt

      default boolean lt​(Measure<U> o)
      Checks if this measure is less than another measure of the same unit.
      Parameters:
      o - the other measure to compare to
      Returns:
      true if this measure has a lesser equivalent magnitude, false otherwise
    • lte

      default boolean lte​(Measure<U> o)
      Checks if this measure is less than or equivalent to another measure of the same unit.
      Parameters:
      o - the other measure to compare to
      Returns:
      true if this measure has an equal or lesser equivalent magnitude, false otherwise
    • max

      @SafeVarargs static <U extends Unit<U>> Measure<U> max​(Measure<U>... measures)
      Returns the measure with the absolute value closest to positive infinity.
      Type Parameters:
      U - the type of the units of the measures
      Parameters:
      measures - the set of measures to compare
      Returns:
      the measure with the greatest positive magnitude, or null if no measures were provided
    • min

      @SafeVarargs static <U extends Unit<U>> Measure<U> min​(Measure<U>... measures)
      Returns the measure with the absolute value closest to negative infinity.
      Type Parameters:
      U - the type of the units of the measures
      Parameters:
      measures - the set of measures to compare
      Returns:
      the measure with the greatest negative magnitude
    • toShortString

      default String toShortString()
      Returns a string representation of this measurement in a shorthand form. The symbol of the backing unit is used, rather than the full name, and the magnitude is represented in scientific notation.
      Returns:
      the short form representation of this measurement
    • toLongString

      default String toLongString()
      Returns a string representation of this measurement in a longhand form. The name of the backing unit is used, rather than its symbol, and the magnitude is represented in a full string, not scientific notation. (Very large values may be represented in scientific notation, however)
      Returns:
      the long form representation of this measurement