• 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         AND_NARROW("[@type=\"standard-narrow\"]"),
21         OR_WIDE("[@type=\"or\"]"),
22         OR_SHORT("[@type=\"or-short\"]"),
23         OR_NARROW("[@type=\"or-narrow\"]"),
24         UNIT_WIDE("[@type=\"unit\"]"),
25         UNIT_SHORT("[@type=\"unit-short\"]"),
26         UNIT_NARROW("[@type=\"unit-narrow\"]")
27         ;
28 
29         public static final ListTypeLength NORMAL = AND_WIDE;
30 
31         final String typeString;
32 
33         static final Map<String, ListTypeLength> stringToEnum;
34         static {
35             Map<String, ListTypeLength> _stringToEnum = new LinkedHashMap<>();
36             for (ListTypeLength value : values()) {
37                 if (value != NORMAL) {
38                     _stringToEnum.put(value.typeString.substring(value.typeString.indexOf('"')+1, value.typeString.lastIndexOf('"')), value);
39                 }
40             }
41             stringToEnum = ImmutableMap.copyOf(_stringToEnum);
42         }
ListTypeLength(String typeString)43         private ListTypeLength(String typeString) {
44             this.typeString = typeString;
45         }
from(String listPatternType)46         public static ListTypeLength from(String listPatternType) {
47             if (listPatternType == null) {
48                 return NORMAL;
49             }
50             return stringToEnum.get(listPatternType);
51         }
getPath()52         public String getPath() {
53             return "//ldml/listPatterns/listPattern"
54                 + typeString
55                 + "/listPatternPart[@type=\"{0}\"]";
56         }
57     }
58 
59     private ListFormatter listFormatter;
60 
XListFormatter(CLDRFile cldrFile, ListTypeLength listTypeLength)61     public XListFormatter(CLDRFile cldrFile, ListTypeLength listTypeLength) {
62         SimpleFormatter listPathFormat = SimpleFormatter.compile(listTypeLength.getPath());
63         String doublePattern = cldrFile.getWinningValue(listPathFormat.format("2"));
64         String startPattern = cldrFile.getWinningValue(listPathFormat.format("start"));
65         String middlePattern = cldrFile.getWinningValue(listPathFormat.format("middle"));
66         String endPattern = cldrFile.getWinningValue(listPathFormat.format("end"));
67         listFormatter = new ListFormatter(doublePattern, startPattern, middlePattern, endPattern);
68     }
69 
format(Object... items)70     public String format(Object... items) {
71         return listFormatter.format(items);
72     }
73 
format(Collection<?> items)74     public String format(Collection<?> items) {
75         return listFormatter.format(items);
76     }
77 
formatCodePoints(String items)78     public String formatCodePoints(String items) {
79         List<String> source = new ArrayList<>();
80         for (int sourceInt : CharSequences.codePoints(items)) { // TODO add utility in CharSequences
81            source.add(UTF16.valueOf(sourceInt));
82         }
83         return listFormatter.format(source);
84     }
85 }