Class Measurement<T extends Enum<?>>

java.lang.Object
com.irurueta.units.Measurement<T>
Type Parameters:
T - a measurement unit.
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
Acceleration, Angle, AngularAcceleration, AngularSpeed, Distance, Energy, Force, Frequency, MagneticFluxDensity, Power, Pressure, Speed, Surface, Temperature, Time, Volume, Weight

public abstract class Measurement<T extends Enum<?>> extends Object implements Serializable
Base class to define a measurement unit and value.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static final record 
    Measurement value and unit that have already been validated to be not null.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
    Multiplier used when computing hash codes.
    private static final int
    Initial value used when computing hash codes.
    private T
    Measurement unit.
    private Number
    Measurement value.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    (package private)
    Constructor.
    private
    Internal constructor with already validated arguments.
    protected
    Measurement(Number initialValue, T initialUnit)
    Constructor.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    equals(Measurement<T> other, double tolerance)
    Determines if two measurements are equal up to a certain tolerance.
    boolean
    Determines if two measurements are equal.
    Returns measurement unit.
    Returns measurement value.
    int
    Hash code generated for this instance.
    void
    setUnit(T newUnit)
    Sets measurement unit.
    void
    setValue(Number newValue)
    Sets measurement value.
    private static <U extends Enum<?>>
    Measurement.ValidArguments<U>
    validate(Number initialValue, U initialUnit)
    Checks that provided measurement value and unit are not null.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • HASH_SEED

      private static final int HASH_SEED
      Initial value used when computing hash codes.
      See Also:
    • HASH_MULTIPLIER

      private static final int HASH_MULTIPLIER
      Multiplier used when computing hash codes.
      See Also:
    • value

      private Number value
      Measurement value.
    • unit

      private T extends Enum<?> unit
      Measurement unit.
  • Constructor Details

    • Measurement

      protected Measurement(Number initialValue, T initialUnit)
      Constructor.
      Parameters:
      initialValue - measurement value.
      initialUnit - measurement unit.
      Throws:
      IllegalArgumentException - if either value or unit is null.
    • Measurement

      private Measurement(Measurement.ValidArguments<T> arguments)
      Internal constructor with already validated arguments.
      Parameters:
      arguments - validated (not null) measurement value and unit.
    • Measurement

      Measurement()
      Constructor.
  • Method Details

    • validate

      private static <U extends Enum<?>> Measurement.ValidArguments<U> validate(Number initialValue, U initialUnit)
      Checks that provided measurement value and unit are not null. Validation is performed here (instead of directly within the constructor) so that no exception is thrown from the constructor body itself.
      Type Parameters:
      U - type of measurement unit.
      Parameters:
      initialValue - measurement value to be checked.
      initialUnit - measurement unit to be checked.
      Returns:
      validated measurement value and unit.
      Throws:
      IllegalArgumentException - if either value or unit is null.
    • equals

      public boolean equals(Object obj)
      Determines if two measurements are equal.
      Overrides:
      equals in class Object
      Parameters:
      obj - another object to compare.
      Returns:
      true if provided object is assumed to be equal to this instance, false otherwise.
    • hashCode

      public int hashCode()
      Hash code generated for this instance. Hash codes can be internally used by some collections to coarsely compare objects.
      Overrides:
      hashCode in class Object
      Returns:
      hash code.
    • equals

      public boolean equals(Measurement<T> other, double tolerance)
      Determines if two measurements are equal up to a certain tolerance.
      Parameters:
      other - another measurement to compare.
      tolerance - amount of tolerance to determine whether two measurements are equal or not.
      Returns:
      true if provided measurement is assumed to be equal to this instance, false otherwise.
    • getValue

      public Number getValue()
      Returns measurement value.
      Returns:
      measurement value.
    • setValue

      public void setValue(Number newValue)
      Sets measurement value.
      Parameters:
      newValue - measurement value.
      Throws:
      IllegalArgumentException - if measurement value is null.
    • getUnit

      public T getUnit()
      Returns measurement unit.
      Returns:
      measurement unit.
    • setUnit

      public void setUnit(T newUnit)
      Sets measurement unit.
      Parameters:
      newUnit - measurement unit.
      Throws:
      IllegalArgumentException - if measurement unit is null.