Package com.ibm.icu.impl.number
Class LongNameHandler
java.lang.Object
com.ibm.icu.impl.number.LongNameHandler
- All Implemented Interfaces:
LongNameMultiplexer.ParentlessMicroPropsGenerator
,MicroPropsGenerator
,ModifierStore
public class LongNameHandler
extends Object
implements MicroPropsGenerator, ModifierStore, LongNameMultiplexer.ParentlessMicroPropsGenerator
Takes care of formatting currency and measurement unit names, as well as populating the gender of measure units.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static final class
private static class
Loads and applies deriveComponent rules from CLDR's grammaticalFeatures.xml.private static class
private static final class
private static enum
private static final class
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final int
private static final int
private static final int
private String
private static final int
private static int
private final Map
<StandardPlural, SimpleModifier> private final MicroPropsGenerator
private static final int
private final PluralRules
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
LongNameHandler
(Map<StandardPlural, SimpleModifier> modifiers, PluralRules rules, MicroPropsGenerator parent) -
Method Summary
Modifier and TypeMethodDescriptionprivate static String
calculateGenderForUnit
(ULocale locale, MeasureUnit unit) Calculates the gender of an arbitrary unit: this is the *second* implementation of an algorithm to do this: Gender is also calculated in "processPatternTimes": that code path is "bottom up", loading the gender for every component of a compound unit (at the same time as loading the Long Names formatting patterns), even if the gender is unneeded, then combining the single units' genders into the compound unit's gender, according to the rules.private static LongNameHandler.ExtractCorePatternResult
extractCorePattern
(String pattern) Returns three outputs extracted from pattern.private static LongNameHandler
forArbitraryUnit
(ULocale loc, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent) static LongNameHandler
forCurrencyLongNames
(ULocale locale, Currency currency, PluralRules rules, MicroPropsGenerator parent) static LongNameHandler
forMeasureUnit
(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent) Construct a localized LongNameHandler for the specified MeasureUnit.private static String
getCompoundValue
(String compoundKey, ULocale locale, NumberFormatter.UnitWidth width) private static void
getCurrencyLongNameData
(ULocale locale, Currency currency, String[] outArray) private static String
getDeriveCompoundRule
(ULocale locale, String feature, String structure) Loads and returns the value in rules that look like these:Currently a fake example, but spec compliant: NOTE: If U_FAILURE(status), returns an empty string. private static String
getDerivedGender
(ULocale locale, String structure, String[] data0, String[] data1) private static String
getGenderForBuiltin
(ULocale locale, MeasureUnit builtinUnit) private static int
(package private) static void
getInflectedMeasureData
(String subKey, ULocale locale, NumberFormatter.UnitWidth width, String gender, String caseVariant, String[] outArray) (package private) static void
getMeasureData
(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, String[] outArray) getModifier
(Modifier.Signum signum, StandardPlural plural) Returns a Modifier with the given parameters (best-effort).static String
getUnitDisplayName
(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width) (package private) static String
getWithPlural
(String[] strings, StandardPlural plural) private static void
maybeCalculateGender
(ULocale locale, MeasureUnit unit, String[] outArray) private void
multiSimpleFormatsToModifiers
(String[] leadFormats, String trailFormat, NumberFormat.Field field) Sets modifiers to a combination ofleadFormats
(one per plural form) andtrailFormat
appended to each.private static void
processPatternTimes
(MeasureUnitImpl productUnit, ULocale loc, NumberFormatter.UnitWidth width, String caseVariant, String[] outArray) Roughly corresponds to patternTimes(...) in the spec: https://unicode.org/reports/tr35/tr35-general.html#compound-unitsprocessQuantity
(DecimalQuantity quantity) Considers the givenDecimalQuantity
, optionally mutates it, and returns aMicroProps
.processQuantityWithMicros
(DecimalQuantity quantity, MicroProps micros) Produces a plural-appropriate Modifier for a unit:quantity
is taken as the final smallest unit, while the larger unit values must be provided viamicros.mixedMeasures
.private void
simpleFormatsToModifiers
(String[] simpleFormats, NumberFormat.Field field) Sets modifiers to use the patterns from simpleFormats.
-
Field Details
-
i
private static int i -
DNAM_INDEX
private static final int DNAM_INDEX -
PER_INDEX
private static final int PER_INDEX -
GENDER_INDEX
private static final int GENDER_INDEX -
CONSTANT_DENOMINATOR_INDEX
private static final int CONSTANT_DENOMINATOR_INDEX -
ARRAY_LENGTH
static final int ARRAY_LENGTH -
modifiers
-
rules
-
parent
-
gender
-
-
Constructor Details
-
LongNameHandler
private LongNameHandler(Map<StandardPlural, SimpleModifier> modifiers, PluralRules rules, MicroPropsGenerator parent)
-
-
Method Details
-
getIndex
-
getWithPlural
-
extractCorePattern
Returns three outputs extracted from pattern.- Parameters:
coreUnit
- is extracted as per Extract(...) in the spec: https://unicode.org/reports/tr35/tr35-general.html#compound-unitsPlaceholderPosition
- indicates where in the string the placeholder was found.joinerChar
- Iff the placeholder was at the beginning or end, joinerChar contains the space character (if any) that separated the placeholder from the rest of the pattern. Otherwise, joinerChar is set to NUL. Only one space character is considered.
-
getGenderForBuiltin
-
getInflectedMeasureData
-
getMeasureData
static void getMeasureData(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, String[] outArray) -
getCurrencyLongNameData
-
getCompoundValue
private static String getCompoundValue(String compoundKey, ULocale locale, NumberFormatter.UnitWidth width) -
getDeriveCompoundRule
Loads and returns the value in rules that look like these:Currently a fake example, but spec compliant: NOTE: If U_FAILURE(status), returns an empty string. -
getDerivedGender
-
calculateGenderForUnit
Calculates the gender of an arbitrary unit: this is the *second* implementation of an algorithm to do this: Gender is also calculated in "processPatternTimes": that code path is "bottom up", loading the gender for every component of a compound unit (at the same time as loading the Long Names formatting patterns), even if the gender is unneeded, then combining the single units' genders into the compound unit's gender, according to the rules. This algorithm does a lazier "top-down" evaluation, starting with the compound unit, calculating which single unit's gender is needed by breaking it down according to the rules, and then loading only the gender of the one single unit who's gender is needed. For future refactorings: 1. we could drop processPatternTimes' gender calculation and just call this function: for UNUM_UNIT_WIDTH_FULL_NAME, the unit gender is in the very same table as the formatting patterns, so loading it then may be efficient. For other unit widths however, it needs to be explicitly looked up anyway. 2. alternatively, if CLDR is providing all the genders we need such that we don't need to calculate them in ICU anymore, we could drop this function and keep only processPatternTimes' calculation. (And optimise it a bit?)- Parameters:
locale
- The desired locale.unit
- The measure unit to calculate the gender for.- Returns:
- The gender string for the unit, or an empty string if unknown or ungendered.
-
maybeCalculateGender
-
getUnitDisplayName
public static String getUnitDisplayName(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width) -
forCurrencyLongNames
public static LongNameHandler forCurrencyLongNames(ULocale locale, Currency currency, PluralRules rules, MicroPropsGenerator parent) -
forMeasureUnit
public static LongNameHandler forMeasureUnit(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent) Construct a localized LongNameHandler for the specified MeasureUnit.Mixed units are not supported, use MixedUnitLongNameHandler.forMeasureUnit.
- Parameters:
locale
- The desired locale.unit
- The measure unit to construct a LongNameHandler for.width
- Specifies the desired unit rendering.unitDisplayCase
- Specifies the desired grammatical case. If the specified case is not found, we fall back to nominative or no-case.rules
- Plural rules.parent
- Plural rules.
-
forArbitraryUnit
private static LongNameHandler forArbitraryUnit(ULocale loc, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent) -
processPatternTimes
private static void processPatternTimes(MeasureUnitImpl productUnit, ULocale loc, NumberFormatter.UnitWidth width, String caseVariant, String[] outArray) Roughly corresponds to patternTimes(...) in the spec: https://unicode.org/reports/tr35/tr35-general.html#compound-units -
simpleFormatsToModifiers
Sets modifiers to use the patterns from simpleFormats. -
multiSimpleFormatsToModifiers
private void multiSimpleFormatsToModifiers(String[] leadFormats, String trailFormat, NumberFormat.Field field) Sets modifiers to a combination ofleadFormats
(one per plural form) andtrailFormat
appended to each. With a leadFormat of "{0}m" and a trailFormat of "{0}/s", it produces a pattern of "{0}m/s" by inserting each leadFormat pattern into trailFormat. -
processQuantity
Description copied from interface:MicroPropsGenerator
Considers the givenDecimalQuantity
, optionally mutates it, and returns aMicroProps
.- Specified by:
processQuantity
in interfaceMicroPropsGenerator
- Parameters:
quantity
- The quantity for consideration and optional mutation.- Returns:
- A MicroProps instance resolved for the quantity.
-
processQuantityWithMicros
Produces a plural-appropriate Modifier for a unit:quantity
is taken as the final smallest unit, while the larger unit values must be provided viamicros.mixedMeasures
. Does not call parent.processQuantity, so cannot get a MicroProps instance that way. Instead, the instance is passed in as a parameter.- Specified by:
processQuantityWithMicros
in interfaceLongNameMultiplexer.ParentlessMicroPropsGenerator
-
getModifier
Description copied from interface:ModifierStore
Returns a Modifier with the given parameters (best-effort).- Specified by:
getModifier
in interfaceModifierStore
-