xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/util/LocaleInheritanceInfo.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.util;
2 
3 /** A class with information about why an inheritance worked the way it did */
4 public final class LocaleInheritanceInfo {
5     /** Reason this entry is there */
6     public enum Reason {
7         /**
8          * An actual value was found in the XML source. This is never returned for constructed or
9          * other synthesized values.
10          */
11         value("Found: explicit value", true),
12         /**
13          * codeFallback does not have a locale, it is effectively the parent of 'root'. A value was
14          * calculated according to the specification.
15          */
16         codeFallback("Found: code fallback", true),
17         /**
18          * This shows the location where an alias was found which affected the inheritance chain.
19          */
20         alias("An alias was found at this location", false),
21         /**
22          * Constructed entries form a block. All such values are in place of the actual constructed
23          * value.
24          */
25         constructed("Constructed value", false),
26         none("The value was not found in this locale.", true),
27         inheritanceMarker("Found: Inheritance marker", false),
28         removedAttribute("Removed attribute: ${attribute}", false),
29         changedAttribute("Changed attribute: ${attribute}", false),
30         /**
31          * @see CLDRFile#getFallbackPath - used for other fallback paths
32          */
33         fallback("Other fallback path", false);
34 
35         private String description;
36         private boolean terminal;
37 
38         /**
39          * An entry is 'terminal' if it represents the end of a successful look up chain. A
40          * nonterminal entry merely contributes to the look up. Only terminal entries will
41          * correspond to a return value from {@link CLDRFile#getSourceLocaleIdExtended(String,
42          * org.unicode.cldr.util.CLDRFile.Status, boolean)} for example. Entries following a
43          * terminal entry are the start of a bailey value.
44          *
45          * @return
46          */
isTerminal()47         public boolean isTerminal() {
48             return terminal;
49         }
50 
Reason(String description, boolean terminal)51         Reason(String description, boolean terminal) {
52             this.description = description;
53             this.terminal = terminal;
54         }
55 
getDescription()56         public String getDescription() {
57             return this.description;
58         }
59 
60         @Override
toString()61         public String toString() {
62             return this.name() + ": " + description;
63         }
64     }
65 
66     private String locale;
67 
68     /**
69      * Optional locale for this entry. or null
70      *
71      * @return
72      */
getLocale()73     public String getLocale() {
74         return locale;
75     }
76 
77     private String path;
78 
79     /**
80      * Optional path for this entry, or null
81      *
82      * @return
83      */
getPath()84     public String getPath() {
85         return path;
86     }
87 
88     private Reason reason;
89 
90     /**
91      * Reason enum for this entry
92      *
93      * @return
94      */
getReason()95     public Reason getReason() {
96         return reason;
97     }
98 
99     private String attribute = null;
100 
101     /**
102      * Which attribute was involved (for Reason.removedAttribute/Reason.changedAttribute)
103      *
104      * @return
105      */
getAttribute()106     public String getAttribute() {
107         return attribute;
108     }
109 
110     /**
111      * @param locale required locale
112      * @param path optional xpath
113      * @param reason required reason
114      */
LocaleInheritanceInfo(String locale, String path, Reason reason)115     LocaleInheritanceInfo(String locale, String path, Reason reason) {
116         this.locale = locale;
117         this.path = path;
118         this.reason = reason;
119     }
120 
LocaleInheritanceInfo(String locale, String path, Reason reason, String attribute)121     LocaleInheritanceInfo(String locale, String path, Reason reason, String attribute) {
122         this.locale = locale;
123         this.path = path;
124         this.reason = reason;
125         this.attribute = attribute;
126     }
127 
128     @Override
toString()129     public String toString() {
130         if (locale == null && path == null) {
131             return reason.name();
132         } else if (path == null) {
133             return String.format("%s: locale %s", reason.name(), locale);
134         } else if (locale == null) {
135             return String.format("%s: %s", reason.name(), path);
136         } else {
137             return String.format("%s: %s:%s", reason.name(), locale, path);
138         }
139     }
140 
141     @Override
equals(Object other)142     public boolean equals(Object other) {
143         if (!(other instanceof LocaleInheritanceInfo)) return false;
144         final LocaleInheritanceInfo o = (LocaleInheritanceInfo) other;
145         if (o.reason != reason) return false;
146         if (!equals(locale, o.locale)) return false;
147         if (!equals(path, o.path)) return false;
148         return true;
149     }
150 
equals(String a, String b)151     private static final boolean equals(String a, String b) {
152         if (a == null && b == null) return true;
153         if (a == null && b != null) return false;
154         return a.equals(b);
155     }
156 }
157