1 16 package org.apache.commons.lang; 17 18 29 public class CharUtils { 30 31 private static final String CHAR_STRING = 32 "\u0000\u0001\u0002\u0003\u0004\u0005\u0006\u0007" + 33 "\b\t\n\u000b\f\r\u000e\u000f" + 34 "\u0010\u0011\u0012\u0013\u0014\u0015\u0016\u0017" + 35 "\u0018\u0019\u001a\u001b\u001c\u001d\u001e\u001f" + 36 "\u0020\u0021\"\u0023\u0024\u0025\u0026\u0027" + 37 "\u0028\u0029\u002a\u002b\u002c\u002d\u002e\u002f" + 38 "\u0030\u0031\u0032\u0033\u0034\u0035\u0036\u0037" + 39 "\u0038\u0039\u003a\u003b\u003c\u003d\u003e\u003f" + 40 "\u0040\u0041\u0042\u0043\u0044\u0045\u0046\u0047" + 41 "\u0048\u0049\u004a\u004b\u004c\u004d\u004e\u004f" + 42 "\u0050\u0051\u0052\u0053\u0054\u0055\u0056\u0057" + 43 "\u0058\u0059\u005a\u005b\\\u005d\u005e\u005f" + 44 "\u0060\u0061\u0062\u0063\u0064\u0065\u0066\u0067" + 45 "\u0068\u0069\u006a\u006b\u006c\u006d\u006e\u006f" + 46 "\u0070\u0071\u0072\u0073\u0074\u0075\u0076\u0077" + 47 "\u0078\u0079\u007a\u007b\u007c\u007d\u007e\u007f"; 48 49 private static final String [] CHAR_STRING_ARRAY = new String [128]; 50 private static final Character [] CHAR_ARRAY = new Character [128]; 51 52 static { 53 for (int i = 127; i >= 0; i--) { 54 CHAR_STRING_ARRAY[i] = CHAR_STRING.substring(i, i + 1); 55 CHAR_ARRAY[i] = new Character ((char) i); 56 } 57 } 58 59 66 public CharUtils() { 67 } 68 69 84 public static Character toCharacterObject(char ch) { 85 if (ch < CHAR_ARRAY.length) { 86 return CHAR_ARRAY[ch]; 87 } else { 88 return new Character (ch); 89 } 90 } 91 92 109 public static Character toCharacterObject(String str) { 110 if (StringUtils.isEmpty(str)) { 111 return null; 112 } 113 return toCharacterObject(str.charAt(0)); 114 } 115 116 130 public static char toChar(Character ch) { 131 if (ch == null) { 132 throw new IllegalArgumentException ("The Character must not be null"); 133 } 134 return ch.charValue(); 135 } 136 137 150 public static char toChar(Character ch, char defaultValue) { 151 if (ch == null) { 152 return defaultValue; 153 } 154 return ch.charValue(); 155 } 156 157 173 public static char toChar(String str) { 174 if (StringUtils.isEmpty(str)) { 175 throw new IllegalArgumentException ("The String must not be empty"); 176 } 177 return str.charAt(0); 178 } 179 180 195 public static char toChar(String str, char defaultValue) { 196 if (StringUtils.isEmpty(str)) { 197 return defaultValue; 198 } 199 return str.charAt(0); 200 } 201 202 218 public static int toIntValue(char ch) { 219 if (isAsciiNumeric(ch) == false) { 220 throw new IllegalArgumentException ("The character " + ch + " is not in the range '0' - '9'"); 221 } 222 return ch - 48; 223 } 224 225 240 public static int toIntValue(char ch, int defaultValue) { 241 if (isAsciiNumeric(ch) == false) { 242 return defaultValue; 243 } 244 return ch - 48; 245 } 246 247 263 public static int toIntValue(Character ch) { 264 if (ch == null) { 265 throw new IllegalArgumentException ("The character must not be null"); 266 } 267 return toIntValue(ch.charValue()); 268 } 269 270 286 public static int toIntValue(Character ch, int defaultValue) { 287 if (ch == null) { 288 return defaultValue; 289 } 290 return toIntValue(ch.charValue(), defaultValue); 291 } 292 293 308 public static String toString(char ch) { 309 if (ch < 128) { 310 return CHAR_STRING_ARRAY[ch]; 311 } else { 312 return new String (new char[] {ch}); 313 } 314 } 315 316 333 public static String toString(Character ch) { 334 if (ch == null) { 335 return null; 336 } else { 337 return toString(ch.charValue()); 338 } 339 } 340 341 355 public static String unicodeEscaped(char ch) { 356 if (ch < 0x10) { 357 return "\\u000" + Integer.toHexString(ch); 358 } else if (ch < 0x100) { 359 return "\\u00" + Integer.toHexString(ch); 360 } else if (ch < 0x1000) { 361 return "\\u0" + Integer.toHexString(ch); 362 } 363 return "\\u" + Integer.toHexString(ch); 364 } 365 366 382 public static String unicodeEscaped(Character ch) { 383 if (ch == null) { 384 return null; 385 } 386 return unicodeEscaped(ch.charValue()); 387 } 388 389 405 public static boolean isAscii(char ch) { 406 return ch < 128; 407 } 408 409 424 public static boolean isAsciiPrintable(char ch) { 425 return ch >= 32 && ch < 127; 426 } 427 428 443 public static boolean isAsciiControl(char ch) { 444 return ch < 32 || ch == 127; 445 } 446 447 462 public static boolean isAsciiAlpha(char ch) { 463 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); 464 } 465 466 481 public static boolean isAsciiAlphaUpper(char ch) { 482 return ch >= 'A' && ch <= 'Z'; 483 } 484 485 500 public static boolean isAsciiAlphaLower(char ch) { 501 return ch >= 'a' && ch <= 'z'; 502 } 503 504 519 public static boolean isAsciiNumeric(char ch) { 520 return ch >= '0' && ch <= '9'; 521 } 522 523 538 public static boolean isAsciiAlphanumeric(char ch) { 539 return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9'); 540 } 541 542 } 543 | Popular Tags |