Class QuantityFormatter


  • class QuantityFormatter
    extends java.lang.Object
    QuantityFormatter represents an unknown quantity of something and formats a known quantity in terms of that something. For example, a QuantityFormatter that represents X apples may format 1 as "1 apple" and 3 as "3 apples"

    QuanitityFormatter appears here instead of in com.ibm.icu.impl because it depends on PluralRules and DecimalFormat. It is package-protected as it is not meant for public use.

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addIfAbsent​(java.lang.CharSequence variant, java.lang.String template)
      Adds a template if there is none yet for the plural form.
      java.lang.String format​(double number, NumberFormat numberFormat, PluralRules pluralRules)
      Format formats a number with this object.
      static java.lang.StringBuilder format​(java.lang.String compiledPattern, java.lang.CharSequence value, java.lang.StringBuilder appendTo, java.text.FieldPosition pos)
      Formats the pattern with the value and adjusts the FieldPosition.
      SimpleFormatter getByVariant​(java.lang.CharSequence variant)
      Gets the SimpleFormatter for a particular variant.
      boolean isValid()  
      static StandardPlural selectPlural​(double number, NumberFormat numberFormat, PluralRules rules)
      Selects the standard plural form for the number/formatter/rules.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • QuantityFormatter

        public QuantityFormatter()
    • Method Detail

      • addIfAbsent

        public void addIfAbsent​(java.lang.CharSequence variant,
                                java.lang.String template)
        Adds a template if there is none yet for the plural form.
        Parameters:
        variant - the plural variant, e.g "zero", "one", "two", "few", "many", "other"
        template - the text for that plural variant with "{0}" as the quantity. For example, in English, the template for the "one" variant may be "{0} apple" while the template for the "other" variant may be "{0} apples"
        Throws:
        java.lang.IllegalArgumentException - if variant is not recognized or if template has more than just the {0} placeholder.
      • isValid

        public boolean isValid()
        Returns:
        true if this object has at least the "other" variant
      • format

        public java.lang.String format​(double number,
                                       NumberFormat numberFormat,
                                       PluralRules pluralRules)
        Format formats a number with this object.
        Parameters:
        number - the number to be formatted
        numberFormat - used to actually format the number.
        pluralRules - uses the number and the numberFormat to determine what plural variant to use for fetching the formatting template.
        Returns:
        the formatted string e.g '3 apples'
      • getByVariant

        public SimpleFormatter getByVariant​(java.lang.CharSequence variant)
        Gets the SimpleFormatter for a particular variant.
        Parameters:
        variant - "zero", "one", "two", "few", "many", "other"
        Returns:
        the SimpleFormatter
      • selectPlural

        public static StandardPlural selectPlural​(double number,
                                                  NumberFormat numberFormat,
                                                  PluralRules rules)
        Selects the standard plural form for the number/formatter/rules.
      • format

        public static java.lang.StringBuilder format​(java.lang.String compiledPattern,
                                                     java.lang.CharSequence value,
                                                     java.lang.StringBuilder appendTo,
                                                     java.text.FieldPosition pos)
        Formats the pattern with the value and adjusts the FieldPosition.