Skip to content

Commit

Permalink
CLDR-13857 Optimize LikelySubtags initilization (#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlintaLu authored Aug 7, 2020
1 parent a6f279f commit a0dc92d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static void main(String[] args) {
}
}

LikelySubtags likely = new LikelySubtags(supplementalDataInfo);
LikelySubtags likely = new LikelySubtags();
LanguageTagParser ltp = new LanguageTagParser();
// get targets of language aliases for macros
Map<String, String> macroToEncompassed = new HashMap<String, String>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ public class LikelySubtagsTest extends TestFmwk {
.getInstance().getSupplementalDataInfo();
static final Map<String, String> likely = SUPPLEMENTAL_DATA_INFO
.getLikelySubtags();
static final LikelySubtags LIKELY = new LikelySubtags(
SUPPLEMENTAL_DATA_INFO, likely);
static final LikelySubtags LIKELY = new LikelySubtags();

public static void main(String[] args) {
new LikelySubtagsTest().run(args);
Expand Down
2 changes: 1 addition & 1 deletion tools/java/org/unicode/cldr/tool/ChartDelta.java
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private void writeLdml(Anchors anchors) throws IOException {
Matcher m = fileMatcher.matcher("");
Set<String> defaultContents = SDI.getDefaultContentLocales();
LanguageTagParser ltp = new LanguageTagParser();
LikelySubtags ls = new LikelySubtags(SDI);
LikelySubtags ls = new LikelySubtags();
for (String file : factories.get(0).getAvailable()) {
if (defaultContents.contains(file)) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion tools/java/org/unicode/cldr/tool/FindPreferredHours.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static void main(String[] args) {
final Relation<String, Hours> lang2Hours = Relation.of(new TreeMap<String, Set<Hours>>(), TreeSet.class);
final Factory factory = INFO.getCldrFactory();
final FormatParser formatDateParser = new FormatParser();
final LikelySubtags likely2Max = new LikelySubtags(INFO.getSupplementalDataInfo());
final LikelySubtags likely2Max = new LikelySubtags();

for (final String locale : factory.getAvailable()) {
if (locale.equals("root")) {
Expand Down
72 changes: 34 additions & 38 deletions tools/java/org/unicode/cldr/tool/LikelySubtags.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,54 +26,50 @@ public class LikelySubtags {

private Map<String, String> toMaximized;
private boolean favorRegion = false;
private SupplementalDataInfo supplementalDataInfo;
private Map<String, String> currencyToLikelyTerritory = new HashMap<>();
private static SupplementalDataInfo supplementalDataInfo;
private static Map<String, String> currencyToLikelyTerritory;
private static final Object SYNC = new Object();

/**
* Create the likely subtags.
*
* @param toMaximized
*/
public LikelySubtags(Map<String, String> toMaximized) {
this(SupplementalDataInfo.getInstance(), toMaximized);
}

/**
* Create the likely subtags.
*
* @param toMaximized
*/
public LikelySubtags(SupplementalDataInfo supplementalDataInfo) {
this(supplementalDataInfo, supplementalDataInfo.getLikelySubtags());
loadStaticVariables();
if (this.toMaximized == null) {
this.toMaximized = supplementalDataInfo.getLikelySubtags();
} else {
this.toMaximized = toMaximized;
}
}

/**
* Create the likely subtags.
*
* @param toMaximized
*/
public LikelySubtags(SupplementalDataInfo supplementalDataInfo, Map<String, String> toMaximized) {
this.supplementalDataInfo = supplementalDataInfo;
this.toMaximized = toMaximized;

Date now = new Date();
Set<Row.R2<Double, String>> sorted = new TreeSet<>();
for (String territory : supplementalDataInfo.getTerritoriesWithPopulationData()) {
PopulationData pop = supplementalDataInfo.getPopulationDataForTerritory(territory);
double population = pop.getPopulation();
sorted.add(Row.of(-population, territory));
private static void loadStaticVariables() {
if (supplementalDataInfo != null && currencyToLikelyTerritory != null) {
return;
}
for (R2<Double, String> item : sorted) {
String territory = item.get1();
Set<CurrencyDateInfo> targetCurrencyInfo = supplementalDataInfo.getCurrencyDateInfo(territory);
if (targetCurrencyInfo == null) {
continue;
synchronized(SYNC) {
supplementalDataInfo = SupplementalDataInfo.getInstance();
currencyToLikelyTerritory = new HashMap<String, String>();
Date now = new Date();
Set<Row.R2<Double, String>> sorted = new TreeSet<>();
for (String territory : supplementalDataInfo.getTerritoriesWithPopulationData()) {
PopulationData pop = supplementalDataInfo.getPopulationDataForTerritory(territory);
double population = pop.getPopulation();
sorted.add(Row.of(-population, territory));
}
for (CurrencyDateInfo cdi : targetCurrencyInfo) {
String currency = cdi.getCurrency();
if (!currencyToLikelyTerritory.containsKey(currency) && cdi.getStart().before(now)
&& cdi.getEnd().after(now) && cdi.isLegalTender()) {
currencyToLikelyTerritory.put(currency, territory);
for (R2<Double, String> item : sorted) {
String territory = item.get1();
Set<CurrencyDateInfo> targetCurrencyInfo = supplementalDataInfo.getCurrencyDateInfo(territory);
if (targetCurrencyInfo == null) {
continue;
}
for (CurrencyDateInfo cdi : targetCurrencyInfo) {
String currency = cdi.getCurrency();
if (!currencyToLikelyTerritory.containsKey(currency) && cdi.getStart().before(now)
&& cdi.getEnd().after(now) && cdi.isLegalTender()) {
currencyToLikelyTerritory.put(currency, territory);
}
}
}
}
Expand All @@ -85,7 +81,7 @@ public LikelySubtags(SupplementalDataInfo supplementalDataInfo, Map<String, Stri
* @param toMaximized
*/
public LikelySubtags() {
this(SupplementalDataInfo.getInstance());
this(null);
}

public boolean isFavorRegion() {
Expand Down
2 changes: 1 addition & 1 deletion tools/java/org/unicode/cldr/util/CoreCoverageInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class CoreCoverageInfo {
private static final CLDRConfig config = CLDRConfig.getInstance();
private static final String CLDR_BASE_DIRECTORY = config.getCldrBaseDirectory().toString();
private static final SupplementalDataInfo sdi = SupplementalDataInfo.getInstance();
private static final LikelySubtags ls = new LikelySubtags(sdi);
private static final LikelySubtags ls = new LikelySubtags();

public enum CoreItems {
// Drop the exemplars, since
Expand Down

0 comments on commit a0dc92d

Please sign in to comment.