Top Description Fields Constructors Methods
com.ibm.icu.text

pack-priv Class QuantityFormatter

extends Object
Class Inheritance
Imports
java.text.FieldPosition, com.ibm.icu.impl.SimpleFormatterImpl, .StandardPlural

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.

Field Summary

Modifier and TypeField and Description
private final SimpleFormatter[]

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addIfAbsent(CharSequence
the plural variant, e.g "zero", "one", "two", "few", "many", "other"
variant
,
String
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"
template
)

Adds a template if there is none yet for the plural form.

public String

Returns:

the formatted string e.g '3 apples'
format
(double
the number to be formatted
number
,
NumberFormat
used to actually format the number.
numberFormat
,
PluralRules
uses the number and the numberFormat to determine what plural variant to use for fetching the formatting template.
pluralRules
)

Format formats a number with this object.

public static StringBuilder
format(String compiledPattern, CharSequence value, StringBuilder appendTo, FieldPosition pos)

Formats the pattern with the value and adjusts the FieldPosition.

public SimpleFormatter

Returns:

the SimpleFormatter
getByVariant
(CharSequence
"zero", "one", "two", "few", "many", "other"
variant
)

Gets the SimpleFormatter for a particular variant.

public boolean

Returns:

true if this object has at least the "other" variant
isValid
()

public static StandardPlural
selectPlural(double number, NumberFormat numberFormat, PluralRules rules)

Selects the standard plural form for the number/formatter/rules.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

templatesback to summary
private final SimpleFormatter[] templates

Constructor Detail

QuantityFormatterback to summary
public QuantityFormatter()

Method Detail

addIfAbsentback to summary
public void addIfAbsent(CharSequence variant, String template)

Adds a template if there is none yet for the plural form.

Parameters
variant:CharSequence

the plural variant, e.g "zero", "one", "two", "few", "many", "other"

template:String

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"

Exceptions
IllegalArgumentException:
if variant is not recognized or if template has more than just the {0} placeholder.
formatback to summary
public String format(double number, NumberFormat numberFormat, PluralRules pluralRules)

Format formats a number with this object.

Parameters
number:double

the number to be formatted

numberFormat:NumberFormat

used to actually format the number.

pluralRules:PluralRules

uses the number and the numberFormat to determine what plural variant to use for fetching the formatting template.

Returns:String

the formatted string e.g '3 apples'

formatback to summary
public static StringBuilder format(String compiledPattern, CharSequence value, StringBuilder appendTo, FieldPosition pos)

Formats the pattern with the value and adjusts the FieldPosition.

getByVariantback to summary
public SimpleFormatter getByVariant(CharSequence variant)

Gets the SimpleFormatter for a particular variant.

Parameters
variant:CharSequence

"zero", "one", "two", "few", "many", "other"

Returns:SimpleFormatter

the SimpleFormatter

isValidback to summary
public boolean isValid()
Returns:boolean

true if this object has at least the "other" variant

selectPluralback to summary
public static StandardPlural selectPlural(double number, NumberFormat numberFormat, PluralRules rules)

Selects the standard plural form for the number/formatter/rules.