Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: libadblockplus-android/src/org/apache/commons/lang/CharUtils.java

Issue 29348972: Issue 4239 - Change indentation to reflect our coding style (Closed)
Patch Set: Created Aug. 2, 2016, 10:43 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Simplified by Andrey Novikov for AdBlock Plus 2 * Simplified by Andrey Novikov for AdBlock Plus
3 */ 3 */
4 4
5 /* 5 /*
6 * Licensed to the Apache Software Foundation (ASF) under one or more 6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with 7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership. 8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0 9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with 10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at 11 * the License. You may obtain a copy of the License at
12 * 12 *
13 * http://www.apache.org/licenses/LICENSE-2.0 13 * http://www.apache.org/licenses/LICENSE-2.0
14 * 14 *
15 * Unless required by applicable law or agreed to in writing, software 15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, 16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and 18 * See the License for the specific language governing permissions and
19 * limitations under the License. 19 * limitations under the License.
20 */ 20 */
21 package org.apache.commons.lang; 21 package org.apache.commons.lang;
22 22
23 /** 23 /**
24 * <p>Operations on char primitives and Character objects.</p> 24 * <p>Operations on char primitives and Character objects.</p>
25 * 25 *
26 * <p>This class tries to handle <code>null</code> input gracefully. 26 * <p>This class tries to handle <code>null</code> input gracefully.
27 * An exception will not be thrown for a <code>null</code> input. 27 * An exception will not be thrown for a <code>null</code> input.
28 * Each method documents its behaviour in more detail.</p> 28 * Each method documents its behaviour in more detail.</p>
29 * 29 *
30 * @author Stephen Colebourne 30 * @author Stephen Colebourne
31 * @since 2.1 31 * @since 2.1
32 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $ 32 * @version $Id: CharUtils.java 437554 2006-08-28 06:21:41Z bayard $
33 */ 33 */
34 public class CharUtils { 34 public class CharUtils
anton 2016/08/02 10:45:35 applied indentation to Apache classes too though t
35 35 {
36 private static final String CHAR_STRING = 36 private static final String CHAR_STRING =
37 "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007" + 37 "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007" +
38 "\b\t\n\u000b\f\r\u000e\u000f" + 38 "\b\t\n\u000b\f\r\u000e\u000f" +
39 "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017" + 39 "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017" +
40 "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f" + 40 "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f" +
41 "\u0020\u0021\"\u0023\u0024\u0025\u0026\u0027" + 41 "\u0020\u0021\"\u0023\u0024\u0025\u0026\u0027" +
42 "\u0028\u0029\u002a\u002b\u002c\u002d\u002e\u002f" + 42 "\u0028\u0029\u002a\u002b\u002c\u002d\u002e\u002f" +
43 "\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037" + 43 "\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037" +
44 "\u0038\u0039\u003a\u003b\u003c\u003d\u003e\u003f" + 44 "\u0038\u0039\u003a\u003b\u003c\u003d\u003e\u003f" +
45 "\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047" + 45 "\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047" +
46 "\u0048\u0049\u004a\u004b\u004c\u004d\u004e\u004f" + 46 "\u0048\u0049\u004a\u004b\u004c\u004d\u004e\u004f" +
47 "\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057" + 47 "\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057" +
48 "\u0058\u0059\u005a\u005b\\\u005d\u005e\u005f" + 48 "\u0058\u0059\u005a\u005b\\\u005d\u005e\u005f" +
49 "\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067" + 49 "\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067" +
50 "\u0068\u0069\u006a\u006b\u006c\u006d\u006e\u006f" + 50 "\u0068\u0069\u006a\u006b\u006c\u006d\u006e\u006f" +
51 "\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077" + 51 "\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077" +
52 "\u0078\u0079\u007a\u007b\u007c\u007d\u007e\u007f"; 52 "\u0078\u0079\u007a\u007b\u007c\u007d\u007e\u007f";
53 53
54 private static final String[] CHAR_STRING_ARRAY = new String[128]; 54 private static final String[] CHAR_STRING_ARRAY = new String[128];
55 private static final Character[] CHAR_ARRAY = new Character[128]; 55 private static final Character[] CHAR_ARRAY = new Character[128];
56 56
57 /** 57 /**
58 * <code>\u000a</code> linefeed LF ('\n'). 58 * <code>\u000a</code> linefeed LF ('\n').
59 * 59 *
60 * @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexic al.html#101089">JLF: Escape Sequences 60 * @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical .html#101089">JLF: Escape Sequences
61 * for Character and String Literals</a> 61 * for Character and String Literals</a>
62 * @since 2.2 62 * @since 2.2
63 */ 63 */
64 public static final char LF = '\n'; 64 public static final char LF = '\n';
65 65
66 /** 66 /**
67 * <code>\u000d</code> carriage return CR ('\r'). 67 * <code>\u000d</code> carriage return CR ('\r').
68 * 68 *
69 * @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexic al.html#101089">JLF: Escape Sequences 69 * @see <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical .html#101089">JLF: Escape Sequences
70 * for Character and String Literals</a> 70 * for Character and String Literals</a>
71 * @since 2.2 71 * @since 2.2
72 */ 72 */
73 public static final char CR = '\r'; 73 public static final char CR = '\r';
74 74
75 75 static
76 static { 76 {
77 for (int i = 127; i >= 0; i--) { 77 for (int i = 127; i >= 0; i--)
78 CHAR_STRING_ARRAY[i] = CHAR_STRING.substring(i, i + 1); 78 {
79 CHAR_ARRAY[i] = new Character((char) i); 79 CHAR_STRING_ARRAY[i] = CHAR_STRING.substring(i, i + 1);
80 } 80 CHAR_ARRAY[i] = new Character((char) i);
81 } 81 }
82 82 }
83 /** 83
84 * <p><code>CharUtils</code> instances should NOT be constructed in standard programming. 84 /**
85 * Instead, the class should be used as <code>CharUtils.toString('c');</code >.</p> 85 * <p><code>CharUtils</code> instances should NOT be constructed in standard p rogramming.
86 * 86 * Instead, the class should be used as <code>CharUtils.toString('c');</code>. </p>
87 * <p>This constructor is public to permit tools that require a JavaBean ins tance 87 *
88 * to operate.</p> 88 * <p>This constructor is public to permit tools that require a JavaBean insta nce
89 */ 89 * to operate.</p>
90 public CharUtils() { 90 */
91 super(); 91 public CharUtils()
92 } 92 {
93 93 super();
94 //----------------------------------------------------------------------- 94 }
95 /** 95
96 * <p>Converts the character to a Character.</p> 96 //-----------------------------------------------------------------------
97 * 97
98 * <p>For ASCII 7 bit characters, this uses a cache that will return the 98 /**
99 * same Character object each time.</p> 99 * <p>Converts the character to a Character.</p>
100 * 100 *
101 * <pre> 101 * <p>For ASCII 7 bit characters, this uses a cache that will return the
102 * CharUtils.toCharacterObject(' ') = ' ' 102 * same Character object each time.</p>
103 * CharUtils.toCharacterObject('A') = 'A' 103 *
104 * </pre> 104 * <pre>
105 * 105 * CharUtils.toCharacterObject(' ') = ' '
106 * @param ch the character to convert 106 * CharUtils.toCharacterObject('A') = 'A'
107 * @return a Character of the specified character 107 * </pre>
108 */ 108 *
109 public static Character toCharacterObject(char ch) { 109 * @param ch the character to convert
110 if (ch < CHAR_ARRAY.length) { 110 * @return a Character of the specified character
111 return CHAR_ARRAY[ch]; 111 */
112 } 112 public static Character toCharacterObject(char ch)
113 return new Character(ch); 113 {
114 } 114 if (ch < CHAR_ARRAY.length)
115 115 {
116 /** 116 return CHAR_ARRAY[ch];
117 * <p>Converts the String to a Character using the first character, returnin g 117 }
118 * null for empty Strings.</p> 118 return new Character(ch);
119 * 119 }
120 * <p>For ASCII 7 bit characters, this uses a cache that will return the 120
121 * same Character object each time.</p> 121 /**
122 * 122 * <p>Converts the String to a Character using the first character, returning
123 * <pre> 123 * null for empty Strings.</p>
124 * CharUtils.toCharacterObject(null) = null 124 *
125 * CharUtils.toCharacterObject("") = null 125 * <p>For ASCII 7 bit characters, this uses a cache that will return the
126 * CharUtils.toCharacterObject("A") = 'A' 126 * same Character object each time.</p>
127 * CharUtils.toCharacterObject("BA") = 'B' 127 *
128 * </pre> 128 * <pre>
129 * 129 * CharUtils.toCharacterObject(null) = null
130 * @param str the character to convert 130 * CharUtils.toCharacterObject("") = null
131 * @return the Character value of the first letter of the String 131 * CharUtils.toCharacterObject("A") = 'A'
132 */ 132 * CharUtils.toCharacterObject("BA") = 'B'
133 public static Character toCharacterObject(String str) { 133 * </pre>
134 if (StringUtils.isEmpty(str)) { 134 *
135 return null; 135 * @param str the character to convert
136 } 136 * @return the Character value of the first letter of the String
137 return toCharacterObject(str.charAt(0)); 137 */
138 } 138 public static Character toCharacterObject(String str)
139 139 {
140 //----------------------------------------------------------------------- 140 if (StringUtils.isEmpty(str))
141 /** 141 {
142 * <p>Converts the Character to a char throwing an exception for <code>null< /code>.</p> 142 return null;
143 * 143 }
144 * <pre> 144 return toCharacterObject(str.charAt(0));
145 * CharUtils.toChar(null) = IllegalArgumentException 145 }
146 * CharUtils.toChar(' ') = ' ' 146
147 * CharUtils.toChar('A') = 'A' 147 //-----------------------------------------------------------------------
148 * </pre> 148
149 * 149 /**
150 * @param ch the character to convert 150 * <p>Converts the Character to a char throwing an exception for <code>null</c ode>.</p>
151 * @return the char value of the Character 151 *
152 * @throws IllegalArgumentException if the Character is null 152 * <pre>
153 */ 153 * CharUtils.toChar(null) = IllegalArgumentException
154 public static char toChar(Character ch) { 154 * CharUtils.toChar(' ') = ' '
155 if (ch == null) { 155 * CharUtils.toChar('A') = 'A'
156 throw new IllegalArgumentException("The Character must not be null") ; 156 * </pre>
157 } 157 *
158 return ch.charValue(); 158 * @param ch the character to convert
159 } 159 * @return the char value of the Character
160 160 * @throws IllegalArgumentException if the Character is null
161 /** 161 */
162 * <p>Converts the Character to a char handling <code>null</code>.</p> 162 public static char toChar(Character ch)
163 * 163 {
164 * <pre> 164 if (ch == null)
165 * CharUtils.toChar(null, 'X') = 'X' 165 {
166 * CharUtils.toChar(' ', 'X') = ' ' 166 throw new IllegalArgumentException("The Character must not be null");
167 * CharUtils.toChar('A', 'X') = 'A' 167 }
168 * </pre> 168 return ch.charValue();
169 * 169 }
170 * @param ch the character to convert 170
171 * @param defaultValue the value to use if the Character is null 171 /**
172 * @return the char value of the Character or the default if null 172 * <p>Converts the Character to a char handling <code>null</code>.</p>
173 */ 173 *
174 public static char toChar(Character ch, char defaultValue) { 174 * <pre>
175 if (ch == null) { 175 * CharUtils.toChar(null, 'X') = 'X'
176 return defaultValue; 176 * CharUtils.toChar(' ', 'X') = ' '
177 } 177 * CharUtils.toChar('A', 'X') = 'A'
178 return ch.charValue(); 178 * </pre>
179 } 179 *
180 180 * @param ch the character to convert
181 //----------------------------------------------------------------------- 181 * @param defaultValue the value to use if the Character is null
182 /** 182 * @return the char value of the Character or the default if null
183 * <p>Converts the String to a char using the first character, throwing 183 */
184 * an exception on empty Strings.</p> 184 public static char toChar(Character ch, char defaultValue)
185 * 185 {
186 * <pre> 186 if (ch == null)
187 * CharUtils.toChar(null) = IllegalArgumentException 187 {
188 * CharUtils.toChar("") = IllegalArgumentException 188 return defaultValue;
189 * CharUtils.toChar("A") = 'A' 189 }
190 * CharUtils.toChar("BA") = 'B' 190 return ch.charValue();
191 * </pre> 191 }
192 * 192
193 * @param str the character to convert 193 //-----------------------------------------------------------------------
194 * @return the char value of the first letter of the String 194
195 * @throws IllegalArgumentException if the String is empty 195 /**
196 */ 196 * <p>Converts the String to a char using the first character, throwing
197 public static char toChar(String str) { 197 * an exception on empty Strings.</p>
198 if (StringUtils.isEmpty(str)) { 198 *
199 throw new IllegalArgumentException("The String must not be empty"); 199 * <pre>
200 } 200 * CharUtils.toChar(null) = IllegalArgumentException
201 return str.charAt(0); 201 * CharUtils.toChar("") = IllegalArgumentException
202 } 202 * CharUtils.toChar("A") = 'A'
203 203 * CharUtils.toChar("BA") = 'B'
204 /** 204 * </pre>
205 * <p>Converts the String to a char using the first character, defaulting 205 *
206 * the value on empty Strings.</p> 206 * @param str the character to convert
207 * 207 * @return the char value of the first letter of the String
208 * <pre> 208 * @throws IllegalArgumentException if the String is empty
209 * CharUtils.toChar(null, 'X') = 'X' 209 */
210 * CharUtils.toChar("", 'X') = 'X' 210 public static char toChar(String str)
211 * CharUtils.toChar("A", 'X') = 'A' 211 {
212 * CharUtils.toChar("BA", 'X') = 'B' 212 if (StringUtils.isEmpty(str))
213 * </pre> 213 {
214 * 214 throw new IllegalArgumentException("The String must not be empty");
215 * @param str the character to convert 215 }
216 * @param defaultValue the value to use if the Character is null 216 return str.charAt(0);
217 * @return the char value of the first letter of the String or the default i f null 217 }
218 */ 218
219 public static char toChar(String str, char defaultValue) { 219 /**
220 if (StringUtils.isEmpty(str)) { 220 * <p>Converts the String to a char using the first character, defaulting
221 return defaultValue; 221 * the value on empty Strings.</p>
222 } 222 *
223 return str.charAt(0); 223 * <pre>
224 } 224 * CharUtils.toChar(null, 'X') = 'X'
225 225 * CharUtils.toChar("", 'X') = 'X'
226 //----------------------------------------------------------------------- 226 * CharUtils.toChar("A", 'X') = 'A'
227 /** 227 * CharUtils.toChar("BA", 'X') = 'B'
228 * <p>Converts the character to the Integer it represents, throwing an 228 * </pre>
229 * exception if the character is not numeric.</p> 229 *
230 * 230 * @param str the character to convert
231 * <p>This method coverts the char '1' to the int 1 and so on.</p> 231 * @param defaultValue the value to use if the Character is null
232 * 232 * @return the char value of the first letter of the String or the default if null
233 * <pre> 233 */
234 * CharUtils.toIntValue('3') = 3 234 public static char toChar(String str, char defaultValue)
235 * CharUtils.toIntValue('A') = IllegalArgumentException 235 {
236 * </pre> 236 if (StringUtils.isEmpty(str))
237 * 237 {
238 * @param ch the character to convert 238 return defaultValue;
239 * @return the int value of the character 239 }
240 * @throws IllegalArgumentException if the character is not ASCII numeric 240 return str.charAt(0);
241 */ 241 }
242 public static int toIntValue(char ch) { 242
243 if (isAsciiNumeric(ch) == false) { 243 //-----------------------------------------------------------------------
244 throw new IllegalArgumentException("The character " + ch + " is not in the range '0' - '9'"); 244
245 } 245 /**
246 return ch - 48; 246 * <p>Converts the character to the Integer it represents, throwing an
247 } 247 * exception if the character is not numeric.</p>
248 248 *
249 /** 249 * <p>This method coverts the char '1' to the int 1 and so on.</p>
250 * <p>Converts the character to the Integer it represents, throwing an 250 *
251 * exception if the character is not numeric.</p> 251 * <pre>
252 * 252 * CharUtils.toIntValue('3') = 3
253 * <p>This method coverts the char '1' to the int 1 and so on.</p> 253 * CharUtils.toIntValue('A') = IllegalArgumentException
254 * 254 * </pre>
255 * <pre> 255 *
256 * CharUtils.toIntValue('3', -1) = 3 256 * @param ch the character to convert
257 * CharUtils.toIntValue('A', -1) = -1 257 * @return the int value of the character
258 * </pre> 258 * @throws IllegalArgumentException if the character is not ASCII numeric
259 * 259 */
260 * @param ch the character to convert 260 public static int toIntValue(char ch)
261 * @param defaultValue the default value to use if the character is not num eric 261 {
262 * @return the int value of the character 262 if (isAsciiNumeric(ch) == false)
263 */ 263 {
264 public static int toIntValue(char ch, int defaultValue) { 264 throw new IllegalArgumentException("The character " + ch + " is not in the range '0' - '9'");
265 if (isAsciiNumeric(ch) == false) { 265 }
266 return defaultValue; 266 return ch - 48;
267 } 267 }
268 return ch - 48; 268
269 } 269 /**
270 270 * <p>Converts the character to the Integer it represents, throwing an
271 /** 271 * exception if the character is not numeric.</p>
272 * <p>Converts the character to the Integer it represents, throwing an 272 *
273 * exception if the character is not numeric.</p> 273 * <p>This method coverts the char '1' to the int 1 and so on.</p>
274 * 274 *
275 * <p>This method coverts the char '1' to the int 1 and so on.</p> 275 * <pre>
276 * 276 * CharUtils.toIntValue('3', -1) = 3
277 * <pre> 277 * CharUtils.toIntValue('A', -1) = -1
278 * CharUtils.toIntValue(null) = IllegalArgumentException 278 * </pre>
279 * CharUtils.toIntValue('3') = 3 279 *
280 * CharUtils.toIntValue('A') = IllegalArgumentException 280 * @param ch the character to convert
281 * </pre> 281 * @param defaultValue the default value to use if the character is not numeri c
282 * 282 * @return the int value of the character
283 * @param ch the character to convert, not null 283 */
284 * @return the int value of the character 284 public static int toIntValue(char ch, int defaultValue)
285 * @throws IllegalArgumentException if the Character is not ASCII numeric or is null 285 {
286 */ 286 if (isAsciiNumeric(ch) == false)
287 public static int toIntValue(Character ch) { 287 {
288 if (ch == null) { 288 return defaultValue;
289 throw new IllegalArgumentException("The character must not be null") ; 289 }
290 } 290 return ch - 48;
291 return toIntValue(ch.charValue()); 291 }
292 } 292
293 293 /**
294 /** 294 * <p>Converts the character to the Integer it represents, throwing an
295 * <p>Converts the character to the Integer it represents, throwing an 295 * exception if the character is not numeric.</p>
296 * exception if the character is not numeric.</p> 296 *
297 * 297 * <p>This method coverts the char '1' to the int 1 and so on.</p>
298 * <p>This method coverts the char '1' to the int 1 and so on.</p> 298 *
299 * 299 * <pre>
300 * <pre> 300 * CharUtils.toIntValue(null) = IllegalArgumentException
301 * CharUtils.toIntValue(null, -1) = -1 301 * CharUtils.toIntValue('3') = 3
302 * CharUtils.toIntValue('3', -1) = 3 302 * CharUtils.toIntValue('A') = IllegalArgumentException
303 * CharUtils.toIntValue('A', -1) = -1 303 * </pre>
304 * </pre> 304 *
305 * 305 * @param ch the character to convert, not null
306 * @param ch the character to convert 306 * @return the int value of the character
307 * @param defaultValue the default value to use if the character is not num eric 307 * @throws IllegalArgumentException if the Character is not ASCII numeric or i s null
308 * @return the int value of the character 308 */
309 */ 309 public static int toIntValue(Character ch)
310 public static int toIntValue(Character ch, int defaultValue) { 310 {
311 if (ch == null) { 311 if (ch == null)
312 return defaultValue; 312 {
313 } 313 throw new IllegalArgumentException("The character must not be null");
314 return toIntValue(ch.charValue(), defaultValue); 314 }
315 } 315 return toIntValue(ch.charValue());
316 316 }
317 //----------------------------------------------------------------------- 317
318 /** 318 /**
319 * <p>Converts the character to a String that contains the one character.</p > 319 * <p>Converts the character to the Integer it represents, throwing an
320 * 320 * exception if the character is not numeric.</p>
321 * <p>For ASCII 7 bit characters, this uses a cache that will return the 321 *
322 * same String object each time.</p> 322 * <p>This method coverts the char '1' to the int 1 and so on.</p>
323 * 323 *
324 * <pre> 324 * <pre>
325 * CharUtils.toString(' ') = " " 325 * CharUtils.toIntValue(null, -1) = -1
326 * CharUtils.toString('A') = "A" 326 * CharUtils.toIntValue('3', -1) = 3
327 * </pre> 327 * CharUtils.toIntValue('A', -1) = -1
328 * 328 * </pre>
329 * @param ch the character to convert 329 *
330 * @return a String containing the one specified character 330 * @param ch the character to convert
331 */ 331 * @param defaultValue the default value to use if the character is not numeri c
332 public static String toString(char ch) { 332 * @return the int value of the character
333 if (ch < 128) { 333 */
334 return CHAR_STRING_ARRAY[ch]; 334 public static int toIntValue(Character ch, int defaultValue)
335 } 335 {
336 return new String(new char[] {ch}); 336 if (ch == null)
337 } 337 {
338 338 return defaultValue;
339 /** 339 }
340 * <p>Converts the character to a String that contains the one character.</p > 340 return toIntValue(ch.charValue(), defaultValue);
341 * 341 }
342 * <p>For ASCII 7 bit characters, this uses a cache that will return the 342
343 * same String object each time.</p> 343 //-----------------------------------------------------------------------
344 * 344
345 * <p>If <code>null</code> is passed in, <code>null</code> will be returned. </p> 345 /**
346 * 346 * <p>Converts the character to a String that contains the one character.</p>
347 * <pre> 347 *
348 * CharUtils.toString(null) = null 348 * <p>For ASCII 7 bit characters, this uses a cache that will return the
349 * CharUtils.toString(' ') = " " 349 * same String object each time.</p>
350 * CharUtils.toString('A') = "A" 350 *
351 * </pre> 351 * <pre>
352 * 352 * CharUtils.toString(' ') = " "
353 * @param ch the character to convert 353 * CharUtils.toString('A') = "A"
354 * @return a String containing the one specified character 354 * </pre>
355 */ 355 *
356 public static String toString(Character ch) { 356 * @param ch the character to convert
357 if (ch == null) { 357 * @return a String containing the one specified character
358 return null; 358 */
359 } 359 public static String toString(char ch)
360 return toString(ch.charValue()); 360 {
361 } 361 if (ch < 128)
362 362 {
363 //-------------------------------------------------------------------------- 363 return CHAR_STRING_ARRAY[ch];
364 /** 364 }
365 * <p>Converts the string to the unicode format '\u0020'.</p> 365 return new String(new char[]{ch});
366 * 366 }
367 * <p>This format is the Java source code format.</p> 367
368 * 368 /**
369 * <pre> 369 * <p>Converts the character to a String that contains the one character.</p>
370 * CharUtils.unicodeEscaped(' ') = "\u0020" 370 *
371 * CharUtils.unicodeEscaped('A') = "\u0041" 371 * <p>For ASCII 7 bit characters, this uses a cache that will return the
372 * </pre> 372 * same String object each time.</p>
373 * 373 *
374 * @param ch the character to convert 374 * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</ p>
375 * @return the escaped unicode string 375 *
376 */ 376 * <pre>
377 public static String unicodeEscaped(char ch) { 377 * CharUtils.toString(null) = null
378 if (ch < 0x10) { 378 * CharUtils.toString(' ') = " "
379 return "\\u000" + Integer.toHexString(ch); 379 * CharUtils.toString('A') = "A"
380 } else if (ch < 0x100) { 380 * </pre>
381 return "\\u00" + Integer.toHexString(ch); 381 *
382 } else if (ch < 0x1000) { 382 * @param ch the character to convert
383 return "\\u0" + Integer.toHexString(ch); 383 * @return a String containing the one specified character
384 } 384 */
385 return "\\u" + Integer.toHexString(ch); 385 public static String toString(Character ch)
386 } 386 {
387 387 if (ch == null)
388 /** 388 {
389 * <p>Converts the string to the unicode format '\u0020'.</p> 389 return null;
390 * 390 }
391 * <p>This format is the Java source code format.</p> 391 return toString(ch.charValue());
392 * 392 }
393 * <p>If <code>null</code> is passed in, <code>null</code> will be returned. </p> 393
394 * 394 //--------------------------------------------------------------------------
395 * <pre> 395
396 * CharUtils.unicodeEscaped(null) = null 396 /**
397 * CharUtils.unicodeEscaped(' ') = "\u0020" 397 * <p>Converts the string to the unicode format '\u0020'.</p>
398 * CharUtils.unicodeEscaped('A') = "\u0041" 398 *
399 * </pre> 399 * <p>This format is the Java source code format.</p>
400 * 400 *
401 * @param ch the character to convert, may be null 401 * <pre>
402 * @return the escaped unicode string, null if null input 402 * CharUtils.unicodeEscaped(' ') = "\u0020"
403 */ 403 * CharUtils.unicodeEscaped('A') = "\u0041"
404 public static String unicodeEscaped(Character ch) { 404 * </pre>
405 if (ch == null) { 405 *
406 return null; 406 * @param ch the character to convert
407 } 407 * @return the escaped unicode string
408 return unicodeEscaped(ch.charValue()); 408 */
409 } 409 public static String unicodeEscaped(char ch)
410 410 {
411 //-------------------------------------------------------------------------- 411 if (ch < 0x10)
412 /** 412 {
413 * <p>Checks whether the character is ASCII 7 bit.</p> 413 return "\\u000" + Integer.toHexString(ch);
414 * 414 } else if (ch < 0x100)
415 * <pre> 415 {
416 * CharUtils.isAscii('a') = true 416 return "\\u00" + Integer.toHexString(ch);
417 * CharUtils.isAscii('A') = true 417 } else if (ch < 0x1000)
418 * CharUtils.isAscii('3') = true 418 {
419 * CharUtils.isAscii('-') = true 419 return "\\u0" + Integer.toHexString(ch);
420 * CharUtils.isAscii('\n') = true 420 }
421 * CharUtils.isAscii('&copy;') = false 421 return "\\u" + Integer.toHexString(ch);
422 * </pre> 422 }
423 * 423
424 * @param ch the character to check 424 /**
425 * @return true if less than 128 425 * <p>Converts the string to the unicode format '\u0020'.</p>
426 */ 426 *
427 public static boolean isAscii(char ch) { 427 * <p>This format is the Java source code format.</p>
428 return ch < 128; 428 *
429 } 429 * <p>If <code>null</code> is passed in, <code>null</code> will be returned.</ p>
430 430 *
431 /** 431 * <pre>
432 * <p>Checks whether the character is ASCII 7 bit printable.</p> 432 * CharUtils.unicodeEscaped(null) = null
433 * 433 * CharUtils.unicodeEscaped(' ') = "\u0020"
434 * <pre> 434 * CharUtils.unicodeEscaped('A') = "\u0041"
435 * CharUtils.isAsciiPrintable('a') = true 435 * </pre>
436 * CharUtils.isAsciiPrintable('A') = true 436 *
437 * CharUtils.isAsciiPrintable('3') = true 437 * @param ch the character to convert, may be null
438 * CharUtils.isAsciiPrintable('-') = true 438 * @return the escaped unicode string, null if null input
439 * CharUtils.isAsciiPrintable('\n') = false 439 */
440 * CharUtils.isAsciiPrintable('&copy;') = false 440 public static String unicodeEscaped(Character ch)
441 * </pre> 441 {
442 * 442 if (ch == null)
443 * @param ch the character to check 443 {
444 * @return true if between 32 and 126 inclusive 444 return null;
445 */ 445 }
446 public static boolean isAsciiPrintable(char ch) { 446 return unicodeEscaped(ch.charValue());
447 return ch >= 32 && ch < 127; 447 }
448 } 448
449 449 //--------------------------------------------------------------------------
450 /** 450
451 * <p>Checks whether the character is ASCII 7 bit control.</p> 451 /**
452 * 452 * <p>Checks whether the character is ASCII 7 bit.</p>
453 * <pre> 453 *
454 * CharUtils.isAsciiControl('a') = false 454 * <pre>
455 * CharUtils.isAsciiControl('A') = false 455 * CharUtils.isAscii('a') = true
456 * CharUtils.isAsciiControl('3') = false 456 * CharUtils.isAscii('A') = true
457 * CharUtils.isAsciiControl('-') = false 457 * CharUtils.isAscii('3') = true
458 * CharUtils.isAsciiControl('\n') = true 458 * CharUtils.isAscii('-') = true
459 * CharUtils.isAsciiControl('&copy;') = false 459 * CharUtils.isAscii('\n') = true
460 * </pre> 460 * CharUtils.isAscii('&copy;') = false
461 * 461 * </pre>
462 * @param ch the character to check 462 *
463 * @return true if less than 32 or equals 127 463 * @param ch the character to check
464 */ 464 * @return true if less than 128
465 public static boolean isAsciiControl(char ch) { 465 */
466 return ch < 32 || ch == 127; 466 public static boolean isAscii(char ch)
467 } 467 {
468 468 return ch < 128;
469 /** 469 }
470 * <p>Checks whether the character is ASCII 7 bit alphabetic.</p> 470
471 * 471 /**
472 * <pre> 472 * <p>Checks whether the character is ASCII 7 bit printable.</p>
473 * CharUtils.isAsciiAlpha('a') = true 473 * <p/>
474 * CharUtils.isAsciiAlpha('A') = true 474 * <pre>
475 * CharUtils.isAsciiAlpha('3') = false 475 * CharUtils.isAsciiPrintable('a') = true
476 * CharUtils.isAsciiAlpha('-') = false 476 * CharUtils.isAsciiPrintable('A') = true
477 * CharUtils.isAsciiAlpha('\n') = false 477 * CharUtils.isAsciiPrintable('3') = true
478 * CharUtils.isAsciiAlpha('&copy;') = false 478 * CharUtils.isAsciiPrintable('-') = true
479 * </pre> 479 * CharUtils.isAsciiPrintable('\n') = false
480 * 480 * CharUtils.isAsciiPrintable('&copy;') = false
481 * @param ch the character to check 481 * </pre>
482 * @return true if between 65 and 90 or 97 and 122 inclusive 482 *
483 */ 483 * @param ch the character to check
484 public static boolean isAsciiAlpha(char ch) { 484 * @return true if between 32 and 126 inclusive
485 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); 485 */
486 } 486 public static boolean isAsciiPrintable(char ch)
487 487 {
488 /** 488 return ch >= 32 && ch < 127;
489 * <p>Checks whether the character is ASCII 7 bit alphabetic upper case.</p> 489 }
490 * 490
491 * <pre> 491 /**
492 * CharUtils.isAsciiAlphaUpper('a') = false 492 * <p>Checks whether the character is ASCII 7 bit control.</p>
493 * CharUtils.isAsciiAlphaUpper('A') = true 493 *
494 * CharUtils.isAsciiAlphaUpper('3') = false 494 * <pre>
495 * CharUtils.isAsciiAlphaUpper('-') = false 495 * CharUtils.isAsciiControl('a') = false
496 * CharUtils.isAsciiAlphaUpper('\n') = false 496 * CharUtils.isAsciiControl('A') = false
497 * CharUtils.isAsciiAlphaUpper('&copy;') = false 497 * CharUtils.isAsciiControl('3') = false
498 * </pre> 498 * CharUtils.isAsciiControl('-') = false
499 * 499 * CharUtils.isAsciiControl('\n') = true
500 * @param ch the character to check 500 * CharUtils.isAsciiControl('&copy;') = false
501 * @return true if between 65 and 90 inclusive 501 * </pre>
502 */ 502 *
503 public static boolean isAsciiAlphaUpper(char ch) { 503 * @param ch the character to check
504 return ch >= 'A' && ch <= 'Z'; 504 * @return true if less than 32 or equals 127
505 } 505 */
506 506 public static boolean isAsciiControl(char ch)
507 /** 507 {
508 * <p>Checks whether the character is ASCII 7 bit alphabetic lower case.</p> 508 return ch < 32 || ch == 127;
509 * 509 }
510 * <pre> 510
511 * CharUtils.isAsciiAlphaLower('a') = true 511 /**
512 * CharUtils.isAsciiAlphaLower('A') = false 512 * <p>Checks whether the character is ASCII 7 bit alphabetic.</p>
513 * CharUtils.isAsciiAlphaLower('3') = false 513 * <p/>
514 * CharUtils.isAsciiAlphaLower('-') = false 514 * <pre>
515 * CharUtils.isAsciiAlphaLower('\n') = false 515 * CharUtils.isAsciiAlpha('a') = true
516 * CharUtils.isAsciiAlphaLower('&copy;') = false 516 * CharUtils.isAsciiAlpha('A') = true
517 * </pre> 517 * CharUtils.isAsciiAlpha('3') = false
518 * 518 * CharUtils.isAsciiAlpha('-') = false
519 * @param ch the character to check 519 * CharUtils.isAsciiAlpha('\n') = false
520 * @return true if between 97 and 122 inclusive 520 * CharUtils.isAsciiAlpha('&copy;') = false
521 */ 521 * </pre>
522 public static boolean isAsciiAlphaLower(char ch) { 522 *
523 return ch >= 'a' && ch <= 'z'; 523 * @param ch the character to check
524 } 524 * @return true if between 65 and 90 or 97 and 122 inclusive
525 525 */
526 /** 526 public static boolean isAsciiAlpha(char ch)
527 * <p>Checks whether the character is ASCII 7 bit numeric.</p> 527 {
528 * 528 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z');
529 * <pre> 529 }
530 * CharUtils.isAsciiNumeric('a') = false 530
531 * CharUtils.isAsciiNumeric('A') = false 531 /**
532 * CharUtils.isAsciiNumeric('3') = true 532 * <p>Checks whether the character is ASCII 7 bit alphabetic upper case.</p>
533 * CharUtils.isAsciiNumeric('-') = false 533 *
534 * CharUtils.isAsciiNumeric('\n') = false 534 * <pre>
535 * CharUtils.isAsciiNumeric('&copy;') = false 535 * CharUtils.isAsciiAlphaUpper('a') = false
536 * </pre> 536 * CharUtils.isAsciiAlphaUpper('A') = true
537 * 537 * CharUtils.isAsciiAlphaUpper('3') = false
538 * @param ch the character to check 538 * CharUtils.isAsciiAlphaUpper('-') = false
539 * @return true if between 48 and 57 inclusive 539 * CharUtils.isAsciiAlphaUpper('\n') = false
540 */ 540 * CharUtils.isAsciiAlphaUpper('&copy;') = false
541 public static boolean isAsciiNumeric(char ch) { 541 * </pre>
542 return ch >= '0' && ch <= '9'; 542 *
543 } 543 * @param ch the character to check
544 544 * @return true if between 65 and 90 inclusive
545 /** 545 */
546 * <p>Checks whether the character is ASCII 7 bit numeric.</p> 546 public static boolean isAsciiAlphaUpper(char ch)
547 * 547 {
548 * <pre> 548 return ch >= 'A' && ch <= 'Z';
549 * CharUtils.isAsciiAlphanumeric('a') = true 549 }
550 * CharUtils.isAsciiAlphanumeric('A') = true 550
551 * CharUtils.isAsciiAlphanumeric('3') = true 551 /**
552 * CharUtils.isAsciiAlphanumeric('-') = false 552 * <p>Checks whether the character is ASCII 7 bit alphabetic lower case.</p>
553 * CharUtils.isAsciiAlphanumeric('\n') = false 553 * <p/>
554 * CharUtils.isAsciiAlphanumeric('&copy;') = false 554 * <pre>
555 * </pre> 555 * CharUtils.isAsciiAlphaLower('a') = true
556 * 556 * CharUtils.isAsciiAlphaLower('A') = false
557 * @param ch the character to check 557 * CharUtils.isAsciiAlphaLower('3') = false
558 * @return true if between 48 and 57 or 65 and 90 or 97 and 122 inclusive 558 * CharUtils.isAsciiAlphaLower('-') = false
559 */ 559 * CharUtils.isAsciiAlphaLower('\n') = false
560 public static boolean isAsciiAlphanumeric(char ch) { 560 * CharUtils.isAsciiAlphaLower('&copy;') = false
561 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0 ' && ch <= '9'); 561 * </pre>
562 } 562 *
563 563 * @param ch the character to check
564 * @return true if between 97 and 122 inclusive
565 */
566 public static boolean isAsciiAlphaLower(char ch)
567 {
568 return ch >= 'a' && ch <= 'z';
569 }
570
571 /**
572 * <p>Checks whether the character is ASCII 7 bit numeric.</p>
573 *
574 * <pre>
575 * CharUtils.isAsciiNumeric('a') = false
576 * CharUtils.isAsciiNumeric('A') = false
577 * CharUtils.isAsciiNumeric('3') = true
578 * CharUtils.isAsciiNumeric('-') = false
579 * CharUtils.isAsciiNumeric('\n') = false
580 * CharUtils.isAsciiNumeric('&copy;') = false
581 * </pre>
582 *
583 * @param ch the character to check
584 * @return true if between 48 and 57 inclusive
585 */
586 public static boolean isAsciiNumeric(char ch)
587 {
588 return ch >= '0' && ch <= '9';
589 }
590
591 /**
592 * <p>Checks whether the character is ASCII 7 bit numeric.</p>
593 *
594 * <pre>
595 * CharUtils.isAsciiAlphanumeric('a') = true
596 * CharUtils.isAsciiAlphanumeric('A') = true
597 * CharUtils.isAsciiAlphanumeric('3') = true
598 * CharUtils.isAsciiAlphanumeric('-') = false
599 * CharUtils.isAsciiAlphanumeric('\n') = false
600 * CharUtils.isAsciiAlphanumeric('&copy;') = false
601 * </pre>
602 *
603 * @param ch the character to check
604 * @return true if between 48 and 57 or 65 and 90 or 97 and 122 inclusive
605 */
606 public static boolean isAsciiAlphanumeric(char ch)
607 {
608 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9');
609 }
610
564 } 611 }
OLDNEW

Powered by Google App Engine
This is Rietveld