1 package org.unicode.cldr.icu; 2 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.HashMap; 6 import java.util.Iterator; 7 import java.util.List; 8 import java.util.Map; 9 import java.util.Map.Entry; 10 import java.util.Set; 11 12 /** 13 * Wrapper class for converted ICU data which RB paths to values. 14 */ 15 public class IcuData implements Iterable<String> { 16 private boolean hasFallback; 17 private String sourceFile; 18 private String name; 19 private Map<String, List<String[]>> rbPathToValues; 20 private String comment; 21 private Map<String, String> enumMap; 22 23 /** 24 * IcuData constructor. 25 * 26 * @param sourceFile 27 * the source file of the IcuData object, displayed in 28 * comments in the file 29 * @param name 30 * The name of the IcuData object, also used as the name of the 31 * root node in the output file 32 * @param hasFallback 33 * true if the output file has another ICU file as a 34 * fallback 35 */ IcuData(String sourceFile, String name, boolean hasFallback)36 public IcuData(String sourceFile, String name, boolean hasFallback) { 37 this(sourceFile, name, hasFallback, new HashMap<String, String>()); 38 } 39 40 /** 41 * IcuData constructor. 42 * 43 * @param sourceFile 44 * the source file of the IcuData object, displayed in 45 * comments in the file 46 * @param name 47 * The name of the IcuData object, also used as the name of the 48 * root node in the output file 49 * @param hasFallback 50 * true if the output file has another ICU file as a 51 * fallback 52 * @param enumMap 53 * a mapping of CLDR string values to their integer values in 54 * ICU 55 */ IcuData(String sourceFile, String name, boolean hasFallback, Map<String, String> enumMap)56 public IcuData(String sourceFile, String name, boolean hasFallback, Map<String, String> enumMap) { 57 this.hasFallback = hasFallback; 58 this.sourceFile = sourceFile; 59 this.name = name; 60 rbPathToValues = new HashMap<String, List<String[]>>(); 61 this.enumMap = enumMap; 62 } 63 64 /** 65 * @return true if data should fallback on data in other files, true by default 66 */ hasFallback()67 public boolean hasFallback() { 68 return hasFallback; 69 } 70 71 /** 72 * Returns the the relative path of the source file used to generate the 73 * ICU data. Used when writing the data to file. 74 * 75 * @return 76 */ getSourceFile()77 public String getSourceFile() { 78 return sourceFile; 79 } 80 81 /** 82 * @return the name to be used for the data. 83 */ getName()84 public String getName() { 85 return name; 86 } 87 88 /** 89 * Sets a comment to be placed above the data structure. 90 * @param comment 91 */ setFileComment(String comment)92 public void setFileComment(String comment) { 93 this.comment = comment; 94 } 95 getFileComment()96 public String getFileComment() { 97 return comment; 98 } 99 100 /** 101 * The RB path,value pair actually has an array as the value. So when we 102 * add to it, add to a list. 103 * 104 * @param path 105 * @param value 106 * @return 107 */ add(String path, String... values)108 public void add(String path, String... values) { 109 List<String[]> list = rbPathToValues.get(path); 110 if (list == null) { 111 rbPathToValues.put(path, list = new ArrayList<String[]>(1)); 112 } 113 list.add(normalizeValues(path, values)); 114 } 115 116 /** 117 * The RB path,value pair actually has an array as the value. So when we 118 * add to it, add to a list. 119 * 120 * @param path 121 * @param value 122 * @return 123 */ add(String path, String value)124 void add(String path, String value) { 125 add(path, new String[] { value }); 126 } 127 addAll(String path, Collection<String[]> valueList)128 void addAll(String path, Collection<String[]> valueList) { 129 for (String[] values : valueList) { 130 add(path, values); 131 } 132 } 133 replace(String path, String... values)134 public void replace(String path, String... values) { 135 List<String[]> list = new ArrayList<String[]>(1); 136 rbPathToValues.put(path, list); 137 list.add(normalizeValues(path, values)); 138 } 139 normalizeValues(String rbPath, String[] values)140 private String[] normalizeValues(String rbPath, String[] values) { 141 if (isIntRbPath(rbPath)) { 142 List<String> normalizedValues = new ArrayList<String>(); 143 for (int i = 0; i < values.length; i++) { 144 String curValue = values[i]; 145 String enumValue = enumMap.get(curValue); 146 if (enumValue != null) curValue = enumValue; 147 normalizedValues.add(curValue); 148 } 149 return normalizedValues.toArray(values); 150 } else { 151 return values; 152 } 153 } 154 155 /** 156 * Get items 157 * 158 * @return 159 */ entrySet()160 public Set<Entry<String, List<String[]>>> entrySet() { 161 return rbPathToValues.entrySet(); 162 } 163 164 /** 165 * Get items 166 * 167 * @return 168 */ keySet()169 public Set<String> keySet() { 170 return rbPathToValues.keySet(); 171 } 172 173 @Override iterator()174 public Iterator<String> iterator() { 175 return rbPathToValues.keySet().iterator(); 176 } 177 size()178 public int size() { 179 return rbPathToValues.size(); 180 } 181 containsKey(String key)182 public boolean containsKey(String key) { 183 return rbPathToValues.containsKey(key); 184 } 185 get(String path)186 public List<String[]> get(String path) { 187 return rbPathToValues.get(path); 188 } 189 190 /** 191 * @param rbPath 192 * @return true if the rbPath is for integer values. 193 */ isIntRbPath(String rbPath)194 public static boolean isIntRbPath(String rbPath) { 195 return rbPath.endsWith(":int") || rbPath.endsWith(":intvector"); 196 } 197 }