emul/src/main/java/java/lang/Integer.java
branchemul
changeset 49 0a115f1c6f3c
child 84 d65b3a2fbfaf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/emul/src/main/java/java/lang/Integer.java	Fri Sep 28 17:59:03 2012 +0200
     1.3 @@ -0,0 +1,1244 @@
     1.4 +/*
     1.5 + * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package java.lang;
    1.30 +
    1.31 +import java.util.Properties;
    1.32 +
    1.33 +/**
    1.34 + * The {@code Integer} class wraps a value of the primitive type
    1.35 + * {@code int} in an object. An object of type {@code Integer}
    1.36 + * contains a single field whose type is {@code int}.
    1.37 + *
    1.38 + * <p>In addition, this class provides several methods for converting
    1.39 + * an {@code int} to a {@code String} and a {@code String} to an
    1.40 + * {@code int}, as well as other constants and methods useful when
    1.41 + * dealing with an {@code int}.
    1.42 + *
    1.43 + * <p>Implementation note: The implementations of the "bit twiddling"
    1.44 + * methods (such as {@link #highestOneBit(int) highestOneBit} and
    1.45 + * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
    1.46 + * based on material from Henry S. Warren, Jr.'s <i>Hacker's
    1.47 + * Delight</i>, (Addison Wesley, 2002).
    1.48 + *
    1.49 + * @author  Lee Boynton
    1.50 + * @author  Arthur van Hoff
    1.51 + * @author  Josh Bloch
    1.52 + * @author  Joseph D. Darcy
    1.53 + * @since JDK1.0
    1.54 + */
    1.55 +public final class Integer extends Number implements Comparable<Integer> {
    1.56 +    /**
    1.57 +     * A constant holding the minimum value an {@code int} can
    1.58 +     * have, -2<sup>31</sup>.
    1.59 +     */
    1.60 +    public static final int   MIN_VALUE = 0x80000000;
    1.61 +
    1.62 +    /**
    1.63 +     * A constant holding the maximum value an {@code int} can
    1.64 +     * have, 2<sup>31</sup>-1.
    1.65 +     */
    1.66 +    public static final int   MAX_VALUE = 0x7fffffff;
    1.67 +
    1.68 +    /**
    1.69 +     * The {@code Class} instance representing the primitive type
    1.70 +     * {@code int}.
    1.71 +     *
    1.72 +     * @since   JDK1.1
    1.73 +     */
    1.74 +    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
    1.75 +
    1.76 +    /**
    1.77 +     * All possible chars for representing a number as a String
    1.78 +     */
    1.79 +    final static char[] digits = {
    1.80 +        '0' , '1' , '2' , '3' , '4' , '5' ,
    1.81 +        '6' , '7' , '8' , '9' , 'a' , 'b' ,
    1.82 +        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
    1.83 +        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
    1.84 +        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
    1.85 +        'u' , 'v' , 'w' , 'x' , 'y' , 'z'
    1.86 +    };
    1.87 +
    1.88 +    /**
    1.89 +     * Returns a string representation of the first argument in the
    1.90 +     * radix specified by the second argument.
    1.91 +     *
    1.92 +     * <p>If the radix is smaller than {@code Character.MIN_RADIX}
    1.93 +     * or larger than {@code Character.MAX_RADIX}, then the radix
    1.94 +     * {@code 10} is used instead.
    1.95 +     *
    1.96 +     * <p>If the first argument is negative, the first element of the
    1.97 +     * result is the ASCII minus character {@code '-'}
    1.98 +     * (<code>'&#92;u002D'</code>). If the first argument is not
    1.99 +     * negative, no sign character appears in the result.
   1.100 +     *
   1.101 +     * <p>The remaining characters of the result represent the magnitude
   1.102 +     * of the first argument. If the magnitude is zero, it is
   1.103 +     * represented by a single zero character {@code '0'}
   1.104 +     * (<code>'&#92;u0030'</code>); otherwise, the first character of
   1.105 +     * the representation of the magnitude will not be the zero
   1.106 +     * character.  The following ASCII characters are used as digits:
   1.107 +     *
   1.108 +     * <blockquote>
   1.109 +     *   {@code 0123456789abcdefghijklmnopqrstuvwxyz}
   1.110 +     * </blockquote>
   1.111 +     *
   1.112 +     * These are <code>'&#92;u0030'</code> through
   1.113 +     * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
   1.114 +     * <code>'&#92;u007A'</code>. If {@code radix} is
   1.115 +     * <var>N</var>, then the first <var>N</var> of these characters
   1.116 +     * are used as radix-<var>N</var> digits in the order shown. Thus,
   1.117 +     * the digits for hexadecimal (radix 16) are
   1.118 +     * {@code 0123456789abcdef}. If uppercase letters are
   1.119 +     * desired, the {@link java.lang.String#toUpperCase()} method may
   1.120 +     * be called on the result:
   1.121 +     *
   1.122 +     * <blockquote>
   1.123 +     *  {@code Integer.toString(n, 16).toUpperCase()}
   1.124 +     * </blockquote>
   1.125 +     *
   1.126 +     * @param   i       an integer to be converted to a string.
   1.127 +     * @param   radix   the radix to use in the string representation.
   1.128 +     * @return  a string representation of the argument in the specified radix.
   1.129 +     * @see     java.lang.Character#MAX_RADIX
   1.130 +     * @see     java.lang.Character#MIN_RADIX
   1.131 +     */
   1.132 +    public static String toString(int i, int radix) {
   1.133 +
   1.134 +        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
   1.135 +            radix = 10;
   1.136 +
   1.137 +        /* Use the faster version */
   1.138 +        if (radix == 10) {
   1.139 +            return toString(i);
   1.140 +        }
   1.141 +
   1.142 +        char buf[] = new char[33];
   1.143 +        boolean negative = (i < 0);
   1.144 +        int charPos = 32;
   1.145 +
   1.146 +        if (!negative) {
   1.147 +            i = -i;
   1.148 +        }
   1.149 +
   1.150 +        while (i <= -radix) {
   1.151 +            buf[charPos--] = digits[-(i % radix)];
   1.152 +            i = i / radix;
   1.153 +        }
   1.154 +        buf[charPos] = digits[-i];
   1.155 +
   1.156 +        if (negative) {
   1.157 +            buf[--charPos] = '-';
   1.158 +        }
   1.159 +
   1.160 +        return new String(buf, charPos, (33 - charPos));
   1.161 +    }
   1.162 +
   1.163 +    /**
   1.164 +     * Returns a string representation of the integer argument as an
   1.165 +     * unsigned integer in base&nbsp;16.
   1.166 +     *
   1.167 +     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
   1.168 +     * if the argument is negative; otherwise, it is equal to the
   1.169 +     * argument.  This value is converted to a string of ASCII digits
   1.170 +     * in hexadecimal (base&nbsp;16) with no extra leading
   1.171 +     * {@code 0}s. If the unsigned magnitude is zero, it is
   1.172 +     * represented by a single zero character {@code '0'}
   1.173 +     * (<code>'&#92;u0030'</code>); otherwise, the first character of
   1.174 +     * the representation of the unsigned magnitude will not be the
   1.175 +     * zero character. The following characters are used as
   1.176 +     * hexadecimal digits:
   1.177 +     *
   1.178 +     * <blockquote>
   1.179 +     *  {@code 0123456789abcdef}
   1.180 +     * </blockquote>
   1.181 +     *
   1.182 +     * These are the characters <code>'&#92;u0030'</code> through
   1.183 +     * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
   1.184 +     * <code>'&#92;u0066'</code>. If uppercase letters are
   1.185 +     * desired, the {@link java.lang.String#toUpperCase()} method may
   1.186 +     * be called on the result:
   1.187 +     *
   1.188 +     * <blockquote>
   1.189 +     *  {@code Integer.toHexString(n).toUpperCase()}
   1.190 +     * </blockquote>
   1.191 +     *
   1.192 +     * @param   i   an integer to be converted to a string.
   1.193 +     * @return  the string representation of the unsigned integer value
   1.194 +     *          represented by the argument in hexadecimal (base&nbsp;16).
   1.195 +     * @since   JDK1.0.2
   1.196 +     */
   1.197 +    public static String toHexString(int i) {
   1.198 +        return toUnsignedString(i, 4);
   1.199 +    }
   1.200 +
   1.201 +    /**
   1.202 +     * Returns a string representation of the integer argument as an
   1.203 +     * unsigned integer in base&nbsp;8.
   1.204 +     *
   1.205 +     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
   1.206 +     * if the argument is negative; otherwise, it is equal to the
   1.207 +     * argument.  This value is converted to a string of ASCII digits
   1.208 +     * in octal (base&nbsp;8) with no extra leading {@code 0}s.
   1.209 +     *
   1.210 +     * <p>If the unsigned magnitude is zero, it is represented by a
   1.211 +     * single zero character {@code '0'}
   1.212 +     * (<code>'&#92;u0030'</code>); otherwise, the first character of
   1.213 +     * the representation of the unsigned magnitude will not be the
   1.214 +     * zero character. The following characters are used as octal
   1.215 +     * digits:
   1.216 +     *
   1.217 +     * <blockquote>
   1.218 +     * {@code 01234567}
   1.219 +     * </blockquote>
   1.220 +     *
   1.221 +     * These are the characters <code>'&#92;u0030'</code> through
   1.222 +     * <code>'&#92;u0037'</code>.
   1.223 +     *
   1.224 +     * @param   i   an integer to be converted to a string.
   1.225 +     * @return  the string representation of the unsigned integer value
   1.226 +     *          represented by the argument in octal (base&nbsp;8).
   1.227 +     * @since   JDK1.0.2
   1.228 +     */
   1.229 +    public static String toOctalString(int i) {
   1.230 +        return toUnsignedString(i, 3);
   1.231 +    }
   1.232 +
   1.233 +    /**
   1.234 +     * Returns a string representation of the integer argument as an
   1.235 +     * unsigned integer in base&nbsp;2.
   1.236 +     *
   1.237 +     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
   1.238 +     * if the argument is negative; otherwise it is equal to the
   1.239 +     * argument.  This value is converted to a string of ASCII digits
   1.240 +     * in binary (base&nbsp;2) with no extra leading {@code 0}s.
   1.241 +     * If the unsigned magnitude is zero, it is represented by a
   1.242 +     * single zero character {@code '0'}
   1.243 +     * (<code>'&#92;u0030'</code>); otherwise, the first character of
   1.244 +     * the representation of the unsigned magnitude will not be the
   1.245 +     * zero character. The characters {@code '0'}
   1.246 +     * (<code>'&#92;u0030'</code>) and {@code '1'}
   1.247 +     * (<code>'&#92;u0031'</code>) are used as binary digits.
   1.248 +     *
   1.249 +     * @param   i   an integer to be converted to a string.
   1.250 +     * @return  the string representation of the unsigned integer value
   1.251 +     *          represented by the argument in binary (base&nbsp;2).
   1.252 +     * @since   JDK1.0.2
   1.253 +     */
   1.254 +    public static String toBinaryString(int i) {
   1.255 +        return toUnsignedString(i, 1);
   1.256 +    }
   1.257 +
   1.258 +    /**
   1.259 +     * Convert the integer to an unsigned number.
   1.260 +     */
   1.261 +    private static String toUnsignedString(int i, int shift) {
   1.262 +        char[] buf = new char[32];
   1.263 +        int charPos = 32;
   1.264 +        int radix = 1 << shift;
   1.265 +        int mask = radix - 1;
   1.266 +        do {
   1.267 +            buf[--charPos] = digits[i & mask];
   1.268 +            i >>>= shift;
   1.269 +        } while (i != 0);
   1.270 +
   1.271 +        return new String(buf, charPos, (32 - charPos));
   1.272 +    }
   1.273 +
   1.274 +
   1.275 +    final static char [] DigitTens = {
   1.276 +        '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
   1.277 +        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
   1.278 +        '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
   1.279 +        '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
   1.280 +        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
   1.281 +        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
   1.282 +        '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
   1.283 +        '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
   1.284 +        '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
   1.285 +        '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
   1.286 +        } ;
   1.287 +
   1.288 +    final static char [] DigitOnes = {
   1.289 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.290 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.291 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.292 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.293 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.294 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.295 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.296 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.297 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.298 +        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
   1.299 +        } ;
   1.300 +
   1.301 +        // I use the "invariant division by multiplication" trick to
   1.302 +        // accelerate Integer.toString.  In particular we want to
   1.303 +        // avoid division by 10.
   1.304 +        //
   1.305 +        // The "trick" has roughly the same performance characteristics
   1.306 +        // as the "classic" Integer.toString code on a non-JIT VM.
   1.307 +        // The trick avoids .rem and .div calls but has a longer code
   1.308 +        // path and is thus dominated by dispatch overhead.  In the
   1.309 +        // JIT case the dispatch overhead doesn't exist and the
   1.310 +        // "trick" is considerably faster than the classic code.
   1.311 +        //
   1.312 +        // TODO-FIXME: convert (x * 52429) into the equiv shift-add
   1.313 +        // sequence.
   1.314 +        //
   1.315 +        // RE:  Division by Invariant Integers using Multiplication
   1.316 +        //      T Gralund, P Montgomery
   1.317 +        //      ACM PLDI 1994
   1.318 +        //
   1.319 +
   1.320 +    /**
   1.321 +     * Returns a {@code String} object representing the
   1.322 +     * specified integer. The argument is converted to signed decimal
   1.323 +     * representation and returned as a string, exactly as if the
   1.324 +     * argument and radix 10 were given as arguments to the {@link
   1.325 +     * #toString(int, int)} method.
   1.326 +     *
   1.327 +     * @param   i   an integer to be converted.
   1.328 +     * @return  a string representation of the argument in base&nbsp;10.
   1.329 +     */
   1.330 +    public static String toString(int i) {
   1.331 +        if (i == Integer.MIN_VALUE)
   1.332 +            return "-2147483648";
   1.333 +        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
   1.334 +        char[] buf = new char[size];
   1.335 +        getChars(i, size, buf);
   1.336 +        return new String(0, size, buf);
   1.337 +    }
   1.338 +
   1.339 +    /**
   1.340 +     * Places characters representing the integer i into the
   1.341 +     * character array buf. The characters are placed into
   1.342 +     * the buffer backwards starting with the least significant
   1.343 +     * digit at the specified index (exclusive), and working
   1.344 +     * backwards from there.
   1.345 +     *
   1.346 +     * Will fail if i == Integer.MIN_VALUE
   1.347 +     */
   1.348 +    static void getChars(int i, int index, char[] buf) {
   1.349 +        int q, r;
   1.350 +        int charPos = index;
   1.351 +        char sign = 0;
   1.352 +
   1.353 +        if (i < 0) {
   1.354 +            sign = '-';
   1.355 +            i = -i;
   1.356 +        }
   1.357 +
   1.358 +        // Generate two digits per iteration
   1.359 +        while (i >= 65536) {
   1.360 +            q = i / 100;
   1.361 +        // really: r = i - (q * 100);
   1.362 +            r = i - ((q << 6) + (q << 5) + (q << 2));
   1.363 +            i = q;
   1.364 +            buf [--charPos] = DigitOnes[r];
   1.365 +            buf [--charPos] = DigitTens[r];
   1.366 +        }
   1.367 +
   1.368 +        // Fall thru to fast mode for smaller numbers
   1.369 +        // assert(i <= 65536, i);
   1.370 +        for (;;) {
   1.371 +            q = (i * 52429) >>> (16+3);
   1.372 +            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
   1.373 +            buf [--charPos] = digits [r];
   1.374 +            i = q;
   1.375 +            if (i == 0) break;
   1.376 +        }
   1.377 +        if (sign != 0) {
   1.378 +            buf [--charPos] = sign;
   1.379 +        }
   1.380 +    }
   1.381 +
   1.382 +    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
   1.383 +                                      99999999, 999999999, Integer.MAX_VALUE };
   1.384 +
   1.385 +    // Requires positive x
   1.386 +    static int stringSize(int x) {
   1.387 +        for (int i=0; ; i++)
   1.388 +            if (x <= sizeTable[i])
   1.389 +                return i+1;
   1.390 +    }
   1.391 +
   1.392 +    /**
   1.393 +     * Parses the string argument as a signed integer in the radix
   1.394 +     * specified by the second argument. The characters in the string
   1.395 +     * must all be digits of the specified radix (as determined by
   1.396 +     * whether {@link java.lang.Character#digit(char, int)} returns a
   1.397 +     * nonnegative value), except that the first character may be an
   1.398 +     * ASCII minus sign {@code '-'} (<code>'&#92;u002D'</code>) to
   1.399 +     * indicate a negative value or an ASCII plus sign {@code '+'}
   1.400 +     * (<code>'&#92;u002B'</code>) to indicate a positive value. The
   1.401 +     * resulting integer value is returned.
   1.402 +     *
   1.403 +     * <p>An exception of type {@code NumberFormatException} is
   1.404 +     * thrown if any of the following situations occurs:
   1.405 +     * <ul>
   1.406 +     * <li>The first argument is {@code null} or is a string of
   1.407 +     * length zero.
   1.408 +     *
   1.409 +     * <li>The radix is either smaller than
   1.410 +     * {@link java.lang.Character#MIN_RADIX} or
   1.411 +     * larger than {@link java.lang.Character#MAX_RADIX}.
   1.412 +     *
   1.413 +     * <li>Any character of the string is not a digit of the specified
   1.414 +     * radix, except that the first character may be a minus sign
   1.415 +     * {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
   1.416 +     * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
   1.417 +     * string is longer than length 1.
   1.418 +     *
   1.419 +     * <li>The value represented by the string is not a value of type
   1.420 +     * {@code int}.
   1.421 +     * </ul>
   1.422 +     *
   1.423 +     * <p>Examples:
   1.424 +     * <blockquote><pre>
   1.425 +     * parseInt("0", 10) returns 0
   1.426 +     * parseInt("473", 10) returns 473
   1.427 +     * parseInt("+42", 10) returns 42
   1.428 +     * parseInt("-0", 10) returns 0
   1.429 +     * parseInt("-FF", 16) returns -255
   1.430 +     * parseInt("1100110", 2) returns 102
   1.431 +     * parseInt("2147483647", 10) returns 2147483647
   1.432 +     * parseInt("-2147483648", 10) returns -2147483648
   1.433 +     * parseInt("2147483648", 10) throws a NumberFormatException
   1.434 +     * parseInt("99", 8) throws a NumberFormatException
   1.435 +     * parseInt("Kona", 10) throws a NumberFormatException
   1.436 +     * parseInt("Kona", 27) returns 411787
   1.437 +     * </pre></blockquote>
   1.438 +     *
   1.439 +     * @param      s   the {@code String} containing the integer
   1.440 +     *                  representation to be parsed
   1.441 +     * @param      radix   the radix to be used while parsing {@code s}.
   1.442 +     * @return     the integer represented by the string argument in the
   1.443 +     *             specified radix.
   1.444 +     * @exception  NumberFormatException if the {@code String}
   1.445 +     *             does not contain a parsable {@code int}.
   1.446 +     */
   1.447 +    public static int parseInt(String s, int radix)
   1.448 +                throws NumberFormatException
   1.449 +    {
   1.450 +        /*
   1.451 +         * WARNING: This method may be invoked early during VM initialization
   1.452 +         * before IntegerCache is initialized. Care must be taken to not use
   1.453 +         * the valueOf method.
   1.454 +         */
   1.455 +
   1.456 +        if (s == null) {
   1.457 +            throw new NumberFormatException("null");
   1.458 +        }
   1.459 +
   1.460 +        if (radix < Character.MIN_RADIX) {
   1.461 +            throw new NumberFormatException("radix " + radix +
   1.462 +                                            " less than Character.MIN_RADIX");
   1.463 +        }
   1.464 +
   1.465 +        if (radix > Character.MAX_RADIX) {
   1.466 +            throw new NumberFormatException("radix " + radix +
   1.467 +                                            " greater than Character.MAX_RADIX");
   1.468 +        }
   1.469 +
   1.470 +        int result = 0;
   1.471 +        boolean negative = false;
   1.472 +        int i = 0, len = s.length();
   1.473 +        int limit = -Integer.MAX_VALUE;
   1.474 +        int multmin;
   1.475 +        int digit;
   1.476 +
   1.477 +        if (len > 0) {
   1.478 +            char firstChar = s.charAt(0);
   1.479 +            if (firstChar < '0') { // Possible leading "+" or "-"
   1.480 +                if (firstChar == '-') {
   1.481 +                    negative = true;
   1.482 +                    limit = Integer.MIN_VALUE;
   1.483 +                } else if (firstChar != '+')
   1.484 +                    throw NumberFormatException.forInputString(s);
   1.485 +
   1.486 +                if (len == 1) // Cannot have lone "+" or "-"
   1.487 +                    throw NumberFormatException.forInputString(s);
   1.488 +                i++;
   1.489 +            }
   1.490 +            multmin = limit / radix;
   1.491 +            while (i < len) {
   1.492 +                // Accumulating negatively avoids surprises near MAX_VALUE
   1.493 +                digit = Character.digit(s.charAt(i++),radix);
   1.494 +                if (digit < 0) {
   1.495 +                    throw NumberFormatException.forInputString(s);
   1.496 +                }
   1.497 +                if (result < multmin) {
   1.498 +                    throw NumberFormatException.forInputString(s);
   1.499 +                }
   1.500 +                result *= radix;
   1.501 +                if (result < limit + digit) {
   1.502 +                    throw NumberFormatException.forInputString(s);
   1.503 +                }
   1.504 +                result -= digit;
   1.505 +            }
   1.506 +        } else {
   1.507 +            throw NumberFormatException.forInputString(s);
   1.508 +        }
   1.509 +        return negative ? result : -result;
   1.510 +    }
   1.511 +
   1.512 +    /**
   1.513 +     * Parses the string argument as a signed decimal integer. The
   1.514 +     * characters in the string must all be decimal digits, except
   1.515 +     * that the first character may be an ASCII minus sign {@code '-'}
   1.516 +     * (<code>'&#92;u002D'</code>) to indicate a negative value or an
   1.517 +     * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
   1.518 +     * indicate a positive value. The resulting integer value is
   1.519 +     * returned, exactly as if the argument and the radix 10 were
   1.520 +     * given as arguments to the {@link #parseInt(java.lang.String,
   1.521 +     * int)} method.
   1.522 +     *
   1.523 +     * @param s    a {@code String} containing the {@code int}
   1.524 +     *             representation to be parsed
   1.525 +     * @return     the integer value represented by the argument in decimal.
   1.526 +     * @exception  NumberFormatException  if the string does not contain a
   1.527 +     *               parsable integer.
   1.528 +     */
   1.529 +    public static int parseInt(String s) throws NumberFormatException {
   1.530 +        return parseInt(s,10);
   1.531 +    }
   1.532 +
   1.533 +    /**
   1.534 +     * Returns an {@code Integer} object holding the value
   1.535 +     * extracted from the specified {@code String} when parsed
   1.536 +     * with the radix given by the second argument. The first argument
   1.537 +     * is interpreted as representing a signed integer in the radix
   1.538 +     * specified by the second argument, exactly as if the arguments
   1.539 +     * were given to the {@link #parseInt(java.lang.String, int)}
   1.540 +     * method. The result is an {@code Integer} object that
   1.541 +     * represents the integer value specified by the string.
   1.542 +     *
   1.543 +     * <p>In other words, this method returns an {@code Integer}
   1.544 +     * object equal to the value of:
   1.545 +     *
   1.546 +     * <blockquote>
   1.547 +     *  {@code new Integer(Integer.parseInt(s, radix))}
   1.548 +     * </blockquote>
   1.549 +     *
   1.550 +     * @param      s   the string to be parsed.
   1.551 +     * @param      radix the radix to be used in interpreting {@code s}
   1.552 +     * @return     an {@code Integer} object holding the value
   1.553 +     *             represented by the string argument in the specified
   1.554 +     *             radix.
   1.555 +     * @exception NumberFormatException if the {@code String}
   1.556 +     *            does not contain a parsable {@code int}.
   1.557 +     */
   1.558 +    public static Integer valueOf(String s, int radix) throws NumberFormatException {
   1.559 +        return Integer.valueOf(parseInt(s,radix));
   1.560 +    }
   1.561 +
   1.562 +    /**
   1.563 +     * Returns an {@code Integer} object holding the
   1.564 +     * value of the specified {@code String}. The argument is
   1.565 +     * interpreted as representing a signed decimal integer, exactly
   1.566 +     * as if the argument were given to the {@link
   1.567 +     * #parseInt(java.lang.String)} method. The result is an
   1.568 +     * {@code Integer} object that represents the integer value
   1.569 +     * specified by the string.
   1.570 +     *
   1.571 +     * <p>In other words, this method returns an {@code Integer}
   1.572 +     * object equal to the value of:
   1.573 +     *
   1.574 +     * <blockquote>
   1.575 +     *  {@code new Integer(Integer.parseInt(s))}
   1.576 +     * </blockquote>
   1.577 +     *
   1.578 +     * @param      s   the string to be parsed.
   1.579 +     * @return     an {@code Integer} object holding the value
   1.580 +     *             represented by the string argument.
   1.581 +     * @exception  NumberFormatException  if the string cannot be parsed
   1.582 +     *             as an integer.
   1.583 +     */
   1.584 +    public static Integer valueOf(String s) throws NumberFormatException {
   1.585 +        return Integer.valueOf(parseInt(s, 10));
   1.586 +    }
   1.587 +
   1.588 +    /**
   1.589 +     * Cache to support the object identity semantics of autoboxing for values between
   1.590 +     * -128 and 127 (inclusive) as required by JLS.
   1.591 +     *
   1.592 +     * The cache is initialized on first usage.  The size of the cache
   1.593 +     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
   1.594 +     * During VM initialization, java.lang.Integer.IntegerCache.high property
   1.595 +     * may be set and saved in the private system properties in the
   1.596 +     * sun.misc.VM class.
   1.597 +     */
   1.598 +
   1.599 +    private static class IntegerCache {
   1.600 +        static final int low = -128;
   1.601 +        static final int high;
   1.602 +        static final Integer cache[];
   1.603 +
   1.604 +        static {
   1.605 +            // high value may be configured by property
   1.606 +            int h = 127;
   1.607 +            String integerCacheHighPropValue =
   1.608 +                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
   1.609 +            if (integerCacheHighPropValue != null) {
   1.610 +                int i = parseInt(integerCacheHighPropValue);
   1.611 +                i = Math.max(i, 127);
   1.612 +                // Maximum array size is Integer.MAX_VALUE
   1.613 +                h = Math.min(i, Integer.MAX_VALUE - (-low));
   1.614 +            }
   1.615 +            high = h;
   1.616 +
   1.617 +            cache = new Integer[(high - low) + 1];
   1.618 +            int j = low;
   1.619 +            for(int k = 0; k < cache.length; k++)
   1.620 +                cache[k] = new Integer(j++);
   1.621 +        }
   1.622 +
   1.623 +        private IntegerCache() {}
   1.624 +    }
   1.625 +
   1.626 +    /**
   1.627 +     * Returns an {@code Integer} instance representing the specified
   1.628 +     * {@code int} value.  If a new {@code Integer} instance is not
   1.629 +     * required, this method should generally be used in preference to
   1.630 +     * the constructor {@link #Integer(int)}, as this method is likely
   1.631 +     * to yield significantly better space and time performance by
   1.632 +     * caching frequently requested values.
   1.633 +     *
   1.634 +     * This method will always cache values in the range -128 to 127,
   1.635 +     * inclusive, and may cache other values outside of this range.
   1.636 +     *
   1.637 +     * @param  i an {@code int} value.
   1.638 +     * @return an {@code Integer} instance representing {@code i}.
   1.639 +     * @since  1.5
   1.640 +     */
   1.641 +    public static Integer valueOf(int i) {
   1.642 +        assert IntegerCache.high >= 127;
   1.643 +        if (i >= IntegerCache.low && i <= IntegerCache.high)
   1.644 +            return IntegerCache.cache[i + (-IntegerCache.low)];
   1.645 +        return new Integer(i);
   1.646 +    }
   1.647 +
   1.648 +    /**
   1.649 +     * The value of the {@code Integer}.
   1.650 +     *
   1.651 +     * @serial
   1.652 +     */
   1.653 +    private final int value;
   1.654 +
   1.655 +    /**
   1.656 +     * Constructs a newly allocated {@code Integer} object that
   1.657 +     * represents the specified {@code int} value.
   1.658 +     *
   1.659 +     * @param   value   the value to be represented by the
   1.660 +     *                  {@code Integer} object.
   1.661 +     */
   1.662 +    public Integer(int value) {
   1.663 +        this.value = value;
   1.664 +    }
   1.665 +
   1.666 +    /**
   1.667 +     * Constructs a newly allocated {@code Integer} object that
   1.668 +     * represents the {@code int} value indicated by the
   1.669 +     * {@code String} parameter. The string is converted to an
   1.670 +     * {@code int} value in exactly the manner used by the
   1.671 +     * {@code parseInt} method for radix 10.
   1.672 +     *
   1.673 +     * @param      s   the {@code String} to be converted to an
   1.674 +     *                 {@code Integer}.
   1.675 +     * @exception  NumberFormatException  if the {@code String} does not
   1.676 +     *               contain a parsable integer.
   1.677 +     * @see        java.lang.Integer#parseInt(java.lang.String, int)
   1.678 +     */
   1.679 +    public Integer(String s) throws NumberFormatException {
   1.680 +        this.value = parseInt(s, 10);
   1.681 +    }
   1.682 +
   1.683 +    /**
   1.684 +     * Returns the value of this {@code Integer} as a
   1.685 +     * {@code byte}.
   1.686 +     */
   1.687 +    public byte byteValue() {
   1.688 +        return (byte)value;
   1.689 +    }
   1.690 +
   1.691 +    /**
   1.692 +     * Returns the value of this {@code Integer} as a
   1.693 +     * {@code short}.
   1.694 +     */
   1.695 +    public short shortValue() {
   1.696 +        return (short)value;
   1.697 +    }
   1.698 +
   1.699 +    /**
   1.700 +     * Returns the value of this {@code Integer} as an
   1.701 +     * {@code int}.
   1.702 +     */
   1.703 +    public int intValue() {
   1.704 +        return value;
   1.705 +    }
   1.706 +
   1.707 +    /**
   1.708 +     * Returns the value of this {@code Integer} as a
   1.709 +     * {@code long}.
   1.710 +     */
   1.711 +    public long longValue() {
   1.712 +        return (long)value;
   1.713 +    }
   1.714 +
   1.715 +    /**
   1.716 +     * Returns the value of this {@code Integer} as a
   1.717 +     * {@code float}.
   1.718 +     */
   1.719 +    public float floatValue() {
   1.720 +        return (float)value;
   1.721 +    }
   1.722 +
   1.723 +    /**
   1.724 +     * Returns the value of this {@code Integer} as a
   1.725 +     * {@code double}.
   1.726 +     */
   1.727 +    public double doubleValue() {
   1.728 +        return (double)value;
   1.729 +    }
   1.730 +
   1.731 +    /**
   1.732 +     * Returns a {@code String} object representing this
   1.733 +     * {@code Integer}'s value. The value is converted to signed
   1.734 +     * decimal representation and returned as a string, exactly as if
   1.735 +     * the integer value were given as an argument to the {@link
   1.736 +     * java.lang.Integer#toString(int)} method.
   1.737 +     *
   1.738 +     * @return  a string representation of the value of this object in
   1.739 +     *          base&nbsp;10.
   1.740 +     */
   1.741 +    public String toString() {
   1.742 +        return toString(value);
   1.743 +    }
   1.744 +
   1.745 +    /**
   1.746 +     * Returns a hash code for this {@code Integer}.
   1.747 +     *
   1.748 +     * @return  a hash code value for this object, equal to the
   1.749 +     *          primitive {@code int} value represented by this
   1.750 +     *          {@code Integer} object.
   1.751 +     */
   1.752 +    public int hashCode() {
   1.753 +        return value;
   1.754 +    }
   1.755 +
   1.756 +    /**
   1.757 +     * Compares this object to the specified object.  The result is
   1.758 +     * {@code true} if and only if the argument is not
   1.759 +     * {@code null} and is an {@code Integer} object that
   1.760 +     * contains the same {@code int} value as this object.
   1.761 +     *
   1.762 +     * @param   obj   the object to compare with.
   1.763 +     * @return  {@code true} if the objects are the same;
   1.764 +     *          {@code false} otherwise.
   1.765 +     */
   1.766 +    public boolean equals(Object obj) {
   1.767 +        if (obj instanceof Integer) {
   1.768 +            return value == ((Integer)obj).intValue();
   1.769 +        }
   1.770 +        return false;
   1.771 +    }
   1.772 +
   1.773 +    /**
   1.774 +     * Determines the integer value of the system property with the
   1.775 +     * specified name.
   1.776 +     *
   1.777 +     * <p>The first argument is treated as the name of a system property.
   1.778 +     * System properties are accessible through the
   1.779 +     * {@link java.lang.System#getProperty(java.lang.String)} method. The
   1.780 +     * string value of this property is then interpreted as an integer
   1.781 +     * value and an {@code Integer} object representing this value is
   1.782 +     * returned. Details of possible numeric formats can be found with
   1.783 +     * the definition of {@code getProperty}.
   1.784 +     *
   1.785 +     * <p>If there is no property with the specified name, if the specified name
   1.786 +     * is empty or {@code null}, or if the property does not have
   1.787 +     * the correct numeric format, then {@code null} is returned.
   1.788 +     *
   1.789 +     * <p>In other words, this method returns an {@code Integer}
   1.790 +     * object equal to the value of:
   1.791 +     *
   1.792 +     * <blockquote>
   1.793 +     *  {@code getInteger(nm, null)}
   1.794 +     * </blockquote>
   1.795 +     *
   1.796 +     * @param   nm   property name.
   1.797 +     * @return  the {@code Integer} value of the property.
   1.798 +     * @see     java.lang.System#getProperty(java.lang.String)
   1.799 +     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
   1.800 +     */
   1.801 +    public static Integer getInteger(String nm) {
   1.802 +        return getInteger(nm, null);
   1.803 +    }
   1.804 +
   1.805 +    /**
   1.806 +     * Determines the integer value of the system property with the
   1.807 +     * specified name.
   1.808 +     *
   1.809 +     * <p>The first argument is treated as the name of a system property.
   1.810 +     * System properties are accessible through the {@link
   1.811 +     * java.lang.System#getProperty(java.lang.String)} method. The
   1.812 +     * string value of this property is then interpreted as an integer
   1.813 +     * value and an {@code Integer} object representing this value is
   1.814 +     * returned. Details of possible numeric formats can be found with
   1.815 +     * the definition of {@code getProperty}.
   1.816 +     *
   1.817 +     * <p>The second argument is the default value. An {@code Integer} object
   1.818 +     * that represents the value of the second argument is returned if there
   1.819 +     * is no property of the specified name, if the property does not have
   1.820 +     * the correct numeric format, or if the specified name is empty or
   1.821 +     * {@code null}.
   1.822 +     *
   1.823 +     * <p>In other words, this method returns an {@code Integer} object
   1.824 +     * equal to the value of:
   1.825 +     *
   1.826 +     * <blockquote>
   1.827 +     *  {@code getInteger(nm, new Integer(val))}
   1.828 +     * </blockquote>
   1.829 +     *
   1.830 +     * but in practice it may be implemented in a manner such as:
   1.831 +     *
   1.832 +     * <blockquote><pre>
   1.833 +     * Integer result = getInteger(nm, null);
   1.834 +     * return (result == null) ? new Integer(val) : result;
   1.835 +     * </pre></blockquote>
   1.836 +     *
   1.837 +     * to avoid the unnecessary allocation of an {@code Integer}
   1.838 +     * object when the default value is not needed.
   1.839 +     *
   1.840 +     * @param   nm   property name.
   1.841 +     * @param   val   default value.
   1.842 +     * @return  the {@code Integer} value of the property.
   1.843 +     * @see     java.lang.System#getProperty(java.lang.String)
   1.844 +     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
   1.845 +     */
   1.846 +    public static Integer getInteger(String nm, int val) {
   1.847 +        Integer result = getInteger(nm, null);
   1.848 +        return (result == null) ? Integer.valueOf(val) : result;
   1.849 +    }
   1.850 +
   1.851 +    /**
   1.852 +     * Returns the integer value of the system property with the
   1.853 +     * specified name.  The first argument is treated as the name of a
   1.854 +     * system property.  System properties are accessible through the
   1.855 +     * {@link java.lang.System#getProperty(java.lang.String)} method.
   1.856 +     * The string value of this property is then interpreted as an
   1.857 +     * integer value, as per the {@code Integer.decode} method,
   1.858 +     * and an {@code Integer} object representing this value is
   1.859 +     * returned.
   1.860 +     *
   1.861 +     * <ul><li>If the property value begins with the two ASCII characters
   1.862 +     *         {@code 0x} or the ASCII character {@code #}, not
   1.863 +     *      followed by a minus sign, then the rest of it is parsed as a
   1.864 +     *      hexadecimal integer exactly as by the method
   1.865 +     *      {@link #valueOf(java.lang.String, int)} with radix 16.
   1.866 +     * <li>If the property value begins with the ASCII character
   1.867 +     *     {@code 0} followed by another character, it is parsed as an
   1.868 +     *     octal integer exactly as by the method
   1.869 +     *     {@link #valueOf(java.lang.String, int)} with radix 8.
   1.870 +     * <li>Otherwise, the property value is parsed as a decimal integer
   1.871 +     * exactly as by the method {@link #valueOf(java.lang.String, int)}
   1.872 +     * with radix 10.
   1.873 +     * </ul>
   1.874 +     *
   1.875 +     * <p>The second argument is the default value. The default value is
   1.876 +     * returned if there is no property of the specified name, if the
   1.877 +     * property does not have the correct numeric format, or if the
   1.878 +     * specified name is empty or {@code null}.
   1.879 +     *
   1.880 +     * @param   nm   property name.
   1.881 +     * @param   val   default value.
   1.882 +     * @return  the {@code Integer} value of the property.
   1.883 +     * @see     java.lang.System#getProperty(java.lang.String)
   1.884 +     * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
   1.885 +     * @see java.lang.Integer#decode
   1.886 +     */
   1.887 +    public static Integer getInteger(String nm, Integer val) {
   1.888 +        String v = null;
   1.889 +        try {
   1.890 +            v = System.getProperty(nm);
   1.891 +        } catch (IllegalArgumentException e) {
   1.892 +        } catch (NullPointerException e) {
   1.893 +        }
   1.894 +        if (v != null) {
   1.895 +            try {
   1.896 +                return Integer.decode(v);
   1.897 +            } catch (NumberFormatException e) {
   1.898 +            }
   1.899 +        }
   1.900 +        return val;
   1.901 +    }
   1.902 +
   1.903 +    /**
   1.904 +     * Decodes a {@code String} into an {@code Integer}.
   1.905 +     * Accepts decimal, hexadecimal, and octal numbers given
   1.906 +     * by the following grammar:
   1.907 +     *
   1.908 +     * <blockquote>
   1.909 +     * <dl>
   1.910 +     * <dt><i>DecodableString:</i>
   1.911 +     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
   1.912 +     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
   1.913 +     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
   1.914 +     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
   1.915 +     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
   1.916 +     * <p>
   1.917 +     * <dt><i>Sign:</i>
   1.918 +     * <dd>{@code -}
   1.919 +     * <dd>{@code +}
   1.920 +     * </dl>
   1.921 +     * </blockquote>
   1.922 +     *
   1.923 +     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
   1.924 +     * are as defined in section 3.10.1 of
   1.925 +     * <cite>The Java&trade; Language Specification</cite>,
   1.926 +     * except that underscores are not accepted between digits.
   1.927 +     *
   1.928 +     * <p>The sequence of characters following an optional
   1.929 +     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
   1.930 +     * "{@code #}", or leading zero) is parsed as by the {@code
   1.931 +     * Integer.parseInt} method with the indicated radix (10, 16, or
   1.932 +     * 8).  This sequence of characters must represent a positive
   1.933 +     * value or a {@link NumberFormatException} will be thrown.  The
   1.934 +     * result is negated if first character of the specified {@code
   1.935 +     * String} is the minus sign.  No whitespace characters are
   1.936 +     * permitted in the {@code String}.
   1.937 +     *
   1.938 +     * @param     nm the {@code String} to decode.
   1.939 +     * @return    an {@code Integer} object holding the {@code int}
   1.940 +     *             value represented by {@code nm}
   1.941 +     * @exception NumberFormatException  if the {@code String} does not
   1.942 +     *            contain a parsable integer.
   1.943 +     * @see java.lang.Integer#parseInt(java.lang.String, int)
   1.944 +     */
   1.945 +    public static Integer decode(String nm) throws NumberFormatException {
   1.946 +        int radix = 10;
   1.947 +        int index = 0;
   1.948 +        boolean negative = false;
   1.949 +        Integer result;
   1.950 +
   1.951 +        if (nm.length() == 0)
   1.952 +            throw new NumberFormatException("Zero length string");
   1.953 +        char firstChar = nm.charAt(0);
   1.954 +        // Handle sign, if present
   1.955 +        if (firstChar == '-') {
   1.956 +            negative = true;
   1.957 +            index++;
   1.958 +        } else if (firstChar == '+')
   1.959 +            index++;
   1.960 +
   1.961 +        // Handle radix specifier, if present
   1.962 +        if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
   1.963 +            index += 2;
   1.964 +            radix = 16;
   1.965 +        }
   1.966 +        else if (nm.startsWith("#", index)) {
   1.967 +            index ++;
   1.968 +            radix = 16;
   1.969 +        }
   1.970 +        else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
   1.971 +            index ++;
   1.972 +            radix = 8;
   1.973 +        }
   1.974 +
   1.975 +        if (nm.startsWith("-", index) || nm.startsWith("+", index))
   1.976 +            throw new NumberFormatException("Sign character in wrong position");
   1.977 +
   1.978 +        try {
   1.979 +            result = Integer.valueOf(nm.substring(index), radix);
   1.980 +            result = negative ? Integer.valueOf(-result.intValue()) : result;
   1.981 +        } catch (NumberFormatException e) {
   1.982 +            // If number is Integer.MIN_VALUE, we'll end up here. The next line
   1.983 +            // handles this case, and causes any genuine format error to be
   1.984 +            // rethrown.
   1.985 +            String constant = negative ? ("-" + nm.substring(index))
   1.986 +                                       : nm.substring(index);
   1.987 +            result = Integer.valueOf(constant, radix);
   1.988 +        }
   1.989 +        return result;
   1.990 +    }
   1.991 +
   1.992 +    /**
   1.993 +     * Compares two {@code Integer} objects numerically.
   1.994 +     *
   1.995 +     * @param   anotherInteger   the {@code Integer} to be compared.
   1.996 +     * @return  the value {@code 0} if this {@code Integer} is
   1.997 +     *          equal to the argument {@code Integer}; a value less than
   1.998 +     *          {@code 0} if this {@code Integer} is numerically less
   1.999 +     *          than the argument {@code Integer}; and a value greater
  1.1000 +     *          than {@code 0} if this {@code Integer} is numerically
  1.1001 +     *           greater than the argument {@code Integer} (signed
  1.1002 +     *           comparison).
  1.1003 +     * @since   1.2
  1.1004 +     */
  1.1005 +    public int compareTo(Integer anotherInteger) {
  1.1006 +        return compare(this.value, anotherInteger.value);
  1.1007 +    }
  1.1008 +
  1.1009 +    /**
  1.1010 +     * Compares two {@code int} values numerically.
  1.1011 +     * The value returned is identical to what would be returned by:
  1.1012 +     * <pre>
  1.1013 +     *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
  1.1014 +     * </pre>
  1.1015 +     *
  1.1016 +     * @param  x the first {@code int} to compare
  1.1017 +     * @param  y the second {@code int} to compare
  1.1018 +     * @return the value {@code 0} if {@code x == y};
  1.1019 +     *         a value less than {@code 0} if {@code x < y}; and
  1.1020 +     *         a value greater than {@code 0} if {@code x > y}
  1.1021 +     * @since 1.7
  1.1022 +     */
  1.1023 +    public static int compare(int x, int y) {
  1.1024 +        return (x < y) ? -1 : ((x == y) ? 0 : 1);
  1.1025 +    }
  1.1026 +
  1.1027 +
  1.1028 +    // Bit twiddling
  1.1029 +
  1.1030 +    /**
  1.1031 +     * The number of bits used to represent an {@code int} value in two's
  1.1032 +     * complement binary form.
  1.1033 +     *
  1.1034 +     * @since 1.5
  1.1035 +     */
  1.1036 +    public static final int SIZE = 32;
  1.1037 +
  1.1038 +    /**
  1.1039 +     * Returns an {@code int} value with at most a single one-bit, in the
  1.1040 +     * position of the highest-order ("leftmost") one-bit in the specified
  1.1041 +     * {@code int} value.  Returns zero if the specified value has no
  1.1042 +     * one-bits in its two's complement binary representation, that is, if it
  1.1043 +     * is equal to zero.
  1.1044 +     *
  1.1045 +     * @return an {@code int} value with a single one-bit, in the position
  1.1046 +     *     of the highest-order one-bit in the specified value, or zero if
  1.1047 +     *     the specified value is itself equal to zero.
  1.1048 +     * @since 1.5
  1.1049 +     */
  1.1050 +    public static int highestOneBit(int i) {
  1.1051 +        // HD, Figure 3-1
  1.1052 +        i |= (i >>  1);
  1.1053 +        i |= (i >>  2);
  1.1054 +        i |= (i >>  4);
  1.1055 +        i |= (i >>  8);
  1.1056 +        i |= (i >> 16);
  1.1057 +        return i - (i >>> 1);
  1.1058 +    }
  1.1059 +
  1.1060 +    /**
  1.1061 +     * Returns an {@code int} value with at most a single one-bit, in the
  1.1062 +     * position of the lowest-order ("rightmost") one-bit in the specified
  1.1063 +     * {@code int} value.  Returns zero if the specified value has no
  1.1064 +     * one-bits in its two's complement binary representation, that is, if it
  1.1065 +     * is equal to zero.
  1.1066 +     *
  1.1067 +     * @return an {@code int} value with a single one-bit, in the position
  1.1068 +     *     of the lowest-order one-bit in the specified value, or zero if
  1.1069 +     *     the specified value is itself equal to zero.
  1.1070 +     * @since 1.5
  1.1071 +     */
  1.1072 +    public static int lowestOneBit(int i) {
  1.1073 +        // HD, Section 2-1
  1.1074 +        return i & -i;
  1.1075 +    }
  1.1076 +
  1.1077 +    /**
  1.1078 +     * Returns the number of zero bits preceding the highest-order
  1.1079 +     * ("leftmost") one-bit in the two's complement binary representation
  1.1080 +     * of the specified {@code int} value.  Returns 32 if the
  1.1081 +     * specified value has no one-bits in its two's complement representation,
  1.1082 +     * in other words if it is equal to zero.
  1.1083 +     *
  1.1084 +     * <p>Note that this method is closely related to the logarithm base 2.
  1.1085 +     * For all positive {@code int} values x:
  1.1086 +     * <ul>
  1.1087 +     * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
  1.1088 +     * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
  1.1089 +     * </ul>
  1.1090 +     *
  1.1091 +     * @return the number of zero bits preceding the highest-order
  1.1092 +     *     ("leftmost") one-bit in the two's complement binary representation
  1.1093 +     *     of the specified {@code int} value, or 32 if the value
  1.1094 +     *     is equal to zero.
  1.1095 +     * @since 1.5
  1.1096 +     */
  1.1097 +    public static int numberOfLeadingZeros(int i) {
  1.1098 +        // HD, Figure 5-6
  1.1099 +        if (i == 0)
  1.1100 +            return 32;
  1.1101 +        int n = 1;
  1.1102 +        if (i >>> 16 == 0) { n += 16; i <<= 16; }
  1.1103 +        if (i >>> 24 == 0) { n +=  8; i <<=  8; }
  1.1104 +        if (i >>> 28 == 0) { n +=  4; i <<=  4; }
  1.1105 +        if (i >>> 30 == 0) { n +=  2; i <<=  2; }
  1.1106 +        n -= i >>> 31;
  1.1107 +        return n;
  1.1108 +    }
  1.1109 +
  1.1110 +    /**
  1.1111 +     * Returns the number of zero bits following the lowest-order ("rightmost")
  1.1112 +     * one-bit in the two's complement binary representation of the specified
  1.1113 +     * {@code int} value.  Returns 32 if the specified value has no
  1.1114 +     * one-bits in its two's complement representation, in other words if it is
  1.1115 +     * equal to zero.
  1.1116 +     *
  1.1117 +     * @return the number of zero bits following the lowest-order ("rightmost")
  1.1118 +     *     one-bit in the two's complement binary representation of the
  1.1119 +     *     specified {@code int} value, or 32 if the value is equal
  1.1120 +     *     to zero.
  1.1121 +     * @since 1.5
  1.1122 +     */
  1.1123 +    public static int numberOfTrailingZeros(int i) {
  1.1124 +        // HD, Figure 5-14
  1.1125 +        int y;
  1.1126 +        if (i == 0) return 32;
  1.1127 +        int n = 31;
  1.1128 +        y = i <<16; if (y != 0) { n = n -16; i = y; }
  1.1129 +        y = i << 8; if (y != 0) { n = n - 8; i = y; }
  1.1130 +        y = i << 4; if (y != 0) { n = n - 4; i = y; }
  1.1131 +        y = i << 2; if (y != 0) { n = n - 2; i = y; }
  1.1132 +        return n - ((i << 1) >>> 31);
  1.1133 +    }
  1.1134 +
  1.1135 +    /**
  1.1136 +     * Returns the number of one-bits in the two's complement binary
  1.1137 +     * representation of the specified {@code int} value.  This function is
  1.1138 +     * sometimes referred to as the <i>population count</i>.
  1.1139 +     *
  1.1140 +     * @return the number of one-bits in the two's complement binary
  1.1141 +     *     representation of the specified {@code int} value.
  1.1142 +     * @since 1.5
  1.1143 +     */
  1.1144 +    public static int bitCount(int i) {
  1.1145 +        // HD, Figure 5-2
  1.1146 +        i = i - ((i >>> 1) & 0x55555555);
  1.1147 +        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
  1.1148 +        i = (i + (i >>> 4)) & 0x0f0f0f0f;
  1.1149 +        i = i + (i >>> 8);
  1.1150 +        i = i + (i >>> 16);
  1.1151 +        return i & 0x3f;
  1.1152 +    }
  1.1153 +
  1.1154 +    /**
  1.1155 +     * Returns the value obtained by rotating the two's complement binary
  1.1156 +     * representation of the specified {@code int} value left by the
  1.1157 +     * specified number of bits.  (Bits shifted out of the left hand, or
  1.1158 +     * high-order, side reenter on the right, or low-order.)
  1.1159 +     *
  1.1160 +     * <p>Note that left rotation with a negative distance is equivalent to
  1.1161 +     * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
  1.1162 +     * distance)}.  Note also that rotation by any multiple of 32 is a
  1.1163 +     * no-op, so all but the last five bits of the rotation distance can be
  1.1164 +     * ignored, even if the distance is negative: {@code rotateLeft(val,
  1.1165 +     * distance) == rotateLeft(val, distance & 0x1F)}.
  1.1166 +     *
  1.1167 +     * @return the value obtained by rotating the two's complement binary
  1.1168 +     *     representation of the specified {@code int} value left by the
  1.1169 +     *     specified number of bits.
  1.1170 +     * @since 1.5
  1.1171 +     */
  1.1172 +    public static int rotateLeft(int i, int distance) {
  1.1173 +        return (i << distance) | (i >>> -distance);
  1.1174 +    }
  1.1175 +
  1.1176 +    /**
  1.1177 +     * Returns the value obtained by rotating the two's complement binary
  1.1178 +     * representation of the specified {@code int} value right by the
  1.1179 +     * specified number of bits.  (Bits shifted out of the right hand, or
  1.1180 +     * low-order, side reenter on the left, or high-order.)
  1.1181 +     *
  1.1182 +     * <p>Note that right rotation with a negative distance is equivalent to
  1.1183 +     * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
  1.1184 +     * distance)}.  Note also that rotation by any multiple of 32 is a
  1.1185 +     * no-op, so all but the last five bits of the rotation distance can be
  1.1186 +     * ignored, even if the distance is negative: {@code rotateRight(val,
  1.1187 +     * distance) == rotateRight(val, distance & 0x1F)}.
  1.1188 +     *
  1.1189 +     * @return the value obtained by rotating the two's complement binary
  1.1190 +     *     representation of the specified {@code int} value right by the
  1.1191 +     *     specified number of bits.
  1.1192 +     * @since 1.5
  1.1193 +     */
  1.1194 +    public static int rotateRight(int i, int distance) {
  1.1195 +        return (i >>> distance) | (i << -distance);
  1.1196 +    }
  1.1197 +
  1.1198 +    /**
  1.1199 +     * Returns the value obtained by reversing the order of the bits in the
  1.1200 +     * two's complement binary representation of the specified {@code int}
  1.1201 +     * value.
  1.1202 +     *
  1.1203 +     * @return the value obtained by reversing order of the bits in the
  1.1204 +     *     specified {@code int} value.
  1.1205 +     * @since 1.5
  1.1206 +     */
  1.1207 +    public static int reverse(int i) {
  1.1208 +        // HD, Figure 7-1
  1.1209 +        i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
  1.1210 +        i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
  1.1211 +        i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
  1.1212 +        i = (i << 24) | ((i & 0xff00) << 8) |
  1.1213 +            ((i >>> 8) & 0xff00) | (i >>> 24);
  1.1214 +        return i;
  1.1215 +    }
  1.1216 +
  1.1217 +    /**
  1.1218 +     * Returns the signum function of the specified {@code int} value.  (The
  1.1219 +     * return value is -1 if the specified value is negative; 0 if the
  1.1220 +     * specified value is zero; and 1 if the specified value is positive.)
  1.1221 +     *
  1.1222 +     * @return the signum function of the specified {@code int} value.
  1.1223 +     * @since 1.5
  1.1224 +     */
  1.1225 +    public static int signum(int i) {
  1.1226 +        // HD, Section 2-7
  1.1227 +        return (i >> 31) | (-i >>> 31);
  1.1228 +    }
  1.1229 +
  1.1230 +    /**
  1.1231 +     * Returns the value obtained by reversing the order of the bytes in the
  1.1232 +     * two's complement representation of the specified {@code int} value.
  1.1233 +     *
  1.1234 +     * @return the value obtained by reversing the bytes in the specified
  1.1235 +     *     {@code int} value.
  1.1236 +     * @since 1.5
  1.1237 +     */
  1.1238 +    public static int reverseBytes(int i) {
  1.1239 +        return ((i >>> 24)           ) |
  1.1240 +               ((i >>   8) &   0xFF00) |
  1.1241 +               ((i <<   8) & 0xFF0000) |
  1.1242 +               ((i << 24));
  1.1243 +    }
  1.1244 +
  1.1245 +    /** use serialVersionUID from JDK 1.0.2 for interoperability */
  1.1246 +    private static final long serialVersionUID = 1360826667806852920L;
  1.1247 +}