Skip to content

Commit

Permalink
CLDR-17313 intern xpaths: update per code review
Browse files Browse the repository at this point in the history
- don't intern in StringId
- simplify intern logic with lambdas
  • Loading branch information
srl295 committed Jan 23, 2024
1 parent 145b379 commit a814c25
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 69 deletions.
90 changes: 46 additions & 44 deletions tools/cldr-code/src/main/java/org/unicode/cldr/util/CLDRFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.unicode.cldr.test.CheckMetazones;
import org.unicode.cldr.util.DayPeriodInfo.DayPeriod;
import org.unicode.cldr.util.GrammarInfo.GrammaticalFeature;
Expand Down Expand Up @@ -3156,8 +3157,8 @@ public Set<String> getRawExtraPaths() {
if (extraPaths == null) {
extraPaths =
ImmutableSet.<String>builder()
.addAll(CharUtilities.internAll(getRawExtraPathsPrivate()))
.addAll(CharUtilities.internAll(CONST_EXTRA_PATHS))
.addAll(getRawExtraPathsPrivate())
.addAll(CONST_EXTRA_PATHS)
.build();
if (DEBUG) {
System.out.println(getLocaleID() + "\textras: " + extraPaths.size());
Expand Down Expand Up @@ -3186,7 +3187,7 @@ public Set<String> getRawExtraPaths() {
* client code. Make sure that updates here are reflected there and vice versa.
* <p>Reference: https://unicode-org.atlassian.net/browse/CLDR-11238
*/
private Set<String> getRawExtraPathsPrivate() {
private List<String> getRawExtraPathsPrivate() {
Set<String> toAddTo = new HashSet<>();
SupplementalDataInfo supplementalData = CLDRConfig.getInstance().getSupplementalDataInfo();
// units
Expand Down Expand Up @@ -3377,7 +3378,7 @@ private Set<String> getRawExtraPathsPrivate() {
}
}
}
return toAddTo;
return toAddTo.stream().map(String::intern).collect(Collectors.toList());
}

private void addPluralCounts(
Expand Down Expand Up @@ -4307,44 +4308,45 @@ private String getStringValueWithBaileyNotConstructed(String path) {
* TestPaths.extraPathAllowsNullValue
*/
static final Set<String> CONST_EXTRA_PATHS =
ImmutableSet.of(
// Individual zone overrides — were in getRawExtraPaths
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/generic",
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/standard",
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/daylight",
"//ldml/dates/timeZoneNames/zone[@type=\"Europe/Dublin\"]/long/daylight",
"//ldml/dates/timeZoneNames/zone[@type=\"Europe/London\"]/long/daylight",
"//ldml/dates/timeZoneNames/zone[@type=\"Etc/UTC\"]/long/standard",
"//ldml/dates/timeZoneNames/zone[@type=\"Etc/UTC\"]/short/standard",
// Person name paths
"//ldml/personNames/sampleName[@item=\"nativeG\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"title\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given-informal\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname-prefix\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname-core\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname2\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"generation\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"credentials\"]",
"//ldml/personNames/sampleName[@item=\"foreignG\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"title\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given-informal\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname-prefix\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname-core\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname2\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"generation\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"credentials\"]");
CharUtilities.internImmutableSet(
Set.of(
// Individual zone overrides — were in getRawExtraPaths
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/generic",
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/standard",
"//ldml/dates/timeZoneNames/zone[@type=\"Pacific/Honolulu\"]/short/daylight",
"//ldml/dates/timeZoneNames/zone[@type=\"Europe/Dublin\"]/long/daylight",
"//ldml/dates/timeZoneNames/zone[@type=\"Europe/London\"]/long/daylight",
"//ldml/dates/timeZoneNames/zone[@type=\"Etc/UTC\"]/long/standard",
"//ldml/dates/timeZoneNames/zone[@type=\"Etc/UTC\"]/short/standard",
// Person name paths
"//ldml/personNames/sampleName[@item=\"nativeG\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"nativeGGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"title\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given-informal\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname-prefix\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname-core\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"surname2\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"generation\"]",
"//ldml/personNames/sampleName[@item=\"nativeFull\"]/nameField[@type=\"credentials\"]",
"//ldml/personNames/sampleName[@item=\"foreignG\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"foreignGGS\"]/nameField[@type=\"surname\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"title\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given-informal\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"given2\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname-prefix\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname-core\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"surname2\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"generation\"]",
"//ldml/personNames/sampleName[@item=\"foreignFull\"]/nameField[@type=\"credentials\"]"));
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,10 @@
package org.unicode.cldr.util;

import java.util.Iterator;
import java.util.Collection;
import java.util.Set;
import java.util.stream.Collectors;

public class CharUtilities {
/** intern everything in the src iterable */
public static Iterable<String> internAll(final Iterable<String> src) {
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
return CharUtilities.internAll(src.iterator());
}
};
}
/** intern everything in the src iterator */
protected static Iterator<String> internAll(final Iterator<String> iterator) {
return new Iterator<String>() {
@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public String next() {
return iterator.next().intern();
}
};
}
/**
* Simple wrapper for CharSequence
*
Expand Down Expand Up @@ -168,4 +147,9 @@ public static int compare(CharSequence text1, CharSequence text2) {
}
}
}

/** intern each element in the string and return a new unmodifiable Set */
public static Set<String> internImmutableSet(Collection<String> s) {
return s.stream().map(String::intern).collect(Collectors.toUnmodifiableSet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public final class StringId {
* @return a value from 0 to 0x7FFFFFFFFFFFFFFFL.
*/
public static long getId(CharSequence charSequence) {
String string = charSequence.toString().intern();
String string = charSequence.toString();
Long resultLong = STRING_TO_ID.get(string);
if (resultLong != null) {
return resultLong;
Expand Down

0 comments on commit a814c25

Please sign in to comment.