Interface UnitValue<U extends Unit<U>>
- All Superinterfaces:
Comparable<UnitValue<U>>
,Serializable
Represents an association between an Unit and its quantity. For example:
10sec duration, 100KB of data or 2USD. The interface also provides ability
to perform mathematical operations between two similar Unit types like
adding 100 ms to 3 seconds or 1KB to 120MBs of data with an exception to
Money.
If mathematical operations between two Units is not supported the method
throws a Unit.UnitMisMatchException.
-
Method Summary
Modifier and TypeMethodDescriptionThe method adds the quantities of two UnitValues.Returns the quantitygetUnit()
Returns the Unitboolean
isZero()
The method returns true if the quantity of this UnitValue is zero.compare with the otherUnitValue and return the maxThe method multiplies the other UnitValue from this UnitValue.round
(int precision, RoundingMode mode) The method rounds the quantity based on the passed precision and the RoundingModescale
(int scaleValue, RoundingMode mode) The method scales the quantity based on the passed scale and the RoundingModevoid
setQuantity
(BigDecimal quantity) set the qtyThe method subtracts the other UnitValue from this UnitValue.Methods inherited from interface java.lang.Comparable
compareTo
-
Method Details
-
isZero
boolean isZero()The method returns true if the quantity of this UnitValue is zero. Returns false otherwise.- Returns:
- boolean
-
add
The method adds the quantities of two UnitValues. If two UnitValues represent the same Unit type like adding Bytes to KibiBytes or KibiBytes to MebiBytes then the returned UnitValue will have the Unit of lowest common denominator (for example 1KBytes + 1MBytes will return the final quantity in 1025KBytes). The method throws Unit.UnitMisMatchException if two UnitValues cannot be added. A typical scenario would be adding two Money units one being in USD and the other in EUR. Such conversions are not supported and the method will throw UnitMisMatchException.- Parameters:
other
- UnitValue The UnitValue to be added- Returns:
- UnitValue Added result
- Throws:
Unit.UnitMisMatchException
- If two Units can not be added.
-
subtract
The method subtracts the other UnitValue from this UnitValue. If two UnitValues represent the same Unit type like KibiBytes and MebiBytes then the result will be of common denominator Unit type between the two. Hance, 1MBytes - 1KBytes will return 1023KBytes. The method throws Unit.UnitMisMatchException if two UnitValues cannot be subtracted. A typical scenario would be subtracting two Money units one being in USD and the other in EUR. Such conversions are not supported and the method will throw UnitMisMatchException.- Parameters:
other
- UnitValue The UnitValue to be subtracted- Returns:
- UnitValue The result of the subtraction represented in the common Unit type.
- Throws:
Unit.UnitMisMatchException
- If the other Unit can not be subtracted.
-
multiply
The method multiplies the other UnitValue from this UnitValue. If two UnitValues represent the same Unit type like KibiBytes and MebiBytes then the result will be of common denominator Unit type between the two. The method throws Unit.UnitMisMatchException if two UnitValues cannot be multiplied. A typical scenario would be multiplying two Money units one being in USD and the other in EUR. Such conversions are not supported and the method will throw UnitMisMatchException.- Parameters:
other
- UnitValue The UnitValue to be multiplied- Returns:
- UnitValue The result of the multiplication represented in the common Unit type.
- Throws:
Unit.UnitMisMatchException
- If the other Unit can not be subtracted.
-
round
The method rounds the quantity based on the passed precision and the RoundingMode- Parameters:
precision
- intmode
- RoundingMode- Returns:
- UnitValue
-
scale
The method scales the quantity based on the passed scale and the RoundingMode- Parameters:
scaleValue
- intmode
- RoundingMode- Returns:
- UnitValue
-
getQuantity
BigDecimal getQuantity()Returns the quantity- Returns:
- BigDecimal
-
setQuantity
set the qty- Parameters:
quantity
-
-
getUnit
U getUnit()Returns the Unit- Returns:
- Unit
-
max
compare with the otherUnitValue and return the max- Parameters:
otherUnitValue
-- Returns:
-