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