• 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 
72     @Override
toString()73     public String toString() {
74         return this.name().toLowerCase();
75     }
76 
77     // public int compareTo(Level o) {
78     // int otherLevel = ((Level) o).level;
79     // return level < otherLevel ? -1 : level > otherLevel ? 1 : 0;
80     // }
81 
82     static final StandardCodes sc = StandardCodes.make();
83 
getDefaultWeight(String organization, String desiredLocale)84     public static int getDefaultWeight(String organization, String desiredLocale) {
85         Level level = sc.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 }