Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Latest commit

 

History

History
19 lines (17 loc) · 1.04 KB

abstractquantity.md

File metadata and controls

19 lines (17 loc) · 1.04 KB

AbstractQuantity

AbstractQuantity provides additional methods to convert values directly into a target unit. They are more useful as meta-data converted to the application's internal representation (typically a double primitive type in some fixed units) before computation or further processing begin. For this purpose AbstractQuantity provides the longValue(Unit< Q >) and doubleValue(Unit< Q >) convenience methods.

For example the doubleValue(Unit< Q >) method is equivalent to the following:

public double doubleValue(Unit<Q> unit) {
   return getUnit().getConverterTo(unit).convert(getValue().doubleValue());
}

With such method, user code like below are slightly easier to write:

double calculateTravelTimeInSeconds(AbstractQuantity<Length> distance, AbstractQuantity<Speed> speed) {
   return distance.doubleValue(METRE) /
          speed.doubleValue(METRE_PER_SECOND);
}

Note that an equivalent operation can also be done using the Quantity.divide(Quantity) method.