• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.util.Locale;
4 import java.util.Map;
5 import java.util.Set;
6 import java.util.TreeMap;
7 import java.util.regex.Pattern;
8 
9 import org.unicode.cldr.test.ExampleGenerator;
10 import org.unicode.cldr.util.StandardCodes.LstrType;
11 import org.unicode.cldr.util.Validity.Status;
12 
13 import com.google.common.collect.BiMap;
14 import com.google.common.collect.ImmutableBiMap;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableMultimap;
17 import com.google.common.collect.Multimap;
18 import com.google.common.collect.TreeMultimap;
19 import com.ibm.icu.text.MessageFormat;
20 import com.ibm.icu.text.UnicodeSet;
21 import com.ibm.icu.text.UnicodeSetSpanner;
22 
23 public class Units {
24 
25     private static final UnicodeSet WHITESPACE = new UnicodeSet("[:whitespace:]").freeze();
26     public static Pattern NO_SPACE_PREFIX = Pattern.compile("\\}" + ExampleGenerator.backgroundEndSymbol + "?\\p{L}|\\p{L}" + ExampleGenerator.backgroundStartSymbol + "?\\{");
27 
combinePattern(String unitFormat, String compoundPattern, boolean lowercaseUnitIfNoSpaceInCompound)28     public static String combinePattern(String unitFormat, String compoundPattern, boolean lowercaseUnitIfNoSpaceInCompound) {
29         // meterFormat of the form {0} meters or {0} Meter
30         // compoundPattern is of the form Z{0} or Zetta{0}
31 
32         // extract the unit
33         String modUnit = (String) SPACE_SPANNER.trim(unitFormat.replace("{0}", ""));
34         String[] parameters = { modUnit };
35 
36         String modFormat = unitFormat.replace(modUnit, MessageFormat.format(compoundPattern, parameters));
37         if (modFormat.equals(unitFormat)) {
38             // didn't work, so fall back
39             String[] parameters1 = { unitFormat };
40             modFormat = MessageFormat.format(compoundPattern, parameters1);
41         }
42 
43         // hack to fix casing
44         if (lowercaseUnitIfNoSpaceInCompound
45             && NO_SPACE_PREFIX.matcher(compoundPattern).find()) {
46             modFormat = modFormat.replace(modUnit, modUnit.toLowerCase(Locale.ENGLISH));
47         }
48 
49         return modFormat;
50     }
51 
52     static final UnicodeSetSpanner SPACE_SPANNER = new UnicodeSetSpanner(WHITESPACE);
53 
54     public static final Map<String,String> CORE_TO_TYPE;
55     public static final Multimap<String,String> TYPE_TO_CORE;
56     public static final BiMap<String,String> LONG_TO_SHORT;
57 
58     static {
59         Set<String> VALID_UNITS = Validity.getInstance().getStatusToCodes(LstrType.unit).get(Status.regular);
60 
61         Map<String, String> coreToType = new TreeMap<>();
62         Multimap<String, String> typeToCore = TreeMultimap.create();
63         Map<String, String> longToShort = new TreeMap<>();
64         for (String s : VALID_UNITS) {
65             int dashPos = s.indexOf('-');
66             String unitType = s.substring(0,dashPos);
67             String coreUnit = s.substring(dashPos+1);
longToShort.put(s, coreUnit)68             longToShort.put(s, coreUnit);
69             //coreUnit = converter.fixDenormalized(coreUnit);
coreToType.put(coreUnit, unitType)70             coreToType.put(coreUnit, unitType);
typeToCore.put(unitType, coreUnit)71             typeToCore.put(unitType, coreUnit);
72         }
73         CORE_TO_TYPE = ImmutableMap.copyOf(coreToType);
74         TYPE_TO_CORE = ImmutableMultimap.copyOf(typeToCore);
75         LONG_TO_SHORT = ImmutableBiMap.copyOf(longToShort);
76     }
77 
getShort(String longUnit)78     public static String getShort(String longUnit) {
79         return LONG_TO_SHORT.get(longUnit);
80     }
81 }
82