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-18217 Remove Iterable interface from CLDRFile, add iteratorDefault, iterableDefault #4285

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

btangmu
Copy link
Member

@btangmu btangmu commented Jan 16, 2025

-Instead of Iterable interface, use new methods iterableDefault, iterableWithoutExtras

-Private final boolean CLDRFile.DEFAULT_ITERATION_INCLUDES_EXTRAS determines whether iteratorDefault and iterableDefault skip extras; either way they skip paths with null values

-Replace original iterator methods (zero or more parameters) with iteratorDefault or iteratorWithoutExtras

-Iterable and iterator are distinct, as in iterableWithoutExtras versus iteratorWithoutExtras

-Call iteratorWithoutExtras/iterableWithoutExtras directly where tests would otherwise fail with DEFAULT_ITERATION_INCLUDES_EXTRAS true

-Make methods private if possible without failing tests; surprisingly, all the WithoutExtras methods can be private

CLDR-18217

  • This PR completes the ticket.

ALLOW_MANY_COMMITS=true

…lt, iterableDefault

-Instead of Iterable interface, use new methods iterableDefault, iterableWithoutExtras

-Private boolean CLDRFile.DEFAULT_ITERATION_INCLUDES_EXTRAS determines whether iteratorDefault and iterableDefault skip extras; either way they skip paths with null values

-Replace original iterator methods (zero or more parameters) with iteratorDefault or iteratorWithoutExtras

-Iterable and iterator are distinct, as in iterableWithoutExtras versus iteratorWithoutExtras

-Call iteratorWithoutExtras/iterableWithoutExtras directly where tests would otherwise fail with DEFAULT_ITERATION_INCLUDES_EXTRAS true
@btangmu
Copy link
Member Author

btangmu commented Jan 16, 2025

This goes further than #4280

@@ -105,7 +105,7 @@ private static void showEnglish(PrintWriter out) throws IOException {
String locale = "en";
Set<String> sorted =
Builder.with(new TreeSet<String>())
.addAll(cldrFile.iterator())
.addAll(cldrFile.iteratorDefault())
.addAll(cldrFile.getExtraPaths())
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: if iteratorDefault includes extra paths, then getExtraPaths would be superfluous here, except that iteratorDefault skips paths with null values (getStringValue(path) == null)

alternatively fullIterator() could be called here

@btangmu
Copy link
Member Author

btangmu commented Jan 16, 2025

An essential difference from #4280 is that here, iteration always skips paths with null values (except for fullIterable which remains unchanged). Because of this, none of the tests need to call the iterableWithoutExtras or iteratorWithoutExtras

@@ -98,7 +99,7 @@
* http://java.sun.com/j2se/1.4.2/docs/api/org/xml/sax/DTDHandler.html
*/

public class CLDRFile implements Freezable<CLDRFile>, Iterable<String>, LocaleStringProvider {
public class CLDRFile implements Freezable<CLDRFile>, LocaleStringProvider {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major change: CLDRFile no longer implements Iterable. Instead, a method must be called to get an iterable/iterator

@@ -1462,22 +1463,70 @@ public static Set<String> getMatchingXMLFiles(File sourceDirs[], Matcher m) {
return s;
}

@Override
public Iterator<String> iterator() {
private final boolean DEFAULT_ITERATION_INCLUDES_EXTRAS = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this boolean determines whether default iterator/iterable includes extra paths


public Iterator<String> iteratorDefault() {
if (DEFAULT_ITERATION_INCLUDES_EXTRAS) {
return Iterators.filter(fullIterable().iterator(), p -> getStringValue(p) != null);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fullIterable is unchanged. If DEFAULT_ITERATION_INCLUDES_EXTRAS is true, iteratorDefault is the same as fullIterable except that it skips paths with null values

return dataSource.iterator();
}

public synchronized Iterator<String> iterator(String prefix) {
private synchronized Iterator<String> iteratorWithoutExtras(String prefix) {
return dataSource.iterator(prefix);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original iterator methods are renamed to iteratorWithoutExtras and they are now private and rarely used if DEFAULT_ITERATION_INCLUDES_EXTRAS is true

@@ -3081,7 +3146,7 @@ public String getWinningPath(String path) {
public Collection<String> getExtraPaths() {
Set<String> toAddTo = new HashSet<>();
toAddTo.addAll(getRawExtraPaths());
for (String path : this) {
for (String path : this.iterableWithoutExtras()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iterableWithoutExtras is required here to prevent infinite recursion, since the caller is getExtraPaths

@@ -25,7 +25,7 @@ private CharacterFallbacks() {
DtdData.getInstance(DtdType.supplementalData).getDtdComparator(null);

for (Iterator<String> it =
characterFallbacks.iterator("//supplementalData/characters/", comp);
characterFallbacks.iteratorDefault("//supplementalData/characters/", comp);
it.hasNext(); ) {
String path = it.next();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the callers of iterator outside of CLDRFile have become iteratorDefault. Note, BTW, that this loop, and many like it, should be simplified by calling iterableDefault instead of iteratorDefault. I've postponed that simplification, pending decision on the overall approach. Also, including the word "default" in the method names might be temporary; for now, it helps to indicate which code is affected by changing the default behavior from excluding to including extra paths

@btangmu btangmu requested review from macchiati and srl295 and removed request for macchiati February 4, 2025 19:12
@btangmu
Copy link
Member Author

btangmu commented Feb 4, 2025

I'm requesting review even though this is still a DRAFT PR. Question is how to proceed, not necessarily to merge this as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant