001package org.kuali.common.util;
002
003public final class Ascii {
004
005        private Ascii() {
006        }
007
008        private static final int LETTER_OFFSET = 13;
009        private static final int NUMBER_OFFSET = 5;
010        private static final char NUMBER_MIDPOINT = '4';
011        private static final char LCASE_MIDPOINT = 'm';
012        private static final char UCASE_MIDPOINT = 'M';
013
014        /**
015         * Return true if the character is in the range A-Z or a-z
016         */
017        public static boolean isLetter(char c) {
018                return isLowerCase(c) || isUpperCase(c);
019        }
020
021        /**
022         * Return true if the character is in the range 0-9
023         */
024        public static boolean isDigit(char c) {
025                return c >= '0' && c <= '9';
026        }
027
028        /**
029         * Return true if the character is in the range a-z
030         */
031        public static boolean isLowerCase(char c) {
032                return c >= 'a' && c <= 'z';
033        }
034
035        /**
036         * Return true if the character is in the range A-Z
037         */
038        public static boolean isUpperCase(char c) {
039                return c >= 'A' && c <= 'Z';
040        }
041
042        /**
043         * <p>
044         * If the character is a letter or digit, apply the flip algorithm to it, otherwise leave it alone.
045         * </p>
046         * 
047         * The flip algorithm makes the character in the top row become the character in the bottom row, and vice versa.
048         * 
049         * <pre>
050         *  01234 abcdefghijklm ABCDEFGHIJKLM
051         *  56789 nopqrstuvwxyz NOPQRSTUVWXYZ
052         * </pre>
053         */
054        public static char flip(char c) {
055                if (isLowerCase(c)) {
056                        if (c > LCASE_MIDPOINT) {
057                                return (char) ((int) c - LETTER_OFFSET);
058                        } else {
059                                return (char) ((int) c + LETTER_OFFSET);
060                        }
061                } else if (isUpperCase(c)) {
062                        if (c > UCASE_MIDPOINT) {
063                                return (char) ((int) c - LETTER_OFFSET);
064                        } else {
065                                return (char) ((int) c + LETTER_OFFSET);
066                        }
067                } else if (isDigit(c)) {
068                        if (c > NUMBER_MIDPOINT) {
069                                return (char) ((int) c - NUMBER_OFFSET);
070                        } else {
071                                return (char) ((int) c + NUMBER_OFFSET);
072                        }
073                } else {
074                        return c;
075                }
076        }
077
078}