emul/mini/src/main/java/java/lang/Integer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 26 Jan 2013 08:47:05 +0100
changeset 592 5e13b1ac2886
parent 179 469199c2994a
permissions -rw-r--r--
In order to support fields of the same name in subclasses we are now prefixing them with name of the class that defines them. To provide convenient way to access them from generated bytecode and also directly from JavaScript, there is a getter/setter function for each field. It starts with _ followed by the field name. If called with a parameter, it sets the field, with a parameter it just returns it.
jaroslav@49
     1
/*
jaroslav@49
     2
 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
jaroslav@49
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@49
     4
 *
jaroslav@49
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@49
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@49
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@49
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@49
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@49
    10
 *
jaroslav@49
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@49
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@49
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@49
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@49
    15
 * accompanied this code).
jaroslav@49
    16
 *
jaroslav@49
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@49
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@49
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@49
    20
 *
jaroslav@49
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@49
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@49
    23
 * questions.
jaroslav@49
    24
 */
jaroslav@49
    25
jaroslav@49
    26
package java.lang;
jaroslav@49
    27
jaroslav@114
    28
import org.apidesign.bck2brwsr.core.JavaScriptBody;
jaroslav@114
    29
jaroslav@49
    30
/**
jaroslav@49
    31
 * The {@code Integer} class wraps a value of the primitive type
jaroslav@49
    32
 * {@code int} in an object. An object of type {@code Integer}
jaroslav@49
    33
 * contains a single field whose type is {@code int}.
jaroslav@49
    34
 *
jaroslav@49
    35
 * <p>In addition, this class provides several methods for converting
jaroslav@49
    36
 * an {@code int} to a {@code String} and a {@code String} to an
jaroslav@49
    37
 * {@code int}, as well as other constants and methods useful when
jaroslav@49
    38
 * dealing with an {@code int}.
jaroslav@49
    39
 *
jaroslav@49
    40
 * <p>Implementation note: The implementations of the "bit twiddling"
jaroslav@49
    41
 * methods (such as {@link #highestOneBit(int) highestOneBit} and
jaroslav@49
    42
 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
jaroslav@49
    43
 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
jaroslav@49
    44
 * Delight</i>, (Addison Wesley, 2002).
jaroslav@49
    45
 *
jaroslav@49
    46
 * @author  Lee Boynton
jaroslav@49
    47
 * @author  Arthur van Hoff
jaroslav@49
    48
 * @author  Josh Bloch
jaroslav@49
    49
 * @author  Joseph D. Darcy
jaroslav@49
    50
 * @since JDK1.0
jaroslav@49
    51
 */
jaroslav@49
    52
public final class Integer extends Number implements Comparable<Integer> {
jaroslav@49
    53
    /**
jaroslav@49
    54
     * A constant holding the minimum value an {@code int} can
jaroslav@49
    55
     * have, -2<sup>31</sup>.
jaroslav@49
    56
     */
jaroslav@49
    57
    public static final int   MIN_VALUE = 0x80000000;
jaroslav@49
    58
jaroslav@49
    59
    /**
jaroslav@49
    60
     * A constant holding the maximum value an {@code int} can
jaroslav@49
    61
     * have, 2<sup>31</sup>-1.
jaroslav@49
    62
     */
jaroslav@49
    63
    public static final int   MAX_VALUE = 0x7fffffff;
jaroslav@49
    64
jaroslav@49
    65
    /**
jaroslav@49
    66
     * The {@code Class} instance representing the primitive type
jaroslav@49
    67
     * {@code int}.
jaroslav@49
    68
     *
jaroslav@49
    69
     * @since   JDK1.1
jaroslav@49
    70
     */
jaroslav@49
    71
    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
jaroslav@49
    72
jaroslav@49
    73
    /**
jaroslav@49
    74
     * All possible chars for representing a number as a String
jaroslav@49
    75
     */
jaroslav@49
    76
    final static char[] digits = {
jaroslav@49
    77
        '0' , '1' , '2' , '3' , '4' , '5' ,
jaroslav@49
    78
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
jaroslav@49
    79
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
jaroslav@49
    80
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
jaroslav@49
    81
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
jaroslav@49
    82
        'u' , 'v' , 'w' , 'x' , 'y' , 'z'
jaroslav@49
    83
    };
jaroslav@49
    84
jaroslav@49
    85
    /**
jaroslav@49
    86
     * Returns a string representation of the first argument in the
jaroslav@49
    87
     * radix specified by the second argument.
jaroslav@49
    88
     *
jaroslav@49
    89
     * <p>If the radix is smaller than {@code Character.MIN_RADIX}
jaroslav@49
    90
     * or larger than {@code Character.MAX_RADIX}, then the radix
jaroslav@49
    91
     * {@code 10} is used instead.
jaroslav@49
    92
     *
jaroslav@49
    93
     * <p>If the first argument is negative, the first element of the
jaroslav@49
    94
     * result is the ASCII minus character {@code '-'}
jaroslav@49
    95
     * (<code>'&#92;u002D'</code>). If the first argument is not
jaroslav@49
    96
     * negative, no sign character appears in the result.
jaroslav@49
    97
     *
jaroslav@49
    98
     * <p>The remaining characters of the result represent the magnitude
jaroslav@49
    99
     * of the first argument. If the magnitude is zero, it is
jaroslav@49
   100
     * represented by a single zero character {@code '0'}
jaroslav@49
   101
     * (<code>'&#92;u0030'</code>); otherwise, the first character of
jaroslav@49
   102
     * the representation of the magnitude will not be the zero
jaroslav@49
   103
     * character.  The following ASCII characters are used as digits:
jaroslav@49
   104
     *
jaroslav@49
   105
     * <blockquote>
jaroslav@49
   106
     *   {@code 0123456789abcdefghijklmnopqrstuvwxyz}
jaroslav@49
   107
     * </blockquote>
jaroslav@49
   108
     *
jaroslav@49
   109
     * These are <code>'&#92;u0030'</code> through
jaroslav@49
   110
     * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
jaroslav@49
   111
     * <code>'&#92;u007A'</code>. If {@code radix} is
jaroslav@49
   112
     * <var>N</var>, then the first <var>N</var> of these characters
jaroslav@49
   113
     * are used as radix-<var>N</var> digits in the order shown. Thus,
jaroslav@49
   114
     * the digits for hexadecimal (radix 16) are
jaroslav@49
   115
     * {@code 0123456789abcdef}. If uppercase letters are
jaroslav@49
   116
     * desired, the {@link java.lang.String#toUpperCase()} method may
jaroslav@49
   117
     * be called on the result:
jaroslav@49
   118
     *
jaroslav@49
   119
     * <blockquote>
jaroslav@49
   120
     *  {@code Integer.toString(n, 16).toUpperCase()}
jaroslav@49
   121
     * </blockquote>
jaroslav@49
   122
     *
jaroslav@49
   123
     * @param   i       an integer to be converted to a string.
jaroslav@49
   124
     * @param   radix   the radix to use in the string representation.
jaroslav@49
   125
     * @return  a string representation of the argument in the specified radix.
jaroslav@49
   126
     * @see     java.lang.Character#MAX_RADIX
jaroslav@49
   127
     * @see     java.lang.Character#MIN_RADIX
jaroslav@49
   128
     */
jaroslav@49
   129
    public static String toString(int i, int radix) {
jaroslav@49
   130
jaroslav@49
   131
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
jaroslav@49
   132
            radix = 10;
jaroslav@49
   133
jaroslav@49
   134
        /* Use the faster version */
jaroslav@49
   135
        if (radix == 10) {
jaroslav@49
   136
            return toString(i);
jaroslav@49
   137
        }
jaroslav@49
   138
jaroslav@49
   139
        char buf[] = new char[33];
jaroslav@49
   140
        boolean negative = (i < 0);
jaroslav@49
   141
        int charPos = 32;
jaroslav@49
   142
jaroslav@49
   143
        if (!negative) {
jaroslav@49
   144
            i = -i;
jaroslav@49
   145
        }
jaroslav@49
   146
jaroslav@49
   147
        while (i <= -radix) {
jaroslav@49
   148
            buf[charPos--] = digits[-(i % radix)];
jaroslav@49
   149
            i = i / radix;
jaroslav@49
   150
        }
jaroslav@49
   151
        buf[charPos] = digits[-i];
jaroslav@49
   152
jaroslav@49
   153
        if (negative) {
jaroslav@49
   154
            buf[--charPos] = '-';
jaroslav@49
   155
        }
jaroslav@49
   156
jaroslav@49
   157
        return new String(buf, charPos, (33 - charPos));
jaroslav@49
   158
    }
jaroslav@49
   159
jaroslav@49
   160
    /**
jaroslav@49
   161
     * Returns a string representation of the integer argument as an
jaroslav@49
   162
     * unsigned integer in base&nbsp;16.
jaroslav@49
   163
     *
jaroslav@49
   164
     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
jaroslav@49
   165
     * if the argument is negative; otherwise, it is equal to the
jaroslav@49
   166
     * argument.  This value is converted to a string of ASCII digits
jaroslav@49
   167
     * in hexadecimal (base&nbsp;16) with no extra leading
jaroslav@49
   168
     * {@code 0}s. If the unsigned magnitude is zero, it is
jaroslav@49
   169
     * represented by a single zero character {@code '0'}
jaroslav@49
   170
     * (<code>'&#92;u0030'</code>); otherwise, the first character of
jaroslav@49
   171
     * the representation of the unsigned magnitude will not be the
jaroslav@49
   172
     * zero character. The following characters are used as
jaroslav@49
   173
     * hexadecimal digits:
jaroslav@49
   174
     *
jaroslav@49
   175
     * <blockquote>
jaroslav@49
   176
     *  {@code 0123456789abcdef}
jaroslav@49
   177
     * </blockquote>
jaroslav@49
   178
     *
jaroslav@49
   179
     * These are the characters <code>'&#92;u0030'</code> through
jaroslav@49
   180
     * <code>'&#92;u0039'</code> and <code>'&#92;u0061'</code> through
jaroslav@49
   181
     * <code>'&#92;u0066'</code>. If uppercase letters are
jaroslav@49
   182
     * desired, the {@link java.lang.String#toUpperCase()} method may
jaroslav@49
   183
     * be called on the result:
jaroslav@49
   184
     *
jaroslav@49
   185
     * <blockquote>
jaroslav@49
   186
     *  {@code Integer.toHexString(n).toUpperCase()}
jaroslav@49
   187
     * </blockquote>
jaroslav@49
   188
     *
jaroslav@49
   189
     * @param   i   an integer to be converted to a string.
jaroslav@49
   190
     * @return  the string representation of the unsigned integer value
jaroslav@49
   191
     *          represented by the argument in hexadecimal (base&nbsp;16).
jaroslav@49
   192
     * @since   JDK1.0.2
jaroslav@49
   193
     */
jaroslav@49
   194
    public static String toHexString(int i) {
jaroslav@49
   195
        return toUnsignedString(i, 4);
jaroslav@49
   196
    }
jaroslav@49
   197
jaroslav@49
   198
    /**
jaroslav@49
   199
     * Returns a string representation of the integer argument as an
jaroslav@49
   200
     * unsigned integer in base&nbsp;8.
jaroslav@49
   201
     *
jaroslav@49
   202
     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
jaroslav@49
   203
     * if the argument is negative; otherwise, it is equal to the
jaroslav@49
   204
     * argument.  This value is converted to a string of ASCII digits
jaroslav@49
   205
     * in octal (base&nbsp;8) with no extra leading {@code 0}s.
jaroslav@49
   206
     *
jaroslav@49
   207
     * <p>If the unsigned magnitude is zero, it is represented by a
jaroslav@49
   208
     * single zero character {@code '0'}
jaroslav@49
   209
     * (<code>'&#92;u0030'</code>); otherwise, the first character of
jaroslav@49
   210
     * the representation of the unsigned magnitude will not be the
jaroslav@49
   211
     * zero character. The following characters are used as octal
jaroslav@49
   212
     * digits:
jaroslav@49
   213
     *
jaroslav@49
   214
     * <blockquote>
jaroslav@49
   215
     * {@code 01234567}
jaroslav@49
   216
     * </blockquote>
jaroslav@49
   217
     *
jaroslav@49
   218
     * These are the characters <code>'&#92;u0030'</code> through
jaroslav@49
   219
     * <code>'&#92;u0037'</code>.
jaroslav@49
   220
     *
jaroslav@49
   221
     * @param   i   an integer to be converted to a string.
jaroslav@49
   222
     * @return  the string representation of the unsigned integer value
jaroslav@49
   223
     *          represented by the argument in octal (base&nbsp;8).
jaroslav@49
   224
     * @since   JDK1.0.2
jaroslav@49
   225
     */
jaroslav@49
   226
    public static String toOctalString(int i) {
jaroslav@49
   227
        return toUnsignedString(i, 3);
jaroslav@49
   228
    }
jaroslav@49
   229
jaroslav@49
   230
    /**
jaroslav@49
   231
     * Returns a string representation of the integer argument as an
jaroslav@49
   232
     * unsigned integer in base&nbsp;2.
jaroslav@49
   233
     *
jaroslav@49
   234
     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
jaroslav@49
   235
     * if the argument is negative; otherwise it is equal to the
jaroslav@49
   236
     * argument.  This value is converted to a string of ASCII digits
jaroslav@49
   237
     * in binary (base&nbsp;2) with no extra leading {@code 0}s.
jaroslav@49
   238
     * If the unsigned magnitude is zero, it is represented by a
jaroslav@49
   239
     * single zero character {@code '0'}
jaroslav@49
   240
     * (<code>'&#92;u0030'</code>); otherwise, the first character of
jaroslav@49
   241
     * the representation of the unsigned magnitude will not be the
jaroslav@49
   242
     * zero character. The characters {@code '0'}
jaroslav@49
   243
     * (<code>'&#92;u0030'</code>) and {@code '1'}
jaroslav@49
   244
     * (<code>'&#92;u0031'</code>) are used as binary digits.
jaroslav@49
   245
     *
jaroslav@49
   246
     * @param   i   an integer to be converted to a string.
jaroslav@49
   247
     * @return  the string representation of the unsigned integer value
jaroslav@49
   248
     *          represented by the argument in binary (base&nbsp;2).
jaroslav@49
   249
     * @since   JDK1.0.2
jaroslav@49
   250
     */
jaroslav@49
   251
    public static String toBinaryString(int i) {
jaroslav@49
   252
        return toUnsignedString(i, 1);
jaroslav@49
   253
    }
jaroslav@49
   254
jaroslav@49
   255
    /**
jaroslav@49
   256
     * Convert the integer to an unsigned number.
jaroslav@49
   257
     */
jaroslav@49
   258
    private static String toUnsignedString(int i, int shift) {
jaroslav@49
   259
        char[] buf = new char[32];
jaroslav@49
   260
        int charPos = 32;
jaroslav@49
   261
        int radix = 1 << shift;
jaroslav@49
   262
        int mask = radix - 1;
jaroslav@49
   263
        do {
jaroslav@49
   264
            buf[--charPos] = digits[i & mask];
jaroslav@49
   265
            i >>>= shift;
jaroslav@49
   266
        } while (i != 0);
jaroslav@49
   267
jaroslav@49
   268
        return new String(buf, charPos, (32 - charPos));
jaroslav@49
   269
    }
jaroslav@49
   270
jaroslav@49
   271
jaroslav@49
   272
    final static char [] DigitTens = {
jaroslav@49
   273
        '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
jaroslav@49
   274
        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
jaroslav@49
   275
        '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
jaroslav@49
   276
        '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
jaroslav@49
   277
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
jaroslav@49
   278
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
jaroslav@49
   279
        '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
jaroslav@49
   280
        '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
jaroslav@49
   281
        '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
jaroslav@49
   282
        '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
jaroslav@49
   283
        } ;
jaroslav@49
   284
jaroslav@49
   285
    final static char [] DigitOnes = {
jaroslav@49
   286
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   287
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   288
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   289
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   290
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   291
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   292
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   293
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   294
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   295
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
jaroslav@49
   296
        } ;
jaroslav@49
   297
jaroslav@49
   298
        // I use the "invariant division by multiplication" trick to
jaroslav@49
   299
        // accelerate Integer.toString.  In particular we want to
jaroslav@49
   300
        // avoid division by 10.
jaroslav@49
   301
        //
jaroslav@49
   302
        // The "trick" has roughly the same performance characteristics
jaroslav@49
   303
        // as the "classic" Integer.toString code on a non-JIT VM.
jaroslav@49
   304
        // The trick avoids .rem and .div calls but has a longer code
jaroslav@49
   305
        // path and is thus dominated by dispatch overhead.  In the
jaroslav@49
   306
        // JIT case the dispatch overhead doesn't exist and the
jaroslav@49
   307
        // "trick" is considerably faster than the classic code.
jaroslav@49
   308
        //
jaroslav@49
   309
        // TODO-FIXME: convert (x * 52429) into the equiv shift-add
jaroslav@49
   310
        // sequence.
jaroslav@49
   311
        //
jaroslav@49
   312
        // RE:  Division by Invariant Integers using Multiplication
jaroslav@49
   313
        //      T Gralund, P Montgomery
jaroslav@49
   314
        //      ACM PLDI 1994
jaroslav@49
   315
        //
jaroslav@49
   316
jaroslav@49
   317
    /**
jaroslav@49
   318
     * Returns a {@code String} object representing the
jaroslav@49
   319
     * specified integer. The argument is converted to signed decimal
jaroslav@49
   320
     * representation and returned as a string, exactly as if the
jaroslav@49
   321
     * argument and radix 10 were given as arguments to the {@link
jaroslav@49
   322
     * #toString(int, int)} method.
jaroslav@49
   323
     *
jaroslav@49
   324
     * @param   i   an integer to be converted.
jaroslav@49
   325
     * @return  a string representation of the argument in base&nbsp;10.
jaroslav@49
   326
     */
jaroslav@179
   327
    @JavaScriptBody(args = "i", body = "return i.toString();")
jaroslav@49
   328
    public static String toString(int i) {
jaroslav@49
   329
        if (i == Integer.MIN_VALUE)
jaroslav@49
   330
            return "-2147483648";
jaroslav@49
   331
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
jaroslav@49
   332
        char[] buf = new char[size];
jaroslav@49
   333
        getChars(i, size, buf);
jaroslav@179
   334
        return new String(buf, 0, size);
jaroslav@49
   335
    }
jaroslav@49
   336
jaroslav@49
   337
    /**
jaroslav@49
   338
     * Places characters representing the integer i into the
jaroslav@49
   339
     * character array buf. The characters are placed into
jaroslav@49
   340
     * the buffer backwards starting with the least significant
jaroslav@49
   341
     * digit at the specified index (exclusive), and working
jaroslav@49
   342
     * backwards from there.
jaroslav@49
   343
     *
jaroslav@49
   344
     * Will fail if i == Integer.MIN_VALUE
jaroslav@49
   345
     */
jaroslav@49
   346
    static void getChars(int i, int index, char[] buf) {
jaroslav@49
   347
        int q, r;
jaroslav@49
   348
        int charPos = index;
jaroslav@49
   349
        char sign = 0;
jaroslav@49
   350
jaroslav@49
   351
        if (i < 0) {
jaroslav@49
   352
            sign = '-';
jaroslav@49
   353
            i = -i;
jaroslav@49
   354
        }
jaroslav@49
   355
jaroslav@49
   356
        // Generate two digits per iteration
jaroslav@49
   357
        while (i >= 65536) {
jaroslav@49
   358
            q = i / 100;
jaroslav@49
   359
        // really: r = i - (q * 100);
jaroslav@49
   360
            r = i - ((q << 6) + (q << 5) + (q << 2));
jaroslav@49
   361
            i = q;
jaroslav@49
   362
            buf [--charPos] = DigitOnes[r];
jaroslav@49
   363
            buf [--charPos] = DigitTens[r];
jaroslav@49
   364
        }
jaroslav@49
   365
jaroslav@49
   366
        // Fall thru to fast mode for smaller numbers
jaroslav@49
   367
        // assert(i <= 65536, i);
jaroslav@49
   368
        for (;;) {
jaroslav@49
   369
            q = (i * 52429) >>> (16+3);
jaroslav@49
   370
            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
jaroslav@49
   371
            buf [--charPos] = digits [r];
jaroslav@49
   372
            i = q;
jaroslav@49
   373
            if (i == 0) break;
jaroslav@49
   374
        }
jaroslav@49
   375
        if (sign != 0) {
jaroslav@49
   376
            buf [--charPos] = sign;
jaroslav@49
   377
        }
jaroslav@49
   378
    }
jaroslav@49
   379
jaroslav@49
   380
    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
jaroslav@49
   381
                                      99999999, 999999999, Integer.MAX_VALUE };
jaroslav@49
   382
jaroslav@49
   383
    // Requires positive x
jaroslav@49
   384
    static int stringSize(int x) {
jaroslav@49
   385
        for (int i=0; ; i++)
jaroslav@49
   386
            if (x <= sizeTable[i])
jaroslav@49
   387
                return i+1;
jaroslav@49
   388
    }
jaroslav@49
   389
jaroslav@49
   390
    /**
jaroslav@49
   391
     * Parses the string argument as a signed integer in the radix
jaroslav@49
   392
     * specified by the second argument. The characters in the string
jaroslav@49
   393
     * must all be digits of the specified radix (as determined by
jaroslav@49
   394
     * whether {@link java.lang.Character#digit(char, int)} returns a
jaroslav@49
   395
     * nonnegative value), except that the first character may be an
jaroslav@49
   396
     * ASCII minus sign {@code '-'} (<code>'&#92;u002D'</code>) to
jaroslav@49
   397
     * indicate a negative value or an ASCII plus sign {@code '+'}
jaroslav@49
   398
     * (<code>'&#92;u002B'</code>) to indicate a positive value. The
jaroslav@49
   399
     * resulting integer value is returned.
jaroslav@49
   400
     *
jaroslav@49
   401
     * <p>An exception of type {@code NumberFormatException} is
jaroslav@49
   402
     * thrown if any of the following situations occurs:
jaroslav@49
   403
     * <ul>
jaroslav@49
   404
     * <li>The first argument is {@code null} or is a string of
jaroslav@49
   405
     * length zero.
jaroslav@49
   406
     *
jaroslav@49
   407
     * <li>The radix is either smaller than
jaroslav@49
   408
     * {@link java.lang.Character#MIN_RADIX} or
jaroslav@49
   409
     * larger than {@link java.lang.Character#MAX_RADIX}.
jaroslav@49
   410
     *
jaroslav@49
   411
     * <li>Any character of the string is not a digit of the specified
jaroslav@49
   412
     * radix, except that the first character may be a minus sign
jaroslav@49
   413
     * {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
jaroslav@49
   414
     * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
jaroslav@49
   415
     * string is longer than length 1.
jaroslav@49
   416
     *
jaroslav@49
   417
     * <li>The value represented by the string is not a value of type
jaroslav@49
   418
     * {@code int}.
jaroslav@49
   419
     * </ul>
jaroslav@49
   420
     *
jaroslav@49
   421
     * <p>Examples:
jaroslav@49
   422
     * <blockquote><pre>
jaroslav@49
   423
     * parseInt("0", 10) returns 0
jaroslav@49
   424
     * parseInt("473", 10) returns 473
jaroslav@49
   425
     * parseInt("+42", 10) returns 42
jaroslav@49
   426
     * parseInt("-0", 10) returns 0
jaroslav@49
   427
     * parseInt("-FF", 16) returns -255
jaroslav@49
   428
     * parseInt("1100110", 2) returns 102
jaroslav@49
   429
     * parseInt("2147483647", 10) returns 2147483647
jaroslav@49
   430
     * parseInt("-2147483648", 10) returns -2147483648
jaroslav@49
   431
     * parseInt("2147483648", 10) throws a NumberFormatException
jaroslav@49
   432
     * parseInt("99", 8) throws a NumberFormatException
jaroslav@49
   433
     * parseInt("Kona", 10) throws a NumberFormatException
jaroslav@49
   434
     * parseInt("Kona", 27) returns 411787
jaroslav@49
   435
     * </pre></blockquote>
jaroslav@49
   436
     *
jaroslav@49
   437
     * @param      s   the {@code String} containing the integer
jaroslav@49
   438
     *                  representation to be parsed
jaroslav@49
   439
     * @param      radix   the radix to be used while parsing {@code s}.
jaroslav@49
   440
     * @return     the integer represented by the string argument in the
jaroslav@49
   441
     *             specified radix.
jaroslav@49
   442
     * @exception  NumberFormatException if the {@code String}
jaroslav@49
   443
     *             does not contain a parsable {@code int}.
jaroslav@49
   444
     */
jaroslav@114
   445
    @JavaScriptBody(args={"s", "radix"}, body="return parseInt(s,radix);")
jaroslav@49
   446
    public static int parseInt(String s, int radix)
jaroslav@49
   447
                throws NumberFormatException
jaroslav@49
   448
    {
jaroslav@49
   449
        /*
jaroslav@49
   450
         * WARNING: This method may be invoked early during VM initialization
jaroslav@49
   451
         * before IntegerCache is initialized. Care must be taken to not use
jaroslav@49
   452
         * the valueOf method.
jaroslav@49
   453
         */
jaroslav@49
   454
jaroslav@49
   455
        if (s == null) {
jaroslav@49
   456
            throw new NumberFormatException("null");
jaroslav@49
   457
        }
jaroslav@49
   458
jaroslav@49
   459
        if (radix < Character.MIN_RADIX) {
jaroslav@49
   460
            throw new NumberFormatException("radix " + radix +
jaroslav@49
   461
                                            " less than Character.MIN_RADIX");
jaroslav@49
   462
        }
jaroslav@49
   463
jaroslav@49
   464
        if (radix > Character.MAX_RADIX) {
jaroslav@49
   465
            throw new NumberFormatException("radix " + radix +
jaroslav@49
   466
                                            " greater than Character.MAX_RADIX");
jaroslav@49
   467
        }
jaroslav@49
   468
jaroslav@49
   469
        int result = 0;
jaroslav@49
   470
        boolean negative = false;
jaroslav@49
   471
        int i = 0, len = s.length();
jaroslav@49
   472
        int limit = -Integer.MAX_VALUE;
jaroslav@49
   473
        int multmin;
jaroslav@49
   474
        int digit;
jaroslav@49
   475
jaroslav@49
   476
        if (len > 0) {
jaroslav@49
   477
            char firstChar = s.charAt(0);
jaroslav@49
   478
            if (firstChar < '0') { // Possible leading "+" or "-"
jaroslav@49
   479
                if (firstChar == '-') {
jaroslav@49
   480
                    negative = true;
jaroslav@49
   481
                    limit = Integer.MIN_VALUE;
jaroslav@49
   482
                } else if (firstChar != '+')
jaroslav@49
   483
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   484
jaroslav@49
   485
                if (len == 1) // Cannot have lone "+" or "-"
jaroslav@49
   486
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   487
                i++;
jaroslav@49
   488
            }
jaroslav@49
   489
            multmin = limit / radix;
jaroslav@49
   490
            while (i < len) {
jaroslav@49
   491
                // Accumulating negatively avoids surprises near MAX_VALUE
jaroslav@49
   492
                digit = Character.digit(s.charAt(i++),radix);
jaroslav@49
   493
                if (digit < 0) {
jaroslav@49
   494
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   495
                }
jaroslav@49
   496
                if (result < multmin) {
jaroslav@49
   497
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   498
                }
jaroslav@49
   499
                result *= radix;
jaroslav@49
   500
                if (result < limit + digit) {
jaroslav@49
   501
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   502
                }
jaroslav@49
   503
                result -= digit;
jaroslav@49
   504
            }
jaroslav@49
   505
        } else {
jaroslav@49
   506
            throw NumberFormatException.forInputString(s);
jaroslav@49
   507
        }
jaroslav@49
   508
        return negative ? result : -result;
jaroslav@49
   509
    }
jaroslav@49
   510
jaroslav@49
   511
    /**
jaroslav@49
   512
     * Parses the string argument as a signed decimal integer. The
jaroslav@49
   513
     * characters in the string must all be decimal digits, except
jaroslav@49
   514
     * that the first character may be an ASCII minus sign {@code '-'}
jaroslav@49
   515
     * (<code>'&#92;u002D'</code>) to indicate a negative value or an
jaroslav@49
   516
     * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
jaroslav@49
   517
     * indicate a positive value. The resulting integer value is
jaroslav@49
   518
     * returned, exactly as if the argument and the radix 10 were
jaroslav@49
   519
     * given as arguments to the {@link #parseInt(java.lang.String,
jaroslav@49
   520
     * int)} method.
jaroslav@49
   521
     *
jaroslav@49
   522
     * @param s    a {@code String} containing the {@code int}
jaroslav@49
   523
     *             representation to be parsed
jaroslav@49
   524
     * @return     the integer value represented by the argument in decimal.
jaroslav@49
   525
     * @exception  NumberFormatException  if the string does not contain a
jaroslav@49
   526
     *               parsable integer.
jaroslav@49
   527
     */
jaroslav@49
   528
    public static int parseInt(String s) throws NumberFormatException {
jaroslav@49
   529
        return parseInt(s,10);
jaroslav@49
   530
    }
jaroslav@49
   531
jaroslav@49
   532
    /**
jaroslav@49
   533
     * Returns an {@code Integer} object holding the value
jaroslav@49
   534
     * extracted from the specified {@code String} when parsed
jaroslav@49
   535
     * with the radix given by the second argument. The first argument
jaroslav@49
   536
     * is interpreted as representing a signed integer in the radix
jaroslav@49
   537
     * specified by the second argument, exactly as if the arguments
jaroslav@49
   538
     * were given to the {@link #parseInt(java.lang.String, int)}
jaroslav@49
   539
     * method. The result is an {@code Integer} object that
jaroslav@49
   540
     * represents the integer value specified by the string.
jaroslav@49
   541
     *
jaroslav@49
   542
     * <p>In other words, this method returns an {@code Integer}
jaroslav@49
   543
     * object equal to the value of:
jaroslav@49
   544
     *
jaroslav@49
   545
     * <blockquote>
jaroslav@49
   546
     *  {@code new Integer(Integer.parseInt(s, radix))}
jaroslav@49
   547
     * </blockquote>
jaroslav@49
   548
     *
jaroslav@49
   549
     * @param      s   the string to be parsed.
jaroslav@49
   550
     * @param      radix the radix to be used in interpreting {@code s}
jaroslav@49
   551
     * @return     an {@code Integer} object holding the value
jaroslav@49
   552
     *             represented by the string argument in the specified
jaroslav@49
   553
     *             radix.
jaroslav@49
   554
     * @exception NumberFormatException if the {@code String}
jaroslav@49
   555
     *            does not contain a parsable {@code int}.
jaroslav@49
   556
     */
jaroslav@49
   557
    public static Integer valueOf(String s, int radix) throws NumberFormatException {
jaroslav@49
   558
        return Integer.valueOf(parseInt(s,radix));
jaroslav@49
   559
    }
jaroslav@49
   560
jaroslav@49
   561
    /**
jaroslav@49
   562
     * Returns an {@code Integer} object holding the
jaroslav@49
   563
     * value of the specified {@code String}. The argument is
jaroslav@49
   564
     * interpreted as representing a signed decimal integer, exactly
jaroslav@49
   565
     * as if the argument were given to the {@link
jaroslav@49
   566
     * #parseInt(java.lang.String)} method. The result is an
jaroslav@49
   567
     * {@code Integer} object that represents the integer value
jaroslav@49
   568
     * specified by the string.
jaroslav@49
   569
     *
jaroslav@49
   570
     * <p>In other words, this method returns an {@code Integer}
jaroslav@49
   571
     * object equal to the value of:
jaroslav@49
   572
     *
jaroslav@49
   573
     * <blockquote>
jaroslav@49
   574
     *  {@code new Integer(Integer.parseInt(s))}
jaroslav@49
   575
     * </blockquote>
jaroslav@49
   576
     *
jaroslav@49
   577
     * @param      s   the string to be parsed.
jaroslav@49
   578
     * @return     an {@code Integer} object holding the value
jaroslav@49
   579
     *             represented by the string argument.
jaroslav@49
   580
     * @exception  NumberFormatException  if the string cannot be parsed
jaroslav@49
   581
     *             as an integer.
jaroslav@49
   582
     */
jaroslav@49
   583
    public static Integer valueOf(String s) throws NumberFormatException {
jaroslav@49
   584
        return Integer.valueOf(parseInt(s, 10));
jaroslav@49
   585
    }
jaroslav@49
   586
jaroslav@49
   587
    /**
jaroslav@49
   588
     * Cache to support the object identity semantics of autoboxing for values between
jaroslav@49
   589
     * -128 and 127 (inclusive) as required by JLS.
jaroslav@49
   590
     *
jaroslav@49
   591
     * The cache is initialized on first usage.  The size of the cache
jaroslav@49
   592
     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
jaroslav@49
   593
     * During VM initialization, java.lang.Integer.IntegerCache.high property
jaroslav@49
   594
     * may be set and saved in the private system properties in the
jaroslav@49
   595
     * sun.misc.VM class.
jaroslav@49
   596
     */
jaroslav@49
   597
jaroslav@49
   598
    private static class IntegerCache {
jaroslav@49
   599
        static final int low = -128;
jaroslav@49
   600
        static final int high;
jaroslav@49
   601
        static final Integer cache[];
jaroslav@49
   602
jaroslav@49
   603
        static {
jaroslav@49
   604
            // high value may be configured by property
jaroslav@49
   605
            int h = 127;
jaroslav@49
   606
            String integerCacheHighPropValue =
jaroslav@104
   607
                AbstractStringBuilder.getProperty("java.lang.Integer.IntegerCache.high");
jaroslav@49
   608
            if (integerCacheHighPropValue != null) {
jaroslav@49
   609
                int i = parseInt(integerCacheHighPropValue);
jaroslav@49
   610
                i = Math.max(i, 127);
jaroslav@49
   611
                // Maximum array size is Integer.MAX_VALUE
jaroslav@49
   612
                h = Math.min(i, Integer.MAX_VALUE - (-low));
jaroslav@49
   613
            }
jaroslav@49
   614
            high = h;
jaroslav@49
   615
jaroslav@49
   616
            cache = new Integer[(high - low) + 1];
jaroslav@49
   617
            int j = low;
jaroslav@49
   618
            for(int k = 0; k < cache.length; k++)
jaroslav@49
   619
                cache[k] = new Integer(j++);
jaroslav@49
   620
        }
jaroslav@49
   621
jaroslav@49
   622
        private IntegerCache() {}
jaroslav@49
   623
    }
jaroslav@49
   624
jaroslav@49
   625
    /**
jaroslav@49
   626
     * Returns an {@code Integer} instance representing the specified
jaroslav@49
   627
     * {@code int} value.  If a new {@code Integer} instance is not
jaroslav@49
   628
     * required, this method should generally be used in preference to
jaroslav@49
   629
     * the constructor {@link #Integer(int)}, as this method is likely
jaroslav@49
   630
     * to yield significantly better space and time performance by
jaroslav@49
   631
     * caching frequently requested values.
jaroslav@49
   632
     *
jaroslav@49
   633
     * This method will always cache values in the range -128 to 127,
jaroslav@49
   634
     * inclusive, and may cache other values outside of this range.
jaroslav@49
   635
     *
jaroslav@49
   636
     * @param  i an {@code int} value.
jaroslav@49
   637
     * @return an {@code Integer} instance representing {@code i}.
jaroslav@49
   638
     * @since  1.5
jaroslav@49
   639
     */
jaroslav@49
   640
    public static Integer valueOf(int i) {
jaroslav@93
   641
        //assert IntegerCache.high >= 127;
jaroslav@49
   642
        if (i >= IntegerCache.low && i <= IntegerCache.high)
jaroslav@49
   643
            return IntegerCache.cache[i + (-IntegerCache.low)];
jaroslav@49
   644
        return new Integer(i);
jaroslav@49
   645
    }
jaroslav@49
   646
jaroslav@49
   647
    /**
jaroslav@49
   648
     * The value of the {@code Integer}.
jaroslav@49
   649
     *
jaroslav@49
   650
     * @serial
jaroslav@49
   651
     */
jaroslav@49
   652
    private final int value;
jaroslav@49
   653
jaroslav@49
   654
    /**
jaroslav@49
   655
     * Constructs a newly allocated {@code Integer} object that
jaroslav@49
   656
     * represents the specified {@code int} value.
jaroslav@49
   657
     *
jaroslav@49
   658
     * @param   value   the value to be represented by the
jaroslav@49
   659
     *                  {@code Integer} object.
jaroslav@49
   660
     */
jaroslav@49
   661
    public Integer(int value) {
jaroslav@49
   662
        this.value = value;
jaroslav@49
   663
    }
jaroslav@49
   664
jaroslav@49
   665
    /**
jaroslav@49
   666
     * Constructs a newly allocated {@code Integer} object that
jaroslav@49
   667
     * represents the {@code int} value indicated by the
jaroslav@49
   668
     * {@code String} parameter. The string is converted to an
jaroslav@49
   669
     * {@code int} value in exactly the manner used by the
jaroslav@49
   670
     * {@code parseInt} method for radix 10.
jaroslav@49
   671
     *
jaroslav@49
   672
     * @param      s   the {@code String} to be converted to an
jaroslav@49
   673
     *                 {@code Integer}.
jaroslav@49
   674
     * @exception  NumberFormatException  if the {@code String} does not
jaroslav@49
   675
     *               contain a parsable integer.
jaroslav@49
   676
     * @see        java.lang.Integer#parseInt(java.lang.String, int)
jaroslav@49
   677
     */
jaroslav@49
   678
    public Integer(String s) throws NumberFormatException {
jaroslav@49
   679
        this.value = parseInt(s, 10);
jaroslav@49
   680
    }
jaroslav@49
   681
jaroslav@49
   682
    /**
jaroslav@49
   683
     * Returns the value of this {@code Integer} as a
jaroslav@49
   684
     * {@code byte}.
jaroslav@49
   685
     */
jaroslav@49
   686
    public byte byteValue() {
jaroslav@49
   687
        return (byte)value;
jaroslav@49
   688
    }
jaroslav@49
   689
jaroslav@49
   690
    /**
jaroslav@49
   691
     * Returns the value of this {@code Integer} as a
jaroslav@49
   692
     * {@code short}.
jaroslav@49
   693
     */
jaroslav@49
   694
    public short shortValue() {
jaroslav@49
   695
        return (short)value;
jaroslav@49
   696
    }
jaroslav@49
   697
jaroslav@49
   698
    /**
jaroslav@49
   699
     * Returns the value of this {@code Integer} as an
jaroslav@49
   700
     * {@code int}.
jaroslav@49
   701
     */
jaroslav@49
   702
    public int intValue() {
jaroslav@49
   703
        return value;
jaroslav@49
   704
    }
jaroslav@49
   705
jaroslav@49
   706
    /**
jaroslav@49
   707
     * Returns the value of this {@code Integer} as a
jaroslav@49
   708
     * {@code long}.
jaroslav@49
   709
     */
jaroslav@49
   710
    public long longValue() {
jaroslav@49
   711
        return (long)value;
jaroslav@49
   712
    }
jaroslav@49
   713
jaroslav@49
   714
    /**
jaroslav@49
   715
     * Returns the value of this {@code Integer} as a
jaroslav@49
   716
     * {@code float}.
jaroslav@49
   717
     */
jaroslav@49
   718
    public float floatValue() {
jaroslav@49
   719
        return (float)value;
jaroslav@49
   720
    }
jaroslav@49
   721
jaroslav@49
   722
    /**
jaroslav@49
   723
     * Returns the value of this {@code Integer} as a
jaroslav@49
   724
     * {@code double}.
jaroslav@49
   725
     */
jaroslav@49
   726
    public double doubleValue() {
jaroslav@49
   727
        return (double)value;
jaroslav@49
   728
    }
jaroslav@49
   729
jaroslav@49
   730
    /**
jaroslav@49
   731
     * Returns a {@code String} object representing this
jaroslav@49
   732
     * {@code Integer}'s value. The value is converted to signed
jaroslav@49
   733
     * decimal representation and returned as a string, exactly as if
jaroslav@49
   734
     * the integer value were given as an argument to the {@link
jaroslav@49
   735
     * java.lang.Integer#toString(int)} method.
jaroslav@49
   736
     *
jaroslav@49
   737
     * @return  a string representation of the value of this object in
jaroslav@49
   738
     *          base&nbsp;10.
jaroslav@49
   739
     */
jaroslav@49
   740
    public String toString() {
jaroslav@49
   741
        return toString(value);
jaroslav@49
   742
    }
jaroslav@49
   743
jaroslav@49
   744
    /**
jaroslav@49
   745
     * Returns a hash code for this {@code Integer}.
jaroslav@49
   746
     *
jaroslav@49
   747
     * @return  a hash code value for this object, equal to the
jaroslav@49
   748
     *          primitive {@code int} value represented by this
jaroslav@49
   749
     *          {@code Integer} object.
jaroslav@49
   750
     */
jaroslav@49
   751
    public int hashCode() {
jaroslav@49
   752
        return value;
jaroslav@49
   753
    }
jaroslav@49
   754
jaroslav@49
   755
    /**
jaroslav@49
   756
     * Compares this object to the specified object.  The result is
jaroslav@49
   757
     * {@code true} if and only if the argument is not
jaroslav@49
   758
     * {@code null} and is an {@code Integer} object that
jaroslav@49
   759
     * contains the same {@code int} value as this object.
jaroslav@49
   760
     *
jaroslav@49
   761
     * @param   obj   the object to compare with.
jaroslav@49
   762
     * @return  {@code true} if the objects are the same;
jaroslav@49
   763
     *          {@code false} otherwise.
jaroslav@49
   764
     */
jaroslav@49
   765
    public boolean equals(Object obj) {
jaroslav@49
   766
        if (obj instanceof Integer) {
jaroslav@49
   767
            return value == ((Integer)obj).intValue();
jaroslav@49
   768
        }
jaroslav@49
   769
        return false;
jaroslav@49
   770
    }
jaroslav@49
   771
jaroslav@49
   772
    /**
jaroslav@49
   773
     * Determines the integer value of the system property with the
jaroslav@49
   774
     * specified name.
jaroslav@49
   775
     *
jaroslav@49
   776
     * <p>The first argument is treated as the name of a system property.
jaroslav@49
   777
     * System properties are accessible through the
jaroslav@49
   778
     * {@link java.lang.System#getProperty(java.lang.String)} method. The
jaroslav@49
   779
     * string value of this property is then interpreted as an integer
jaroslav@49
   780
     * value and an {@code Integer} object representing this value is
jaroslav@49
   781
     * returned. Details of possible numeric formats can be found with
jaroslav@49
   782
     * the definition of {@code getProperty}.
jaroslav@49
   783
     *
jaroslav@49
   784
     * <p>If there is no property with the specified name, if the specified name
jaroslav@49
   785
     * is empty or {@code null}, or if the property does not have
jaroslav@49
   786
     * the correct numeric format, then {@code null} is returned.
jaroslav@49
   787
     *
jaroslav@49
   788
     * <p>In other words, this method returns an {@code Integer}
jaroslav@49
   789
     * object equal to the value of:
jaroslav@49
   790
     *
jaroslav@49
   791
     * <blockquote>
jaroslav@49
   792
     *  {@code getInteger(nm, null)}
jaroslav@49
   793
     * </blockquote>
jaroslav@49
   794
     *
jaroslav@49
   795
     * @param   nm   property name.
jaroslav@49
   796
     * @return  the {@code Integer} value of the property.
jaroslav@49
   797
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@49
   798
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@49
   799
     */
jaroslav@49
   800
    public static Integer getInteger(String nm) {
jaroslav@49
   801
        return getInteger(nm, null);
jaroslav@49
   802
    }
jaroslav@49
   803
jaroslav@49
   804
    /**
jaroslav@49
   805
     * Determines the integer value of the system property with the
jaroslav@49
   806
     * specified name.
jaroslav@49
   807
     *
jaroslav@49
   808
     * <p>The first argument is treated as the name of a system property.
jaroslav@49
   809
     * System properties are accessible through the {@link
jaroslav@49
   810
     * java.lang.System#getProperty(java.lang.String)} method. The
jaroslav@49
   811
     * string value of this property is then interpreted as an integer
jaroslav@49
   812
     * value and an {@code Integer} object representing this value is
jaroslav@49
   813
     * returned. Details of possible numeric formats can be found with
jaroslav@49
   814
     * the definition of {@code getProperty}.
jaroslav@49
   815
     *
jaroslav@49
   816
     * <p>The second argument is the default value. An {@code Integer} object
jaroslav@49
   817
     * that represents the value of the second argument is returned if there
jaroslav@49
   818
     * is no property of the specified name, if the property does not have
jaroslav@49
   819
     * the correct numeric format, or if the specified name is empty or
jaroslav@49
   820
     * {@code null}.
jaroslav@49
   821
     *
jaroslav@49
   822
     * <p>In other words, this method returns an {@code Integer} object
jaroslav@49
   823
     * equal to the value of:
jaroslav@49
   824
     *
jaroslav@49
   825
     * <blockquote>
jaroslav@49
   826
     *  {@code getInteger(nm, new Integer(val))}
jaroslav@49
   827
     * </blockquote>
jaroslav@49
   828
     *
jaroslav@49
   829
     * but in practice it may be implemented in a manner such as:
jaroslav@49
   830
     *
jaroslav@49
   831
     * <blockquote><pre>
jaroslav@49
   832
     * Integer result = getInteger(nm, null);
jaroslav@49
   833
     * return (result == null) ? new Integer(val) : result;
jaroslav@49
   834
     * </pre></blockquote>
jaroslav@49
   835
     *
jaroslav@49
   836
     * to avoid the unnecessary allocation of an {@code Integer}
jaroslav@49
   837
     * object when the default value is not needed.
jaroslav@49
   838
     *
jaroslav@49
   839
     * @param   nm   property name.
jaroslav@49
   840
     * @param   val   default value.
jaroslav@49
   841
     * @return  the {@code Integer} value of the property.
jaroslav@49
   842
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@49
   843
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@49
   844
     */
jaroslav@49
   845
    public static Integer getInteger(String nm, int val) {
jaroslav@49
   846
        Integer result = getInteger(nm, null);
jaroslav@49
   847
        return (result == null) ? Integer.valueOf(val) : result;
jaroslav@49
   848
    }
jaroslav@49
   849
jaroslav@49
   850
    /**
jaroslav@49
   851
     * Returns the integer value of the system property with the
jaroslav@49
   852
     * specified name.  The first argument is treated as the name of a
jaroslav@49
   853
     * system property.  System properties are accessible through the
jaroslav@49
   854
     * {@link java.lang.System#getProperty(java.lang.String)} method.
jaroslav@49
   855
     * The string value of this property is then interpreted as an
jaroslav@49
   856
     * integer value, as per the {@code Integer.decode} method,
jaroslav@49
   857
     * and an {@code Integer} object representing this value is
jaroslav@49
   858
     * returned.
jaroslav@49
   859
     *
jaroslav@49
   860
     * <ul><li>If the property value begins with the two ASCII characters
jaroslav@49
   861
     *         {@code 0x} or the ASCII character {@code #}, not
jaroslav@49
   862
     *      followed by a minus sign, then the rest of it is parsed as a
jaroslav@49
   863
     *      hexadecimal integer exactly as by the method
jaroslav@49
   864
     *      {@link #valueOf(java.lang.String, int)} with radix 16.
jaroslav@49
   865
     * <li>If the property value begins with the ASCII character
jaroslav@49
   866
     *     {@code 0} followed by another character, it is parsed as an
jaroslav@49
   867
     *     octal integer exactly as by the method
jaroslav@49
   868
     *     {@link #valueOf(java.lang.String, int)} with radix 8.
jaroslav@49
   869
     * <li>Otherwise, the property value is parsed as a decimal integer
jaroslav@49
   870
     * exactly as by the method {@link #valueOf(java.lang.String, int)}
jaroslav@49
   871
     * with radix 10.
jaroslav@49
   872
     * </ul>
jaroslav@49
   873
     *
jaroslav@49
   874
     * <p>The second argument is the default value. The default value is
jaroslav@49
   875
     * returned if there is no property of the specified name, if the
jaroslav@49
   876
     * property does not have the correct numeric format, or if the
jaroslav@49
   877
     * specified name is empty or {@code null}.
jaroslav@49
   878
     *
jaroslav@49
   879
     * @param   nm   property name.
jaroslav@49
   880
     * @param   val   default value.
jaroslav@49
   881
     * @return  the {@code Integer} value of the property.
jaroslav@49
   882
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@49
   883
     * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@49
   884
     * @see java.lang.Integer#decode
jaroslav@49
   885
     */
jaroslav@49
   886
    public static Integer getInteger(String nm, Integer val) {
jaroslav@49
   887
        String v = null;
jaroslav@49
   888
        try {
jaroslav@104
   889
            v = AbstractStringBuilder.getProperty(nm);
jaroslav@49
   890
        } catch (IllegalArgumentException e) {
jaroslav@49
   891
        } catch (NullPointerException e) {
jaroslav@49
   892
        }
jaroslav@49
   893
        if (v != null) {
jaroslav@49
   894
            try {
jaroslav@49
   895
                return Integer.decode(v);
jaroslav@49
   896
            } catch (NumberFormatException e) {
jaroslav@49
   897
            }
jaroslav@49
   898
        }
jaroslav@49
   899
        return val;
jaroslav@49
   900
    }
jaroslav@49
   901
jaroslav@49
   902
    /**
jaroslav@49
   903
     * Decodes a {@code String} into an {@code Integer}.
jaroslav@49
   904
     * Accepts decimal, hexadecimal, and octal numbers given
jaroslav@49
   905
     * by the following grammar:
jaroslav@49
   906
     *
jaroslav@49
   907
     * <blockquote>
jaroslav@49
   908
     * <dl>
jaroslav@49
   909
     * <dt><i>DecodableString:</i>
jaroslav@49
   910
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
jaroslav@49
   911
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
jaroslav@49
   912
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
jaroslav@49
   913
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
jaroslav@49
   914
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
jaroslav@49
   915
     * <p>
jaroslav@49
   916
     * <dt><i>Sign:</i>
jaroslav@49
   917
     * <dd>{@code -}
jaroslav@49
   918
     * <dd>{@code +}
jaroslav@49
   919
     * </dl>
jaroslav@49
   920
     * </blockquote>
jaroslav@49
   921
     *
jaroslav@49
   922
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
jaroslav@49
   923
     * are as defined in section 3.10.1 of
jaroslav@49
   924
     * <cite>The Java&trade; Language Specification</cite>,
jaroslav@49
   925
     * except that underscores are not accepted between digits.
jaroslav@49
   926
     *
jaroslav@49
   927
     * <p>The sequence of characters following an optional
jaroslav@49
   928
     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
jaroslav@49
   929
     * "{@code #}", or leading zero) is parsed as by the {@code
jaroslav@49
   930
     * Integer.parseInt} method with the indicated radix (10, 16, or
jaroslav@49
   931
     * 8).  This sequence of characters must represent a positive
jaroslav@49
   932
     * value or a {@link NumberFormatException} will be thrown.  The
jaroslav@49
   933
     * result is negated if first character of the specified {@code
jaroslav@49
   934
     * String} is the minus sign.  No whitespace characters are
jaroslav@49
   935
     * permitted in the {@code String}.
jaroslav@49
   936
     *
jaroslav@49
   937
     * @param     nm the {@code String} to decode.
jaroslav@49
   938
     * @return    an {@code Integer} object holding the {@code int}
jaroslav@49
   939
     *             value represented by {@code nm}
jaroslav@49
   940
     * @exception NumberFormatException  if the {@code String} does not
jaroslav@49
   941
     *            contain a parsable integer.
jaroslav@49
   942
     * @see java.lang.Integer#parseInt(java.lang.String, int)
jaroslav@49
   943
     */
jaroslav@49
   944
    public static Integer decode(String nm) throws NumberFormatException {
jaroslav@49
   945
        int radix = 10;
jaroslav@49
   946
        int index = 0;
jaroslav@49
   947
        boolean negative = false;
jaroslav@49
   948
        Integer result;
jaroslav@49
   949
jaroslav@49
   950
        if (nm.length() == 0)
jaroslav@49
   951
            throw new NumberFormatException("Zero length string");
jaroslav@49
   952
        char firstChar = nm.charAt(0);
jaroslav@49
   953
        // Handle sign, if present
jaroslav@49
   954
        if (firstChar == '-') {
jaroslav@49
   955
            negative = true;
jaroslav@49
   956
            index++;
jaroslav@49
   957
        } else if (firstChar == '+')
jaroslav@49
   958
            index++;
jaroslav@49
   959
jaroslav@49
   960
        // Handle radix specifier, if present
jaroslav@49
   961
        if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
jaroslav@49
   962
            index += 2;
jaroslav@49
   963
            radix = 16;
jaroslav@49
   964
        }
jaroslav@49
   965
        else if (nm.startsWith("#", index)) {
jaroslav@49
   966
            index ++;
jaroslav@49
   967
            radix = 16;
jaroslav@49
   968
        }
jaroslav@49
   969
        else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
jaroslav@49
   970
            index ++;
jaroslav@49
   971
            radix = 8;
jaroslav@49
   972
        }
jaroslav@49
   973
jaroslav@49
   974
        if (nm.startsWith("-", index) || nm.startsWith("+", index))
jaroslav@49
   975
            throw new NumberFormatException("Sign character in wrong position");
jaroslav@49
   976
jaroslav@49
   977
        try {
jaroslav@49
   978
            result = Integer.valueOf(nm.substring(index), radix);
jaroslav@49
   979
            result = negative ? Integer.valueOf(-result.intValue()) : result;
jaroslav@49
   980
        } catch (NumberFormatException e) {
jaroslav@49
   981
            // If number is Integer.MIN_VALUE, we'll end up here. The next line
jaroslav@49
   982
            // handles this case, and causes any genuine format error to be
jaroslav@49
   983
            // rethrown.
jaroslav@49
   984
            String constant = negative ? ("-" + nm.substring(index))
jaroslav@49
   985
                                       : nm.substring(index);
jaroslav@49
   986
            result = Integer.valueOf(constant, radix);
jaroslav@49
   987
        }
jaroslav@49
   988
        return result;
jaroslav@49
   989
    }
jaroslav@49
   990
jaroslav@49
   991
    /**
jaroslav@49
   992
     * Compares two {@code Integer} objects numerically.
jaroslav@49
   993
     *
jaroslav@49
   994
     * @param   anotherInteger   the {@code Integer} to be compared.
jaroslav@49
   995
     * @return  the value {@code 0} if this {@code Integer} is
jaroslav@49
   996
     *          equal to the argument {@code Integer}; a value less than
jaroslav@49
   997
     *          {@code 0} if this {@code Integer} is numerically less
jaroslav@49
   998
     *          than the argument {@code Integer}; and a value greater
jaroslav@49
   999
     *          than {@code 0} if this {@code Integer} is numerically
jaroslav@49
  1000
     *           greater than the argument {@code Integer} (signed
jaroslav@49
  1001
     *           comparison).
jaroslav@49
  1002
     * @since   1.2
jaroslav@49
  1003
     */
jaroslav@49
  1004
    public int compareTo(Integer anotherInteger) {
jaroslav@49
  1005
        return compare(this.value, anotherInteger.value);
jaroslav@49
  1006
    }
jaroslav@49
  1007
jaroslav@49
  1008
    /**
jaroslav@49
  1009
     * Compares two {@code int} values numerically.
jaroslav@49
  1010
     * The value returned is identical to what would be returned by:
jaroslav@49
  1011
     * <pre>
jaroslav@49
  1012
     *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
jaroslav@49
  1013
     * </pre>
jaroslav@49
  1014
     *
jaroslav@49
  1015
     * @param  x the first {@code int} to compare
jaroslav@49
  1016
     * @param  y the second {@code int} to compare
jaroslav@49
  1017
     * @return the value {@code 0} if {@code x == y};
jaroslav@49
  1018
     *         a value less than {@code 0} if {@code x < y}; and
jaroslav@49
  1019
     *         a value greater than {@code 0} if {@code x > y}
jaroslav@49
  1020
     * @since 1.7
jaroslav@49
  1021
     */
jaroslav@49
  1022
    public static int compare(int x, int y) {
jaroslav@49
  1023
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
jaroslav@49
  1024
    }
jaroslav@49
  1025
jaroslav@49
  1026
jaroslav@49
  1027
    // Bit twiddling
jaroslav@49
  1028
jaroslav@49
  1029
    /**
jaroslav@49
  1030
     * The number of bits used to represent an {@code int} value in two's
jaroslav@49
  1031
     * complement binary form.
jaroslav@49
  1032
     *
jaroslav@49
  1033
     * @since 1.5
jaroslav@49
  1034
     */
jaroslav@49
  1035
    public static final int SIZE = 32;
jaroslav@49
  1036
jaroslav@49
  1037
    /**
jaroslav@49
  1038
     * Returns an {@code int} value with at most a single one-bit, in the
jaroslav@49
  1039
     * position of the highest-order ("leftmost") one-bit in the specified
jaroslav@49
  1040
     * {@code int} value.  Returns zero if the specified value has no
jaroslav@49
  1041
     * one-bits in its two's complement binary representation, that is, if it
jaroslav@49
  1042
     * is equal to zero.
jaroslav@49
  1043
     *
jaroslav@49
  1044
     * @return an {@code int} value with a single one-bit, in the position
jaroslav@49
  1045
     *     of the highest-order one-bit in the specified value, or zero if
jaroslav@49
  1046
     *     the specified value is itself equal to zero.
jaroslav@49
  1047
     * @since 1.5
jaroslav@49
  1048
     */
jaroslav@49
  1049
    public static int highestOneBit(int i) {
jaroslav@49
  1050
        // HD, Figure 3-1
jaroslav@49
  1051
        i |= (i >>  1);
jaroslav@49
  1052
        i |= (i >>  2);
jaroslav@49
  1053
        i |= (i >>  4);
jaroslav@49
  1054
        i |= (i >>  8);
jaroslav@49
  1055
        i |= (i >> 16);
jaroslav@49
  1056
        return i - (i >>> 1);
jaroslav@49
  1057
    }
jaroslav@49
  1058
jaroslav@49
  1059
    /**
jaroslav@49
  1060
     * Returns an {@code int} value with at most a single one-bit, in the
jaroslav@49
  1061
     * position of the lowest-order ("rightmost") one-bit in the specified
jaroslav@49
  1062
     * {@code int} value.  Returns zero if the specified value has no
jaroslav@49
  1063
     * one-bits in its two's complement binary representation, that is, if it
jaroslav@49
  1064
     * is equal to zero.
jaroslav@49
  1065
     *
jaroslav@49
  1066
     * @return an {@code int} value with a single one-bit, in the position
jaroslav@49
  1067
     *     of the lowest-order one-bit in the specified value, or zero if
jaroslav@49
  1068
     *     the specified value is itself equal to zero.
jaroslav@49
  1069
     * @since 1.5
jaroslav@49
  1070
     */
jaroslav@49
  1071
    public static int lowestOneBit(int i) {
jaroslav@49
  1072
        // HD, Section 2-1
jaroslav@49
  1073
        return i & -i;
jaroslav@49
  1074
    }
jaroslav@49
  1075
jaroslav@49
  1076
    /**
jaroslav@49
  1077
     * Returns the number of zero bits preceding the highest-order
jaroslav@49
  1078
     * ("leftmost") one-bit in the two's complement binary representation
jaroslav@49
  1079
     * of the specified {@code int} value.  Returns 32 if the
jaroslav@49
  1080
     * specified value has no one-bits in its two's complement representation,
jaroslav@49
  1081
     * in other words if it is equal to zero.
jaroslav@49
  1082
     *
jaroslav@49
  1083
     * <p>Note that this method is closely related to the logarithm base 2.
jaroslav@49
  1084
     * For all positive {@code int} values x:
jaroslav@49
  1085
     * <ul>
jaroslav@49
  1086
     * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
jaroslav@49
  1087
     * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
jaroslav@49
  1088
     * </ul>
jaroslav@49
  1089
     *
jaroslav@49
  1090
     * @return the number of zero bits preceding the highest-order
jaroslav@49
  1091
     *     ("leftmost") one-bit in the two's complement binary representation
jaroslav@49
  1092
     *     of the specified {@code int} value, or 32 if the value
jaroslav@49
  1093
     *     is equal to zero.
jaroslav@49
  1094
     * @since 1.5
jaroslav@49
  1095
     */
jaroslav@49
  1096
    public static int numberOfLeadingZeros(int i) {
jaroslav@49
  1097
        // HD, Figure 5-6
jaroslav@49
  1098
        if (i == 0)
jaroslav@49
  1099
            return 32;
jaroslav@49
  1100
        int n = 1;
jaroslav@49
  1101
        if (i >>> 16 == 0) { n += 16; i <<= 16; }
jaroslav@49
  1102
        if (i >>> 24 == 0) { n +=  8; i <<=  8; }
jaroslav@49
  1103
        if (i >>> 28 == 0) { n +=  4; i <<=  4; }
jaroslav@49
  1104
        if (i >>> 30 == 0) { n +=  2; i <<=  2; }
jaroslav@49
  1105
        n -= i >>> 31;
jaroslav@49
  1106
        return n;
jaroslav@49
  1107
    }
jaroslav@49
  1108
jaroslav@49
  1109
    /**
jaroslav@49
  1110
     * Returns the number of zero bits following the lowest-order ("rightmost")
jaroslav@49
  1111
     * one-bit in the two's complement binary representation of the specified
jaroslav@49
  1112
     * {@code int} value.  Returns 32 if the specified value has no
jaroslav@49
  1113
     * one-bits in its two's complement representation, in other words if it is
jaroslav@49
  1114
     * equal to zero.
jaroslav@49
  1115
     *
jaroslav@49
  1116
     * @return the number of zero bits following the lowest-order ("rightmost")
jaroslav@49
  1117
     *     one-bit in the two's complement binary representation of the
jaroslav@49
  1118
     *     specified {@code int} value, or 32 if the value is equal
jaroslav@49
  1119
     *     to zero.
jaroslav@49
  1120
     * @since 1.5
jaroslav@49
  1121
     */
jaroslav@49
  1122
    public static int numberOfTrailingZeros(int i) {
jaroslav@49
  1123
        // HD, Figure 5-14
jaroslav@49
  1124
        int y;
jaroslav@49
  1125
        if (i == 0) return 32;
jaroslav@49
  1126
        int n = 31;
jaroslav@49
  1127
        y = i <<16; if (y != 0) { n = n -16; i = y; }
jaroslav@49
  1128
        y = i << 8; if (y != 0) { n = n - 8; i = y; }
jaroslav@49
  1129
        y = i << 4; if (y != 0) { n = n - 4; i = y; }
jaroslav@49
  1130
        y = i << 2; if (y != 0) { n = n - 2; i = y; }
jaroslav@49
  1131
        return n - ((i << 1) >>> 31);
jaroslav@49
  1132
    }
jaroslav@49
  1133
jaroslav@49
  1134
    /**
jaroslav@49
  1135
     * Returns the number of one-bits in the two's complement binary
jaroslav@49
  1136
     * representation of the specified {@code int} value.  This function is
jaroslav@49
  1137
     * sometimes referred to as the <i>population count</i>.
jaroslav@49
  1138
     *
jaroslav@49
  1139
     * @return the number of one-bits in the two's complement binary
jaroslav@49
  1140
     *     representation of the specified {@code int} value.
jaroslav@49
  1141
     * @since 1.5
jaroslav@49
  1142
     */
jaroslav@49
  1143
    public static int bitCount(int i) {
jaroslav@49
  1144
        // HD, Figure 5-2
jaroslav@49
  1145
        i = i - ((i >>> 1) & 0x55555555);
jaroslav@49
  1146
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
jaroslav@49
  1147
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
jaroslav@49
  1148
        i = i + (i >>> 8);
jaroslav@49
  1149
        i = i + (i >>> 16);
jaroslav@49
  1150
        return i & 0x3f;
jaroslav@49
  1151
    }
jaroslav@49
  1152
jaroslav@49
  1153
    /**
jaroslav@49
  1154
     * Returns the value obtained by rotating the two's complement binary
jaroslav@49
  1155
     * representation of the specified {@code int} value left by the
jaroslav@49
  1156
     * specified number of bits.  (Bits shifted out of the left hand, or
jaroslav@49
  1157
     * high-order, side reenter on the right, or low-order.)
jaroslav@49
  1158
     *
jaroslav@49
  1159
     * <p>Note that left rotation with a negative distance is equivalent to
jaroslav@49
  1160
     * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
jaroslav@49
  1161
     * distance)}.  Note also that rotation by any multiple of 32 is a
jaroslav@49
  1162
     * no-op, so all but the last five bits of the rotation distance can be
jaroslav@49
  1163
     * ignored, even if the distance is negative: {@code rotateLeft(val,
jaroslav@49
  1164
     * distance) == rotateLeft(val, distance & 0x1F)}.
jaroslav@49
  1165
     *
jaroslav@49
  1166
     * @return the value obtained by rotating the two's complement binary
jaroslav@49
  1167
     *     representation of the specified {@code int} value left by the
jaroslav@49
  1168
     *     specified number of bits.
jaroslav@49
  1169
     * @since 1.5
jaroslav@49
  1170
     */
jaroslav@49
  1171
    public static int rotateLeft(int i, int distance) {
jaroslav@49
  1172
        return (i << distance) | (i >>> -distance);
jaroslav@49
  1173
    }
jaroslav@49
  1174
jaroslav@49
  1175
    /**
jaroslav@49
  1176
     * Returns the value obtained by rotating the two's complement binary
jaroslav@49
  1177
     * representation of the specified {@code int} value right by the
jaroslav@49
  1178
     * specified number of bits.  (Bits shifted out of the right hand, or
jaroslav@49
  1179
     * low-order, side reenter on the left, or high-order.)
jaroslav@49
  1180
     *
jaroslav@49
  1181
     * <p>Note that right rotation with a negative distance is equivalent to
jaroslav@49
  1182
     * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
jaroslav@49
  1183
     * distance)}.  Note also that rotation by any multiple of 32 is a
jaroslav@49
  1184
     * no-op, so all but the last five bits of the rotation distance can be
jaroslav@49
  1185
     * ignored, even if the distance is negative: {@code rotateRight(val,
jaroslav@49
  1186
     * distance) == rotateRight(val, distance & 0x1F)}.
jaroslav@49
  1187
     *
jaroslav@49
  1188
     * @return the value obtained by rotating the two's complement binary
jaroslav@49
  1189
     *     representation of the specified {@code int} value right by the
jaroslav@49
  1190
     *     specified number of bits.
jaroslav@49
  1191
     * @since 1.5
jaroslav@49
  1192
     */
jaroslav@49
  1193
    public static int rotateRight(int i, int distance) {
jaroslav@49
  1194
        return (i >>> distance) | (i << -distance);
jaroslav@49
  1195
    }
jaroslav@49
  1196
jaroslav@49
  1197
    /**
jaroslav@49
  1198
     * Returns the value obtained by reversing the order of the bits in the
jaroslav@49
  1199
     * two's complement binary representation of the specified {@code int}
jaroslav@49
  1200
     * value.
jaroslav@49
  1201
     *
jaroslav@49
  1202
     * @return the value obtained by reversing order of the bits in the
jaroslav@49
  1203
     *     specified {@code int} value.
jaroslav@49
  1204
     * @since 1.5
jaroslav@49
  1205
     */
jaroslav@49
  1206
    public static int reverse(int i) {
jaroslav@49
  1207
        // HD, Figure 7-1
jaroslav@49
  1208
        i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
jaroslav@49
  1209
        i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
jaroslav@49
  1210
        i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
jaroslav@49
  1211
        i = (i << 24) | ((i & 0xff00) << 8) |
jaroslav@49
  1212
            ((i >>> 8) & 0xff00) | (i >>> 24);
jaroslav@49
  1213
        return i;
jaroslav@49
  1214
    }
jaroslav@49
  1215
jaroslav@49
  1216
    /**
jaroslav@49
  1217
     * Returns the signum function of the specified {@code int} value.  (The
jaroslav@49
  1218
     * return value is -1 if the specified value is negative; 0 if the
jaroslav@49
  1219
     * specified value is zero; and 1 if the specified value is positive.)
jaroslav@49
  1220
     *
jaroslav@49
  1221
     * @return the signum function of the specified {@code int} value.
jaroslav@49
  1222
     * @since 1.5
jaroslav@49
  1223
     */
jaroslav@49
  1224
    public static int signum(int i) {
jaroslav@49
  1225
        // HD, Section 2-7
jaroslav@49
  1226
        return (i >> 31) | (-i >>> 31);
jaroslav@49
  1227
    }
jaroslav@49
  1228
jaroslav@49
  1229
    /**
jaroslav@49
  1230
     * Returns the value obtained by reversing the order of the bytes in the
jaroslav@49
  1231
     * two's complement representation of the specified {@code int} value.
jaroslav@49
  1232
     *
jaroslav@49
  1233
     * @return the value obtained by reversing the bytes in the specified
jaroslav@49
  1234
     *     {@code int} value.
jaroslav@49
  1235
     * @since 1.5
jaroslav@49
  1236
     */
jaroslav@49
  1237
    public static int reverseBytes(int i) {
jaroslav@49
  1238
        return ((i >>> 24)           ) |
jaroslav@49
  1239
               ((i >>   8) &   0xFF00) |
jaroslav@49
  1240
               ((i <<   8) & 0xFF0000) |
jaroslav@49
  1241
               ((i << 24));
jaroslav@49
  1242
    }
jaroslav@49
  1243
jaroslav@49
  1244
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
jaroslav@49
  1245
    private static final long serialVersionUID = 1360826667806852920L;
jaroslav@49
  1246
}