Enum Class Cardinality
- All Implemented Interfaces:
LanguageForm
,Serializable
,Comparable<Cardinality>
,Constable
For example, English has two: 1 dog, 2 dogs
, while Welsh has many: 0 cŵn, 1 ci, 2 gi, 3 chi, 4 ci
.
See the Unicode Common Locale Data Repository and its Language Plural Rules for details.
Per the CLDR:
These categories are only mnemonics -- the names don't necessarily imply the exact contents of the category. For example, for both English and French the number 1 has the category one (singular).In English, every other number has a plural form, and is given the category other. French is similar, except that the number 0 also has the category one and not other or zero, because the form of units qualified by 0 is also singular.
This is worth emphasizing: A common mistake is to think that "one" is only for only the number 1. Instead, "one" is a category for any number that behaves like 1. So in some languages, for example, one → numbers that end in "1" (like 1, 21, 151) but that don't end in 11 (like "11, 111, 10311).
- Author:
- Mark Allen
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Enum
Enum.EnumDesc<E extends Enum<E>>
-
Enum Constant Summary
Enum ConstantDescriptionThe form that falls betweenTWO
andMANY
.The form that falls betweenFEW
andOTHER
.The form used with 1.General "catchall" form which comprises any cases not handled by the other forms.Normally the form used with 2, if it is limited to numbers whose integer values end with 2.Normally the form used with 0, if it is limited to numbers whose integer values end with 0. -
Method Summary
Modifier and TypeMethodDescriptionstatic SortedMap<Cardinality,
Range<BigDecimal>> exampleDecimalValuesForLocale
(Locale locale) Gets a mapping of cardinalities to example decimal values for the given locale.static SortedMap<Cardinality,
Range<Integer>> exampleIntegerValuesForLocale
(Locale locale) Gets a mapping of cardinalities to example integer values for the given locale.static Cardinality
Gets an appropriate plural cardinality for the given number, visible decimal places, and locale.static Cardinality
Gets an appropriate plural cardinality for the given number and locale.static Cardinality
forRange
(Cardinality start, Cardinality end, Locale locale) Gets an appropriate plural cardinality for the given range (start, end) and locale.Gets the ISO 639 language codes for which cardinality operations are supported.static SortedSet<Cardinality>
Gets the set of cardinalities supported for the given locale.static Cardinality
Returns the enum constant of this class with the specified name.static Cardinality[]
values()
Returns an array containing the constants of this enum class, in the order they are declared.Methods inherited from class java.lang.Enum
compareTo, describeConstable, equals, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
-
Enum Constant Details
-
ZERO
Normally the form used with 0, if it is limited to numbers whose integer values end with 0.For example: the Welsh
0 cŵn, 0 cathod
means0 dogs, 0 cats
in English. -
ONE
The form used with 1.For example: the Welsh
1 ci, 1 gath
means1 dog, 1 cat
in English. -
TWO
Normally the form used with 2, if it is limited to numbers whose integer values end with 2.For example: the Welsh
2 gi, 2 gath
means2 dogs, 2 cats
in English. -
FEW
The form that falls betweenTWO
andMANY
.For example: the Welsh
3 chi, 3 cath
means3 dogs, 3 cats
in English. -
MANY
The form that falls betweenFEW
andOTHER
.For example: the Welsh
6 chi, 6 chath
means6 dogs, 6 cats
in English. -
OTHER
General "catchall" form which comprises any cases not handled by the other forms.For example: the Welsh
4 ci, 4 cath
means4 dogs, 4 cats
in English.
-
-
Method Details
-
values
Returns an array containing the constants of this enum class, in the order they are declared.- Returns:
- an array containing the constants of this enum class, in the order they are declared
-
valueOf
Returns the enum constant of this class with the specified name. The string must match exactly an identifier used to declare an enum constant in this class. (Extraneous whitespace characters are not permitted.)- Parameters:
name
- the name of the enum constant to be returned.- Returns:
- the enum constant with the specified name
- Throws:
IllegalArgumentException
- if this enum class has no constant with the specified nameNullPointerException
- if the argument is null
-
forNumber
Gets an appropriate plural cardinality for the given number and locale.When determining cardinality, the decimal places of
number
will be computed and used. Note that if trailing zeroes are important, e.g.1.00
instead of1
, you must either specify aBigDecimal
with appropriate scale or supply a non-nullvisibleDecimalPlaces
value.If you do not provide a
BigDecimal
and wish to manually specify the number of visible decimals, useforNumber(Number, Integer, Locale)
instead.See the CLDR Language Plural Rules for further details.
- Parameters:
number
- the number that drives pluralization, not nulllocale
- the locale that drives pluralization, not null- Returns:
- an appropriate plural cardinality, not null
- Throws:
UnsupportedLocaleException
- if the locale is not supported
-
forNumber
@Nonnull public static Cardinality forNumber(@Nonnull Number number, @Nullable Integer visibleDecimalPlaces, @Nonnull Locale locale) Gets an appropriate plural cardinality for the given number, visible decimal places, and locale.If
visibleDecimalPlaces
is null, then the decimal places ofnumber
will be computed and used. Note that if trailing zeroes are important, e.g.1.00
instead of1
, you must either specify aBigDecimal
with appropriate scale or supply a non-nullvisibleDecimalPlaces
value.See the CLDR Language Plural Rules for further details.
- Parameters:
number
- the number that drives pluralization, not nullvisibleDecimalPlaces
- the number of decimal places that will ultimately be displayed, may be nulllocale
- the locale that drives pluralization, not null- Returns:
- an appropriate plural cardinality, not null
- Throws:
UnsupportedLocaleException
- if the locale is not supported
-
forRange
@Nonnull public static Cardinality forRange(@Nonnull Cardinality start, @Nonnull Cardinality end, @Nonnull Locale locale) Gets an appropriate plural cardinality for the given range (start, end) and locale.For example, a range might be
"1-3 hours"
.Note that the cardinality of the end of the range does not necessarily determine the range's cardinality. In English, we say
"0–1 days"
- the value1
isCARDINALITY_ONE
but the range isCARDINALITY_OTHER
.See the CLDR Language Plural Rules for further details.
- Parameters:
start
- the cardinality for the start of the range, not nullend
- the cardinality for the end of the range, not nulllocale
- the locale that drives pluralization, not null- Returns:
- an appropriate plural cardinality for the range, not null
- Throws:
UnsupportedLocaleException
- if the locale is not supported
-
supportedCardinalitiesForLocale
@Nonnull public static SortedSet<Cardinality> supportedCardinalitiesForLocale(@Nonnull Locale locale) Gets the set of cardinalities supported for the given locale.The empty set will be returned if the locale is not supported.
The set's values are sorted by the natural ordering of the
Cardinality
enumeration.- Parameters:
locale
- the locale to use for lookup, not null- Returns:
- the cardinalities supported by the given locale, not null
-
exampleIntegerValuesForLocale
@Nonnull public static SortedMap<Cardinality,Range<Integer>> exampleIntegerValuesForLocale(@Nonnull Locale locale) Gets a mapping of cardinalities to example integer values for the given locale.The empty map will be returned if the locale is not supported or if no example values are available.
The map's keys are sorted by the natural ordering of the
Cardinality
enumeration.- Parameters:
locale
- the locale to use for lookup, not null- Returns:
- a mapping of cardinalities to example integer values, not null
-
exampleDecimalValuesForLocale
@Nonnull public static SortedMap<Cardinality,Range<BigDecimal>> exampleDecimalValuesForLocale(@Nonnull Locale locale) Gets a mapping of cardinalities to example decimal values for the given locale.The empty map will be returned if the locale is not supported or if no example values are available.
The map's keys are sorted by the natural ordering of the
Cardinality
enumeration.- Parameters:
locale
- the locale to use for lookup, not null- Returns:
- a mapping of cardinalities to example decimal values, not null
-
getSupportedLanguageCodes
Gets the ISO 639 language codes for which cardinality operations are supported.The set's values are ISO 639 codes and therefore sorted using English collation.
- Returns:
- the ISO 639 language codes for which cardinality operations are supported, not null
-