Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/util/locale/UnicodeLocaleExtension.java
38918 views
1/*2* Copyright (c) 2010, 2011, 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;4041public class UnicodeLocaleExtension extends Extension {42public static final char SINGLETON = 'u';4344private final Set<String> attributes;45private final Map<String, String> keywords;4647public static final UnicodeLocaleExtension CA_JAPANESE48= new UnicodeLocaleExtension("ca", "japanese");49public static final UnicodeLocaleExtension NU_THAI50= new UnicodeLocaleExtension("nu", "thai");5152private UnicodeLocaleExtension(String key, String value) {53super(SINGLETON, key + "-" + value);54attributes = Collections.emptySet();55keywords = Collections.singletonMap(key, value);56}5758UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) {59super(SINGLETON);60if (attributes != null) {61this.attributes = attributes;62} else {63this.attributes = Collections.emptySet();64}65if (keywords != null) {66this.keywords = keywords;67} else {68this.keywords = Collections.emptyMap();69}7071if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) {72StringBuilder sb = new StringBuilder();73for (String attribute : this.attributes) {74sb.append(LanguageTag.SEP).append(attribute);75}76for (Entry<String, String> keyword : this.keywords.entrySet()) {77String key = keyword.getKey();78String value = keyword.getValue();7980sb.append(LanguageTag.SEP).append(key);81if (value.length() > 0) {82sb.append(LanguageTag.SEP).append(value);83}84}85setValue(sb.substring(1)); // skip leading '-'86}87}8889public Set<String> getUnicodeLocaleAttributes() {90if (attributes == Collections.EMPTY_SET) {91return attributes;92}93return Collections.unmodifiableSet(attributes);94}9596public Set<String> getUnicodeLocaleKeys() {97if (keywords == Collections.EMPTY_MAP) {98return Collections.emptySet();99}100return Collections.unmodifiableSet(keywords.keySet());101}102103public String getUnicodeLocaleType(String unicodeLocaleKey) {104return keywords.get(unicodeLocaleKey);105}106107public static boolean isSingletonChar(char c) {108return (SINGLETON == LocaleUtils.toLower(c));109}110111public static boolean isAttribute(String s) {112// 3*8alphanum113int len = s.length();114return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);115}116117public static boolean isKey(String s) {118// 2alphanum119return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);120}121122public static boolean isTypeSubtag(String s) {123// 3*8alphanum124int len = s.length();125return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);126}127}128129130