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

public Class PluralRules

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.IOException, .NotSerializableException, .ObjectInputStream, .ObjectOutputStream, .ObjectStreamException, .Serializable, java.math.BigDecimal, java.text.ParseException, java.util.ArrayList, .Collection, .Collections, .Iterator, .LinkedHashSet, .List, .Locale, .Set, java.util.regex.Pattern, com.ibm.icu.impl.PluralRulesLoader, .StandardPlural, com.ibm.icu.impl.number.DecimalQuantity, .DecimalQuantity_DualStorageBCD, com.ibm.icu.impl.number.range.StandardPluralRanges, com.ibm.icu.number.FormattedNumber, .FormattedNumberRange, .NumberFormatter, com.ibm.icu.util.Output, .ULocale

Defines rules for mapping non-negative numeric values onto a small set of keywords.

Rules are constructed from a text description, consisting of a series of keywords and conditions. The select method examines each condition in order and returns the keyword for the first condition that matches the number. If none match, KEYWORD_OTHER is returned.

A PluralRules object is immutable. It contains caches for sample values, but those are synchronized.

PluralRules is Serializable so that it can be used in formatters, which are serializable.

For more information, details, and tips for writing rules, see the LDML spec, Part 3.5 Language Plural Rules

Examples:

"one: n is 1; few: n in 2..4"

This defines two rules, for 'one' and 'few'. The condition for 'one' is "n is 1" which means that the number must be equal to 1 for this condition to pass. The condition for 'few' is "n in 2..4" which means that the number must be between 2 and 4 inclusive - and be an integer - for this condition to pass. All other numbers are assigned the keyword "other" by the default rule.

"zero: n is 0; one: n is 1; zero: n mod 100 in 1..19"

This illustrates that the same keyword can be defined multiple times. Each rule is examined in order, and the first keyword whose condition passes is the one returned. Also notes that a modulus is applied to n in the last rule. Thus its condition holds for 119, 219, 319...

"one: n is 1; few: n mod 10 in 2..4 and n mod 100 not in 12..14"

This illustrates conjunction and negation. The condition for 'few' has two parts, both of which must be met: "n mod 10 in 2..4" and "n mod 100 not in 12..14". The first part applies a modulus to n before the test as in the previous example. The second part applies a different modulus and also uses negation, thus it matches all numbers not in 12, 13, 14, 112, 113, 114, 212, 213, 214...

Syntax:

rules         = rule (';' rule)*
rule          = keyword ':' condition
keyword       = <identifier>
condition     = and_condition ('or' and_condition)*
and_condition = relation ('and' relation)*
relation      = not? expr not? rel not? range_list
expr          = ('n' | 'i' | 'f' | 'v' | 't') (mod value)?
not           = 'not' | '!'
rel           = 'in' | 'is' | '=' | '≠' | 'within'
mod           = 'mod' | '%'
range_list    = (range | value) (',' range_list)*
value         = digit+
digit         = 0|1|2|3|4|5|6|7|8|9
range         = value'..'value

Each not term inverts the meaning; however, there should not be more than one of them.

The i, f, t, and v values are defined as follows:

Examples are in the following table:

n i f v
1.0 1 0 1
1.00 1 0 2
1.3 1 3 1
1.03 1 3 2
1.23 1 23 2

An "identifier" is a sequence of characters that do not have the Unicode Pattern_Syntax or Pattern_White_Space properties.

The difference between 'in' and 'within' is that 'in' only includes integers in the specified range, while 'within' includes all values. Using 'within' with a range_list consisting entirely of values is the same as using 'in' (it's not an error).

ICU Status
Stable since ICU 3.8.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private abstract static class
private static interface
public static class
PluralRules.DecimalQuantitySamples

Deprecated This API is ICU internal only.
A list of DecimalQuantity representing PluralRules that includes all values with the same visibleFractionDigitCount.
public static class
PluralRules.DecimalQuantitySamplesRange

Deprecated This API is ICU internal only.
A range of DecimalQuantity representing PluralRules samples that includes all values with the same visibleFractionDigitCount.
public abstract static class
PluralRules.Factory

Deprecated This API is ICU internal only.
Provides a factory for returning plural rules
public static class
PluralRules.FixedDecimal

Deprecated This API is ICU internal only.
public static interface
PluralRules.IFixedDecimal

Deprecated This API is ICU internal only.
An interface to FixedDecimal, allowing for other implementations.
public static enum
PluralRules.KeywordStatus

Status of the keyword for the rules, given a set of explicit values.

public static enum
PluralRules.Operand

Deprecated This API is ICU internal only.
private static class
public static enum
PluralRules.PluralType

Type of plurals and PluralRules.

private static class
private static class
private static class
public static enum
PluralRules.SampleType

Deprecated This API is ICU internal only.
Selection parameter for either integer-only or decimal-only.
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv static final UnicodeSet
pack-priv static final Pattern
pack-priv static final Pattern
private static final String
pack-priv static final Pattern
public static final PluralRules
DEFAULT

The default rules that accept any number and return KEYWORD_OTHER.

private static final PluralRules.Rule
pack-priv static final Pattern
public static final String
KEYWORD_FEW

Common name for the 'paucal' or other special plural form.

public static final String
KEYWORD_MANY

Common name for the arabic (11 to 99) plural form.

public static final String
KEYWORD_ONE

Common name for the 'singular' plural form.

public static final String
KEYWORD_OTHER

Common name for the default plural form.

public static final String
KEYWORD_TWO

Common name for the 'dual' plural form.

public static final String
KEYWORD_ZERO

Common name for the 'zero' plural form.

private final transient Set<String>
private static final PluralRules.Constraint
public static final double
NO_UNIQUE_VALUE

Value returned by getUniqueKeywordValue when there is no unique value to return.

public static final DecimalQuantity
NO_UNIQUE_VALUE_DECIMAL_QUANTITY

Value returned by getUniqueKeywordDecimalQuantityValue when there is no unique value to return.

pack-priv static final Pattern
private final PluralRules.RuleList
pack-priv static final Pattern
private static final long
private final transient StandardPluralRanges
pack-priv static final Pattern

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private boolean
private static void
addRange(StringBuilder result, double lb, double ub, boolean addSeparator)

private boolean
addSample(String keyword, DecimalQuantity sample, int maxCount, Set<DecimalQuantity> result)

Deprecated as a consequence of DecimalQuantity being deprecated.
public int
compareTo(PluralRules other)

Deprecated internal
public boolean
computeLimited(String keyword, PluralRules.SampleType sampleType)

Deprecated internal
public static PluralRules

Returns:

the PluralRules
createRules
(String
the rule description.
description
)

Creates a PluralRules from a description if it is parsable, otherwise returns null.

public boolean
equals(Object
the reference object with which to compare.
rhs
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public boolean

Returns:

true if this and rhs are equal.
equals
(PluralRules
the PluralRules to compare to.
rhs
)

Returns true if rhs is equal to this.

public static PluralRules

Returns:

The predefined PluralRules object for this locale. If there's no predefined rules for this locale, the rules for the closest parent in the locale hierarchy that has one will be returned. The final fallback always returns the default rules.
forLocale
(ULocale
The locale for which a PluralRules object is returned.
locale
)

Provides access to the predefined cardinal-number PluralRules for a given locale.

public static PluralRules

Returns:

The predefined PluralRules object for this locale. If there's no predefined rules for this locale, the rules for the closest parent in the locale hierarchy that has one will be returned. The final fallback always returns the default rules.
forLocale
(Locale
The locale for which a PluralRules object is returned.
locale
)

Provides access to the predefined cardinal-number PluralRules for a given java.util.Locale.

public static PluralRules

Returns:

The predefined PluralRules object for this locale. If there's no predefined rules for this locale, the rules for the closest parent in the locale hierarchy that has one will be returned. The final fallback always returns the default rules.
forLocale
(ULocale
The locale for which a PluralRules object is returned.
locale
,
PluralRules.PluralType
The plural type (e.g., cardinal or ordinal).
type
)

Provides access to the predefined PluralRules for a given locale and the plural type.

public static PluralRules

Returns:

The predefined PluralRules object for this locale. If there's no predefined rules for this locale, the rules for the closest parent in the locale hierarchy that has one will be returned. The final fallback always returns the default rules.
forLocale
(Locale
The locale for which a PluralRules object is returned.
locale
,
PluralRules.PluralType
The plural type (e.g., cardinal or ordinal).
type
)

Provides access to the predefined PluralRules for a given java.util.Locale and the plural type.

private static String
format(double lb)

public Collection<DecimalQuantity>

Returns:

the values that trigger this keyword, or null. The returned collection is immutable. It will be empty if the keyword is not defined.
getAllKeywordDecimalQuantityValues
(String
the keyword
keyword
)

Deprecated This API is ICU internal only.
Returns all the values that trigger this keyword, or null if the number of such values is unlimited.
public Collection<Double>

Returns:

the values that trigger this keyword, or null. The returned collection is immutable. It will be empty if the keyword is not defined.
getAllKeywordValues
(String
the keyword
keyword
)

Returns all the values that trigger this keyword, or null if the number of such values is unlimited.

public Collection<DecimalQuantity>

Returns:

the values that trigger this keyword, or null. The returned collection is immutable. It will be empty if the keyword is not defined.
getAllKeywordValues
(String
the keyword
keyword
,
PluralRules.SampleType
the type of samples requested, INTEGER or DECIMAL
type
)

Deprecated This API is ICU internal only.
Returns all the values that trigger this keyword, or null if the number of such values is unlimited.
public static ULocale[]

Returns:

the set of locales for which PluralRules are known, as a list
getAvailableULocales
()

Returns the set of locales for which PluralRules are known.

public Collection<DecimalQuantity>

Returns:

a list of values matching the keyword.
getDecimalQuantitySamples
(String
the keyword to test
keyword
)

Deprecated ICU internal only
Returns a list of integer values for which select() would return that keyword, or null if the keyword is not defined.
public Collection<DecimalQuantity>

Returns:

a list of values matching the keyword.
getDecimalQuantitySamples
(String
the keyword to test
keyword
,
PluralRules.SampleType
the type of samples requested, INTEGER or DECIMAL
sampleType
)

Deprecated ICU internal only
Returns a list of values for which select() would return that keyword, or null if the keyword is not defined.
public PluralRules.DecimalQuantitySamples

Returns:

a list of values matching the keyword.
getDecimalSamples
(String
the keyword to test
keyword
,
PluralRules.SampleType
the type of samples requested, INTEGER or DECIMAL
sampleType
)

Deprecated This API is ICU internal only.
Returns a list of values for which select() would return that keyword, or null if the keyword is not defined or no samples are available.
public static ULocale

Returns:

the functionally-equivalent locale
getFunctionalEquivalent
(ULocale
the locale to check
locale
,
boolean[]
if not null and of length > 0, this will hold 'true' at index 0 if locale is directly defined (without fallback) as having plural rules
isAvailable
)

Returns the 'functionally equivalent' locale with respect to plural rules.

public Set<String>

Returns:

The set of keywords.
getKeywords
()

Returns a set of all rule keywords used in this PluralRules object.

public PluralRules.KeywordStatus

Returns:

the KeywordStatus
getKeywordStatus
(String
the particular keyword (call rules.getKeywords() to get the valid ones)
keyword
,
int
the offset used, or 0.0d if not. Internally, the offset is subtracted from each explicit value before checking against the keyword values.
offset
,
Set<DecimalQuantity>
a set of DecimalQuantitys that are used explicitly (eg [=0], "[=1]"). May be empty or null.
explicits
,
Output<DecimalQuantity>
If non null, set to the unique value.
uniqueValue
)

Find the status for the keyword, given a certain set of explicit values.

public PluralRules.KeywordStatus

Returns:

the KeywordStatus
getKeywordStatus
(String
the particular keyword (call rules.getKeywords() to get the valid ones)
keyword
,
int
the offset used, or 0.0d if not. Internally, the offset is subtracted from each explicit value before checking against the keyword values.
offset
,
Set<DecimalQuantity>
a set of DecimalQuantitys that are used explicitly (eg [=0], "[=1]"). May be empty or null.
explicits
,
Output<DecimalQuantity>
If non null, set to the unique value.
uniqueValue
,
PluralRules.SampleType
request KeywordStatus relative to INTEGER or DECIMAL values
sampleType
)

Deprecated This API is ICU internal only.
Find the status for the keyword, given a certain set of explicit values.
public String
getRules(String keyword)

Deprecated This API is ICU internal only.
public Collection<Double>

Returns:

a list of values matching the keyword.
getSamples
(String
the keyword to test
keyword
)

Returns a list of integer values for which select() would return that keyword, or null if the keyword is not defined.

public Collection<Double>

Returns:

a list of values matching the keyword.
getSamples
(String
the keyword to test
keyword
,
PluralRules.SampleType
the type of samples requested, INTEGER or DECIMAL
sampleType
)

Deprecated ICU internal only
Returns a list of values for which select() would return that keyword, or null if the keyword is not defined.
public DecimalQuantity
getUniqueKeywordDecimalQuantityValue(String
the keyword to check for a unique value
keyword
)

Deprecated This API is ICU internal only.
Returns the unique value that this keyword matches, or NO_UNIQUE_VALUE if the keyword matches multiple values or is not defined for this PluralRules.
public double

Returns:

The unique value for the keyword, or NO_UNIQUE_VALUE.
getUniqueKeywordValue
(String
the keyword to check for a unique value
keyword
)

Returns the unique value that this keyword matches, or NO_UNIQUE_VALUE if the keyword matches multiple values or is not defined for this PluralRules.

public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
pack-priv Boolean
isLimited(String keyword)

public boolean
isLimited(String keyword, PluralRules.SampleType sampleType)

Deprecated internal
private static boolean
public boolean
matches(PluralRules.FixedDecimal
The number information for which the rule has to be determined.
sample
,
String
The keyword to filter on
keyword
)

Deprecated This API is ICU internal only.
Given a number information, and keyword, return whether the keyword would match the number.
public static PluralRules
newInternal(String description, StandardPluralRanges ranges)

Deprecated This API is ICU internal only.
private static String
nextToken(String[] tokens, int x, String context)

private static PluralRules.Constraint
parseConstraint(String description)

public static PluralRules
parseDescription(String
the rule description.
description
)

Parses a plural rules description and returns a PluralRules.

private static PluralRules.Rule
parseRule(String description)

private static PluralRules.RuleList
parseRuleChain(String description)

private void
public String

Returns:

The keyword of the selected rule.
select
(double
The number for which the rule has to be determined.
number
)

Given a floating-point number, returns the keyword of the first rule that applies to the number.

public String

Returns:

The keyword of the selected rule.
select
(FormattedNumber
The number for which the rule has to be determined.
number
)

Given a formatted number, returns the keyword of the first rule that applies to the number.

public String

Returns:

The keyword of the selected rule.
select
(FormattedNumberRange
The number range onto which the rules will be applied.
range
)

Given a formatted number range, returns the overall plural form of the range.

public String

Returns:

The keyword of the selected rule.
select
(double
The number for which the rule has to be determined.
number
,
int countVisibleFractionDigits, long fractionaldigits)

Deprecated This API is ICU internal only.
Given a number, returns the keyword of the first rule that applies to the number.
public String

Returns:

The keyword of the selected rule.
select
(PluralRules.IFixedDecimal
The number information for which the rule has to be determined.
number
)

Deprecated This API is ICU internal only.
Given a number information, returns the keyword of the first rule that applies to the number.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
private static ParseException
unexpected(String token, String context)

private void
private Object
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait