xref: /aosp_15_r20/external/cldr/tools/cldr-code/src/main/java/org/unicode/cldr/util/Level.java (revision 912701f9769bb47905792267661f0baf2b85bed5)
1 package org.unicode.cldr.util;
2 
3 import com.google.common.collect.ImmutableSortedSet;
4 import java.util.Locale;
5 import java.util.Set;
6 
7 /**
8  * A simple class representing an enumeration of possible CLDR coverage levels. Levels may change in
9  * the future.
10  *
11  * @author davis
12  */
13 public enum Level {
14     UNDETERMINED(0, "none", 0, "�"),
15     CORE(10, "G4", 100, "ⓒ"),
16     BASIC(40, "G3", 80, "ⓑ"),
17     MODERATE(60, "G2", 70, "ⓜ"),
18     MODERN(80, "G1", 50, "��"),
19     COMPREHENSIVE(100, "G0", 2, "��");
20 
21     public static final Set<Level> CORE_TO_MODERN =
22             ImmutableSortedSet.of(CORE, BASIC, MODERATE, MODERN);
23 
24     @Deprecated public static final Level POSIX = BASIC;
25     @Deprecated public static final Level MINIMAL = BASIC;
26     @Deprecated public static final Level OPTIONAL = COMPREHENSIVE;
27 
28     private final byte level;
29     private final String altName;
30     private final int value;
31     private String abbreviation;
32 
33     private static final Level[] VALUES = values();
34 
35     /**
36      * returns value ranging from 100 (core) to 1 (optional). Most clients want getLevel instead.
37      *
38      * @return
39      */
getValue()40     public int getValue() {
41         return value;
42     }
43 
Level(int i, String altName, int value, String abbreviation)44     private Level(int i, String altName, int value, String abbreviation) {
45         this.level = ((byte) i);
46         this.altName = altName;
47         this.value = value;
48         this.abbreviation = abbreviation;
49     }
50 
get(String name)51     public static Level get(String name) {
52         try {
53             return Level.valueOf(name.toUpperCase(Locale.ENGLISH));
54         } catch (RuntimeException e) {
55             for (Level level : VALUES) {
56                 if (name.equalsIgnoreCase(level.altName)) {
57                     return level;
58                 }
59             }
60             if (name.equalsIgnoreCase("POSIX")) {
61                 return POSIX;
62             } else if (name.equalsIgnoreCase("MINIMAL")) {
63                 return MINIMAL;
64             } else if (name.equalsIgnoreCase("OPTIONAL")) {
65                 return OPTIONAL;
66             }
67             return UNDETERMINED;
68         }
69     }
70 
getAltName()71     public String getAltName() {
72         return altName;
73     }
74 
getAbbreviation()75     public String getAbbreviation() {
76         return abbreviation;
77     }
78 
79     @Override
toString()80     public String toString() {
81         return this.name().toLowerCase();
82     }
83 
getDefaultWeight(String organization, String desiredLocale)84     public static int getDefaultWeight(String organization, String desiredLocale) {
85         Level level = StandardCodes.make().getLocaleCoverageLevel(organization, desiredLocale);
86         if (level.compareTo(Level.MODERATE) >= 0) {
87             return 4;
88         }
89         return 1;
90     }
91 
92     /**
93      * This is the numeric value used in the coverage level XML.
94      *
95      * @return range from 10 (core) to 100 (comprehensive).
96      */
getLevel()97     public byte getLevel() {
98         return level;
99     }
100 
fromLevel(int level)101     public static Level fromLevel(int level) {
102         for (Level result : Level.values()) {
103             if (level == result.level) {
104                 return result;
105             }
106         }
107 
108         if (level == 20) {
109             return Level.POSIX;
110         } else if (level == 30) {
111             return Level.MINIMAL;
112         } else if (level == 101) {
113             return Level.OPTIONAL;
114         }
115         throw new IllegalArgumentException(String.valueOf(level));
116     }
117 
fromString(String source)118     public static Level fromString(String source) {
119         return Level.get(source);
120     }
121 
122     /**
123      * Return the minimum level between two For example, Level.min(COMPREHENSIVE, MODERN) = MODERN
124      *
125      * @param a
126      * @param b
127      * @return level with the minimal getLevel() value
128      */
min(Level a, Level b)129     public static Level min(Level a, Level b) {
130         return Level.fromLevel(Math.min(a.getLevel(), b.getLevel()));
131     }
132 
133     /**
134      * Return the maximum level between two For example, Level.min(COMPREHENSIVE, MODERN) = MODERN
135      *
136      * @param a
137      * @param b
138      * @return level with the minimal getLevel() value
139      */
max(Level a, Level b)140     public static Level max(Level a, Level b) {
141         if (a == null) {
142             return b;
143         }
144         return Level.fromLevel(Math.max(a.getLevel(), b.getLevel()));
145     }
146 
max(Level... levels)147     public static Level max(Level... levels) {
148         Level result = Level.UNDETERMINED;
149         for (Level level : levels) {
150             if (level != null) {
151                 result = max(result, level);
152             }
153         }
154         return result;
155     }
156 
157     /**
158      * Returns true if this is at least the other level. Example: lev.isAtLeast(Level.MODERN)
159      *
160      * @param other
161      * @return
162      */
isAtLeast(Level other)163     public boolean isAtLeast(Level other) {
164         return getLevel() >= other.getLevel();
165     }
166 }
167