Class Quantities


  • public final class Quantities
    extends Object
    Facade to access Quantity instances.
    Since:
    1.0
    Version:
    3.0, October 3, 2024
    Author:
    Werner Keil, Otavio Santana
    • Method Detail

      • getQuantity

        public static Quantity<?> getQuantity​(CharSequence csq)
        Returns the scalar quantity of unknown type corresponding to the specified representation. This method can be used to parse mixed quantities. All of these expressions:
        Quantity<Length> height = Quantities.getQuantity("1.70 m").asType(Length.class);
        Quantity<Length> heightinCm = Quantities.getQuantity("170 cm").asType(Length.class);
        Quantity<Length> heightMixed = Quantities.getQuantity("1 m 70 cm").asType(Length.class);
        are equally supported.

        Note: This method handles only Locale-neutral quantity formatting and parsing are handled by the SimpleQuantityFormat class.
        Due to the versatile parsing of this method recognizing both single and mixed quantities, a unit must be provided, otherwise it'll fail. If you need to parse a unit-less quantity, please use the parse() method of AbstractQuantity instead.

        Parameters:
        csq - the decimal value(s) and unit(s) separated by space(s).
        Returns:
        SimpleQuantityFormat.getInstance("n u~ ").parse(csq)
        Throws:
        IllegalArgumentException - if no unit part was provided to parse
      • getQuantity

        public static <Q extends Quantity<Q>> Quantity<Q> getQuantity​(Number[] values,
                                                                      Unit<Q>[] units,
                                                                      Quantity.Scale scale)
        Returns the mixed radix values and units combined into a single quantity of type NumberQuantity in the specified unit and scale.
        Parameters:
        values - the measurement values.
        units - the measurement units.
        scale - the measurement scale.
        Returns:
        the corresponding quantity.
        Throws:
        NullPointerException - if values or scale were null
        IllegalArgumentException - if the size of the values array does not match that of units.
        Since:
        2.0
      • getQuantity

        @SafeVarargs
        public static <Q extends Quantity<Q>> Quantity<Q> getQuantity​(Number[] values,
                                                                      Unit<Q>... units)
        Returns the mixed radix values and units combined into a single quantity in the ABSOLUTE scale.
        Parameters:
        values - the measurement values.
        units - the measurement units.
        Returns:
        the corresponding quantity.
        Throws:
        NullPointerException - if values or units were null
        IllegalArgumentException - if the size of the values array does not match that of units.
        Since:
        2.0