Skip to content

Commit

Permalink
CLDR-16232 reformatted
Browse files Browse the repository at this point in the history
  • Loading branch information
macchiati committed Nov 15, 2023
1 parent a3fc051 commit 478e37e
Show file tree
Hide file tree
Showing 4 changed files with 1,977 additions and 1,524 deletions.
160 changes: 91 additions & 69 deletions tools/cldr-code/src/main/java/org/unicode/cldr/tool/Inheritance.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package org.unicode.cldr.tool;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.ibm.icu.util.Output;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -10,7 +14,6 @@
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.unicode.cldr.util.CLDRFile;
import org.unicode.cldr.util.CldrUtility;
import org.unicode.cldr.util.DtdData.Attribute;
Expand All @@ -20,44 +23,46 @@
import org.unicode.cldr.util.XMLSource.Alias;
import org.unicode.cldr.util.XPathParts;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.ibm.icu.util.Output;

public class Inheritance {
private Inheritance.AliasMapper aliasMapper;
private Factory factory;
public enum LateralAliasType {remove, removeWhenOptional, count}
public static final Map<String,Inheritance.LateralAliasType> LATERAL_ATTRIBUTES = ImmutableMap.of(
"alt", LateralAliasType.remove,
"case", LateralAliasType.removeWhenOptional,
"gender", LateralAliasType.removeWhenOptional,
"count", LateralAliasType.count
);

public enum LateralAliasType {
remove,
removeWhenOptional,
count
}

public static final Map<String, Inheritance.LateralAliasType> LATERAL_ATTRIBUTES =
ImmutableMap.of(
"alt", LateralAliasType.remove,
"case", LateralAliasType.removeWhenOptional,
"gender", LateralAliasType.removeWhenOptional,
"count", LateralAliasType.count);

/**
* Is this a hard (explicit) value? (convenience method)
*
* @param value
* @return
*/
public static boolean isHardValue(String value) {
return value != null && !value.equals(CldrUtility.INHERITANCE_MARKER);
}

/**
* Creat an Inheritance class from a factory.
*/
/** Creat an Inheritance class from a factory. */
public Inheritance(Factory factory) {
CLDRFile root = factory.make("root", false);
aliasMapper = new AliasMapper(root);
this.factory = factory;
}

/**
* Get a String value with Bailey, as in current CLDRFile.
*/
public String getStringValueWithBailey(String locale, String path, Output<String> pathWhereFound, Output<String> localeWhereFound) {
/** Get a String value with Bailey, as in current CLDRFile. */
public String getStringValueWithBailey(
String locale,
String path,
Output<String> pathWhereFound,
Output<String> localeWhereFound) {
List<CLDRFile> verticalChain = getVerticalLocaleChain(locale);
String result = searchVertical(path, pathWhereFound, localeWhereFound, verticalChain);
if (result != null) {
Expand All @@ -66,14 +71,16 @@ public String getStringValueWithBailey(String locale, String path, Output<String
return getBaileyValue(path, pathWhereFound, localeWhereFound, verticalChain);
}

/**
* Get a Bailey value, as in current CLDRFile.
*/
public String getBaileyValue(String locale, String path, Output<String> pathWhereFound, Output<String> localeWhereFound) {
return getBaileyValue(path, pathWhereFound, localeWhereFound, getVerticalLocaleChain(locale));
/** Get a Bailey value, as in current CLDRFile. */
public String getBaileyValue(
String locale,
String path,
Output<String> pathWhereFound,
Output<String> localeWhereFound) {
return getBaileyValue(
path, pathWhereFound, localeWhereFound, getVerticalLocaleChain(locale));
}


private List<CLDRFile> getVerticalLocaleChain(String locale) {
List<CLDRFile> result = new ArrayList<>();
while (locale != null) {
Expand All @@ -83,9 +90,14 @@ private List<CLDRFile> getVerticalLocaleChain(String locale) {
return ImmutableList.copyOf(result);
}

private String getBaileyValue(String path, Output<String> pathWhereFound, Output<String> localeWhereFound, List<CLDRFile> verticalChain) {
private String getBaileyValue(
String path,
Output<String> pathWhereFound,
Output<String> localeWhereFound,
List<CLDRFile> verticalChain) {
String result;
Set<String> inheritanceChain = getInheritanceChain(verticalChain.get(0).getLocaleID(), path);
Set<String> inheritanceChain =
getInheritanceChain(verticalChain.get(0).getLocaleID(), path);
for (String path2 : inheritanceChain) {
result = searchVertical(path2, pathWhereFound, localeWhereFound, verticalChain);
if (result != null) {
Expand All @@ -98,7 +110,11 @@ private String getBaileyValue(String path, Output<String> pathWhereFound, Output
return null;
}

private String searchVertical(String path, Output<String> pathWhereFound, Output<String> localeWhereFound, List<CLDRFile> verticalChain) {
private String searchVertical(
String path,
Output<String> pathWhereFound,
Output<String> localeWhereFound,
List<CLDRFile> verticalChain) {
for (CLDRFile file : verticalChain) {
String value = file.getStringValue(path);
if (isHardValue(value)) {
Expand All @@ -110,9 +126,7 @@ private String searchVertical(String path, Output<String> pathWhereFound, Output
return null;
}

/**
* Find out if there is vertical inheritance
*/
/** Find out if there is vertical inheritance */
public Set<String> getInheritanceChain(String locale, String path) {
Set<String> result = new LinkedHashSet<>(); // prevent dups
addVerticals(path, result);
Expand All @@ -125,33 +139,41 @@ public Set<String> getInheritanceChain(String locale, String path) {
}
parts = parts.cloneAsThawed();
switch (entry.getValue()) {
case removeWhenOptional:
Attribute a = InheritanceStats.dtdData.getAttribute(parts.getElement(elementNumber), attribute);
if (a.getMode() == Mode.OPTIONAL) {
case removeWhenOptional:
Attribute a =
InheritanceStats.dtdData.getAttribute(
parts.getElement(elementNumber), attribute);
if (a.getMode() == Mode.OPTIONAL) {
addPathReplacingAttribute(parts, elementNumber, attribute, null, result);
}
break;
case remove:
addPathReplacingAttribute(parts, elementNumber, attribute, null, result);
}
break;
case remove:
addPathReplacingAttribute(parts, elementNumber, attribute, null, result);
break;
case count:
// TBD if count is decimal, use locale to get category
String attValue = parts.getAttributeValue(elementNumber, attribute);
if (!"other".equals(attValue)) {
addPathReplacingAttribute(parts, elementNumber, attribute, "other", result);
}
addPathReplacingAttribute(parts, elementNumber, attribute, null, result);
break;
break;
case count:
// TBD if count is decimal, use locale to get category
String attValue = parts.getAttributeValue(elementNumber, attribute);
if (!"other".equals(attValue)) {
addPathReplacingAttribute(parts, elementNumber, attribute, "other", result);
}
addPathReplacingAttribute(parts, elementNumber, attribute, null, result);
break;
}
int elementNumber2 = getFirstElementForAttribute(parts, attribute);
if (elementNumber2 > 0) {
throw new IllegalArgumentException("Multiple instances of " + attribute + " in " + parts);
throw new IllegalArgumentException(
"Multiple instances of " + attribute + " in " + parts);
}
}
return result;
}

private void addPathReplacingAttribute(XPathParts parts, int elementNumber, String attribute, String attValue, Set<String> result) {
private void addPathReplacingAttribute(
XPathParts parts,
int elementNumber,
String attribute,
String attValue,
Set<String> result) {
if (attValue == null) {
parts.removeAttribute(elementNumber, attribute);
} else {
Expand All @@ -177,8 +199,8 @@ public int getFirstElementForAttribute(XPathParts parts, String key) {
}

/**
* Class to gather all the aliases in root into a form useful for
* processing lateral alias inheritance.
* Class to gather all the aliases in root into a form useful for processing lateral alias
* inheritance.
*/
public static class AliasMapper {
// this map is sorted in reverse, so that longer substrings always come before shorter
Expand All @@ -187,15 +209,14 @@ public static class AliasMapper {

/**
* Get all the prefixes
*
* @return
*/
public Set<String> getInheritingPathPrefixes() {
return sorted.keySet();
}

/**
* Get all the inherited paths for a given path
*/
/** Get all the inherited paths for a given path */
public <T extends Collection<String>> T getInheritedPaths(String path, T result) {
while (true) {
String trial = getInheritedPath(path);
Expand All @@ -211,32 +232,32 @@ public <T extends Collection<String>> T getInheritedPaths(String path, T result)
}

/**
* Given a path in a resolving CLDRFile that inherits laterally with aliases,
* return the path it inherits from.
* <br>If the CLDRFile is not resolving, an exception is thrown.
* Given a path in a resolving CLDRFile that inherits laterally with aliases, return the
* path it inherits from. <br>
* If the CLDRFile is not resolving, an exception is thrown.
*
* @param path
* @return the path that the input path inherits from laterally,
* or null if there is no such path.
* <br>If the file is not resolving, an exception is thrown.
* @return the path that the input path inherits from laterally, or null if there is no such
* path. <br>
* If the file is not resolving, an exception is thrown.
*/

public String getInheritedPath(String path) {
SortedMap<String, String> less = sorted.tailMap(path);
String firstLess = less.firstKey();
if (!path.startsWith(firstLess)) {
return null;
}
String result = sorted.get(firstLess)
+ path.substring(firstLess.length());
String result = sorted.get(firstLess) + path.substring(firstLess.length());
// System.out.println(path + " ==> " + result);
return result;
}

/**
* Given a path in a resolving CLDRFile, find all of the paths that inherit from it laterally.
* That is, the result is the set of all paths P such that getInheritedPath(P) == path
* If there are no such paths, the empty set is returned.
* <br>If the CLDRFile is not resolving, an exception is thrown.
* Given a path in a resolving CLDRFile, find all of the paths that inherit from it
* laterally. That is, the result is the set of all paths P such that getInheritedPath(P) ==
* path If there are no such paths, the empty set is returned. <br>
* If the CLDRFile is not resolving, an exception is thrown.
*
* @param path
* @return immutable set of laterally inheriting paths
*/
Expand All @@ -246,6 +267,7 @@ public Set<String> getInheritingPaths(String path) {

/**
* Put together all the alias paths into the format: prefix => result.
*
* @param root
*/
public AliasMapper(CLDRFile root) {
Expand All @@ -270,4 +292,4 @@ public AliasMapper(CLDRFile root) {
this.sorted = ImmutableSortedMap.copyOf(sorted);
}
}
}
}
Loading

0 comments on commit 478e37e

Please sign in to comment.