Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLDR-17289 make setCldrFileToCheck a lazy load #3422

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public CheckCLDR handleCheck(
return this;
}

if (!accept(result)) return this;

String strippedPath = CLDRFile.getNondraftNonaltXPath(path);
if (strippedPath.equals(path)) {
return this; // paths equal, skip
Expand All @@ -48,7 +50,7 @@ public CheckCLDR handleCheck(
}

@Override
public CheckCLDR setCldrFileToCheck(
public CheckCLDR handleSetCldrFileToCheck(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
if (cldrFileToCheck == null) return this;
// Skip if the phase is not final testing
Expand All @@ -59,7 +61,7 @@ public CheckCLDR setCldrFileToCheck(
return this;
}

super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
super.handleSetCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
seenSoFar.clear();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public CheckCLDR handleCheck(
if (file.getConstructedValue(nonAltPath) != null) {
return this;
}
if (!accept(result)) return this;
/*
* If the source locale is not code-fallback, it's not an error.
* getSourceLocaleIdExtended with skipInheritanceMarker = false means that if the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public CheckCLDR handleCheck(
|| !getCldrFileToCheck().isNotRoot(path)) {
return this;
}
if (!accept(result)) return this;
final String ecode = hasAnnotationECode(value);

if (ecode != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public CheckCLDR handleCheck(
if (!getCldrFileToCheck().getLocaleID().equals(locale)) {
return this;
}
if (!accept(result)) return this;
XPathParts parts = XPathParts.getFrozenInstance(fullPath);
for (int i = 0; i < parts.size(); ++i) {
if (parts.getAttributeCount(i) == 0) {
Expand Down Expand Up @@ -280,7 +281,7 @@ String getReplacement(String value, String attributeValue) {
LocaleIDParser localeIDParser = new LocaleIDParser();

@Override
public CheckCLDR setCldrFileToCheck(
public CheckCLDR handleSetCldrFileToCheck(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
if (cldrFileToCheck == null) return this;
if (Phase.FINAL_TESTING == getPhase() || Phase.BUILD == getPhase()) {
Expand All @@ -292,7 +293,7 @@ public CheckCLDR setCldrFileToCheck(

pluralInfo =
supplementalData.getPlurals(PluralType.cardinal, cldrFileToCheck.getLocaleID());
super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
super.handleSetCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
isEnglish = "en".equals(localeIDParser.set(cldrFileToCheck.getLocaleID()).getLanguage());
synchronized (elementOrder) {
if (!initialized) {
Expand Down
131 changes: 79 additions & 52 deletions tools/cldr-code/src/main/java/org/unicode/cldr/test/CheckCLDR.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ public Options() {
public Options(Options options2) {
this.options = Arrays.copyOf(options2.options, options2.options.length);
this.key = options2.key;
this.locale = options2.locale;
}

public Options(
Expand Down Expand Up @@ -650,36 +651,39 @@ public final CLDRFile getCldrFileToCheck() {
}

/**
* Don't override this, use the other setCldrFileToCheck which takes an Options instead of a
* Map<>
* Often subclassed for initializing. If so, make the first 2 lines: if (cldrFileToCheck ==
* null) return this; super.handleSetCldrFileToCheck(cldrFileToCheck); do stuff
*
* <p>Called late via accept().
*
* @param cldrFileToCheck
* @param options
* @param possibleErrors
* @param possibleErrors any deferred possibleErrors can be set here. They will be appended to
* every handleCheck() call.
* @return
* @see #setCldrFileToCheck(CLDRFile, Options, List)
* @deprecated
*/
@Deprecated
public final CheckCLDR setCldrFileToCheck(
CLDRFile cldrFileToCheck,
Map<String, String> options,
List<CheckStatus> possibleErrors) {
return setCldrFileToCheck(cldrFileToCheck, new Options(options), possibleErrors);
public CheckCLDR handleSetCldrFileToCheck(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {

// nothing by default
return this;
}

/**
* Set the CLDRFile. Must be done before calling check. If null is called, just skip Often
* subclassed for initializing. If so, make the first 2 lines: if (cldrFileToCheck == null)
* return this; super.setCldrFileToCheck(cldrFileToCheck); do stuff
* Set the CLDRFile. Must be done before calling check.
*
* @param cldrFileToCheck
* @param options (not currently used)
* @param possibleErrors TODO
* @param possibleErrors
*/
public CheckCLDR setCldrFileToCheck(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
this.cldrFileToCheck = cldrFileToCheck;
reset();
// clear the *cached* possible Errors. Not counting any set immediately by subclasses.
cachedPossibleErrors.clear();
cachedOptions = new Options(options);
// we must load filters here, as they are used by check()

// Shortlist error filters for this locale.
loadFilters();
Expand All @@ -694,9 +698,58 @@ public CheckCLDR setCldrFileToCheck(
}
xpaths.add(filter.get2());
}

// hook for checks that want to set possibleErrors early
handleCheckPossibleErrors(cldrFileToCheck, options, possibleErrors);

return this;
}

/** override this if you want to return errors immediately when setCldrFileToCheck is called */
protected void handleCheckPossibleErrors(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
// nothing by default.
}

/** override this if you want to reset state immediately when setCldrFileToCheck is called */
protected void reset() {
initted = false;
}

/**
* Subclasses must call this, after any skip calculation to indicate that an xpath is relevant
* to them.
*
* @param result out-parameter to contain any deferred errors
* @return false if test is skipped and should exit
*/
protected boolean accept(List<CheckStatus> result) {
if (!initted) {
if (this.cldrFileToCheck == null) {
throw new NullPointerException("accept() was called before setCldrFileToCheck()");
}
// clear this again.
cachedPossibleErrors.clear();
// call into the subclass
handleSetCldrFileToCheck(this.cldrFileToCheck, cachedOptions, cachedPossibleErrors);
initted = true;
}
// unconditionally append all cached possible errors
result.addAll(cachedPossibleErrors);
if (isSkipTest()) {
return false;
}
return true;
}

/** has accept() been called since setCldrFileToCheck() was called? */
boolean initted = false;

/** cache of possible errors, for handleSetCldrFileToCheck */
List<CheckStatus> cachedPossibleErrors = new ArrayList<>();

Options cachedOptions = null;

/** Status value returned from check */
public static class CheckStatus {
public static final Type alertType = Type.Comment,
Expand Down Expand Up @@ -1139,33 +1192,6 @@ public static void appendTitle(StringBuffer htmlMessage) {
}
}

/**
* Wraps the options in an Options and delegates.
*
* @param path Must be a distinguished path, such as what comes out of CLDRFile.iterator()
* @param fullPath Must be the full path
* @param value the value associated with the path
* @param options A set of test-specific options. Set these with code of the form:<br>
* options.put("CoverageLevel.localeType", "G0")<br>
* That is, the key is of the form <testname>.<optiontype>, and the value is of the form
* <optionvalue>.<br>
* There is one general option; the following will select only the tests that should be run
* during this phase.<br>
* options.put("phase", Phase.<something>); It can be used for new data entry.
* @param result
* @return
* @deprecated use CheckCLDR#check(String, String, String, Options, List)
*/
@Deprecated
public final CheckCLDR check(
String path,
String fullPath,
String value,
Map<String, String> options,
List<CheckStatus> result) {
return check(path, fullPath, value, new Options(options), result);
}

/**
* Checks the path/value in the cldrFileToCheck for correctness, according to some criterion. If
* the path is relevant to the check, there is an alert or warning, then a CheckStatus is added
Expand Down Expand Up @@ -1253,9 +1279,10 @@ protected CheckCLDR handleGetExamples(
}

/**
* This is what the subclasses override. If they ever use pathParts or fullPathParts, they need
* to call initialize() with the respective path. Otherwise they must NOT change pathParts or
* fullPathParts.
* This is what the subclasses override.
*
* <p>If a path is not applicable, exit early with <code>return this;</code> Once a path is
* applicable, call <code>accept(result);</code> to add deferred possible problems.
*
* <p>If something is found, a CheckStatus is added to result. This can be done multiple times
* in one call, if multiple errors or warnings are found. The CheckStatus may return warnings,
Expand All @@ -1268,8 +1295,6 @@ protected CheckCLDR handleGetExamples(
* .setType(CheckStatus.errorType)
* .setMessage(&quot;Value should be {0}&quot;, new Object[] { pattern }));
* </pre>
*
* @param options TODO
*/
public abstract CheckCLDR handleCheck(
String path, String fullPath, String value, Options options, List<CheckStatus> result);
Expand Down Expand Up @@ -1308,6 +1333,9 @@ public CheckCLDR handleCheck(
Options options,
List<CheckStatus> result) {
result.clear();

if (!accept(result)) return this;

// If we're being asked to run tests for an inheritance marker, then we need to change
// it
// to the "real" value first before running tests. Testing the value
Expand Down Expand Up @@ -1380,15 +1408,15 @@ private void addError(List<CheckStatus> result, CheckCLDR item, Exception e) {
}

@Override
public CheckCLDR setCldrFileToCheck(
public void handleCheckPossibleErrors(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
ElapsedTimer testTime = null, testOverallTime = null;
if (cldrFileToCheck == null) return this;
if (cldrFileToCheck == null) return;
boolean SHOW_TIMES = options.contains(Options.Option.SHOW_TIMES);
setPhase(Phase.forString(options.get(Options.Option.phase)));
if (SHOW_TIMES)
testOverallTime = new ElapsedTimer("Test setup time for setCldrFileToCheck: {0}");
super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
super.handleCheckPossibleErrors(cldrFileToCheck, options, possibleErrors);
possibleErrors.clear();

for (Iterator<CheckCLDR> it = filteredCheckList.iterator(); it.hasNext(); ) {
Expand All @@ -1413,7 +1441,6 @@ public CheckCLDR setCldrFileToCheck(
}
}
if (SHOW_TIMES) System.out.println("Overall: " + testOverallTime + ": {0}");
return this;
}

public Matcher getFilter() {
Expand All @@ -1427,7 +1454,7 @@ public CompoundCheckCLDR setFilter(Matcher filter) {
CheckCLDR item = it.next();
if (filter == null || filter.reset(item.getClass().getName()).matches()) {
filteredCheckList.add(item);
item.setCldrFileToCheck(getCldrFileToCheck(), (Options) null, null);
item.handleSetCldrFileToCheck(getCldrFileToCheck(), (Options) null, null);
}
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ public static Case forString(String input) {
BreakIterator breaker = null;

@Override
public CheckCLDR setCldrFileToCheck(
public CheckCLDR handleSetCldrFileToCheck(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
if (cldrFileToCheck == null) return this;
super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
super.handleSetCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
uLocale = new ULocale(cldrFileToCheck.getLocaleID());
breaker = BreakIterator.getWordInstance(uLocale);
return this;
Expand All @@ -46,6 +46,7 @@ public CheckCLDR handleCheck(
if (fullPath == null) return this; // skip paths that we don't have
if (fullPath.indexOf("casing") < 0) return this;

if (!accept(result)) return this;
// pick up the casing attributes from the full path
XPathParts parts = XPathParts.getFrozenInstance(fullPath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import org.unicode.cldr.util.CldrUtility;
import org.unicode.cldr.util.Factory;

public class CheckChildren extends FactoryCheckCLDR {
public class CheckChildren<resolvedCldrFileToCheck> extends FactoryCheckCLDR {
CLDRFile[] immediateChildren;
Map<String, String> tempSet = new HashMap<>();

Expand All @@ -22,15 +22,14 @@ public CheckChildren(Factory factory) {
@Override
public CheckCLDR handleCheck(
String path, String fullPath, String value, Options options, List<CheckStatus> result) {
if (immediateChildren == null) return this; // skip - test isn't even relevant
if (isSkipTest()) return this; // disabled
if (fullPath == null) return this; // skip paths that we don't have
if (value == null) return this; // skip null values
if (!accept(result)) return this;
if (immediateChildren == null) return this; // skip - test isn't even relevant
String winningValue = this.getCldrFileToCheck().getWinningValue(fullPath);
if (!value.equals(winningValue)) {
return this; // only run this test against winning values.
}

// String current = getResolvedCldrFileToCheck().getStringValue(path);
tempSet.clear();
for (int i = 0; i < immediateChildren.length; ++i) {
Expand Down Expand Up @@ -63,7 +62,14 @@ public CheckCLDR handleCheck(
}

@Override
public CheckCLDR setCldrFileToCheck(
public void reset() {
super.reset();
immediateChildren = null; // reset this beforehand
tempSet.clear();
}

@Override
public CheckCLDR handleSetCldrFileToCheck(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
if (cldrFileToCheck == null) return this;
if (cldrFileToCheck.getLocaleID().equals("root"))
Expand All @@ -78,7 +84,7 @@ public CheckCLDR setCldrFileToCheck(
}

List<CLDRFile> iChildren = new ArrayList<>();
super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
super.handleSetCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
CLDRLocale myLocale = CLDRLocale.getInstance(cldrFileToCheck.getLocaleID());
if (myLocale.getCountry() != null && myLocale.getCountry().length() == 2) {
immediateChildren = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ public CheckConsistentCasing(Factory factory) {
}

@Override
public CheckCLDR setCldrFileToCheck(
public CheckCLDR handleSetCldrFileToCheck(
CLDRFile cldrFileToCheck, Options options, List<CheckStatus> possibleErrors) {
if (cldrFileToCheck == null) return this;
super.setCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
super.handleSetCldrFileToCheck(cldrFileToCheck, options, possibleErrors);
locale = cldrFileToCheck.getLocaleID();
// get info about casing; note that this is done in two steps since
// ScriptMetadata.getInfo() returns null, in some instances.
Expand Down Expand Up @@ -89,6 +89,7 @@ public CheckCLDR handleCheck(
String path, String fullPath, String value, Options options, List<CheckStatus> result) {
// it helps performance to have a quick reject of most paths
if (fullPath == null) return this; // skip paths that we don't have
if (!accept(result)) return this; // causes hasCasingInfo to be calculated
if (!hasCasingInfo) return this;

String locale2 = getCldrFileToCheck().getSourceLocaleID(path, null);
Expand Down
Loading
Loading