Interface UnitValue<U extends Unit<U>>

All Superinterfaces:
Comparable<UnitValue<U>>, Serializable

public interface UnitValue<U extends Unit<U>> extends 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 Details

    • isZero

      boolean isZero()
      The method returns true if the quantity of this UnitValue is zero. Returns false otherwise.
      Returns:
      boolean
    • add

      UnitValue<U> add(UnitValue<U> other)
      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

      UnitValue<U> subtract(UnitValue<U> other)
      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

      UnitValue<U> multiply(UnitValue<U> other)
      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

      UnitValue<U> round(int precision, RoundingMode mode)
      The method rounds the quantity based on the passed precision and the RoundingMode
      Parameters:
      precision - int
      mode - RoundingMode
      Returns:
      UnitValue
    • scale

      UnitValue<U> scale(int scaleValue, RoundingMode mode)
      The method scales the quantity based on the passed scale and the RoundingMode
      Parameters:
      scaleValue - int
      mode - RoundingMode
      Returns:
      UnitValue
    • getQuantity

      BigDecimal getQuantity()
      Returns the quantity
      Returns:
      BigDecimal
    • setQuantity

      void setQuantity(BigDecimal quantity)
      set the qty
      Parameters:
      quantity -
    • getUnit

      U getUnit()
      Returns the Unit
      Returns:
      Unit
    • max

      UnitValue max(UnitValue otherUnitValue)
      compare with the otherUnitValue and return the max
      Parameters:
      otherUnitValue -
      Returns: