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 -
Method Summary
Modifier and TypeMethodDescriptionHelper function to convert this Measure to a new Measure with the supplied newUnit parameter.booleanbooleanequalsWithTolerance(Measure other, double tolerance) Helper function for checking if the Measure is equal to within a supplied tolerance.doubleGets the quantity of Units stored in this Measure.getUnit()Sets the Unit of this Measure.inthashCode()voidsetQuantity(double quantity) Sets the quantity of Units stored in this Measure.
-
Constructor Details
-
Measure
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
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
Sets the Unit of this Measure.- Returns:
- the Unit of this Measure.
-
convertTo
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() -
equals
-