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 java.lang.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 Classes Modifier and Type Class Description private static class
LongNameHandler.AliasSink
private static class
LongNameHandler.DerivedComponents
Loads and applies deriveComponent rules from CLDR's grammaticalFeatures.xml.private static class
LongNameHandler.ExtractCorePatternResult
private static class
LongNameHandler.InflectedPluralSink
private static class
LongNameHandler.PlaceholderPosition
private static class
LongNameHandler.PluralTableSink
-
Field Summary
Fields Modifier and Type Field Description (package private) static int
ARRAY_LENGTH
private static int
DNAM_INDEX
private java.lang.String
gender
private static int
GENDER_INDEX
private static int
i
private java.util.Map<StandardPlural,SimpleModifier>
modifiers
private MicroPropsGenerator
parent
private static int
PER_INDEX
private PluralRules
rules
-
Constructor Summary
Constructors Modifier Constructor Description private
LongNameHandler(java.util.Map<StandardPlural,SimpleModifier> modifiers, PluralRules rules, MicroPropsGenerator parent)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static java.lang.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(java.lang.String pattern)
Returns three outputs extracted from pattern.private static LongNameHandler
forArbitraryUnit(ULocale loc, MeasureUnit unit, NumberFormatter.UnitWidth width, java.lang.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, java.lang.String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent)
Construct a localized LongNameHandler for the specified MeasureUnit.private static java.lang.String
getCompoundValue(java.lang.String compoundKey, ULocale locale, NumberFormatter.UnitWidth width)
private static void
getCurrencyLongNameData(ULocale locale, Currency currency, java.lang.String[] outArray)
private static java.lang.String
getDeriveCompoundRule(ULocale locale, java.lang.String feature, java.lang.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 java.lang.String
getDerivedGender(ULocale locale, java.lang.String structure, java.lang.String[] data0, java.lang.String[] data1)
private static java.lang.String
getGenderForBuiltin(ULocale locale, MeasureUnit builtinUnit)
private static int
getIndex(java.lang.String pluralKeyword)
(package private) static void
getInflectedMeasureData(java.lang.String subKey, ULocale locale, NumberFormatter.UnitWidth width, java.lang.String gender, java.lang.String caseVariant, java.lang.String[] outArray)
(package private) static void
getMeasureData(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width, java.lang.String unitDisplayCase, java.lang.String[] outArray)
Modifier
getModifier(Modifier.Signum signum, StandardPlural plural)
Returns a Modifier with the given parameters (best-effort).static java.lang.String
getUnitDisplayName(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width)
(package private) static java.lang.String
getWithPlural(java.lang.String[] strings, StandardPlural plural)
private static void
maybeCalculateGender(ULocale locale, MeasureUnit unit, java.lang.String[] outArray)
private void
multiSimpleFormatsToModifiers(java.lang.String[] leadFormats, java.lang.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, java.lang.String caseVariant, java.lang.String[] outArray)
Roughly corresponds to patternTimes(...) in the spec: https://unicode.org/reports/tr35/tr35-general.html#compound-unitsMicroProps
processQuantity(DecimalQuantity quantity)
Considers the givenDecimalQuantity
, optionally mutates it, and returns aMicroProps
.MicroProps
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(java.lang.String[] simpleFormats, NumberFormat.Field field)
Sets modifiers to use the patterns from simpleFormats.
-
-
-
Field Detail
-
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
-
ARRAY_LENGTH
static final int ARRAY_LENGTH
-
modifiers
private final java.util.Map<StandardPlural,SimpleModifier> modifiers
-
rules
private final PluralRules rules
-
parent
private final MicroPropsGenerator parent
-
gender
private java.lang.String gender
-
-
Constructor Detail
-
LongNameHandler
private LongNameHandler(java.util.Map<StandardPlural,SimpleModifier> modifiers, PluralRules rules, MicroPropsGenerator parent)
-
-
Method Detail
-
getIndex
private static int getIndex(java.lang.String pluralKeyword)
-
getWithPlural
static java.lang.String getWithPlural(java.lang.String[] strings, StandardPlural plural)
-
extractCorePattern
private static LongNameHandler.ExtractCorePatternResult extractCorePattern(java.lang.String pattern)
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
private static java.lang.String getGenderForBuiltin(ULocale locale, MeasureUnit builtinUnit)
-
getInflectedMeasureData
static void getInflectedMeasureData(java.lang.String subKey, ULocale locale, NumberFormatter.UnitWidth width, java.lang.String gender, java.lang.String caseVariant, java.lang.String[] outArray)
-
getMeasureData
static void getMeasureData(ULocale locale, MeasureUnit unit, NumberFormatter.UnitWidth width, java.lang.String unitDisplayCase, java.lang.String[] outArray)
-
getCurrencyLongNameData
private static void getCurrencyLongNameData(ULocale locale, Currency currency, java.lang.String[] outArray)
-
getCompoundValue
private static java.lang.String getCompoundValue(java.lang.String compoundKey, ULocale locale, NumberFormatter.UnitWidth width)
-
getDeriveCompoundRule
private static java.lang.String getDeriveCompoundRule(ULocale locale, java.lang.String feature, java.lang.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.
-
getDerivedGender
private static java.lang.String getDerivedGender(ULocale locale, java.lang.String structure, java.lang.String[] data0, java.lang.String[] data1)
-
calculateGenderForUnit
private static java.lang.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. 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
private static void maybeCalculateGender(ULocale locale, MeasureUnit unit, java.lang.String[] outArray)
-
getUnitDisplayName
public static java.lang.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, java.lang.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, java.lang.String unitDisplayCase, PluralRules rules, MicroPropsGenerator parent)
-
processPatternTimes
private static void processPatternTimes(MeasureUnitImpl productUnit, ULocale loc, NumberFormatter.UnitWidth width, java.lang.String caseVariant, java.lang.String[] outArray)
Roughly corresponds to patternTimes(...) in the spec: https://unicode.org/reports/tr35/tr35-general.html#compound-units
-
simpleFormatsToModifiers
private void simpleFormatsToModifiers(java.lang.String[] simpleFormats, NumberFormat.Field field)
Sets modifiers to use the patterns from simpleFormats.
-
multiSimpleFormatsToModifiers
private void multiSimpleFormatsToModifiers(java.lang.String[] leadFormats, java.lang.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
public MicroProps processQuantity(DecimalQuantity quantity)
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
public MicroProps 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
. 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
public Modifier getModifier(Modifier.Signum signum, StandardPlural plural)
Description copied from interface:ModifierStore
Returns a Modifier with the given parameters (best-effort).- Specified by:
getModifier
in interfaceModifierStore
-
-