Path: blob/master/src/java.base/share/classes/sun/util/locale/UnicodeLocaleExtension.java
41159 views
1/*2* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation. Oracle designates this8* particular file as subject to the "Classpath" exception as provided9* by Oracle in the LICENSE file that accompanied this code.10*11* This code is distributed in the hope that it will be useful, but WITHOUT12* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or13* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License14* version 2 for more details (a copy is included in the LICENSE file that15* accompanied this code).16*17* You should have received a copy of the GNU General Public License version18* 2 along with this work; if not, write to the Free Software Foundation,19* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.20*21* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA22* or visit www.oracle.com if you need additional information or have any23* questions.24*/2526/*27*******************************************************************************28* Copyright (C) 2009-2010, International Business Machines Corporation and *29* others. All Rights Reserved. *30*******************************************************************************31*/32package sun.util.locale;3334import java.util.Collections;35import java.util.Map;36import java.util.Map.Entry;37import java.util.Set;38import java.util.SortedMap;39import java.util.SortedSet;40import java.util.StringJoiner;4142public class UnicodeLocaleExtension extends Extension {43public static final char SINGLETON = 'u';4445private final Set<String> attributes;46private final Map<String, String> keywords;4748public static final UnicodeLocaleExtension CA_JAPANESE49= new UnicodeLocaleExtension("ca", "japanese");50public static final UnicodeLocaleExtension NU_THAI51= new UnicodeLocaleExtension("nu", "thai");5253private UnicodeLocaleExtension(String key, String value) {54super(SINGLETON, key + "-" + value);55attributes = Collections.emptySet();56keywords = Collections.singletonMap(key, value);57}5859UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) {60super(SINGLETON);61if (attributes != null) {62this.attributes = attributes;63} else {64this.attributes = Collections.emptySet();65}66if (keywords != null) {67this.keywords = keywords;68} else {69this.keywords = Collections.emptyMap();70}7172if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) {73StringJoiner sj = new StringJoiner(LanguageTag.SEP);74for (String attribute : this.attributes) {75sj.add(attribute);76}77for (Entry<String, String> keyword : this.keywords.entrySet()) {78String key = keyword.getKey();79String value = keyword.getValue();8081sj.add(key);82if (!value.isEmpty()) {83sj.add(value);84}85}86setValue(sj.toString());87}88}8990public Set<String> getUnicodeLocaleAttributes() {91if (attributes == Collections.EMPTY_SET) {92return attributes;93}94return Collections.unmodifiableSet(attributes);95}9697public Set<String> getUnicodeLocaleKeys() {98if (keywords == Collections.EMPTY_MAP) {99return Collections.emptySet();100}101return Collections.unmodifiableSet(keywords.keySet());102}103104public String getUnicodeLocaleType(String unicodeLocaleKey) {105return keywords.get(unicodeLocaleKey);106}107108public static boolean isSingletonChar(char c) {109return (SINGLETON == LocaleUtils.toLower(c));110}111112public static boolean isAttribute(String s) {113// 3*8alphanum114int len = s.length();115return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);116}117118public static boolean isKey(String s) {119// 2alphanum120return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);121}122123public static boolean isTypeSubtag(String s) {124// 3*8alphanum125int len = s.length();126return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);127}128}129130131