author | michaelm |
Thu, 14 Nov 2019 15:01:49 +0000 | |
branch | unixdomainchannels |
changeset 59082 | 5e250ee9259e |
parent 54412 | 41356f083e93 |
permissions | -rw-r--r-- |
6501 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13583
diff
changeset
|
2 |
* Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved. |
6501 | 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 |
||
9224 | 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 |
*/ |
|
6501 | 50 |
public static boolean caseIgnoreMatch(String s1, String s2) { |
51 |
if (s1 == s2) { |
|
52 |
return true; |
|
53 |
} |
|
9224 | 54 |
|
6501 | 55 |
int len = s1.length(); |
56 |
if (len != s2.length()) { |
|
57 |
return false; |
|
58 |
} |
|
9224 | 59 |
|
60 |
for (int i = 0; i < len; i++) { |
|
6501 | 61 |
char c1 = s1.charAt(i); |
62 |
char c2 = s2.charAt(i); |
|
63 |
if (c1 != c2 && toLower(c1) != toLower(c2)) { |
|
9224 | 64 |
return false; |
6501 | 65 |
} |
66 |
} |
|
9224 | 67 |
return true; |
6501 | 68 |
} |
69 |
||
9224 | 70 |
static int caseIgnoreCompare(String s1, String s2) { |
6501 | 71 |
if (s1 == s2) { |
72 |
return 0; |
|
73 |
} |
|
9224 | 74 |
return toLowerString(s1).compareTo(toLowerString(s2)); |
6501 | 75 |
} |
76 |
||
9224 | 77 |
static char toUpper(char c) { |
78 |
return isLower(c) ? (char)(c - 0x20) : c; |
|
6501 | 79 |
} |
80 |
||
9224 | 81 |
static char toLower(char c) { |
82 |
return isUpper(c) ? (char)(c + 0x20) : c; |
|
6501 | 83 |
} |
84 |
||
9224 | 85 |
/** |
86 |
* Converts the given ASCII String to lower-case. |
|
87 |
*/ |
|
6501 | 88 |
public static String toLowerString(String s) { |
9224 | 89 |
int len = s.length(); |
6501 | 90 |
int idx = 0; |
9224 | 91 |
for (; idx < len; idx++) { |
92 |
if (isUpper(s.charAt(idx))) { |
|
6501 | 93 |
break; |
94 |
} |
|
95 |
} |
|
9224 | 96 |
if (idx == len) { |
6501 | 97 |
return s; |
98 |
} |
|
9224 | 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); |
|
6501 | 104 |
} |
9224 | 105 |
return new String(buf); |
6501 | 106 |
} |
107 |
||
9224 | 108 |
static String toUpperString(String s) { |
109 |
int len = s.length(); |
|
6501 | 110 |
int idx = 0; |
9224 | 111 |
for (; idx < len; idx++) { |
112 |
if (isLower(s.charAt(idx))) { |
|
6501 | 113 |
break; |
114 |
} |
|
115 |
} |
|
9224 | 116 |
if (idx == len) { |
6501 | 117 |
return s; |
118 |
} |
|
9224 | 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); |
|
6501 | 124 |
} |
9224 | 125 |
return new String(buf); |
6501 | 126 |
} |
127 |
||
9224 | 128 |
static String toTitleString(String s) { |
129 |
int len; |
|
130 |
if ((len = s.length()) == 0) { |
|
6501 | 131 |
return s; |
132 |
} |
|
133 |
int idx = 0; |
|
9224 | 134 |
if (!isLower(s.charAt(idx))) { |
135 |
for (idx = 1; idx < len; idx++) { |
|
136 |
if (isUpper(s.charAt(idx))) { |
|
6501 | 137 |
break; |
138 |
} |
|
139 |
} |
|
140 |
} |
|
9224 | 141 |
if (idx == len) { |
6501 | 142 |
return s; |
143 |
} |
|
9224 | 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 |
} |
|
6501 | 155 |
} |
9224 | 156 |
return new String(buf); |
6501 | 157 |
} |
158 |
||
9224 | 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) { |
|
6501 | 168 |
return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'); |
169 |
} |
|
170 |
||
9224 | 171 |
static boolean isAlphaString(String s) { |
172 |
int len = s.length(); |
|
173 |
for (int i = 0; i < len; i++) { |
|
6501 | 174 |
if (!isAlpha(s.charAt(i))) { |
9224 | 175 |
return false; |
6501 | 176 |
} |
177 |
} |
|
9224 | 178 |
return true; |
6501 | 179 |
} |
180 |
||
9224 | 181 |
static boolean isNumeric(char c) { |
6501 | 182 |
return (c >= '0' && c <= '9'); |
183 |
} |
|
184 |
||
9224 | 185 |
static boolean isNumericString(String s) { |
186 |
int len = s.length(); |
|
187 |
for (int i = 0; i < len; i++) { |
|
6501 | 188 |
if (!isNumeric(s.charAt(i))) { |
9224 | 189 |
return false; |
6501 | 190 |
} |
191 |
} |
|
9224 | 192 |
return true; |
6501 | 193 |
} |
194 |
||
9224 | 195 |
static boolean isAlphaNumeric(char c) { |
6501 | 196 |
return isAlpha(c) || isNumeric(c); |
197 |
} |
|
198 |
||
13583 | 199 |
public static boolean isAlphaNumericString(String s) { |
9224 | 200 |
int len = s.length(); |
201 |
for (int i = 0; i < len; i++) { |
|
6501 | 202 |
if (!isAlphaNumeric(s.charAt(i))) { |
9224 | 203 |
return false; |
6501 | 204 |
} |
205 |
} |
|
9224 | 206 |
return true; |
6501 | 207 |
} |
208 |
||
54412 | 209 |
public static boolean isEmpty(String str) { |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
47216
diff
changeset
|
210 |
return str == null || str.isEmpty(); |
9224 | 211 |
} |
6501 | 212 |
|
54412 | 213 |
public static boolean isEmpty(Set<?> set) { |
9224 | 214 |
return set == null || set.isEmpty(); |
215 |
} |
|
6501 | 216 |
|
54412 | 217 |
public static boolean isEmpty(Map<?, ?> map) { |
9224 | 218 |
return map == null || map.isEmpty(); |
219 |
} |
|
220 |
||
54412 | 221 |
public static boolean isEmpty(List<?> list) { |
9224 | 222 |
return list == null || list.isEmpty(); |
6501 | 223 |
} |
224 |
} |