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.primitives;
017
018public final class Numbers {
019
020        private Numbers() {
021        }
022
023        public static boolean between(int number, int min, int max) {
024                return number >= min && number <= max;
025        }
026
027        /**
028         * Return the smallest Number it is safe to return. Returns a Byte, Short, Integer, or Long.
029         */
030        public static Number narrow(long number) {
031                if (isByte(number)) {
032                        return (byte) number;
033                } else if (isShort(number)) {
034                        return (short) number;
035                } else if (isInt(number)) {
036                        return (int) number;
037                } else {
038                        return number;
039                }
040        }
041
042        public static boolean isByte(long number) {
043                return number >= Byte.MIN_VALUE && number <= Byte.MAX_VALUE;
044        }
045
046        public static boolean isShort(long number) {
047                return number >= Short.MIN_VALUE && number <= Short.MAX_VALUE;
048        }
049
050        public static boolean isInt(long number) {
051                return number >= Integer.MIN_VALUE && number <= Integer.MAX_VALUE;
052        }
053
054}