Class NumberQuantity<Q extends javax.measure.Quantity<Q>>

  • Type Parameters:
    Q - The type of the quantity.
    All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<javax.measure.Quantity<Q>>, javax.measure.Quantity<Q>, ComparableQuantity<Q>, tech.uom.lib.common.function.QuantityConverter<Q>, tech.uom.lib.common.function.UnitSupplier<Q>, tech.uom.lib.common.function.ValueSupplier<java.lang.Number>

    public class NumberQuantity<Q extends javax.measure.Quantity<Q>>
    extends AbstractQuantity<Q>
    implements java.io.Serializable
    An amount of quantity, implementation of ComparableQuantity that keep Number as possible otherwise converts to DecimalQuantity, this object is immutable.
    Since:
    1.0
    Version:
    1.0.1, $Date: 2017-05-28 $
    See Also:
    AbstractQuantity, Quantity, ComparableQuantity, Serialized Form
    • Field Detail

      • value

        private final java.lang.Number value
      • isBig

        private final boolean isBig
        Indicates if this quantity is big.
    • Constructor Detail

      • NumberQuantity

        protected NumberQuantity​(java.lang.Number number,
                                 javax.measure.Unit<Q> unit)
    • Method Detail

      • doubleValue

        public double doubleValue​(javax.measure.Unit<Q> unit)
        Specified by:
        doubleValue in class AbstractQuantity<Q extends javax.measure.Quantity<Q>>
      • getValue

        public java.lang.Number getValue()
        Description copied from class: AbstractQuantity
        Returns the numeric value of the quantity.
        Specified by:
        getValue in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        getValue in interface tech.uom.lib.common.function.ValueSupplier<Q extends javax.measure.Quantity<Q>>
        Specified by:
        getValue in class AbstractQuantity<Q extends javax.measure.Quantity<Q>>
        Returns:
        the quantity value.
      • isBig

        public boolean isBig()
        Indicates if this measured amount is a big number, i.E. BigDecimal or BigInteger. In all other cases this would be false.
        Specified by:
        isBig in class AbstractQuantity<Q extends javax.measure.Quantity<Q>>
        Returns:
        true if this quantity is big; false otherwise.
      • add

        public ComparableQuantity<Q> add​(javax.measure.Quantity<Q> that)
        Specified by:
        add in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        add in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        See Also:
        Quantity.add(Quantity)
      • multiply

        public ComparableQuantity<?> multiply​(javax.measure.Quantity<?> that)
        Specified by:
        multiply in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        multiply in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        See Also:
        Quantity.multiply(Quantity)
      • multiply

        public ComparableQuantity<Q> multiply​(java.lang.Number that)
        Specified by:
        multiply in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        multiply in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        See Also:
        Quantity.multiply(Number)
      • divide

        public ComparableQuantity<?> divide​(javax.measure.Quantity<?> that)
        Specified by:
        divide in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        divide in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        See Also:
        Quantity.divide(Quantity)
      • divide

        public ComparableQuantity<Q> divide​(java.lang.Number that)
        Specified by:
        divide in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        divide in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        See Also:
        Quantity.divide(Number)
      • inverse

        public ComparableQuantity<Q> inverse()
        Specified by:
        inverse in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        inverse in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        See Also:
        Quantity.inverse()
      • decimalValue

        public java.math.BigDecimal decimalValue​(javax.measure.Unit<Q> unit)
                                          throws java.lang.ArithmeticException
        Specified by:
        decimalValue in class AbstractQuantity<Q extends javax.measure.Quantity<Q>>
        Throws:
        java.lang.ArithmeticException
      • subtract

        public ComparableQuantity<Q> subtract​(javax.measure.Quantity<Q> that)
        Specified by:
        subtract in interface ComparableQuantity<Q extends javax.measure.Quantity<Q>>
        Specified by:
        subtract in interface javax.measure.Quantity<Q extends javax.measure.Quantity<Q>>
        See Also:
        Quantity.subtract(Quantity)
      • of

        public static <Q extends javax.measure.Quantity<Q>> AbstractQuantity<Q> of​(long longValue,
                                                                                   javax.measure.Unit<Q> unit)
        Returns the scalar quantity for the specified long stated in the specified unit.
        Parameters:
        longValue - the quantity value.
        unit - the measurement unit.
        Returns:
        the corresponding int quantity.
      • of

        public static <Q extends javax.measure.Quantity<Q>> AbstractQuantity<Q> of​(int intValue,
                                                                                   javax.measure.Unit<Q> unit)
        Returns the scalar quantity for the specified int stated in the specified unit.
        Parameters:
        intValue - the quantity value.
        unit - the measurement unit.
        Returns:
        the corresponding int quantity.
      • of

        public static <Q extends javax.measure.Quantity<Q>> AbstractQuantity<Q> of​(short value,
                                                                                   javax.measure.Unit<Q> unit)
        Returns the scalar quantity for the specified short stated in the specified unit.
        Parameters:
        value - the quantity value.
        unit - the measurement unit.
        Returns:
        the corresponding short quantity.
      • of

        public static <Q extends javax.measure.Quantity<Q>> AbstractQuantity<Q> of​(byte value,
                                                                                   javax.measure.Unit<Q> unit)
        Returns the scalar quantity for the specified byte stated in the specified unit.
        Parameters:
        value - the quantity value.
        unit - the measurement unit.
        Returns:
        the corresponding byte quantity.
      • of

        public static <Q extends javax.measure.Quantity<Q>> AbstractQuantity<Q> of​(float floatValue,
                                                                                   javax.measure.Unit<Q> unit)
        Returns the scalar quantity for the specified float stated in the specified unit.
        Parameters:
        floatValue - the measurement value.
        unit - the measurement unit.
        Returns:
        the corresponding float quantity.
      • of

        public static <Q extends javax.measure.Quantity<Q>> AbstractQuantity<Q> of​(double doubleValue,
                                                                                   javax.measure.Unit<Q> unit)
        Returns the scalar quantity for the specified double stated in the specified unit.
        Parameters:
        doubleValue - the measurement value.
        unit - the measurement unit.
        Returns:
        the corresponding double quantity.
      • equals

        public boolean equals​(java.lang.Object obj)
        Description copied from class: AbstractQuantity
        Compares this quantity against the specified object for strict equality (same unit and same amount).

        Similarly to the BigDecimal.equals(java.lang.Object) method which consider 2.0 and 2.00 as different objects because of different internal scales, quantities such as Quantities.getQuantity(3.0, KILOGRAM) Quantities.getQuantity(3, KILOGRAM) and Quantities.getQuantity("3 kg") might not be considered equals because of possible differences in their implementations.

        To compare quantities stated using different units or using different amount implementations the compareTo or equals(Quantity, epsilon, epsilonUnit) methods should be used.

        Overrides:
        equals in class AbstractQuantity<Q extends javax.measure.Quantity<Q>>
        Parameters:
        obj - the object to compare with.
        Returns:
        this.getUnit.equals(obj.getUnit()) && this.getValue().equals(obj.getValue())