• 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.ImmutableSet;
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 = ImmutableSet.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 
35     private static final Level[] VALUES = values();
36 
37     /**
38      * returns value ranging from 100 (core) to 1 (optional). Most clients want getLevel instead.
39      *
40      * @return
41      */
getValue()42     public int getValue() {
43         return value;
44     }
45 
Level(int i, String altName, int value)46     private Level(int i, String altName, int value) {
47         this.level = ((byte) i);
48         this.altName = altName;
49         this.value = value;
50     }
51 
get(String name)52     public static Level get(String name) {
53         try {
54             return Level.valueOf(name.toUpperCase(Locale.ENGLISH));
55         } catch (RuntimeException e) {
56             for (Level level : VALUES) {
57                 if (name.equalsIgnoreCase(level.altName)) {
58                     return level;
59                 }
60             }
61             if (name.equalsIgnoreCase("POSIX")) {
62                 return POSIX;
63             } else if (name.equalsIgnoreCase("MINIMAL")) {
64                 return MINIMAL;
65             } else if (name.equalsIgnoreCase("OPTIONAL")) {
66                 return OPTIONAL;
67             }
68             return UNDETERMINED;
69         }
70     }
71 
getAltName()72     public String getAltName() {
73         return altName;
74     }
75 
76     @Override
toString()77     public String toString() {
78         return this.name().toLowerCase();
79     }
80 
81     // public int compareTo(Level o) {
82     // int otherLevel = ((Level) o).level;
83     // return level < otherLevel ? -1 : level > otherLevel ? 1 : 0;
84     // }
85 
86     static final StandardCodes sc = StandardCodes.make();
87 
getDefaultWeight(String organization, String desiredLocale)88     public static int getDefaultWeight(String organization, String desiredLocale) {
89         Level level = sc.getLocaleCoverageLevel(organization, desiredLocale);
90         if (level.compareTo(Level.MODERATE) >= 0) {
91             return 4;
92         }
93         return 1;
94     }
95 
96     /**
97      * This is the numeric value used in the coverage level XML.
98      *
99      * @return range from 10 (core) to 100 (comprehensive).
100      */
getLevel()101     public byte getLevel() {
102         return level;
103     }
104 
fromLevel(int level)105     public static Level fromLevel(int level) {
106         for (Level result : Level.values()) {
107             if (level == result.level) {
108                 return result;
109             }
110         }
111 
112         if (level == 20) {
113             return Level.POSIX;
114         } else if (level == 30) {
115             return Level.MINIMAL;
116         } else if (level == 101) {
117             return Level.OPTIONAL;
118         }
119         throw new IllegalArgumentException(String.valueOf(level));
120     }
121 
fromString(String source)122     public static Level fromString(String source) {
123         return Level.get(source);
124     }
125 }