• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.util.Locale;
4 
5 /**
6  * A simple class representing an enumeration of possible CLDR coverage levels. Levels may change in the future.
7  *
8  * @author davis
9  *
10  */
11 public enum Level {
12     UNDETERMINED(0, "none", 0), CORE(10, "G4", 100),
13     // POSIX(20, "G4", 100),
14     // MINIMAL(30, "G3.5", 90),
15     BASIC(40, "G3", 80), MODERATE(60, "G2", 70), MODERN(80, "G1", 50), COMPREHENSIVE(100, "G0", 2);
16     //OPTIONAL(101, "optional", 1);
17 
18     @Deprecated
19     public static final Level POSIX = BASIC;
20     @Deprecated
21     public static final Level MINIMAL = BASIC;
22     @Deprecated
23     public static final Level OPTIONAL = COMPREHENSIVE;
24 
25     private final byte level;
26     private final String altName;
27     private final int value;
28 
29     private static final Level[] VALUES = values();
30 
31     /**
32      * returns value ranging from 100 (core) to 1 (optional). Most clients want getLevel instead.
33      *
34      * @return
35      */
getValue()36     public int getValue() {
37         return value;
38     }
39 
Level(int i, String altName, int value)40     private Level(int i, String altName, int value) {
41         this.level = ((byte) i);
42         this.altName = altName;
43         this.value = value;
44     }
45 
get(String name)46     public static Level get(String name) {
47         try {
48             return Level.valueOf(name.toUpperCase(Locale.ENGLISH));
49         } catch (RuntimeException e) {
50             for (Level level : VALUES) {
51                 if (name.equalsIgnoreCase(level.altName)) {
52                     return level;
53                 }
54             }
55             if (name.equalsIgnoreCase("POSIX")) {
56                 return POSIX;
57             } else if (name.equalsIgnoreCase("MINIMAL")) {
58                 return MINIMAL;
59             } else if (name.equalsIgnoreCase("OPTIONAL")) {
60                 return OPTIONAL;
61             }
62             return UNDETERMINED;
63         }
64     }
65 
toString()66     public String toString() {
67         return this.name().toLowerCase();
68     }
69 
70     // public int compareTo(Level o) {
71     // int otherLevel = ((Level) o).level;
72     // return level < otherLevel ? -1 : level > otherLevel ? 1 : 0;
73     // }
74 
75     static final StandardCodes sc = StandardCodes.make();
76 
getDefaultWeight(String organization, String desiredLocale)77     public static int getDefaultWeight(String organization, String desiredLocale) {
78         Level level = sc.getLocaleCoverageLevel(organization, desiredLocale);
79         if (level.compareTo(Level.MODERATE) >= 0) {
80             return 4;
81         }
82         return 1;
83     }
84 
85     /**
86      * This is the numeric value used in the coverage level XML.
87      *
88      * @return range from 10 (core) to 100 (comprehensive).
89      */
getLevel()90     public byte getLevel() {
91         return level;
92     }
93 
fromLevel(int level)94     public static Level fromLevel(int level) {
95         for (Level result : Level.values()) {
96             if (level == result.level) {
97                 return result;
98             }
99         }
100 
101         if (level == 20) {
102             return Level.POSIX;
103         } else if (level == 30) {
104             return Level.MINIMAL;
105         } else if (level == 101) {
106             return Level.OPTIONAL;
107         }
108         throw new IllegalArgumentException(String.valueOf(level));
109     }
110 
fromString(String source)111     public static Level fromString(String source) {
112         return Level.get(source);
113     }
114 }