• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.unicode.cldr.util;
2 
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.LinkedHashMap;
6 import java.util.List;
7 import java.util.Map;
8 
9 import com.google.common.collect.ImmutableMap;
10 import com.ibm.icu.lang.CharSequences;
11 import com.ibm.icu.text.ListFormatter;
12 import com.ibm.icu.text.SimpleFormatter;
13 import com.ibm.icu.text.UTF16;
14 
15 public class XListFormatter {
16 
17     public enum ListTypeLength {
18         AND_WIDE(""),
19         AND_SHORT("[@type=\"standard-short\"]"),
20         OR_WIDE("[@type=\"or\"]"),
21         UNIT_WIDE("[@type=\"unit\"]"),
22         UNIT_SHORT("[@type=\"unit-short\"]"),
23         UNIT_NARROW("[@type=\"unit-narrow\"]")
24         ;
25 
26         public static final ListTypeLength NORMAL = AND_WIDE;
27 
28         final String typeString;
29 
30         static final Map<String, ListTypeLength> stringToEnum;
31         static {
32             Map<String, ListTypeLength> _stringToEnum = new LinkedHashMap<>();
33             for (ListTypeLength value : values()) {
34                 if (value != NORMAL) {
35                     _stringToEnum.put(value.typeString.substring(value.typeString.indexOf('"')+1, value.typeString.lastIndexOf('"')), value);
36                 }
37             }
38             stringToEnum = ImmutableMap.copyOf(_stringToEnum);
39         }
ListTypeLength(String typeString)40         private ListTypeLength(String typeString) {
41             this.typeString = typeString;
42         }
from(String listPatternType)43         public static ListTypeLength from(String listPatternType) {
44             if (listPatternType == null) {
45                 return NORMAL;
46             }
47             return stringToEnum.get(listPatternType);
48         }
getPath()49         public String getPath() {
50             return "//ldml/listPatterns/listPattern"
51                 + typeString
52                 + "/listPatternPart[@type=\"{0}\"]";
53         }
54     }
55 
56     private ListFormatter listFormatter;
57 
XListFormatter(CLDRFile cldrFile, ListTypeLength listTypeLength)58     public XListFormatter(CLDRFile cldrFile, ListTypeLength listTypeLength) {
59         SimpleFormatter listPathFormat = SimpleFormatter.compile(listTypeLength.getPath());
60         String doublePattern = cldrFile.getWinningValue(listPathFormat.format("2"));
61         String startPattern = cldrFile.getWinningValue(listPathFormat.format("start"));
62         String middlePattern = cldrFile.getWinningValue(listPathFormat.format("middle"));
63         String endPattern = cldrFile.getWinningValue(listPathFormat.format("end"));
64         listFormatter = new ListFormatter(doublePattern, startPattern, middlePattern, endPattern);
65     }
66 
format(Object... items)67     public String format(Object... items) {
68         return listFormatter.format(items);
69     }
70 
format(Collection<?> items)71     public String format(Collection<?> items) {
72         return listFormatter.format(items);
73     }
74 
formatCodePoints(String items)75     public String formatCodePoints(String items) {
76         List<String> source = new ArrayList<>();
77         for (int sourceInt : CharSequences.codePoints(items)) { // TODO add utility in CharSequences
78            source.add(UTF16.valueOf(sourceInt));
79         }
80         return listFormatter.format(source);
81     }
82 }