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 it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * 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 WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 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 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 /*
  27  *******************************************************************************
  28  * Copyright (C) 2009, International Business Machines Corporation and         *
  29  * others. All Rights Reserved.                                                *
  30  *******************************************************************************
  31  */
  32 package sun.util.locale;
  33 
  34 import java.util.List;
  35 import java.util.Map;
  36 import java.util.Set;
  37 
  38 /**
  39  * Collection of static utility methods for Locale support. The
  40  * methods which manipulate characters or strings support ASCII only.
  41  */
  42 public final class LocaleUtils {
  43 
  44     private LocaleUtils() {
  45     }
  46 
  47     /**
  48      * Compares two ASCII Strings s1 and s2, ignoring case.
  49      */
  50     public static boolean caseIgnoreMatch(String s1, String s2) {
  51         if (s1 == s2) {
  52             return true;
  53         }
  54 
  55         int len = s1.length();
  56         if (len != s2.length()) {
  57             return false;
  58         }
  59 
  60         for (int i = 0; i < len; i++) {
  61             char c1 = s1.charAt(i);
  62             char c2 = s2.charAt(i);
  63             if (c1 != c2 && toLower(c1) != toLower(c2)) {
  64                 return false;
  65             }
  66         }
  67         return true;
  68     }
  69 
  70     static int caseIgnoreCompare(String s1, String s2) {
  71         if (s1 == s2) {
  72             return 0;
  73         }
  74         return toLowerString(s1).compareTo(toLowerString(s2));
  75     }
  76 
  77     static char toUpper(char c) {
  78         return isLower(c) ? (char)(c - 0x20) : c;
  79     }
  80 
  81     static char toLower(char c) {
  82         return isUpper(c) ? (char)(c + 0x20) : c;
  83     }
  84 
  85     /**
  86      * Converts the given ASCII String to lower-case.
  87      */
  88     public static String toLowerString(String s) {
  89         int len = s.length();
  90         int idx = 0;
  91         for (; idx < len; idx++) {
  92             if (isUpper(s.charAt(idx))) {
  93                 break;
  94             }
  95         }
  96         if (idx == len) {
  97             return s;
  98         }
  99 
 100         char[] buf = new char[len];
 101         for (int i = 0; i < len; i++) {
 102             char c = s.charAt(i);
 103             buf[i] = (i < idx) ? c : toLower(c);
 104         }
 105         return new String(buf);
 106     }
 107 
 108     static String toUpperString(String s) {
 109         int len = s.length();
 110         int idx = 0;
 111         for (; idx < len; idx++) {
 112             if (isLower(s.charAt(idx))) {
 113                 break;
 114             }
 115         }
 116         if (idx == len) {
 117             return s;
 118         }
 119 
 120         char[] buf = new char[len];
 121         for (int i = 0; i < len; i++) {
 122             char c = s.charAt(i);
 123             buf[i] = (i < idx) ? c : toUpper(c);
 124         }
 125         return new String(buf);
 126     }
 127 
 128     static String toTitleString(String s) {
 129         int len;
 130         if ((len = s.length()) == 0) {
 131             return s;
 132         }
 133         int idx = 0;
 134         if (!isLower(s.charAt(idx))) {
 135             for (idx = 1; idx < len; idx++) {
 136                 if (isUpper(s.charAt(idx))) {
 137                     break;
 138                 }
 139             }
 140         }
 141         if (idx == len) {
 142             return s;
 143         }
 144 
 145         char[] buf = new char[len];
 146         for (int i = 0; i < len; i++) {
 147             char c = s.charAt(i);
 148             if (i == 0 && idx == 0) {
 149                 buf[i] = toUpper(c);
 150             } else if (i < idx) {
 151                 buf[i] = c;
 152             } else {
 153                 buf[i] = toLower(c);
 154             }
 155         }
 156         return new String(buf);
 157     }
 158 
 159     private static boolean isUpper(char c) {
 160         return c >= 'A' && c <= 'Z';
 161     }
 162 
 163     private static boolean isLower(char c) {
 164         return c >= 'a' && c <= 'z';
 165     }
 166 
 167     static boolean isAlpha(char c) {
 168         return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
 169     }
 170 
 171     static boolean isAlphaString(String s) {
 172         int len = s.length();
 173         for (int i = 0; i < len; i++) {
 174             if (!isAlpha(s.charAt(i))) {
 175                 return false;
 176             }
 177         }
 178         return true;
 179     }
 180 
 181     static boolean isNumeric(char c) {
 182         return (c >= '0' && c <= '9');
 183     }
 184 
 185     static boolean isNumericString(String s) {
 186         int len = s.length();
 187         for (int i = 0; i < len; i++) {
 188             if (!isNumeric(s.charAt(i))) {
 189                 return false;
 190             }
 191         }
 192         return true;
 193     }
 194 
 195     static boolean isAlphaNumeric(char c) {
 196         return isAlpha(c) || isNumeric(c);
 197     }
 198 
 199     public static boolean isAlphaNumericString(String s) {
 200         int len = s.length();
 201         for (int i = 0; i < len; i++) {
 202             if (!isAlphaNumeric(s.charAt(i))) {
 203                 return false;
 204             }
 205         }
 206         return true;
 207     }
 208 
 209     static boolean isEmpty(String str) {
 210         return str == null || str.length() == 0;
 211     }
 212 
 213     static boolean isEmpty(Set<?> set) {
 214         return set == null || set.isEmpty();
 215     }
 216 
 217     static boolean isEmpty(Map<?, ?> map) {
 218         return map == null || map.isEmpty();
 219     }
 220 
 221     static boolean isEmpty(List<?> list) {
 222         return list == null || list.isEmpty();
 223     }
 224 }