Skip to content

Commit fbbee50

Browse files
authored
CLDR-17014 Move more extra-path code from CLDRFile to ExtraPaths (#4332)
1 parent d669e24 commit fbbee50

File tree

2 files changed

+281
-286
lines changed

2 files changed

+281
-286
lines changed

tools/cldr-code/src/main/java/org/unicode/cldr/util/CLDRFile.java

Lines changed: 4 additions & 285 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import java.util.HashSet;
4242
import java.util.Iterator;
4343
import java.util.LinkedHashMap;
44-
import java.util.LinkedHashSet;
4544
import java.util.LinkedList;
4645
import java.util.List;
4746
import java.util.Locale;
@@ -53,15 +52,9 @@
5352
import java.util.regex.Matcher;
5453
import java.util.regex.Pattern;
5554
import java.util.stream.Collectors;
56-
import org.unicode.cldr.test.CheckMetazones;
57-
import org.unicode.cldr.util.DayPeriodInfo.DayPeriod;
5855
import org.unicode.cldr.util.GrammarInfo.GrammaticalFeature;
59-
import org.unicode.cldr.util.GrammarInfo.GrammaticalScope;
60-
import org.unicode.cldr.util.GrammarInfo.GrammaticalTarget;
6156
import org.unicode.cldr.util.LocaleInheritanceInfo.Reason;
62-
import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo;
6357
import org.unicode.cldr.util.SupplementalDataInfo.PluralInfo.Count;
64-
import org.unicode.cldr.util.SupplementalDataInfo.PluralType;
6558
import org.unicode.cldr.util.With.SimpleIterator;
6659
import org.unicode.cldr.util.XMLFileReader.AllHandler;
6760
import org.unicode.cldr.util.XMLSource.ResolvingSource;
@@ -98,9 +91,6 @@
9891

9992
public class CLDRFile implements Freezable<CLDRFile>, Iterable<String>, LocaleStringProvider {
10093

101-
private static final ImmutableSet<String> casesNominativeOnly =
102-
ImmutableSet.of(GrammaticalFeature.grammaticalCase.getDefault(null));
103-
10494
/**
10595
* Variable to control whether File reads are buffered; this will about halve the time spent in
10696
* loadFromFile() and Factory.make() from about 20 % to about 10 %. It will also noticeably
@@ -3082,8 +3072,7 @@ public String getWinningPath(String path) {
30823072
* @return
30833073
*/
30843074
public Collection<String> getExtraPaths() {
3085-
Set<String> toAddTo = new HashSet<>();
3086-
toAddTo.addAll(getRawExtraPaths());
3075+
Set<String> toAddTo = new HashSet<>(getRawExtraPaths());
30873076
for (String path : this) {
30883077
toAddTo.remove(path);
30893078
}
@@ -3116,11 +3105,7 @@ public Collection<String> getExtraPaths(String prefix, Collection<String> toAddT
31163105
*/
31173106
public Set<String> getRawExtraPaths() {
31183107
if (extraPaths == null) {
3119-
extraPaths =
3120-
ImmutableSet.<String>builder()
3121-
.addAll(getRawExtraPathsPrivate())
3122-
.addAll(CONST_EXTRA_PATHS)
3123-
.build();
3108+
extraPaths = ImmutableSet.<String>builder().addAll(getRawExtraPathsPrivate()).build();
31243109
if (DEBUG) {
31253110
System.out.println(getLocaleID() + "\textras: " + extraPaths.size());
31263111
}
@@ -3134,8 +3119,6 @@ public Set<String> getRawExtraPaths() {
31343119
* are common to all locales, and some of them are specific to the given locale, based on
31353120
* features like the plural rules for the locale.
31363121
*
3137-
* <p>The ones that are constant for all locales should go into CONST_EXTRA_PATHS.
3138-
*
31393122
* @return toAddTo (the collection)
31403123
* <p>Called only by getRawExtraPaths.
31413124
* <p>"Raw" refers to the fact that some of the paths may duplicate paths that are already
@@ -3150,227 +3133,11 @@ public Set<String> getRawExtraPaths() {
31503133
*/
31513134
private List<String> getRawExtraPathsPrivate() {
31523135
Set<String> toAddTo = new HashSet<>();
3153-
SupplementalDataInfo supplementalData = CLDRConfig.getInstance().getSupplementalDataInfo();
3154-
3155-
ExtraPaths.add(toAddTo);
3156-
3157-
// units
3158-
PluralInfo plurals = supplementalData.getPlurals(PluralType.cardinal, getLocaleID());
3159-
if (plurals == null && DEBUG) {
3160-
System.err.println(
3161-
"No "
3162-
+ PluralType.cardinal
3163-
+ " plurals for "
3164-
+ getLocaleID()
3165-
+ " in "
3166-
+ supplementalData.getDirectory().getAbsolutePath());
3167-
}
3168-
Set<Count> pluralCounts = Collections.emptySet();
3169-
if (plurals != null) {
3170-
pluralCounts = plurals.getAdjustedCounts();
3171-
Set<Count> pluralCountsRaw = plurals.getCounts();
3172-
if (pluralCountsRaw.size() != 1) {
3173-
// we get all the root paths with count
3174-
addPluralCounts(toAddTo, pluralCounts, pluralCountsRaw, this);
3175-
}
3176-
}
3177-
// dayPeriods
3178-
String locale = getLocaleID();
3179-
DayPeriodInfo dayPeriods =
3180-
supplementalData.getDayPeriods(DayPeriodInfo.Type.format, locale);
3181-
if (dayPeriods != null) {
3182-
LinkedHashSet<DayPeriod> items = new LinkedHashSet<>(dayPeriods.getPeriods());
3183-
items.add(DayPeriod.am);
3184-
items.add(DayPeriod.pm);
3185-
for (String context : new String[] {"format", "stand-alone"}) {
3186-
for (String width : new String[] {"narrow", "abbreviated", "wide"}) {
3187-
for (DayPeriod dayPeriod : items) {
3188-
// ldml/dates/calendars/calendar[@type="gregorian"]/dayPeriods/dayPeriodContext[@type="format"]/dayPeriodWidth[@type="wide"]/dayPeriod[@type="am"]
3189-
toAddTo.add(
3190-
"//ldml/dates/calendars/calendar[@type=\"gregorian\"]/dayPeriods/"
3191-
+ "dayPeriodContext[@type=\""
3192-
+ context
3193-
+ "\"]/dayPeriodWidth[@type=\""
3194-
+ width
3195-
+ "\"]/dayPeriod[@type=\""
3196-
+ dayPeriod
3197-
+ "\"]");
3198-
}
3199-
}
3200-
}
3201-
}
3202-
3203-
// metazones
3204-
Set<String> zones = supplementalData.getAllMetazones();
3205-
3206-
for (String zone : zones) {
3207-
final boolean metazoneUsesDST = CheckMetazones.metazoneUsesDST(zone);
3208-
for (String width : new String[] {"long", "short"}) {
3209-
for (String type : new String[] {"generic", "standard", "daylight"}) {
3210-
if (metazoneUsesDST || type.equals("standard")) {
3211-
// Only add /standard for non-DST metazones
3212-
final String path =
3213-
"//ldml/dates/timeZoneNames/metazone[@type=\""
3214-
+ zone
3215-
+ "\"]/"
3216-
+ width
3217-
+ "/"
3218-
+ type;
3219-
toAddTo.add(path);
3220-
}
3221-
}
3222-
}
3223-
}
3224-
3225-
// // Individual zone overrides
3226-
// final String[] overrides = {
3227-
// "Pacific/Honolulu\"]/short/generic",
3228-
// "Pacific/Honolulu\"]/short/standard",
3229-
// "Pacific/Honolulu\"]/short/daylight",
3230-
// "Europe/Dublin\"]/long/daylight",
3231-
// "Europe/London\"]/long/daylight",
3232-
// "Etc/UTC\"]/long/standard",
3233-
// "Etc/UTC\"]/short/standard"
3234-
// };
3235-
// for (String override : overrides) {
3236-
// toAddTo.add("//ldml/dates/timeZoneNames/zone[@type=\"" + override);
3237-
// }
3238-
3239-
// Currencies
3240-
Set<String> codes = supplementalData.getBcp47Keys().getAll("cu");
3241-
for (String code : codes) {
3242-
String currencyCode = code.toUpperCase();
3243-
toAddTo.add(
3244-
"//ldml/numbers/currencies/currency[@type=\"" + currencyCode + "\"]/symbol");
3245-
toAddTo.add(
3246-
"//ldml/numbers/currencies/currency[@type=\""
3247-
+ currencyCode
3248-
+ "\"]/displayName");
3249-
if (!pluralCounts.isEmpty()) {
3250-
for (Count count : pluralCounts) {
3251-
toAddTo.add(
3252-
"//ldml/numbers/currencies/currency[@type=\""
3253-
+ currencyCode
3254-
+ "\"]/displayName[@count=\""
3255-
+ count.toString()
3256-
+ "\"]");
3257-
}
3258-
}
3259-
}
3260-
3261-
// grammatical info
3262-
3263-
GrammarInfo grammarInfo = supplementalData.getGrammarInfo(getLocaleID(), true);
3264-
if (grammarInfo != null) {
3265-
if (grammarInfo.hasInfo(GrammaticalTarget.nominal)) {
3266-
Collection<String> genders =
3267-
grammarInfo.get(
3268-
GrammaticalTarget.nominal,
3269-
GrammaticalFeature.grammaticalGender,
3270-
GrammaticalScope.units);
3271-
Collection<String> rawCases =
3272-
grammarInfo.get(
3273-
GrammaticalTarget.nominal,
3274-
GrammaticalFeature.grammaticalCase,
3275-
GrammaticalScope.units);
3276-
Collection<String> nomCases = rawCases.isEmpty() ? casesNominativeOnly : rawCases;
3277-
Collection<Count> adjustedPlurals = pluralCounts;
3278-
// There was code here allowing fewer plurals to be used, but is retracted for now
3279-
// (needs more thorough integration in logical groups, etc.)
3280-
// This note is left for 'blame' to find the old code in case we revive that.
3281-
3282-
// TODO use UnitPathType to get paths
3283-
if (!genders.isEmpty()) {
3284-
for (String unit : GrammarInfo.getUnitsToAddGrammar()) {
3285-
toAddTo.add(
3286-
"//ldml/units/unitLength[@type=\"long\"]/unit[@type=\""
3287-
+ unit
3288-
+ "\"]/gender");
3289-
}
3290-
for (Count plural : adjustedPlurals) {
3291-
for (String gender : genders) {
3292-
for (String case1 : nomCases) {
3293-
final String grammaticalAttributes =
3294-
GrammarInfo.getGrammaticalInfoAttributes(
3295-
grammarInfo,
3296-
UnitPathType.power,
3297-
plural.toString(),
3298-
gender,
3299-
case1);
3300-
toAddTo.add(
3301-
"//ldml/units/unitLength[@type=\"long\"]/compoundUnit[@type=\"power2\"]/compoundUnitPattern1"
3302-
+ grammaticalAttributes);
3303-
toAddTo.add(
3304-
"//ldml/units/unitLength[@type=\"long\"]/compoundUnit[@type=\"power3\"]/compoundUnitPattern1"
3305-
+ grammaticalAttributes);
3306-
}
3307-
}
3308-
}
3309-
// <genderMinimalPairs gender="masculine">Der {0} ist
3310-
// …</genderMinimalPairs>
3311-
for (String gender : genders) {
3312-
toAddTo.add(
3313-
"//ldml/numbers/minimalPairs/genderMinimalPairs[@gender=\""
3314-
+ gender
3315-
+ "\"]");
3316-
}
3317-
}
3318-
if (!rawCases.isEmpty()) {
3319-
for (String case1 : rawCases) {
3320-
// <caseMinimalPairs case="nominative">{0} kostet
3321-
// €3,50.</caseMinimalPairs>
3322-
toAddTo.add(
3323-
"//ldml/numbers/minimalPairs/caseMinimalPairs[@case=\""
3324-
+ case1
3325-
+ "\"]");
3326-
3327-
for (Count plural : adjustedPlurals) {
3328-
for (String unit : GrammarInfo.getUnitsToAddGrammar()) {
3329-
toAddTo.add(
3330-
"//ldml/units/unitLength[@type=\"long\"]/unit[@type=\""
3331-
+ unit
3332-
+ "\"]/unitPattern"
3333-
+ GrammarInfo.getGrammaticalInfoAttributes(
3334-
grammarInfo,
3335-
UnitPathType.unit,
3336-
plural.toString(),
3337-
null,
3338-
case1));
3339-
}
3340-
}
3341-
}
3342-
}
3343-
}
3344-
}
3136+
ExtraPaths.addConstant(toAddTo);
3137+
ExtraPaths.addLocaleDependent(toAddTo, this, getLocaleID());
33453138
return toAddTo.stream().map(String::intern).collect(Collectors.toList());
33463139
}
33473140

3348-
private void addPluralCounts(
3349-
Collection<String> toAddTo,
3350-
final Set<Count> pluralCounts,
3351-
final Set<Count> pluralCountsRaw,
3352-
Iterable<String> file) {
3353-
for (String path : file) {
3354-
String countAttr = "[@count=\"other\"]";
3355-
int countPos = path.indexOf(countAttr);
3356-
if (countPos < 0) {
3357-
continue;
3358-
}
3359-
Set<Count> pluralCountsNeeded =
3360-
path.startsWith("//ldml/numbers/minimalPairs") ? pluralCountsRaw : pluralCounts;
3361-
if (pluralCountsNeeded.size() > 1) {
3362-
String start = path.substring(0, countPos) + "[@count=\"";
3363-
String end = "\"]" + path.substring(countPos + countAttr.length());
3364-
for (Count count : pluralCounts) {
3365-
if (count == Count.other) {
3366-
continue;
3367-
}
3368-
toAddTo.add(start + count + end);
3369-
}
3370-
}
3371-
}
3372-
}
3373-
33743141
/**
33753142
* Get the path with the given count, case, or gender, with fallback. The fallback acts like an
33763143
* alias in root.
@@ -3744,52 +3511,4 @@ String getStringValueWithBaileyNotConstructed(String path) {
37443511
}
37453512
return value;
37463513
}
3747-
3748-
/**
3749-
* A set of paths to be added to getRawExtraPaths(). These are constant across locales, and
3750-
* don't have good fallback values in root. NOTE: if this is changed, you'll need to modify
3751-
* TestPaths.extraPathAllowsNullValue
3752-
*/
3753-
static final Set<String> CONST_EXTRA_PATHS =
3754-
CharUtilities.internImmutableSet(
3755-
Set.of(
3756-
// Individual zone overrides — were in getRawExtraPaths
3757-
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/generic",
3758-
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/standard",
3759-
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/daylight",
3760-
"//ldml/dates/timeZoneNames/zone[@type=\"Europe/Dublin\"]/long/daylight",
3761-
"//ldml/dates/timeZoneNames/zone[@type=\"Europe/London\"]/long/daylight",
3762-
"//ldml/dates/timeZoneNames/zone[@type=\"Etc/UTC\"]/long/standard",
3763-
"//ldml/dates/timeZoneNames/zone[@type=\"Etc/UTC\"]/short/standard",
3764-
// Person name paths
3765-
"//ldml/personNames/sampleName[@item=\"nativeG\"]/nameField[@type=\"given\"]",
3766-
"//ldml/personNames/sampleName[@item=\"nativeGS\"]/nameField[@type=\"given\"]",
3767-
"//ldml/personNames/sampleName[@item=\"nativeGS\"]/nameField[@type=\"surname\"]",
3768-
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"given\"]",
3769-
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"given2\"]",
3770-
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"surname\"]",
3771-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"title\"]",
3772-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given\"]",
3773-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given-informal\"]",
3774-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given2\"]",
3775-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname-prefix\"]",
3776-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname-core\"]",
3777-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname2\"]",
3778-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"generation\"]",
3779-
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"credentials\"]",
3780-
"//ldml/personNames/sampleName[@item=\"foreignG\"]/nameField[@type=\"given\"]",
3781-
"//ldml/personNames/sampleName[@item=\"foreignGS\"]/nameField[@type=\"given\"]",
3782-
"//ldml/personNames/sampleName[@item=\"foreignGS\"]/nameField[@type=\"surname\"]",
3783-
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"given\"]",
3784-
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"given2\"]",
3785-
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"surname\"]",
3786-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"title\"]",
3787-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given\"]",
3788-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given-informal\"]",
3789-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given2\"]",
3790-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname-prefix\"]",
3791-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname-core\"]",
3792-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname2\"]",
3793-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"generation\"]",
3794-
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"credentials\"]"));
37953514
}

0 commit comments

Comments
 (0)