Class Measure

java.lang.Object
de.turnertech.measures.Measure

public class Measure extends Object

A Measure is a description of some quantity of a Unit, where a Unit is a standardised and accepted quantity, size, ammount, period or other kind of measurement. For example, a Unit.METRE represents a standardised length.

This implementation stores an immutable Unit instance. To obtain a Measure with another Unit you must either explicitely instantiate a new instance, or convert the Measure using one of the either convertTo(Unit) or the class UnitConverter

  • Constructor Summary

    Constructors
    Constructor
    Description
    Measure(double quantity, Unit unit)
    Constructs an instance with the provided parameters.
  • Method Summary

    Modifier and Type
    Method
    Description
    convertTo(Unit newUnit)
    Helper function to convert this Measure to a new Measure with the supplied newUnit parameter.
    boolean
     
    boolean
    equalsWithTolerance(Measure other, double tolerance)
    Helper function for checking if the Measure is equal to within a supplied tolerance.
    double
    Gets the quantity of Units stored in this Measure.
    Sets the Unit of this Measure.
    int
     
    void
    setQuantity(double quantity)
    Sets the quantity of Units stored in this Measure.

    Methods inherited from class java.lang.Object

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

    • Measure

      public Measure(double quantity, Unit unit)
      Constructs an instance with the provided parameters.
      Parameters:
      quantity - The quantity of Units in this Measure. Mutable.
      unit - The Unit of this Measure. This is immutable.
  • Method Details

    • equalsWithTolerance

      public boolean equalsWithTolerance(Measure other, double tolerance)

      Helper function for checking if the Measure is equal to within a supplied tolerance. This is usefull in several situations as the Double Precision mechanism can very quickly introduce innacuracies which prevent .equals() from providing a useful result.

      If the Measures do not suare the same base unit, a conversion will first be attempted.

      Parameters:
      other - Measure to check against.
      tolerance - allowed difference between the numbers.
      Returns:
      true or false.
    • getQuantity

      public double getQuantity()
      Gets the quantity of Units stored in this Measure.
      Returns:
      the quantity of Units stored in this Measure.
    • setQuantity

      public void setQuantity(double quantity)
      Sets the quantity of Units stored in this Measure.
      Parameters:
      quantity - the quantity of Units stored in this Measure.
    • getUnit

      public Unit getUnit()
      Sets the Unit of this Measure.
      Returns:
      the Unit of this Measure.
    • convertTo

      public Measure convertTo(Unit newUnit)

      Helper function to convert this Measure to a new Measure with the supplied newUnit parameter. This function exists in place of a setter as the unit member is immutable.

      Internally this function simply delegates to UnitConverter.convert(Measure, Unit)

      Parameters:
      newUnit - to be used in the creation of the new Measure instance.
      Returns:
      a new Measure instance in the supplied Unit.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object