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 classprivate static classLoads and applies deriveComponent rules from CLDR's grammaticalFeatures.xml.private static classprivate static final classprivate static enumprivate static final class -
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final intprivate static final intprivate static final intprivate Stringprivate static final intprivate static intprivate final Map<StandardPlural, SimpleModifier> private final MicroPropsGeneratorprivate static final intprivate final PluralRules -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivateLongNameHandler(Map<StandardPlural, SimpleModifier> modifiers, PluralRules rules, MicroPropsGenerator parent) -
Method Summary
Modifier and TypeMethodDescriptionprivate static StringcalculateGenderForUnit(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.ExtractCorePatternResultextractCorePattern(String pattern) Returns three outputs extracted from pattern.private static LongNameHandlerforArbitraryUnit(ULocale loc, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent) static LongNameHandlerforCurrencyLongNames(ULocale locale, Currency currency, PluralRules rules, MicroPropsGenerator parent) static LongNameHandlerforMeasureUnit(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width, String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent) Construct a localized LongNameHandler for the specified MeasureUnit.private static StringgetCompoundValue(String compoundKey, ULocale locale, NumberFormatter.UnitWidth width) private static voidgetCurrencyLongNameData(ULocale locale, Currency currency, String[] outArray) private static StringgetDeriveCompoundRule(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 StringgetDerivedGender(ULocale locale, String structure, String[] data0, String[] data1) private static StringgetGenderForBuiltin(ULocale locale, MeasureUnit builtinUnit) /////////////////////// BEGIN DATA LOADING /// ///////////////////////private static int(package private) static voidgetInflectedMeasureData(String subKey, ULocale locale, NumberFormatter.UnitWidth width, String gender, String caseVariant, String[] outArray) (package private) static voidgetMeasureData(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 StringgetUnitDisplayName(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width) (package private) static StringgetWithPlural(String[] strings, StandardPlural plural) private static voidmaybeCalculateGender(ULocale locale, MeasureUnit unit, String[] outArray) private voidmultiSimpleFormatsToModifiers(String[] leadFormats, String trailFormat, NumberFormat.Field field) Sets modifiers to a combination ofleadFormats(one per plural form) andtrailFormatappended to each.private static voidprocessPatternTimes(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:quantityis taken as the final smallest unit, while the larger unit values must be provided viamicros.mixedMeasures.private voidsimpleFormatsToModifiers(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
/////////////////////// BEGIN DATA LOADING /// /////////////////////// -
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
-
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) andtrailFormatappended 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:MicroPropsGeneratorConsiders the givenDecimalQuantity, optionally mutates it, and returns aMicroProps.- Specified by:
processQuantityin 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:quantityis 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:
processQuantityWithMicrosin interfaceLongNameMultiplexer.ParentlessMicroPropsGenerator
-
getModifier
Description copied from interface:ModifierStoreReturns a Modifier with the given parameters (best-effort).- Specified by:
getModifierin interfaceModifierStore
-