001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018public final class Ascii {
019
020        private Ascii() {
021        }
022
023        private static final int LETTER_OFFSET = 13;
024        private static final int NUMBER_OFFSET = 5;
025        private static final char NUMBER_MIDPOINT = '4';
026        private static final char LCASE_MIDPOINT = 'm';
027        private static final char UCASE_MIDPOINT = 'M';
028
029        /**
030         * Return true if the character is in the range A-Z or a-z
031         */
032        public static boolean isLetter(char c) {
033                return isLowerCase(c) || isUpperCase(c);
034        }
035
036        /**
037         * Return true if the character is in the range 0-9
038         */
039        public static boolean isDigit(char c) {
040                return c >= '0' && c <= '9';
041        }
042
043        /**
044         * Return true if the character is in the range a-z
045         */
046        public static boolean isLowerCase(char c) {
047                return c >= 'a' && c <= 'z';
048        }
049
050        /**
051         * Return true if the character is in the range A-Z
052         */
053        public static boolean isUpperCase(char c) {
054                return c >= 'A' && c <= 'Z';
055        }
056
057        /**
058         * <p>
059         * If the character is a letter or digit, apply the flip algorithm to it, otherwise leave it alone.
060         * </p>
061         * 
062         * The flip algorithm makes the character in the top row become the character in the bottom row, and vice versa.
063         * 
064         * <pre>
065         *  01234 abcdefghijklm ABCDEFGHIJKLM
066         *  56789 nopqrstuvwxyz NOPQRSTUVWXYZ
067         * </pre>
068         */
069        public static char flip(char c) {
070                if (isLowerCase(c)) {
071                        if (c > LCASE_MIDPOINT) {
072                                return (char) ((int) c - LETTER_OFFSET);
073                        } else {
074                                return (char) ((int) c + LETTER_OFFSET);
075                        }
076                } else if (isUpperCase(c)) {
077                        if (c > UCASE_MIDPOINT) {
078                                return (char) ((int) c - LETTER_OFFSET);
079                        } else {
080                                return (char) ((int) c + LETTER_OFFSET);
081                        }
082                } else if (isDigit(c)) {
083                        if (c > NUMBER_MIDPOINT) {
084                                return (char) ((int) c - NUMBER_OFFSET);
085                        } else {
086                                return (char) ((int) c + NUMBER_OFFSET);
087                        }
088                } else {
089                        return c;
090                }
091        }
092
093}