1.1 --- a/emul/mini/src/main/java/java/lang/Integer.java Mon Feb 25 19:00:08 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,1246 +0,0 @@
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 org.apidesign.bck2brwsr.core.JavaScriptBody;
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>'\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>'\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>'\u0030'</code> through
1.113 - * <code>'\u0039'</code> and <code>'\u0061'</code> through
1.114 - * <code>'\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 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 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>'\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>'\u0030'</code> through
1.183 - * <code>'\u0039'</code> and <code>'\u0061'</code> through
1.184 - * <code>'\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 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 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 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>'\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>'\u0030'</code> through
1.222 - * <code>'\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 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 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 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>'\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>'\u0030'</code>) and {@code '1'}
1.247 - * (<code>'\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 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 10.
1.329 - */
1.330 - @JavaScriptBody(args = "i", body = "return i.toString();")
1.331 - public static String toString(int i) {
1.332 - if (i == Integer.MIN_VALUE)
1.333 - return "-2147483648";
1.334 - int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
1.335 - char[] buf = new char[size];
1.336 - getChars(i, size, buf);
1.337 - return new String(buf, 0, size);
1.338 - }
1.339 -
1.340 - /**
1.341 - * Places characters representing the integer i into the
1.342 - * character array buf. The characters are placed into
1.343 - * the buffer backwards starting with the least significant
1.344 - * digit at the specified index (exclusive), and working
1.345 - * backwards from there.
1.346 - *
1.347 - * Will fail if i == Integer.MIN_VALUE
1.348 - */
1.349 - static void getChars(int i, int index, char[] buf) {
1.350 - int q, r;
1.351 - int charPos = index;
1.352 - char sign = 0;
1.353 -
1.354 - if (i < 0) {
1.355 - sign = '-';
1.356 - i = -i;
1.357 - }
1.358 -
1.359 - // Generate two digits per iteration
1.360 - while (i >= 65536) {
1.361 - q = i / 100;
1.362 - // really: r = i - (q * 100);
1.363 - r = i - ((q << 6) + (q << 5) + (q << 2));
1.364 - i = q;
1.365 - buf [--charPos] = DigitOnes[r];
1.366 - buf [--charPos] = DigitTens[r];
1.367 - }
1.368 -
1.369 - // Fall thru to fast mode for smaller numbers
1.370 - // assert(i <= 65536, i);
1.371 - for (;;) {
1.372 - q = (i * 52429) >>> (16+3);
1.373 - r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
1.374 - buf [--charPos] = digits [r];
1.375 - i = q;
1.376 - if (i == 0) break;
1.377 - }
1.378 - if (sign != 0) {
1.379 - buf [--charPos] = sign;
1.380 - }
1.381 - }
1.382 -
1.383 - final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
1.384 - 99999999, 999999999, Integer.MAX_VALUE };
1.385 -
1.386 - // Requires positive x
1.387 - static int stringSize(int x) {
1.388 - for (int i=0; ; i++)
1.389 - if (x <= sizeTable[i])
1.390 - return i+1;
1.391 - }
1.392 -
1.393 - /**
1.394 - * Parses the string argument as a signed integer in the radix
1.395 - * specified by the second argument. The characters in the string
1.396 - * must all be digits of the specified radix (as determined by
1.397 - * whether {@link java.lang.Character#digit(char, int)} returns a
1.398 - * nonnegative value), except that the first character may be an
1.399 - * ASCII minus sign {@code '-'} (<code>'\u002D'</code>) to
1.400 - * indicate a negative value or an ASCII plus sign {@code '+'}
1.401 - * (<code>'\u002B'</code>) to indicate a positive value. The
1.402 - * resulting integer value is returned.
1.403 - *
1.404 - * <p>An exception of type {@code NumberFormatException} is
1.405 - * thrown if any of the following situations occurs:
1.406 - * <ul>
1.407 - * <li>The first argument is {@code null} or is a string of
1.408 - * length zero.
1.409 - *
1.410 - * <li>The radix is either smaller than
1.411 - * {@link java.lang.Character#MIN_RADIX} or
1.412 - * larger than {@link java.lang.Character#MAX_RADIX}.
1.413 - *
1.414 - * <li>Any character of the string is not a digit of the specified
1.415 - * radix, except that the first character may be a minus sign
1.416 - * {@code '-'} (<code>'\u002D'</code>) or plus sign
1.417 - * {@code '+'} (<code>'\u002B'</code>) provided that the
1.418 - * string is longer than length 1.
1.419 - *
1.420 - * <li>The value represented by the string is not a value of type
1.421 - * {@code int}.
1.422 - * </ul>
1.423 - *
1.424 - * <p>Examples:
1.425 - * <blockquote><pre>
1.426 - * parseInt("0", 10) returns 0
1.427 - * parseInt("473", 10) returns 473
1.428 - * parseInt("+42", 10) returns 42
1.429 - * parseInt("-0", 10) returns 0
1.430 - * parseInt("-FF", 16) returns -255
1.431 - * parseInt("1100110", 2) returns 102
1.432 - * parseInt("2147483647", 10) returns 2147483647
1.433 - * parseInt("-2147483648", 10) returns -2147483648
1.434 - * parseInt("2147483648", 10) throws a NumberFormatException
1.435 - * parseInt("99", 8) throws a NumberFormatException
1.436 - * parseInt("Kona", 10) throws a NumberFormatException
1.437 - * parseInt("Kona", 27) returns 411787
1.438 - * </pre></blockquote>
1.439 - *
1.440 - * @param s the {@code String} containing the integer
1.441 - * representation to be parsed
1.442 - * @param radix the radix to be used while parsing {@code s}.
1.443 - * @return the integer represented by the string argument in the
1.444 - * specified radix.
1.445 - * @exception NumberFormatException if the {@code String}
1.446 - * does not contain a parsable {@code int}.
1.447 - */
1.448 - @JavaScriptBody(args={"s", "radix"}, body="return parseInt(s,radix);")
1.449 - public static int parseInt(String s, int radix)
1.450 - throws NumberFormatException
1.451 - {
1.452 - /*
1.453 - * WARNING: This method may be invoked early during VM initialization
1.454 - * before IntegerCache is initialized. Care must be taken to not use
1.455 - * the valueOf method.
1.456 - */
1.457 -
1.458 - if (s == null) {
1.459 - throw new NumberFormatException("null");
1.460 - }
1.461 -
1.462 - if (radix < Character.MIN_RADIX) {
1.463 - throw new NumberFormatException("radix " + radix +
1.464 - " less than Character.MIN_RADIX");
1.465 - }
1.466 -
1.467 - if (radix > Character.MAX_RADIX) {
1.468 - throw new NumberFormatException("radix " + radix +
1.469 - " greater than Character.MAX_RADIX");
1.470 - }
1.471 -
1.472 - int result = 0;
1.473 - boolean negative = false;
1.474 - int i = 0, len = s.length();
1.475 - int limit = -Integer.MAX_VALUE;
1.476 - int multmin;
1.477 - int digit;
1.478 -
1.479 - if (len > 0) {
1.480 - char firstChar = s.charAt(0);
1.481 - if (firstChar < '0') { // Possible leading "+" or "-"
1.482 - if (firstChar == '-') {
1.483 - negative = true;
1.484 - limit = Integer.MIN_VALUE;
1.485 - } else if (firstChar != '+')
1.486 - throw NumberFormatException.forInputString(s);
1.487 -
1.488 - if (len == 1) // Cannot have lone "+" or "-"
1.489 - throw NumberFormatException.forInputString(s);
1.490 - i++;
1.491 - }
1.492 - multmin = limit / radix;
1.493 - while (i < len) {
1.494 - // Accumulating negatively avoids surprises near MAX_VALUE
1.495 - digit = Character.digit(s.charAt(i++),radix);
1.496 - if (digit < 0) {
1.497 - throw NumberFormatException.forInputString(s);
1.498 - }
1.499 - if (result < multmin) {
1.500 - throw NumberFormatException.forInputString(s);
1.501 - }
1.502 - result *= radix;
1.503 - if (result < limit + digit) {
1.504 - throw NumberFormatException.forInputString(s);
1.505 - }
1.506 - result -= digit;
1.507 - }
1.508 - } else {
1.509 - throw NumberFormatException.forInputString(s);
1.510 - }
1.511 - return negative ? result : -result;
1.512 - }
1.513 -
1.514 - /**
1.515 - * Parses the string argument as a signed decimal integer. The
1.516 - * characters in the string must all be decimal digits, except
1.517 - * that the first character may be an ASCII minus sign {@code '-'}
1.518 - * (<code>'\u002D'</code>) to indicate a negative value or an
1.519 - * ASCII plus sign {@code '+'} (<code>'\u002B'</code>) to
1.520 - * indicate a positive value. The resulting integer value is
1.521 - * returned, exactly as if the argument and the radix 10 were
1.522 - * given as arguments to the {@link #parseInt(java.lang.String,
1.523 - * int)} method.
1.524 - *
1.525 - * @param s a {@code String} containing the {@code int}
1.526 - * representation to be parsed
1.527 - * @return the integer value represented by the argument in decimal.
1.528 - * @exception NumberFormatException if the string does not contain a
1.529 - * parsable integer.
1.530 - */
1.531 - public static int parseInt(String s) throws NumberFormatException {
1.532 - return parseInt(s,10);
1.533 - }
1.534 -
1.535 - /**
1.536 - * Returns an {@code Integer} object holding the value
1.537 - * extracted from the specified {@code String} when parsed
1.538 - * with the radix given by the second argument. The first argument
1.539 - * is interpreted as representing a signed integer in the radix
1.540 - * specified by the second argument, exactly as if the arguments
1.541 - * were given to the {@link #parseInt(java.lang.String, int)}
1.542 - * method. The result is an {@code Integer} object that
1.543 - * represents the integer value specified by the string.
1.544 - *
1.545 - * <p>In other words, this method returns an {@code Integer}
1.546 - * object equal to the value of:
1.547 - *
1.548 - * <blockquote>
1.549 - * {@code new Integer(Integer.parseInt(s, radix))}
1.550 - * </blockquote>
1.551 - *
1.552 - * @param s the string to be parsed.
1.553 - * @param radix the radix to be used in interpreting {@code s}
1.554 - * @return an {@code Integer} object holding the value
1.555 - * represented by the string argument in the specified
1.556 - * radix.
1.557 - * @exception NumberFormatException if the {@code String}
1.558 - * does not contain a parsable {@code int}.
1.559 - */
1.560 - public static Integer valueOf(String s, int radix) throws NumberFormatException {
1.561 - return Integer.valueOf(parseInt(s,radix));
1.562 - }
1.563 -
1.564 - /**
1.565 - * Returns an {@code Integer} object holding the
1.566 - * value of the specified {@code String}. The argument is
1.567 - * interpreted as representing a signed decimal integer, exactly
1.568 - * as if the argument were given to the {@link
1.569 - * #parseInt(java.lang.String)} method. The result is an
1.570 - * {@code Integer} object that represents the integer value
1.571 - * specified by the string.
1.572 - *
1.573 - * <p>In other words, this method returns an {@code Integer}
1.574 - * object equal to the value of:
1.575 - *
1.576 - * <blockquote>
1.577 - * {@code new Integer(Integer.parseInt(s))}
1.578 - * </blockquote>
1.579 - *
1.580 - * @param s the string to be parsed.
1.581 - * @return an {@code Integer} object holding the value
1.582 - * represented by the string argument.
1.583 - * @exception NumberFormatException if the string cannot be parsed
1.584 - * as an integer.
1.585 - */
1.586 - public static Integer valueOf(String s) throws NumberFormatException {
1.587 - return Integer.valueOf(parseInt(s, 10));
1.588 - }
1.589 -
1.590 - /**
1.591 - * Cache to support the object identity semantics of autoboxing for values between
1.592 - * -128 and 127 (inclusive) as required by JLS.
1.593 - *
1.594 - * The cache is initialized on first usage. The size of the cache
1.595 - * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
1.596 - * During VM initialization, java.lang.Integer.IntegerCache.high property
1.597 - * may be set and saved in the private system properties in the
1.598 - * sun.misc.VM class.
1.599 - */
1.600 -
1.601 - private static class IntegerCache {
1.602 - static final int low = -128;
1.603 - static final int high;
1.604 - static final Integer cache[];
1.605 -
1.606 - static {
1.607 - // high value may be configured by property
1.608 - int h = 127;
1.609 - String integerCacheHighPropValue =
1.610 - AbstractStringBuilder.getProperty("java.lang.Integer.IntegerCache.high");
1.611 - if (integerCacheHighPropValue != null) {
1.612 - int i = parseInt(integerCacheHighPropValue);
1.613 - i = Math.max(i, 127);
1.614 - // Maximum array size is Integer.MAX_VALUE
1.615 - h = Math.min(i, Integer.MAX_VALUE - (-low));
1.616 - }
1.617 - high = h;
1.618 -
1.619 - cache = new Integer[(high - low) + 1];
1.620 - int j = low;
1.621 - for(int k = 0; k < cache.length; k++)
1.622 - cache[k] = new Integer(j++);
1.623 - }
1.624 -
1.625 - private IntegerCache() {}
1.626 - }
1.627 -
1.628 - /**
1.629 - * Returns an {@code Integer} instance representing the specified
1.630 - * {@code int} value. If a new {@code Integer} instance is not
1.631 - * required, this method should generally be used in preference to
1.632 - * the constructor {@link #Integer(int)}, as this method is likely
1.633 - * to yield significantly better space and time performance by
1.634 - * caching frequently requested values.
1.635 - *
1.636 - * This method will always cache values in the range -128 to 127,
1.637 - * inclusive, and may cache other values outside of this range.
1.638 - *
1.639 - * @param i an {@code int} value.
1.640 - * @return an {@code Integer} instance representing {@code i}.
1.641 - * @since 1.5
1.642 - */
1.643 - public static Integer valueOf(int i) {
1.644 - //assert IntegerCache.high >= 127;
1.645 - if (i >= IntegerCache.low && i <= IntegerCache.high)
1.646 - return IntegerCache.cache[i + (-IntegerCache.low)];
1.647 - return new Integer(i);
1.648 - }
1.649 -
1.650 - /**
1.651 - * The value of the {@code Integer}.
1.652 - *
1.653 - * @serial
1.654 - */
1.655 - private final int value;
1.656 -
1.657 - /**
1.658 - * Constructs a newly allocated {@code Integer} object that
1.659 - * represents the specified {@code int} value.
1.660 - *
1.661 - * @param value the value to be represented by the
1.662 - * {@code Integer} object.
1.663 - */
1.664 - public Integer(int value) {
1.665 - this.value = value;
1.666 - }
1.667 -
1.668 - /**
1.669 - * Constructs a newly allocated {@code Integer} object that
1.670 - * represents the {@code int} value indicated by the
1.671 - * {@code String} parameter. The string is converted to an
1.672 - * {@code int} value in exactly the manner used by the
1.673 - * {@code parseInt} method for radix 10.
1.674 - *
1.675 - * @param s the {@code String} to be converted to an
1.676 - * {@code Integer}.
1.677 - * @exception NumberFormatException if the {@code String} does not
1.678 - * contain a parsable integer.
1.679 - * @see java.lang.Integer#parseInt(java.lang.String, int)
1.680 - */
1.681 - public Integer(String s) throws NumberFormatException {
1.682 - this.value = parseInt(s, 10);
1.683 - }
1.684 -
1.685 - /**
1.686 - * Returns the value of this {@code Integer} as a
1.687 - * {@code byte}.
1.688 - */
1.689 - public byte byteValue() {
1.690 - return (byte)value;
1.691 - }
1.692 -
1.693 - /**
1.694 - * Returns the value of this {@code Integer} as a
1.695 - * {@code short}.
1.696 - */
1.697 - public short shortValue() {
1.698 - return (short)value;
1.699 - }
1.700 -
1.701 - /**
1.702 - * Returns the value of this {@code Integer} as an
1.703 - * {@code int}.
1.704 - */
1.705 - public int intValue() {
1.706 - return value;
1.707 - }
1.708 -
1.709 - /**
1.710 - * Returns the value of this {@code Integer} as a
1.711 - * {@code long}.
1.712 - */
1.713 - public long longValue() {
1.714 - return (long)value;
1.715 - }
1.716 -
1.717 - /**
1.718 - * Returns the value of this {@code Integer} as a
1.719 - * {@code float}.
1.720 - */
1.721 - public float floatValue() {
1.722 - return (float)value;
1.723 - }
1.724 -
1.725 - /**
1.726 - * Returns the value of this {@code Integer} as a
1.727 - * {@code double}.
1.728 - */
1.729 - public double doubleValue() {
1.730 - return (double)value;
1.731 - }
1.732 -
1.733 - /**
1.734 - * Returns a {@code String} object representing this
1.735 - * {@code Integer}'s value. The value is converted to signed
1.736 - * decimal representation and returned as a string, exactly as if
1.737 - * the integer value were given as an argument to the {@link
1.738 - * java.lang.Integer#toString(int)} method.
1.739 - *
1.740 - * @return a string representation of the value of this object in
1.741 - * base 10.
1.742 - */
1.743 - public String toString() {
1.744 - return toString(value);
1.745 - }
1.746 -
1.747 - /**
1.748 - * Returns a hash code for this {@code Integer}.
1.749 - *
1.750 - * @return a hash code value for this object, equal to the
1.751 - * primitive {@code int} value represented by this
1.752 - * {@code Integer} object.
1.753 - */
1.754 - public int hashCode() {
1.755 - return value;
1.756 - }
1.757 -
1.758 - /**
1.759 - * Compares this object to the specified object. The result is
1.760 - * {@code true} if and only if the argument is not
1.761 - * {@code null} and is an {@code Integer} object that
1.762 - * contains the same {@code int} value as this object.
1.763 - *
1.764 - * @param obj the object to compare with.
1.765 - * @return {@code true} if the objects are the same;
1.766 - * {@code false} otherwise.
1.767 - */
1.768 - public boolean equals(Object obj) {
1.769 - if (obj instanceof Integer) {
1.770 - return value == ((Integer)obj).intValue();
1.771 - }
1.772 - return false;
1.773 - }
1.774 -
1.775 - /**
1.776 - * Determines the integer value of the system property with the
1.777 - * specified name.
1.778 - *
1.779 - * <p>The first argument is treated as the name of a system property.
1.780 - * System properties are accessible through the
1.781 - * {@link java.lang.System#getProperty(java.lang.String)} method. The
1.782 - * string value of this property is then interpreted as an integer
1.783 - * value and an {@code Integer} object representing this value is
1.784 - * returned. Details of possible numeric formats can be found with
1.785 - * the definition of {@code getProperty}.
1.786 - *
1.787 - * <p>If there is no property with the specified name, if the specified name
1.788 - * is empty or {@code null}, or if the property does not have
1.789 - * the correct numeric format, then {@code null} is returned.
1.790 - *
1.791 - * <p>In other words, this method returns an {@code Integer}
1.792 - * object equal to the value of:
1.793 - *
1.794 - * <blockquote>
1.795 - * {@code getInteger(nm, null)}
1.796 - * </blockquote>
1.797 - *
1.798 - * @param nm property name.
1.799 - * @return the {@code Integer} value of the property.
1.800 - * @see java.lang.System#getProperty(java.lang.String)
1.801 - * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
1.802 - */
1.803 - public static Integer getInteger(String nm) {
1.804 - return getInteger(nm, null);
1.805 - }
1.806 -
1.807 - /**
1.808 - * Determines the integer value of the system property with the
1.809 - * specified name.
1.810 - *
1.811 - * <p>The first argument is treated as the name of a system property.
1.812 - * System properties are accessible through the {@link
1.813 - * java.lang.System#getProperty(java.lang.String)} method. The
1.814 - * string value of this property is then interpreted as an integer
1.815 - * value and an {@code Integer} object representing this value is
1.816 - * returned. Details of possible numeric formats can be found with
1.817 - * the definition of {@code getProperty}.
1.818 - *
1.819 - * <p>The second argument is the default value. An {@code Integer} object
1.820 - * that represents the value of the second argument is returned if there
1.821 - * is no property of the specified name, if the property does not have
1.822 - * the correct numeric format, or if the specified name is empty or
1.823 - * {@code null}.
1.824 - *
1.825 - * <p>In other words, this method returns an {@code Integer} object
1.826 - * equal to the value of:
1.827 - *
1.828 - * <blockquote>
1.829 - * {@code getInteger(nm, new Integer(val))}
1.830 - * </blockquote>
1.831 - *
1.832 - * but in practice it may be implemented in a manner such as:
1.833 - *
1.834 - * <blockquote><pre>
1.835 - * Integer result = getInteger(nm, null);
1.836 - * return (result == null) ? new Integer(val) : result;
1.837 - * </pre></blockquote>
1.838 - *
1.839 - * to avoid the unnecessary allocation of an {@code Integer}
1.840 - * object when the default value is not needed.
1.841 - *
1.842 - * @param nm property name.
1.843 - * @param val default value.
1.844 - * @return the {@code Integer} value of the property.
1.845 - * @see java.lang.System#getProperty(java.lang.String)
1.846 - * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
1.847 - */
1.848 - public static Integer getInteger(String nm, int val) {
1.849 - Integer result = getInteger(nm, null);
1.850 - return (result == null) ? Integer.valueOf(val) : result;
1.851 - }
1.852 -
1.853 - /**
1.854 - * Returns the integer value of the system property with the
1.855 - * specified name. The first argument is treated as the name of a
1.856 - * system property. System properties are accessible through the
1.857 - * {@link java.lang.System#getProperty(java.lang.String)} method.
1.858 - * The string value of this property is then interpreted as an
1.859 - * integer value, as per the {@code Integer.decode} method,
1.860 - * and an {@code Integer} object representing this value is
1.861 - * returned.
1.862 - *
1.863 - * <ul><li>If the property value begins with the two ASCII characters
1.864 - * {@code 0x} or the ASCII character {@code #}, not
1.865 - * followed by a minus sign, then the rest of it is parsed as a
1.866 - * hexadecimal integer exactly as by the method
1.867 - * {@link #valueOf(java.lang.String, int)} with radix 16.
1.868 - * <li>If the property value begins with the ASCII character
1.869 - * {@code 0} followed by another character, it is parsed as an
1.870 - * octal integer exactly as by the method
1.871 - * {@link #valueOf(java.lang.String, int)} with radix 8.
1.872 - * <li>Otherwise, the property value is parsed as a decimal integer
1.873 - * exactly as by the method {@link #valueOf(java.lang.String, int)}
1.874 - * with radix 10.
1.875 - * </ul>
1.876 - *
1.877 - * <p>The second argument is the default value. The default value is
1.878 - * returned if there is no property of the specified name, if the
1.879 - * property does not have the correct numeric format, or if the
1.880 - * specified name is empty or {@code null}.
1.881 - *
1.882 - * @param nm property name.
1.883 - * @param val default value.
1.884 - * @return the {@code Integer} value of the property.
1.885 - * @see java.lang.System#getProperty(java.lang.String)
1.886 - * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
1.887 - * @see java.lang.Integer#decode
1.888 - */
1.889 - public static Integer getInteger(String nm, Integer val) {
1.890 - String v = null;
1.891 - try {
1.892 - v = AbstractStringBuilder.getProperty(nm);
1.893 - } catch (IllegalArgumentException e) {
1.894 - } catch (NullPointerException e) {
1.895 - }
1.896 - if (v != null) {
1.897 - try {
1.898 - return Integer.decode(v);
1.899 - } catch (NumberFormatException e) {
1.900 - }
1.901 - }
1.902 - return val;
1.903 - }
1.904 -
1.905 - /**
1.906 - * Decodes a {@code String} into an {@code Integer}.
1.907 - * Accepts decimal, hexadecimal, and octal numbers given
1.908 - * by the following grammar:
1.909 - *
1.910 - * <blockquote>
1.911 - * <dl>
1.912 - * <dt><i>DecodableString:</i>
1.913 - * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
1.914 - * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
1.915 - * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
1.916 - * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
1.917 - * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
1.918 - * <p>
1.919 - * <dt><i>Sign:</i>
1.920 - * <dd>{@code -}
1.921 - * <dd>{@code +}
1.922 - * </dl>
1.923 - * </blockquote>
1.924 - *
1.925 - * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
1.926 - * are as defined in section 3.10.1 of
1.927 - * <cite>The Java™ Language Specification</cite>,
1.928 - * except that underscores are not accepted between digits.
1.929 - *
1.930 - * <p>The sequence of characters following an optional
1.931 - * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
1.932 - * "{@code #}", or leading zero) is parsed as by the {@code
1.933 - * Integer.parseInt} method with the indicated radix (10, 16, or
1.934 - * 8). This sequence of characters must represent a positive
1.935 - * value or a {@link NumberFormatException} will be thrown. The
1.936 - * result is negated if first character of the specified {@code
1.937 - * String} is the minus sign. No whitespace characters are
1.938 - * permitted in the {@code String}.
1.939 - *
1.940 - * @param nm the {@code String} to decode.
1.941 - * @return an {@code Integer} object holding the {@code int}
1.942 - * value represented by {@code nm}
1.943 - * @exception NumberFormatException if the {@code String} does not
1.944 - * contain a parsable integer.
1.945 - * @see java.lang.Integer#parseInt(java.lang.String, int)
1.946 - */
1.947 - public static Integer decode(String nm) throws NumberFormatException {
1.948 - int radix = 10;
1.949 - int index = 0;
1.950 - boolean negative = false;
1.951 - Integer result;
1.952 -
1.953 - if (nm.length() == 0)
1.954 - throw new NumberFormatException("Zero length string");
1.955 - char firstChar = nm.charAt(0);
1.956 - // Handle sign, if present
1.957 - if (firstChar == '-') {
1.958 - negative = true;
1.959 - index++;
1.960 - } else if (firstChar == '+')
1.961 - index++;
1.962 -
1.963 - // Handle radix specifier, if present
1.964 - if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
1.965 - index += 2;
1.966 - radix = 16;
1.967 - }
1.968 - else if (nm.startsWith("#", index)) {
1.969 - index ++;
1.970 - radix = 16;
1.971 - }
1.972 - else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
1.973 - index ++;
1.974 - radix = 8;
1.975 - }
1.976 -
1.977 - if (nm.startsWith("-", index) || nm.startsWith("+", index))
1.978 - throw new NumberFormatException("Sign character in wrong position");
1.979 -
1.980 - try {
1.981 - result = Integer.valueOf(nm.substring(index), radix);
1.982 - result = negative ? Integer.valueOf(-result.intValue()) : result;
1.983 - } catch (NumberFormatException e) {
1.984 - // If number is Integer.MIN_VALUE, we'll end up here. The next line
1.985 - // handles this case, and causes any genuine format error to be
1.986 - // rethrown.
1.987 - String constant = negative ? ("-" + nm.substring(index))
1.988 - : nm.substring(index);
1.989 - result = Integer.valueOf(constant, radix);
1.990 - }
1.991 - return result;
1.992 - }
1.993 -
1.994 - /**
1.995 - * Compares two {@code Integer} objects numerically.
1.996 - *
1.997 - * @param anotherInteger the {@code Integer} to be compared.
1.998 - * @return the value {@code 0} if this {@code Integer} is
1.999 - * equal to the argument {@code Integer}; a value less than
1.1000 - * {@code 0} if this {@code Integer} is numerically less
1.1001 - * than the argument {@code Integer}; and a value greater
1.1002 - * than {@code 0} if this {@code Integer} is numerically
1.1003 - * greater than the argument {@code Integer} (signed
1.1004 - * comparison).
1.1005 - * @since 1.2
1.1006 - */
1.1007 - public int compareTo(Integer anotherInteger) {
1.1008 - return compare(this.value, anotherInteger.value);
1.1009 - }
1.1010 -
1.1011 - /**
1.1012 - * Compares two {@code int} values numerically.
1.1013 - * The value returned is identical to what would be returned by:
1.1014 - * <pre>
1.1015 - * Integer.valueOf(x).compareTo(Integer.valueOf(y))
1.1016 - * </pre>
1.1017 - *
1.1018 - * @param x the first {@code int} to compare
1.1019 - * @param y the second {@code int} to compare
1.1020 - * @return the value {@code 0} if {@code x == y};
1.1021 - * a value less than {@code 0} if {@code x < y}; and
1.1022 - * a value greater than {@code 0} if {@code x > y}
1.1023 - * @since 1.7
1.1024 - */
1.1025 - public static int compare(int x, int y) {
1.1026 - return (x < y) ? -1 : ((x == y) ? 0 : 1);
1.1027 - }
1.1028 -
1.1029 -
1.1030 - // Bit twiddling
1.1031 -
1.1032 - /**
1.1033 - * The number of bits used to represent an {@code int} value in two's
1.1034 - * complement binary form.
1.1035 - *
1.1036 - * @since 1.5
1.1037 - */
1.1038 - public static final int SIZE = 32;
1.1039 -
1.1040 - /**
1.1041 - * Returns an {@code int} value with at most a single one-bit, in the
1.1042 - * position of the highest-order ("leftmost") one-bit in the specified
1.1043 - * {@code int} value. Returns zero if the specified value has no
1.1044 - * one-bits in its two's complement binary representation, that is, if it
1.1045 - * is equal to zero.
1.1046 - *
1.1047 - * @return an {@code int} value with a single one-bit, in the position
1.1048 - * of the highest-order one-bit in the specified value, or zero if
1.1049 - * the specified value is itself equal to zero.
1.1050 - * @since 1.5
1.1051 - */
1.1052 - public static int highestOneBit(int i) {
1.1053 - // HD, Figure 3-1
1.1054 - i |= (i >> 1);
1.1055 - i |= (i >> 2);
1.1056 - i |= (i >> 4);
1.1057 - i |= (i >> 8);
1.1058 - i |= (i >> 16);
1.1059 - return i - (i >>> 1);
1.1060 - }
1.1061 -
1.1062 - /**
1.1063 - * Returns an {@code int} value with at most a single one-bit, in the
1.1064 - * position of the lowest-order ("rightmost") one-bit in the specified
1.1065 - * {@code int} value. Returns zero if the specified value has no
1.1066 - * one-bits in its two's complement binary representation, that is, if it
1.1067 - * is equal to zero.
1.1068 - *
1.1069 - * @return an {@code int} value with a single one-bit, in the position
1.1070 - * of the lowest-order one-bit in the specified value, or zero if
1.1071 - * the specified value is itself equal to zero.
1.1072 - * @since 1.5
1.1073 - */
1.1074 - public static int lowestOneBit(int i) {
1.1075 - // HD, Section 2-1
1.1076 - return i & -i;
1.1077 - }
1.1078 -
1.1079 - /**
1.1080 - * Returns the number of zero bits preceding the highest-order
1.1081 - * ("leftmost") one-bit in the two's complement binary representation
1.1082 - * of the specified {@code int} value. Returns 32 if the
1.1083 - * specified value has no one-bits in its two's complement representation,
1.1084 - * in other words if it is equal to zero.
1.1085 - *
1.1086 - * <p>Note that this method is closely related to the logarithm base 2.
1.1087 - * For all positive {@code int} values x:
1.1088 - * <ul>
1.1089 - * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
1.1090 - * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
1.1091 - * </ul>
1.1092 - *
1.1093 - * @return the number of zero bits preceding the highest-order
1.1094 - * ("leftmost") one-bit in the two's complement binary representation
1.1095 - * of the specified {@code int} value, or 32 if the value
1.1096 - * is equal to zero.
1.1097 - * @since 1.5
1.1098 - */
1.1099 - public static int numberOfLeadingZeros(int i) {
1.1100 - // HD, Figure 5-6
1.1101 - if (i == 0)
1.1102 - return 32;
1.1103 - int n = 1;
1.1104 - if (i >>> 16 == 0) { n += 16; i <<= 16; }
1.1105 - if (i >>> 24 == 0) { n += 8; i <<= 8; }
1.1106 - if (i >>> 28 == 0) { n += 4; i <<= 4; }
1.1107 - if (i >>> 30 == 0) { n += 2; i <<= 2; }
1.1108 - n -= i >>> 31;
1.1109 - return n;
1.1110 - }
1.1111 -
1.1112 - /**
1.1113 - * Returns the number of zero bits following the lowest-order ("rightmost")
1.1114 - * one-bit in the two's complement binary representation of the specified
1.1115 - * {@code int} value. Returns 32 if the specified value has no
1.1116 - * one-bits in its two's complement representation, in other words if it is
1.1117 - * equal to zero.
1.1118 - *
1.1119 - * @return the number of zero bits following the lowest-order ("rightmost")
1.1120 - * one-bit in the two's complement binary representation of the
1.1121 - * specified {@code int} value, or 32 if the value is equal
1.1122 - * to zero.
1.1123 - * @since 1.5
1.1124 - */
1.1125 - public static int numberOfTrailingZeros(int i) {
1.1126 - // HD, Figure 5-14
1.1127 - int y;
1.1128 - if (i == 0) return 32;
1.1129 - int n = 31;
1.1130 - y = i <<16; if (y != 0) { n = n -16; i = y; }
1.1131 - y = i << 8; if (y != 0) { n = n - 8; i = y; }
1.1132 - y = i << 4; if (y != 0) { n = n - 4; i = y; }
1.1133 - y = i << 2; if (y != 0) { n = n - 2; i = y; }
1.1134 - return n - ((i << 1) >>> 31);
1.1135 - }
1.1136 -
1.1137 - /**
1.1138 - * Returns the number of one-bits in the two's complement binary
1.1139 - * representation of the specified {@code int} value. This function is
1.1140 - * sometimes referred to as the <i>population count</i>.
1.1141 - *
1.1142 - * @return the number of one-bits in the two's complement binary
1.1143 - * representation of the specified {@code int} value.
1.1144 - * @since 1.5
1.1145 - */
1.1146 - public static int bitCount(int i) {
1.1147 - // HD, Figure 5-2
1.1148 - i = i - ((i >>> 1) & 0x55555555);
1.1149 - i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
1.1150 - i = (i + (i >>> 4)) & 0x0f0f0f0f;
1.1151 - i = i + (i >>> 8);
1.1152 - i = i + (i >>> 16);
1.1153 - return i & 0x3f;
1.1154 - }
1.1155 -
1.1156 - /**
1.1157 - * Returns the value obtained by rotating the two's complement binary
1.1158 - * representation of the specified {@code int} value left by the
1.1159 - * specified number of bits. (Bits shifted out of the left hand, or
1.1160 - * high-order, side reenter on the right, or low-order.)
1.1161 - *
1.1162 - * <p>Note that left rotation with a negative distance is equivalent to
1.1163 - * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
1.1164 - * distance)}. Note also that rotation by any multiple of 32 is a
1.1165 - * no-op, so all but the last five bits of the rotation distance can be
1.1166 - * ignored, even if the distance is negative: {@code rotateLeft(val,
1.1167 - * distance) == rotateLeft(val, distance & 0x1F)}.
1.1168 - *
1.1169 - * @return the value obtained by rotating the two's complement binary
1.1170 - * representation of the specified {@code int} value left by the
1.1171 - * specified number of bits.
1.1172 - * @since 1.5
1.1173 - */
1.1174 - public static int rotateLeft(int i, int distance) {
1.1175 - return (i << distance) | (i >>> -distance);
1.1176 - }
1.1177 -
1.1178 - /**
1.1179 - * Returns the value obtained by rotating the two's complement binary
1.1180 - * representation of the specified {@code int} value right by the
1.1181 - * specified number of bits. (Bits shifted out of the right hand, or
1.1182 - * low-order, side reenter on the left, or high-order.)
1.1183 - *
1.1184 - * <p>Note that right rotation with a negative distance is equivalent to
1.1185 - * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
1.1186 - * distance)}. Note also that rotation by any multiple of 32 is a
1.1187 - * no-op, so all but the last five bits of the rotation distance can be
1.1188 - * ignored, even if the distance is negative: {@code rotateRight(val,
1.1189 - * distance) == rotateRight(val, distance & 0x1F)}.
1.1190 - *
1.1191 - * @return the value obtained by rotating the two's complement binary
1.1192 - * representation of the specified {@code int} value right by the
1.1193 - * specified number of bits.
1.1194 - * @since 1.5
1.1195 - */
1.1196 - public static int rotateRight(int i, int distance) {
1.1197 - return (i >>> distance) | (i << -distance);
1.1198 - }
1.1199 -
1.1200 - /**
1.1201 - * Returns the value obtained by reversing the order of the bits in the
1.1202 - * two's complement binary representation of the specified {@code int}
1.1203 - * value.
1.1204 - *
1.1205 - * @return the value obtained by reversing order of the bits in the
1.1206 - * specified {@code int} value.
1.1207 - * @since 1.5
1.1208 - */
1.1209 - public static int reverse(int i) {
1.1210 - // HD, Figure 7-1
1.1211 - i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
1.1212 - i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
1.1213 - i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
1.1214 - i = (i << 24) | ((i & 0xff00) << 8) |
1.1215 - ((i >>> 8) & 0xff00) | (i >>> 24);
1.1216 - return i;
1.1217 - }
1.1218 -
1.1219 - /**
1.1220 - * Returns the signum function of the specified {@code int} value. (The
1.1221 - * return value is -1 if the specified value is negative; 0 if the
1.1222 - * specified value is zero; and 1 if the specified value is positive.)
1.1223 - *
1.1224 - * @return the signum function of the specified {@code int} value.
1.1225 - * @since 1.5
1.1226 - */
1.1227 - public static int signum(int i) {
1.1228 - // HD, Section 2-7
1.1229 - return (i >> 31) | (-i >>> 31);
1.1230 - }
1.1231 -
1.1232 - /**
1.1233 - * Returns the value obtained by reversing the order of the bytes in the
1.1234 - * two's complement representation of the specified {@code int} value.
1.1235 - *
1.1236 - * @return the value obtained by reversing the bytes in the specified
1.1237 - * {@code int} value.
1.1238 - * @since 1.5
1.1239 - */
1.1240 - public static int reverseBytes(int i) {
1.1241 - return ((i >>> 24) ) |
1.1242 - ((i >> 8) & 0xFF00) |
1.1243 - ((i << 8) & 0xFF0000) |
1.1244 - ((i << 24));
1.1245 - }
1.1246 -
1.1247 - /** use serialVersionUID from JDK 1.0.2 for interoperability */
1.1248 - private static final long serialVersionUID = 1360826667806852920L;
1.1249 -}