001    package serp.util;
002    
003    /**
004     * Number utilities.
005     *
006     * @author Abe White
007     */
008    public class Numbers {
009        private static final Integer INT_NEGONE = new Integer(-1);
010        private static final Long LONG_NEGONE = new Long(-1);
011        private static final Integer[] INTEGERS = new Integer[50];
012        private static final Long[] LONGS = new Long[50];
013    
014        static {
015            for (int i = 0; i < INTEGERS.length; i++)
016                INTEGERS[i] = new Integer(i);
017            for (int i = 0; i < LONGS.length; i++)
018                LONGS[i] = new Long(i);
019        }
020    
021        /**
022         * Return the wrapper for the given number, taking advantage of cached
023         * common values.
024         */
025        public static Integer valueOf(int n) {
026            if (n == -1)
027                return INT_NEGONE;
028            if (n >= 0 && n < INTEGERS.length)
029                return INTEGERS[n];
030            return new Integer(n);
031        }
032    
033        /**
034         * Return the wrapper for the given number, taking advantage of cached
035         * common values.
036         */
037        public static Long valueOf(long n) {
038            if (n == -1)
039                return LONG_NEGONE;
040            if (n >= 0 && n < LONGS.length)
041                return LONGS[(int) n];
042            return new Long(n);
043        }
044    }