Path: blob/master/src/java.base/share/classes/sun/util/locale/LocaleUtils.java
41159 views
/*1* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425/*26*******************************************************************************27* Copyright (C) 2009, International Business Machines Corporation and *28* others. All Rights Reserved. *29*******************************************************************************30*/31package sun.util.locale;3233import java.util.List;34import java.util.Map;35import java.util.Set;3637/**38* Collection of static utility methods for Locale support. The39* methods which manipulate characters or strings support ASCII only.40*/41public final class LocaleUtils {4243private LocaleUtils() {44}4546/**47* Compares two ASCII Strings s1 and s2, ignoring case.48*/49public static boolean caseIgnoreMatch(String s1, String s2) {50if (s1 == s2) {51return true;52}5354int len = s1.length();55if (len != s2.length()) {56return false;57}5859for (int i = 0; i < len; i++) {60char c1 = s1.charAt(i);61char c2 = s2.charAt(i);62if (c1 != c2 && toLower(c1) != toLower(c2)) {63return false;64}65}66return true;67}6869static int caseIgnoreCompare(String s1, String s2) {70if (s1 == s2) {71return 0;72}73return toLowerString(s1).compareTo(toLowerString(s2));74}7576static char toUpper(char c) {77return isLower(c) ? (char)(c - 0x20) : c;78}7980static char toLower(char c) {81return isUpper(c) ? (char)(c + 0x20) : c;82}8384/**85* Converts the given ASCII String to lower-case.86*/87public static String toLowerString(String s) {88int len = s.length();89int idx = 0;90for (; idx < len; idx++) {91if (isUpper(s.charAt(idx))) {92break;93}94}95if (idx == len) {96return s;97}9899char[] buf = new char[len];100for (int i = 0; i < len; i++) {101char c = s.charAt(i);102buf[i] = (i < idx) ? c : toLower(c);103}104return new String(buf);105}106107static String toUpperString(String s) {108int len = s.length();109int idx = 0;110for (; idx < len; idx++) {111if (isLower(s.charAt(idx))) {112break;113}114}115if (idx == len) {116return s;117}118119char[] buf = new char[len];120for (int i = 0; i < len; i++) {121char c = s.charAt(i);122buf[i] = (i < idx) ? c : toUpper(c);123}124return new String(buf);125}126127static String toTitleString(String s) {128int len;129if ((len = s.length()) == 0) {130return s;131}132int idx = 0;133if (!isLower(s.charAt(idx))) {134for (idx = 1; idx < len; idx++) {135if (isUpper(s.charAt(idx))) {136break;137}138}139}140if (idx == len) {141return s;142}143144char[] buf = new char[len];145for (int i = 0; i < len; i++) {146char c = s.charAt(i);147if (i == 0 && idx == 0) {148buf[i] = toUpper(c);149} else if (i < idx) {150buf[i] = c;151} else {152buf[i] = toLower(c);153}154}155return new String(buf);156}157158private static boolean isUpper(char c) {159return c >= 'A' && c <= 'Z';160}161162private static boolean isLower(char c) {163return c >= 'a' && c <= 'z';164}165166static boolean isAlpha(char c) {167return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');168}169170static boolean isAlphaString(String s) {171int len = s.length();172for (int i = 0; i < len; i++) {173if (!isAlpha(s.charAt(i))) {174return false;175}176}177return true;178}179180static boolean isNumeric(char c) {181return (c >= '0' && c <= '9');182}183184static boolean isNumericString(String s) {185int len = s.length();186for (int i = 0; i < len; i++) {187if (!isNumeric(s.charAt(i))) {188return false;189}190}191return true;192}193194static boolean isAlphaNumeric(char c) {195return isAlpha(c) || isNumeric(c);196}197198public static boolean isAlphaNumericString(String s) {199int len = s.length();200for (int i = 0; i < len; i++) {201if (!isAlphaNumeric(s.charAt(i))) {202return false;203}204}205return true;206}207208public static boolean isEmpty(String str) {209return str == null || str.isEmpty();210}211212public static boolean isEmpty(Set<?> set) {213return set == null || set.isEmpty();214}215216public static boolean isEmpty(Map<?, ?> map) {217return map == null || map.isEmpty();218}219220public static boolean isEmpty(List<?> list) {221return list == null || list.isEmpty();222}223}224225226