View Javadoc
1   package org.kuali.common.util;
2   
3   public final class Ascii {
4   
5   	private Ascii() {
6   	}
7   
8   	private static final int LETTER_OFFSET = 13;
9   	private static final int NUMBER_OFFSET = 5;
10  	private static final char NUMBER_MIDPOINT = '4';
11  	private static final char LCASE_MIDPOINT = 'm';
12  	private static final char UCASE_MIDPOINT = 'M';
13  
14  	/**
15  	 * Return true if the character is in the range A-Z or a-z
16  	 */
17  	public static boolean isLetter(char c) {
18  		return isLowerCase(c) || isUpperCase(c);
19  	}
20  
21  	/**
22  	 * Return true if the character is in the range 0-9
23  	 */
24  	public static boolean isDigit(char c) {
25  		return c >= '0' && c <= '9';
26  	}
27  
28  	/**
29  	 * Return true if the character is in the range a-z
30  	 */
31  	public static boolean isLowerCase(char c) {
32  		return c >= 'a' && c <= 'z';
33  	}
34  
35  	/**
36  	 * Return true if the character is in the range A-Z
37  	 */
38  	public static boolean isUpperCase(char c) {
39  		return c >= 'A' && c <= 'Z';
40  	}
41  
42  	/**
43  	 * <p>
44  	 * If the character is a letter or digit, apply the flip algorithm to it, otherwise leave it alone.
45  	 * </p>
46  	 * 
47  	 * The flip algorithm makes the character in the top row become the character in the bottom row, and vice versa.
48  	 * 
49  	 * <pre>
50  	 *  01234 abcdefghijklm ABCDEFGHIJKLM
51  	 *  56789 nopqrstuvwxyz NOPQRSTUVWXYZ
52  	 * </pre>
53  	 */
54  	public static char flip(char c) {
55  		if (isLowerCase(c)) {
56  			if (c > LCASE_MIDPOINT) {
57  				return (char) ((int) c - LETTER_OFFSET);
58  			} else {
59  				return (char) ((int) c + LETTER_OFFSET);
60  			}
61  		} else if (isUpperCase(c)) {
62  			if (c > UCASE_MIDPOINT) {
63  				return (char) ((int) c - LETTER_OFFSET);
64  			} else {
65  				return (char) ((int) c + LETTER_OFFSET);
66  			}
67  		} else if (isDigit(c)) {
68  			if (c > NUMBER_MIDPOINT) {
69  				return (char) ((int) c - NUMBER_OFFSET);
70  			} else {
71  				return (char) ((int) c + NUMBER_OFFSET);
72  			}
73  		} else {
74  			return c;
75  		}
76  	}
77  
78  }