UnicodeLocaleExtension.java revision 1537b2f39245c07b00aa78c3600f7aebcb172490
1/* GENERATED SOURCE. DO NOT MODIFY. */
2/*
3 *******************************************************************************
4 * Copyright (C) 2009-2014, International Business Machines Corporation and    *
5 * others. All Rights Reserved.                                                *
6 *******************************************************************************
7 */
8package android.icu.impl.locale;
9
10import java.util.Collections;
11import java.util.Map.Entry;
12import java.util.Set;
13import java.util.SortedMap;
14import java.util.SortedSet;
15import java.util.TreeMap;
16import java.util.TreeSet;
17
18/**
19 * @hide Only a subset of ICU is exposed in Android
20 * @hide All android.icu classes are currently hidden
21 */
22public class UnicodeLocaleExtension extends Extension {
23    public static final char SINGLETON = 'u';
24
25    private static final SortedSet<String> EMPTY_SORTED_SET = new TreeSet<String>();
26    private static final SortedMap<String, String> EMPTY_SORTED_MAP = new TreeMap<String, String>();
27
28    private SortedSet<String> _attributes = EMPTY_SORTED_SET;
29    private SortedMap<String, String> _keywords = EMPTY_SORTED_MAP;
30
31    public static final UnicodeLocaleExtension CA_JAPANESE;
32    public static final UnicodeLocaleExtension NU_THAI;
33
34    static {
35        CA_JAPANESE = new UnicodeLocaleExtension();
36        CA_JAPANESE._keywords = new TreeMap<String, String>();
37        CA_JAPANESE._keywords.put("ca", "japanese");
38        CA_JAPANESE._value = "ca-japanese";
39
40        NU_THAI = new UnicodeLocaleExtension();
41        NU_THAI._keywords = new TreeMap<String, String>();
42        NU_THAI._keywords.put("nu", "thai");
43        NU_THAI._value = "nu-thai";
44    }
45
46    private UnicodeLocaleExtension() {
47        super(SINGLETON);
48    }
49
50    UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) {
51        this();
52        if (attributes != null && attributes.size() > 0) {
53            _attributes = attributes;
54        }
55        if (keywords != null && keywords.size() > 0) {
56            _keywords = keywords;
57        }
58
59        if (_attributes.size() > 0 || _keywords.size() > 0) {
60            StringBuilder sb = new StringBuilder();
61            for (String attribute : _attributes) {
62                sb.append(LanguageTag.SEP).append(attribute);
63            }
64            for (Entry<String, String> keyword : _keywords.entrySet()) {
65                String key = keyword.getKey();
66                String value = keyword.getValue();
67
68                sb.append(LanguageTag.SEP).append(key);
69                if (value.length() > 0) {
70                    sb.append(LanguageTag.SEP).append(value);
71                }
72            }
73            _value = sb.substring(1);   // skip leading '-'
74        }
75    }
76
77    public Set<String> getUnicodeLocaleAttributes() {
78        return Collections.unmodifiableSet(_attributes);
79    }
80
81    public Set<String> getUnicodeLocaleKeys() {
82        return Collections.unmodifiableSet(_keywords.keySet());
83    }
84
85    public String getUnicodeLocaleType(String unicodeLocaleKey) {
86        return _keywords.get(unicodeLocaleKey);
87    }
88
89    public static boolean isSingletonChar(char c) {
90        return (SINGLETON == AsciiUtil.toLower(c));
91    }
92
93    public static boolean isAttribute(String s) {
94        // 3*8alphanum
95        return (s.length() >= 3) && (s.length() <= 8) && AsciiUtil.isAlphaNumericString(s);
96    }
97
98    public static boolean isKey(String s) {
99        // 2alphanum
100        return (s.length() == 2) && AsciiUtil.isAlphaNumericString(s);
101    }
102
103    public static boolean isTypeSubtag(String s) {
104        // 3*8alphanum
105        return (s.length() >= 3) && (s.length() <= 8) && AsciiUtil.isAlphaNumericString(s);
106    }
107
108    public static boolean isType(String s) {
109        // sequence of type subtags delimited by '-'
110        int startIdx = 0;
111        boolean sawSubtag = false;
112        while (true) {
113            int idx = s.indexOf(LanguageTag.SEP, startIdx);
114            String subtag = idx < 0 ? s.substring(startIdx) : s.substring(startIdx, idx);
115            if (!isTypeSubtag(subtag)) {
116                return false;
117            }
118            sawSubtag = true;
119            if (idx < 0) {
120                break;
121            }
122            startIdx = idx + 1;
123        }
124        return sawSubtag && startIdx < s.length();
125    }
126}
127