View Javadoc
1   /**
2    * Copyright 2010-2014 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  public final class Ascii {
19  
20  	private Ascii() {
21  	}
22  
23  	private static final int LETTER_OFFSET = 13;
24  	private static final int NUMBER_OFFSET = 5;
25  	private static final char NUMBER_MIDPOINT = '4';
26  	private static final char LCASE_MIDPOINT = 'm';
27  	private static final char UCASE_MIDPOINT = 'M';
28  
29  	/**
30  	 * Return true if the character is in the range A-Z or a-z
31  	 */
32  	public static boolean isLetter(char c) {
33  		return isLowerCase(c) || isUpperCase(c);
34  	}
35  
36  	/**
37  	 * Return true if the character is in the range 0-9
38  	 */
39  	public static boolean isDigit(char c) {
40  		return c >= '0' && c <= '9';
41  	}
42  
43  	/**
44  	 * Return true if the character is in the range a-z
45  	 */
46  	public static boolean isLowerCase(char c) {
47  		return c >= 'a' && c <= 'z';
48  	}
49  
50  	/**
51  	 * Return true if the character is in the range A-Z
52  	 */
53  	public static boolean isUpperCase(char c) {
54  		return c >= 'A' && c <= 'Z';
55  	}
56  
57  	/**
58  	 * <p>
59  	 * If the character is a letter or digit, apply the flip algorithm to it, otherwise leave it alone.
60  	 * </p>
61  	 * 
62  	 * The flip algorithm makes the character in the top row become the character in the bottom row, and vice versa.
63  	 * 
64  	 * <pre>
65  	 *  01234 abcdefghijklm ABCDEFGHIJKLM
66  	 *  56789 nopqrstuvwxyz NOPQRSTUVWXYZ
67  	 * </pre>
68  	 */
69  	public static char flip(char c) {
70  		if (isLowerCase(c)) {
71  			if (c > LCASE_MIDPOINT) {
72  				return (char) ((int) c - LETTER_OFFSET);
73  			} else {
74  				return (char) ((int) c + LETTER_OFFSET);
75  			}
76  		} else if (isUpperCase(c)) {
77  			if (c > UCASE_MIDPOINT) {
78  				return (char) ((int) c - LETTER_OFFSET);
79  			} else {
80  				return (char) ((int) c + LETTER_OFFSET);
81  			}
82  		} else if (isDigit(c)) {
83  			if (c > NUMBER_MIDPOINT) {
84  				return (char) ((int) c - NUMBER_OFFSET);
85  			} else {
86  				return (char) ((int) c + NUMBER_OFFSET);
87  			}
88  		} else {
89  			return c;
90  		}
91  	}
92  
93  }