emul/src/main/java/java/lang/Integer.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 28 Sep 2012 17:59:03 +0200
branchemul
changeset 49 0a115f1c6f3c
child 84 d65b3a2fbfaf
permissions -rw-r--r--
Bringing in core Java classes as of OpenJDK tag jdk7-b147
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@49
    28
import java.util.Properties;
jaroslav@49
    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@49
   327
    public static String toString(int i) {
jaroslav@49
   328
        if (i == Integer.MIN_VALUE)
jaroslav@49
   329
            return "-2147483648";
jaroslav@49
   330
        int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
jaroslav@49
   331
        char[] buf = new char[size];
jaroslav@49
   332
        getChars(i, size, buf);
jaroslav@49
   333
        return new String(0, size, buf);
jaroslav@49
   334
    }
jaroslav@49
   335
jaroslav@49
   336
    /**
jaroslav@49
   337
     * Places characters representing the integer i into the
jaroslav@49
   338
     * character array buf. The characters are placed into
jaroslav@49
   339
     * the buffer backwards starting with the least significant
jaroslav@49
   340
     * digit at the specified index (exclusive), and working
jaroslav@49
   341
     * backwards from there.
jaroslav@49
   342
     *
jaroslav@49
   343
     * Will fail if i == Integer.MIN_VALUE
jaroslav@49
   344
     */
jaroslav@49
   345
    static void getChars(int i, int index, char[] buf) {
jaroslav@49
   346
        int q, r;
jaroslav@49
   347
        int charPos = index;
jaroslav@49
   348
        char sign = 0;
jaroslav@49
   349
jaroslav@49
   350
        if (i < 0) {
jaroslav@49
   351
            sign = '-';
jaroslav@49
   352
            i = -i;
jaroslav@49
   353
        }
jaroslav@49
   354
jaroslav@49
   355
        // Generate two digits per iteration
jaroslav@49
   356
        while (i >= 65536) {
jaroslav@49
   357
            q = i / 100;
jaroslav@49
   358
        // really: r = i - (q * 100);
jaroslav@49
   359
            r = i - ((q << 6) + (q << 5) + (q << 2));
jaroslav@49
   360
            i = q;
jaroslav@49
   361
            buf [--charPos] = DigitOnes[r];
jaroslav@49
   362
            buf [--charPos] = DigitTens[r];
jaroslav@49
   363
        }
jaroslav@49
   364
jaroslav@49
   365
        // Fall thru to fast mode for smaller numbers
jaroslav@49
   366
        // assert(i <= 65536, i);
jaroslav@49
   367
        for (;;) {
jaroslav@49
   368
            q = (i * 52429) >>> (16+3);
jaroslav@49
   369
            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...
jaroslav@49
   370
            buf [--charPos] = digits [r];
jaroslav@49
   371
            i = q;
jaroslav@49
   372
            if (i == 0) break;
jaroslav@49
   373
        }
jaroslav@49
   374
        if (sign != 0) {
jaroslav@49
   375
            buf [--charPos] = sign;
jaroslav@49
   376
        }
jaroslav@49
   377
    }
jaroslav@49
   378
jaroslav@49
   379
    final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
jaroslav@49
   380
                                      99999999, 999999999, Integer.MAX_VALUE };
jaroslav@49
   381
jaroslav@49
   382
    // Requires positive x
jaroslav@49
   383
    static int stringSize(int x) {
jaroslav@49
   384
        for (int i=0; ; i++)
jaroslav@49
   385
            if (x <= sizeTable[i])
jaroslav@49
   386
                return i+1;
jaroslav@49
   387
    }
jaroslav@49
   388
jaroslav@49
   389
    /**
jaroslav@49
   390
     * Parses the string argument as a signed integer in the radix
jaroslav@49
   391
     * specified by the second argument. The characters in the string
jaroslav@49
   392
     * must all be digits of the specified radix (as determined by
jaroslav@49
   393
     * whether {@link java.lang.Character#digit(char, int)} returns a
jaroslav@49
   394
     * nonnegative value), except that the first character may be an
jaroslav@49
   395
     * ASCII minus sign {@code '-'} (<code>'&#92;u002D'</code>) to
jaroslav@49
   396
     * indicate a negative value or an ASCII plus sign {@code '+'}
jaroslav@49
   397
     * (<code>'&#92;u002B'</code>) to indicate a positive value. The
jaroslav@49
   398
     * resulting integer value is returned.
jaroslav@49
   399
     *
jaroslav@49
   400
     * <p>An exception of type {@code NumberFormatException} is
jaroslav@49
   401
     * thrown if any of the following situations occurs:
jaroslav@49
   402
     * <ul>
jaroslav@49
   403
     * <li>The first argument is {@code null} or is a string of
jaroslav@49
   404
     * length zero.
jaroslav@49
   405
     *
jaroslav@49
   406
     * <li>The radix is either smaller than
jaroslav@49
   407
     * {@link java.lang.Character#MIN_RADIX} or
jaroslav@49
   408
     * larger than {@link java.lang.Character#MAX_RADIX}.
jaroslav@49
   409
     *
jaroslav@49
   410
     * <li>Any character of the string is not a digit of the specified
jaroslav@49
   411
     * radix, except that the first character may be a minus sign
jaroslav@49
   412
     * {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
jaroslav@49
   413
     * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
jaroslav@49
   414
     * string is longer than length 1.
jaroslav@49
   415
     *
jaroslav@49
   416
     * <li>The value represented by the string is not a value of type
jaroslav@49
   417
     * {@code int}.
jaroslav@49
   418
     * </ul>
jaroslav@49
   419
     *
jaroslav@49
   420
     * <p>Examples:
jaroslav@49
   421
     * <blockquote><pre>
jaroslav@49
   422
     * parseInt("0", 10) returns 0
jaroslav@49
   423
     * parseInt("473", 10) returns 473
jaroslav@49
   424
     * parseInt("+42", 10) returns 42
jaroslav@49
   425
     * parseInt("-0", 10) returns 0
jaroslav@49
   426
     * parseInt("-FF", 16) returns -255
jaroslav@49
   427
     * parseInt("1100110", 2) returns 102
jaroslav@49
   428
     * parseInt("2147483647", 10) returns 2147483647
jaroslav@49
   429
     * parseInt("-2147483648", 10) returns -2147483648
jaroslav@49
   430
     * parseInt("2147483648", 10) throws a NumberFormatException
jaroslav@49
   431
     * parseInt("99", 8) throws a NumberFormatException
jaroslav@49
   432
     * parseInt("Kona", 10) throws a NumberFormatException
jaroslav@49
   433
     * parseInt("Kona", 27) returns 411787
jaroslav@49
   434
     * </pre></blockquote>
jaroslav@49
   435
     *
jaroslav@49
   436
     * @param      s   the {@code String} containing the integer
jaroslav@49
   437
     *                  representation to be parsed
jaroslav@49
   438
     * @param      radix   the radix to be used while parsing {@code s}.
jaroslav@49
   439
     * @return     the integer represented by the string argument in the
jaroslav@49
   440
     *             specified radix.
jaroslav@49
   441
     * @exception  NumberFormatException if the {@code String}
jaroslav@49
   442
     *             does not contain a parsable {@code int}.
jaroslav@49
   443
     */
jaroslav@49
   444
    public static int parseInt(String s, int radix)
jaroslav@49
   445
                throws NumberFormatException
jaroslav@49
   446
    {
jaroslav@49
   447
        /*
jaroslav@49
   448
         * WARNING: This method may be invoked early during VM initialization
jaroslav@49
   449
         * before IntegerCache is initialized. Care must be taken to not use
jaroslav@49
   450
         * the valueOf method.
jaroslav@49
   451
         */
jaroslav@49
   452
jaroslav@49
   453
        if (s == null) {
jaroslav@49
   454
            throw new NumberFormatException("null");
jaroslav@49
   455
        }
jaroslav@49
   456
jaroslav@49
   457
        if (radix < Character.MIN_RADIX) {
jaroslav@49
   458
            throw new NumberFormatException("radix " + radix +
jaroslav@49
   459
                                            " less than Character.MIN_RADIX");
jaroslav@49
   460
        }
jaroslav@49
   461
jaroslav@49
   462
        if (radix > Character.MAX_RADIX) {
jaroslav@49
   463
            throw new NumberFormatException("radix " + radix +
jaroslav@49
   464
                                            " greater than Character.MAX_RADIX");
jaroslav@49
   465
        }
jaroslav@49
   466
jaroslav@49
   467
        int result = 0;
jaroslav@49
   468
        boolean negative = false;
jaroslav@49
   469
        int i = 0, len = s.length();
jaroslav@49
   470
        int limit = -Integer.MAX_VALUE;
jaroslav@49
   471
        int multmin;
jaroslav@49
   472
        int digit;
jaroslav@49
   473
jaroslav@49
   474
        if (len > 0) {
jaroslav@49
   475
            char firstChar = s.charAt(0);
jaroslav@49
   476
            if (firstChar < '0') { // Possible leading "+" or "-"
jaroslav@49
   477
                if (firstChar == '-') {
jaroslav@49
   478
                    negative = true;
jaroslav@49
   479
                    limit = Integer.MIN_VALUE;
jaroslav@49
   480
                } else if (firstChar != '+')
jaroslav@49
   481
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   482
jaroslav@49
   483
                if (len == 1) // Cannot have lone "+" or "-"
jaroslav@49
   484
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   485
                i++;
jaroslav@49
   486
            }
jaroslav@49
   487
            multmin = limit / radix;
jaroslav@49
   488
            while (i < len) {
jaroslav@49
   489
                // Accumulating negatively avoids surprises near MAX_VALUE
jaroslav@49
   490
                digit = Character.digit(s.charAt(i++),radix);
jaroslav@49
   491
                if (digit < 0) {
jaroslav@49
   492
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   493
                }
jaroslav@49
   494
                if (result < multmin) {
jaroslav@49
   495
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   496
                }
jaroslav@49
   497
                result *= radix;
jaroslav@49
   498
                if (result < limit + digit) {
jaroslav@49
   499
                    throw NumberFormatException.forInputString(s);
jaroslav@49
   500
                }
jaroslav@49
   501
                result -= digit;
jaroslav@49
   502
            }
jaroslav@49
   503
        } else {
jaroslav@49
   504
            throw NumberFormatException.forInputString(s);
jaroslav@49
   505
        }
jaroslav@49
   506
        return negative ? result : -result;
jaroslav@49
   507
    }
jaroslav@49
   508
jaroslav@49
   509
    /**
jaroslav@49
   510
     * Parses the string argument as a signed decimal integer. The
jaroslav@49
   511
     * characters in the string must all be decimal digits, except
jaroslav@49
   512
     * that the first character may be an ASCII minus sign {@code '-'}
jaroslav@49
   513
     * (<code>'&#92;u002D'</code>) to indicate a negative value or an
jaroslav@49
   514
     * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
jaroslav@49
   515
     * indicate a positive value. The resulting integer value is
jaroslav@49
   516
     * returned, exactly as if the argument and the radix 10 were
jaroslav@49
   517
     * given as arguments to the {@link #parseInt(java.lang.String,
jaroslav@49
   518
     * int)} method.
jaroslav@49
   519
     *
jaroslav@49
   520
     * @param s    a {@code String} containing the {@code int}
jaroslav@49
   521
     *             representation to be parsed
jaroslav@49
   522
     * @return     the integer value represented by the argument in decimal.
jaroslav@49
   523
     * @exception  NumberFormatException  if the string does not contain a
jaroslav@49
   524
     *               parsable integer.
jaroslav@49
   525
     */
jaroslav@49
   526
    public static int parseInt(String s) throws NumberFormatException {
jaroslav@49
   527
        return parseInt(s,10);
jaroslav@49
   528
    }
jaroslav@49
   529
jaroslav@49
   530
    /**
jaroslav@49
   531
     * Returns an {@code Integer} object holding the value
jaroslav@49
   532
     * extracted from the specified {@code String} when parsed
jaroslav@49
   533
     * with the radix given by the second argument. The first argument
jaroslav@49
   534
     * is interpreted as representing a signed integer in the radix
jaroslav@49
   535
     * specified by the second argument, exactly as if the arguments
jaroslav@49
   536
     * were given to the {@link #parseInt(java.lang.String, int)}
jaroslav@49
   537
     * method. The result is an {@code Integer} object that
jaroslav@49
   538
     * represents the integer value specified by the string.
jaroslav@49
   539
     *
jaroslav@49
   540
     * <p>In other words, this method returns an {@code Integer}
jaroslav@49
   541
     * object equal to the value of:
jaroslav@49
   542
     *
jaroslav@49
   543
     * <blockquote>
jaroslav@49
   544
     *  {@code new Integer(Integer.parseInt(s, radix))}
jaroslav@49
   545
     * </blockquote>
jaroslav@49
   546
     *
jaroslav@49
   547
     * @param      s   the string to be parsed.
jaroslav@49
   548
     * @param      radix the radix to be used in interpreting {@code s}
jaroslav@49
   549
     * @return     an {@code Integer} object holding the value
jaroslav@49
   550
     *             represented by the string argument in the specified
jaroslav@49
   551
     *             radix.
jaroslav@49
   552
     * @exception NumberFormatException if the {@code String}
jaroslav@49
   553
     *            does not contain a parsable {@code int}.
jaroslav@49
   554
     */
jaroslav@49
   555
    public static Integer valueOf(String s, int radix) throws NumberFormatException {
jaroslav@49
   556
        return Integer.valueOf(parseInt(s,radix));
jaroslav@49
   557
    }
jaroslav@49
   558
jaroslav@49
   559
    /**
jaroslav@49
   560
     * Returns an {@code Integer} object holding the
jaroslav@49
   561
     * value of the specified {@code String}. The argument is
jaroslav@49
   562
     * interpreted as representing a signed decimal integer, exactly
jaroslav@49
   563
     * as if the argument were given to the {@link
jaroslav@49
   564
     * #parseInt(java.lang.String)} method. The result is an
jaroslav@49
   565
     * {@code Integer} object that represents the integer value
jaroslav@49
   566
     * specified by the string.
jaroslav@49
   567
     *
jaroslav@49
   568
     * <p>In other words, this method returns an {@code Integer}
jaroslav@49
   569
     * object equal to the value of:
jaroslav@49
   570
     *
jaroslav@49
   571
     * <blockquote>
jaroslav@49
   572
     *  {@code new Integer(Integer.parseInt(s))}
jaroslav@49
   573
     * </blockquote>
jaroslav@49
   574
     *
jaroslav@49
   575
     * @param      s   the string to be parsed.
jaroslav@49
   576
     * @return     an {@code Integer} object holding the value
jaroslav@49
   577
     *             represented by the string argument.
jaroslav@49
   578
     * @exception  NumberFormatException  if the string cannot be parsed
jaroslav@49
   579
     *             as an integer.
jaroslav@49
   580
     */
jaroslav@49
   581
    public static Integer valueOf(String s) throws NumberFormatException {
jaroslav@49
   582
        return Integer.valueOf(parseInt(s, 10));
jaroslav@49
   583
    }
jaroslav@49
   584
jaroslav@49
   585
    /**
jaroslav@49
   586
     * Cache to support the object identity semantics of autoboxing for values between
jaroslav@49
   587
     * -128 and 127 (inclusive) as required by JLS.
jaroslav@49
   588
     *
jaroslav@49
   589
     * The cache is initialized on first usage.  The size of the cache
jaroslav@49
   590
     * may be controlled by the -XX:AutoBoxCacheMax=<size> option.
jaroslav@49
   591
     * During VM initialization, java.lang.Integer.IntegerCache.high property
jaroslav@49
   592
     * may be set and saved in the private system properties in the
jaroslav@49
   593
     * sun.misc.VM class.
jaroslav@49
   594
     */
jaroslav@49
   595
jaroslav@49
   596
    private static class IntegerCache {
jaroslav@49
   597
        static final int low = -128;
jaroslav@49
   598
        static final int high;
jaroslav@49
   599
        static final Integer cache[];
jaroslav@49
   600
jaroslav@49
   601
        static {
jaroslav@49
   602
            // high value may be configured by property
jaroslav@49
   603
            int h = 127;
jaroslav@49
   604
            String integerCacheHighPropValue =
jaroslav@49
   605
                sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
jaroslav@49
   606
            if (integerCacheHighPropValue != null) {
jaroslav@49
   607
                int i = parseInt(integerCacheHighPropValue);
jaroslav@49
   608
                i = Math.max(i, 127);
jaroslav@49
   609
                // Maximum array size is Integer.MAX_VALUE
jaroslav@49
   610
                h = Math.min(i, Integer.MAX_VALUE - (-low));
jaroslav@49
   611
            }
jaroslav@49
   612
            high = h;
jaroslav@49
   613
jaroslav@49
   614
            cache = new Integer[(high - low) + 1];
jaroslav@49
   615
            int j = low;
jaroslav@49
   616
            for(int k = 0; k < cache.length; k++)
jaroslav@49
   617
                cache[k] = new Integer(j++);
jaroslav@49
   618
        }
jaroslav@49
   619
jaroslav@49
   620
        private IntegerCache() {}
jaroslav@49
   621
    }
jaroslav@49
   622
jaroslav@49
   623
    /**
jaroslav@49
   624
     * Returns an {@code Integer} instance representing the specified
jaroslav@49
   625
     * {@code int} value.  If a new {@code Integer} instance is not
jaroslav@49
   626
     * required, this method should generally be used in preference to
jaroslav@49
   627
     * the constructor {@link #Integer(int)}, as this method is likely
jaroslav@49
   628
     * to yield significantly better space and time performance by
jaroslav@49
   629
     * caching frequently requested values.
jaroslav@49
   630
     *
jaroslav@49
   631
     * This method will always cache values in the range -128 to 127,
jaroslav@49
   632
     * inclusive, and may cache other values outside of this range.
jaroslav@49
   633
     *
jaroslav@49
   634
     * @param  i an {@code int} value.
jaroslav@49
   635
     * @return an {@code Integer} instance representing {@code i}.
jaroslav@49
   636
     * @since  1.5
jaroslav@49
   637
     */
jaroslav@49
   638
    public static Integer valueOf(int i) {
jaroslav@49
   639
        assert IntegerCache.high >= 127;
jaroslav@49
   640
        if (i >= IntegerCache.low && i <= IntegerCache.high)
jaroslav@49
   641
            return IntegerCache.cache[i + (-IntegerCache.low)];
jaroslav@49
   642
        return new Integer(i);
jaroslav@49
   643
    }
jaroslav@49
   644
jaroslav@49
   645
    /**
jaroslav@49
   646
     * The value of the {@code Integer}.
jaroslav@49
   647
     *
jaroslav@49
   648
     * @serial
jaroslav@49
   649
     */
jaroslav@49
   650
    private final int value;
jaroslav@49
   651
jaroslav@49
   652
    /**
jaroslav@49
   653
     * Constructs a newly allocated {@code Integer} object that
jaroslav@49
   654
     * represents the specified {@code int} value.
jaroslav@49
   655
     *
jaroslav@49
   656
     * @param   value   the value to be represented by the
jaroslav@49
   657
     *                  {@code Integer} object.
jaroslav@49
   658
     */
jaroslav@49
   659
    public Integer(int value) {
jaroslav@49
   660
        this.value = value;
jaroslav@49
   661
    }
jaroslav@49
   662
jaroslav@49
   663
    /**
jaroslav@49
   664
     * Constructs a newly allocated {@code Integer} object that
jaroslav@49
   665
     * represents the {@code int} value indicated by the
jaroslav@49
   666
     * {@code String} parameter. The string is converted to an
jaroslav@49
   667
     * {@code int} value in exactly the manner used by the
jaroslav@49
   668
     * {@code parseInt} method for radix 10.
jaroslav@49
   669
     *
jaroslav@49
   670
     * @param      s   the {@code String} to be converted to an
jaroslav@49
   671
     *                 {@code Integer}.
jaroslav@49
   672
     * @exception  NumberFormatException  if the {@code String} does not
jaroslav@49
   673
     *               contain a parsable integer.
jaroslav@49
   674
     * @see        java.lang.Integer#parseInt(java.lang.String, int)
jaroslav@49
   675
     */
jaroslav@49
   676
    public Integer(String s) throws NumberFormatException {
jaroslav@49
   677
        this.value = parseInt(s, 10);
jaroslav@49
   678
    }
jaroslav@49
   679
jaroslav@49
   680
    /**
jaroslav@49
   681
     * Returns the value of this {@code Integer} as a
jaroslav@49
   682
     * {@code byte}.
jaroslav@49
   683
     */
jaroslav@49
   684
    public byte byteValue() {
jaroslav@49
   685
        return (byte)value;
jaroslav@49
   686
    }
jaroslav@49
   687
jaroslav@49
   688
    /**
jaroslav@49
   689
     * Returns the value of this {@code Integer} as a
jaroslav@49
   690
     * {@code short}.
jaroslav@49
   691
     */
jaroslav@49
   692
    public short shortValue() {
jaroslav@49
   693
        return (short)value;
jaroslav@49
   694
    }
jaroslav@49
   695
jaroslav@49
   696
    /**
jaroslav@49
   697
     * Returns the value of this {@code Integer} as an
jaroslav@49
   698
     * {@code int}.
jaroslav@49
   699
     */
jaroslav@49
   700
    public int intValue() {
jaroslav@49
   701
        return value;
jaroslav@49
   702
    }
jaroslav@49
   703
jaroslav@49
   704
    /**
jaroslav@49
   705
     * Returns the value of this {@code Integer} as a
jaroslav@49
   706
     * {@code long}.
jaroslav@49
   707
     */
jaroslav@49
   708
    public long longValue() {
jaroslav@49
   709
        return (long)value;
jaroslav@49
   710
    }
jaroslav@49
   711
jaroslav@49
   712
    /**
jaroslav@49
   713
     * Returns the value of this {@code Integer} as a
jaroslav@49
   714
     * {@code float}.
jaroslav@49
   715
     */
jaroslav@49
   716
    public float floatValue() {
jaroslav@49
   717
        return (float)value;
jaroslav@49
   718
    }
jaroslav@49
   719
jaroslav@49
   720
    /**
jaroslav@49
   721
     * Returns the value of this {@code Integer} as a
jaroslav@49
   722
     * {@code double}.
jaroslav@49
   723
     */
jaroslav@49
   724
    public double doubleValue() {
jaroslav@49
   725
        return (double)value;
jaroslav@49
   726
    }
jaroslav@49
   727
jaroslav@49
   728
    /**
jaroslav@49
   729
     * Returns a {@code String} object representing this
jaroslav@49
   730
     * {@code Integer}'s value. The value is converted to signed
jaroslav@49
   731
     * decimal representation and returned as a string, exactly as if
jaroslav@49
   732
     * the integer value were given as an argument to the {@link
jaroslav@49
   733
     * java.lang.Integer#toString(int)} method.
jaroslav@49
   734
     *
jaroslav@49
   735
     * @return  a string representation of the value of this object in
jaroslav@49
   736
     *          base&nbsp;10.
jaroslav@49
   737
     */
jaroslav@49
   738
    public String toString() {
jaroslav@49
   739
        return toString(value);
jaroslav@49
   740
    }
jaroslav@49
   741
jaroslav@49
   742
    /**
jaroslav@49
   743
     * Returns a hash code for this {@code Integer}.
jaroslav@49
   744
     *
jaroslav@49
   745
     * @return  a hash code value for this object, equal to the
jaroslav@49
   746
     *          primitive {@code int} value represented by this
jaroslav@49
   747
     *          {@code Integer} object.
jaroslav@49
   748
     */
jaroslav@49
   749
    public int hashCode() {
jaroslav@49
   750
        return value;
jaroslav@49
   751
    }
jaroslav@49
   752
jaroslav@49
   753
    /**
jaroslav@49
   754
     * Compares this object to the specified object.  The result is
jaroslav@49
   755
     * {@code true} if and only if the argument is not
jaroslav@49
   756
     * {@code null} and is an {@code Integer} object that
jaroslav@49
   757
     * contains the same {@code int} value as this object.
jaroslav@49
   758
     *
jaroslav@49
   759
     * @param   obj   the object to compare with.
jaroslav@49
   760
     * @return  {@code true} if the objects are the same;
jaroslav@49
   761
     *          {@code false} otherwise.
jaroslav@49
   762
     */
jaroslav@49
   763
    public boolean equals(Object obj) {
jaroslav@49
   764
        if (obj instanceof Integer) {
jaroslav@49
   765
            return value == ((Integer)obj).intValue();
jaroslav@49
   766
        }
jaroslav@49
   767
        return false;
jaroslav@49
   768
    }
jaroslav@49
   769
jaroslav@49
   770
    /**
jaroslav@49
   771
     * Determines the integer value of the system property with the
jaroslav@49
   772
     * specified name.
jaroslav@49
   773
     *
jaroslav@49
   774
     * <p>The first argument is treated as the name of a system property.
jaroslav@49
   775
     * System properties are accessible through the
jaroslav@49
   776
     * {@link java.lang.System#getProperty(java.lang.String)} method. The
jaroslav@49
   777
     * string value of this property is then interpreted as an integer
jaroslav@49
   778
     * value and an {@code Integer} object representing this value is
jaroslav@49
   779
     * returned. Details of possible numeric formats can be found with
jaroslav@49
   780
     * the definition of {@code getProperty}.
jaroslav@49
   781
     *
jaroslav@49
   782
     * <p>If there is no property with the specified name, if the specified name
jaroslav@49
   783
     * is empty or {@code null}, or if the property does not have
jaroslav@49
   784
     * the correct numeric format, then {@code null} is returned.
jaroslav@49
   785
     *
jaroslav@49
   786
     * <p>In other words, this method returns an {@code Integer}
jaroslav@49
   787
     * object equal to the value of:
jaroslav@49
   788
     *
jaroslav@49
   789
     * <blockquote>
jaroslav@49
   790
     *  {@code getInteger(nm, null)}
jaroslav@49
   791
     * </blockquote>
jaroslav@49
   792
     *
jaroslav@49
   793
     * @param   nm   property name.
jaroslav@49
   794
     * @return  the {@code Integer} value of the property.
jaroslav@49
   795
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@49
   796
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@49
   797
     */
jaroslav@49
   798
    public static Integer getInteger(String nm) {
jaroslav@49
   799
        return getInteger(nm, null);
jaroslav@49
   800
    }
jaroslav@49
   801
jaroslav@49
   802
    /**
jaroslav@49
   803
     * Determines the integer value of the system property with the
jaroslav@49
   804
     * specified name.
jaroslav@49
   805
     *
jaroslav@49
   806
     * <p>The first argument is treated as the name of a system property.
jaroslav@49
   807
     * System properties are accessible through the {@link
jaroslav@49
   808
     * java.lang.System#getProperty(java.lang.String)} method. The
jaroslav@49
   809
     * string value of this property is then interpreted as an integer
jaroslav@49
   810
     * value and an {@code Integer} object representing this value is
jaroslav@49
   811
     * returned. Details of possible numeric formats can be found with
jaroslav@49
   812
     * the definition of {@code getProperty}.
jaroslav@49
   813
     *
jaroslav@49
   814
     * <p>The second argument is the default value. An {@code Integer} object
jaroslav@49
   815
     * that represents the value of the second argument is returned if there
jaroslav@49
   816
     * is no property of the specified name, if the property does not have
jaroslav@49
   817
     * the correct numeric format, or if the specified name is empty or
jaroslav@49
   818
     * {@code null}.
jaroslav@49
   819
     *
jaroslav@49
   820
     * <p>In other words, this method returns an {@code Integer} object
jaroslav@49
   821
     * equal to the value of:
jaroslav@49
   822
     *
jaroslav@49
   823
     * <blockquote>
jaroslav@49
   824
     *  {@code getInteger(nm, new Integer(val))}
jaroslav@49
   825
     * </blockquote>
jaroslav@49
   826
     *
jaroslav@49
   827
     * but in practice it may be implemented in a manner such as:
jaroslav@49
   828
     *
jaroslav@49
   829
     * <blockquote><pre>
jaroslav@49
   830
     * Integer result = getInteger(nm, null);
jaroslav@49
   831
     * return (result == null) ? new Integer(val) : result;
jaroslav@49
   832
     * </pre></blockquote>
jaroslav@49
   833
     *
jaroslav@49
   834
     * to avoid the unnecessary allocation of an {@code Integer}
jaroslav@49
   835
     * object when the default value is not needed.
jaroslav@49
   836
     *
jaroslav@49
   837
     * @param   nm   property name.
jaroslav@49
   838
     * @param   val   default value.
jaroslav@49
   839
     * @return  the {@code Integer} value of the property.
jaroslav@49
   840
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@49
   841
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@49
   842
     */
jaroslav@49
   843
    public static Integer getInteger(String nm, int val) {
jaroslav@49
   844
        Integer result = getInteger(nm, null);
jaroslav@49
   845
        return (result == null) ? Integer.valueOf(val) : result;
jaroslav@49
   846
    }
jaroslav@49
   847
jaroslav@49
   848
    /**
jaroslav@49
   849
     * Returns the integer value of the system property with the
jaroslav@49
   850
     * specified name.  The first argument is treated as the name of a
jaroslav@49
   851
     * system property.  System properties are accessible through the
jaroslav@49
   852
     * {@link java.lang.System#getProperty(java.lang.String)} method.
jaroslav@49
   853
     * The string value of this property is then interpreted as an
jaroslav@49
   854
     * integer value, as per the {@code Integer.decode} method,
jaroslav@49
   855
     * and an {@code Integer} object representing this value is
jaroslav@49
   856
     * returned.
jaroslav@49
   857
     *
jaroslav@49
   858
     * <ul><li>If the property value begins with the two ASCII characters
jaroslav@49
   859
     *         {@code 0x} or the ASCII character {@code #}, not
jaroslav@49
   860
     *      followed by a minus sign, then the rest of it is parsed as a
jaroslav@49
   861
     *      hexadecimal integer exactly as by the method
jaroslav@49
   862
     *      {@link #valueOf(java.lang.String, int)} with radix 16.
jaroslav@49
   863
     * <li>If the property value begins with the ASCII character
jaroslav@49
   864
     *     {@code 0} followed by another character, it is parsed as an
jaroslav@49
   865
     *     octal integer exactly as by the method
jaroslav@49
   866
     *     {@link #valueOf(java.lang.String, int)} with radix 8.
jaroslav@49
   867
     * <li>Otherwise, the property value is parsed as a decimal integer
jaroslav@49
   868
     * exactly as by the method {@link #valueOf(java.lang.String, int)}
jaroslav@49
   869
     * with radix 10.
jaroslav@49
   870
     * </ul>
jaroslav@49
   871
     *
jaroslav@49
   872
     * <p>The second argument is the default value. The default value is
jaroslav@49
   873
     * returned if there is no property of the specified name, if the
jaroslav@49
   874
     * property does not have the correct numeric format, or if the
jaroslav@49
   875
     * specified name is empty or {@code null}.
jaroslav@49
   876
     *
jaroslav@49
   877
     * @param   nm   property name.
jaroslav@49
   878
     * @param   val   default value.
jaroslav@49
   879
     * @return  the {@code Integer} value of the property.
jaroslav@49
   880
     * @see     java.lang.System#getProperty(java.lang.String)
jaroslav@49
   881
     * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
jaroslav@49
   882
     * @see java.lang.Integer#decode
jaroslav@49
   883
     */
jaroslav@49
   884
    public static Integer getInteger(String nm, Integer val) {
jaroslav@49
   885
        String v = null;
jaroslav@49
   886
        try {
jaroslav@49
   887
            v = System.getProperty(nm);
jaroslav@49
   888
        } catch (IllegalArgumentException e) {
jaroslav@49
   889
        } catch (NullPointerException e) {
jaroslav@49
   890
        }
jaroslav@49
   891
        if (v != null) {
jaroslav@49
   892
            try {
jaroslav@49
   893
                return Integer.decode(v);
jaroslav@49
   894
            } catch (NumberFormatException e) {
jaroslav@49
   895
            }
jaroslav@49
   896
        }
jaroslav@49
   897
        return val;
jaroslav@49
   898
    }
jaroslav@49
   899
jaroslav@49
   900
    /**
jaroslav@49
   901
     * Decodes a {@code String} into an {@code Integer}.
jaroslav@49
   902
     * Accepts decimal, hexadecimal, and octal numbers given
jaroslav@49
   903
     * by the following grammar:
jaroslav@49
   904
     *
jaroslav@49
   905
     * <blockquote>
jaroslav@49
   906
     * <dl>
jaroslav@49
   907
     * <dt><i>DecodableString:</i>
jaroslav@49
   908
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
jaroslav@49
   909
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
jaroslav@49
   910
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
jaroslav@49
   911
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
jaroslav@49
   912
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
jaroslav@49
   913
     * <p>
jaroslav@49
   914
     * <dt><i>Sign:</i>
jaroslav@49
   915
     * <dd>{@code -}
jaroslav@49
   916
     * <dd>{@code +}
jaroslav@49
   917
     * </dl>
jaroslav@49
   918
     * </blockquote>
jaroslav@49
   919
     *
jaroslav@49
   920
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
jaroslav@49
   921
     * are as defined in section 3.10.1 of
jaroslav@49
   922
     * <cite>The Java&trade; Language Specification</cite>,
jaroslav@49
   923
     * except that underscores are not accepted between digits.
jaroslav@49
   924
     *
jaroslav@49
   925
     * <p>The sequence of characters following an optional
jaroslav@49
   926
     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
jaroslav@49
   927
     * "{@code #}", or leading zero) is parsed as by the {@code
jaroslav@49
   928
     * Integer.parseInt} method with the indicated radix (10, 16, or
jaroslav@49
   929
     * 8).  This sequence of characters must represent a positive
jaroslav@49
   930
     * value or a {@link NumberFormatException} will be thrown.  The
jaroslav@49
   931
     * result is negated if first character of the specified {@code
jaroslav@49
   932
     * String} is the minus sign.  No whitespace characters are
jaroslav@49
   933
     * permitted in the {@code String}.
jaroslav@49
   934
     *
jaroslav@49
   935
     * @param     nm the {@code String} to decode.
jaroslav@49
   936
     * @return    an {@code Integer} object holding the {@code int}
jaroslav@49
   937
     *             value represented by {@code nm}
jaroslav@49
   938
     * @exception NumberFormatException  if the {@code String} does not
jaroslav@49
   939
     *            contain a parsable integer.
jaroslav@49
   940
     * @see java.lang.Integer#parseInt(java.lang.String, int)
jaroslav@49
   941
     */
jaroslav@49
   942
    public static Integer decode(String nm) throws NumberFormatException {
jaroslav@49
   943
        int radix = 10;
jaroslav@49
   944
        int index = 0;
jaroslav@49
   945
        boolean negative = false;
jaroslav@49
   946
        Integer result;
jaroslav@49
   947
jaroslav@49
   948
        if (nm.length() == 0)
jaroslav@49
   949
            throw new NumberFormatException("Zero length string");
jaroslav@49
   950
        char firstChar = nm.charAt(0);
jaroslav@49
   951
        // Handle sign, if present
jaroslav@49
   952
        if (firstChar == '-') {
jaroslav@49
   953
            negative = true;
jaroslav@49
   954
            index++;
jaroslav@49
   955
        } else if (firstChar == '+')
jaroslav@49
   956
            index++;
jaroslav@49
   957
jaroslav@49
   958
        // Handle radix specifier, if present
jaroslav@49
   959
        if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
jaroslav@49
   960
            index += 2;
jaroslav@49
   961
            radix = 16;
jaroslav@49
   962
        }
jaroslav@49
   963
        else if (nm.startsWith("#", index)) {
jaroslav@49
   964
            index ++;
jaroslav@49
   965
            radix = 16;
jaroslav@49
   966
        }
jaroslav@49
   967
        else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
jaroslav@49
   968
            index ++;
jaroslav@49
   969
            radix = 8;
jaroslav@49
   970
        }
jaroslav@49
   971
jaroslav@49
   972
        if (nm.startsWith("-", index) || nm.startsWith("+", index))
jaroslav@49
   973
            throw new NumberFormatException("Sign character in wrong position");
jaroslav@49
   974
jaroslav@49
   975
        try {
jaroslav@49
   976
            result = Integer.valueOf(nm.substring(index), radix);
jaroslav@49
   977
            result = negative ? Integer.valueOf(-result.intValue()) : result;
jaroslav@49
   978
        } catch (NumberFormatException e) {
jaroslav@49
   979
            // If number is Integer.MIN_VALUE, we'll end up here. The next line
jaroslav@49
   980
            // handles this case, and causes any genuine format error to be
jaroslav@49
   981
            // rethrown.
jaroslav@49
   982
            String constant = negative ? ("-" + nm.substring(index))
jaroslav@49
   983
                                       : nm.substring(index);
jaroslav@49
   984
            result = Integer.valueOf(constant, radix);
jaroslav@49
   985
        }
jaroslav@49
   986
        return result;
jaroslav@49
   987
    }
jaroslav@49
   988
jaroslav@49
   989
    /**
jaroslav@49
   990
     * Compares two {@code Integer} objects numerically.
jaroslav@49
   991
     *
jaroslav@49
   992
     * @param   anotherInteger   the {@code Integer} to be compared.
jaroslav@49
   993
     * @return  the value {@code 0} if this {@code Integer} is
jaroslav@49
   994
     *          equal to the argument {@code Integer}; a value less than
jaroslav@49
   995
     *          {@code 0} if this {@code Integer} is numerically less
jaroslav@49
   996
     *          than the argument {@code Integer}; and a value greater
jaroslav@49
   997
     *          than {@code 0} if this {@code Integer} is numerically
jaroslav@49
   998
     *           greater than the argument {@code Integer} (signed
jaroslav@49
   999
     *           comparison).
jaroslav@49
  1000
     * @since   1.2
jaroslav@49
  1001
     */
jaroslav@49
  1002
    public int compareTo(Integer anotherInteger) {
jaroslav@49
  1003
        return compare(this.value, anotherInteger.value);
jaroslav@49
  1004
    }
jaroslav@49
  1005
jaroslav@49
  1006
    /**
jaroslav@49
  1007
     * Compares two {@code int} values numerically.
jaroslav@49
  1008
     * The value returned is identical to what would be returned by:
jaroslav@49
  1009
     * <pre>
jaroslav@49
  1010
     *    Integer.valueOf(x).compareTo(Integer.valueOf(y))
jaroslav@49
  1011
     * </pre>
jaroslav@49
  1012
     *
jaroslav@49
  1013
     * @param  x the first {@code int} to compare
jaroslav@49
  1014
     * @param  y the second {@code int} to compare
jaroslav@49
  1015
     * @return the value {@code 0} if {@code x == y};
jaroslav@49
  1016
     *         a value less than {@code 0} if {@code x < y}; and
jaroslav@49
  1017
     *         a value greater than {@code 0} if {@code x > y}
jaroslav@49
  1018
     * @since 1.7
jaroslav@49
  1019
     */
jaroslav@49
  1020
    public static int compare(int x, int y) {
jaroslav@49
  1021
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
jaroslav@49
  1022
    }
jaroslav@49
  1023
jaroslav@49
  1024
jaroslav@49
  1025
    // Bit twiddling
jaroslav@49
  1026
jaroslav@49
  1027
    /**
jaroslav@49
  1028
     * The number of bits used to represent an {@code int} value in two's
jaroslav@49
  1029
     * complement binary form.
jaroslav@49
  1030
     *
jaroslav@49
  1031
     * @since 1.5
jaroslav@49
  1032
     */
jaroslav@49
  1033
    public static final int SIZE = 32;
jaroslav@49
  1034
jaroslav@49
  1035
    /**
jaroslav@49
  1036
     * Returns an {@code int} value with at most a single one-bit, in the
jaroslav@49
  1037
     * position of the highest-order ("leftmost") one-bit in the specified
jaroslav@49
  1038
     * {@code int} value.  Returns zero if the specified value has no
jaroslav@49
  1039
     * one-bits in its two's complement binary representation, that is, if it
jaroslav@49
  1040
     * is equal to zero.
jaroslav@49
  1041
     *
jaroslav@49
  1042
     * @return an {@code int} value with a single one-bit, in the position
jaroslav@49
  1043
     *     of the highest-order one-bit in the specified value, or zero if
jaroslav@49
  1044
     *     the specified value is itself equal to zero.
jaroslav@49
  1045
     * @since 1.5
jaroslav@49
  1046
     */
jaroslav@49
  1047
    public static int highestOneBit(int i) {
jaroslav@49
  1048
        // HD, Figure 3-1
jaroslav@49
  1049
        i |= (i >>  1);
jaroslav@49
  1050
        i |= (i >>  2);
jaroslav@49
  1051
        i |= (i >>  4);
jaroslav@49
  1052
        i |= (i >>  8);
jaroslav@49
  1053
        i |= (i >> 16);
jaroslav@49
  1054
        return i - (i >>> 1);
jaroslav@49
  1055
    }
jaroslav@49
  1056
jaroslav@49
  1057
    /**
jaroslav@49
  1058
     * Returns an {@code int} value with at most a single one-bit, in the
jaroslav@49
  1059
     * position of the lowest-order ("rightmost") one-bit in the specified
jaroslav@49
  1060
     * {@code int} value.  Returns zero if the specified value has no
jaroslav@49
  1061
     * one-bits in its two's complement binary representation, that is, if it
jaroslav@49
  1062
     * is equal to zero.
jaroslav@49
  1063
     *
jaroslav@49
  1064
     * @return an {@code int} value with a single one-bit, in the position
jaroslav@49
  1065
     *     of the lowest-order one-bit in the specified value, or zero if
jaroslav@49
  1066
     *     the specified value is itself equal to zero.
jaroslav@49
  1067
     * @since 1.5
jaroslav@49
  1068
     */
jaroslav@49
  1069
    public static int lowestOneBit(int i) {
jaroslav@49
  1070
        // HD, Section 2-1
jaroslav@49
  1071
        return i & -i;
jaroslav@49
  1072
    }
jaroslav@49
  1073
jaroslav@49
  1074
    /**
jaroslav@49
  1075
     * Returns the number of zero bits preceding the highest-order
jaroslav@49
  1076
     * ("leftmost") one-bit in the two's complement binary representation
jaroslav@49
  1077
     * of the specified {@code int} value.  Returns 32 if the
jaroslav@49
  1078
     * specified value has no one-bits in its two's complement representation,
jaroslav@49
  1079
     * in other words if it is equal to zero.
jaroslav@49
  1080
     *
jaroslav@49
  1081
     * <p>Note that this method is closely related to the logarithm base 2.
jaroslav@49
  1082
     * For all positive {@code int} values x:
jaroslav@49
  1083
     * <ul>
jaroslav@49
  1084
     * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
jaroslav@49
  1085
     * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
jaroslav@49
  1086
     * </ul>
jaroslav@49
  1087
     *
jaroslav@49
  1088
     * @return the number of zero bits preceding the highest-order
jaroslav@49
  1089
     *     ("leftmost") one-bit in the two's complement binary representation
jaroslav@49
  1090
     *     of the specified {@code int} value, or 32 if the value
jaroslav@49
  1091
     *     is equal to zero.
jaroslav@49
  1092
     * @since 1.5
jaroslav@49
  1093
     */
jaroslav@49
  1094
    public static int numberOfLeadingZeros(int i) {
jaroslav@49
  1095
        // HD, Figure 5-6
jaroslav@49
  1096
        if (i == 0)
jaroslav@49
  1097
            return 32;
jaroslav@49
  1098
        int n = 1;
jaroslav@49
  1099
        if (i >>> 16 == 0) { n += 16; i <<= 16; }
jaroslav@49
  1100
        if (i >>> 24 == 0) { n +=  8; i <<=  8; }
jaroslav@49
  1101
        if (i >>> 28 == 0) { n +=  4; i <<=  4; }
jaroslav@49
  1102
        if (i >>> 30 == 0) { n +=  2; i <<=  2; }
jaroslav@49
  1103
        n -= i >>> 31;
jaroslav@49
  1104
        return n;
jaroslav@49
  1105
    }
jaroslav@49
  1106
jaroslav@49
  1107
    /**
jaroslav@49
  1108
     * Returns the number of zero bits following the lowest-order ("rightmost")
jaroslav@49
  1109
     * one-bit in the two's complement binary representation of the specified
jaroslav@49
  1110
     * {@code int} value.  Returns 32 if the specified value has no
jaroslav@49
  1111
     * one-bits in its two's complement representation, in other words if it is
jaroslav@49
  1112
     * equal to zero.
jaroslav@49
  1113
     *
jaroslav@49
  1114
     * @return the number of zero bits following the lowest-order ("rightmost")
jaroslav@49
  1115
     *     one-bit in the two's complement binary representation of the
jaroslav@49
  1116
     *     specified {@code int} value, or 32 if the value is equal
jaroslav@49
  1117
     *     to zero.
jaroslav@49
  1118
     * @since 1.5
jaroslav@49
  1119
     */
jaroslav@49
  1120
    public static int numberOfTrailingZeros(int i) {
jaroslav@49
  1121
        // HD, Figure 5-14
jaroslav@49
  1122
        int y;
jaroslav@49
  1123
        if (i == 0) return 32;
jaroslav@49
  1124
        int n = 31;
jaroslav@49
  1125
        y = i <<16; if (y != 0) { n = n -16; i = y; }
jaroslav@49
  1126
        y = i << 8; if (y != 0) { n = n - 8; i = y; }
jaroslav@49
  1127
        y = i << 4; if (y != 0) { n = n - 4; i = y; }
jaroslav@49
  1128
        y = i << 2; if (y != 0) { n = n - 2; i = y; }
jaroslav@49
  1129
        return n - ((i << 1) >>> 31);
jaroslav@49
  1130
    }
jaroslav@49
  1131
jaroslav@49
  1132
    /**
jaroslav@49
  1133
     * Returns the number of one-bits in the two's complement binary
jaroslav@49
  1134
     * representation of the specified {@code int} value.  This function is
jaroslav@49
  1135
     * sometimes referred to as the <i>population count</i>.
jaroslav@49
  1136
     *
jaroslav@49
  1137
     * @return the number of one-bits in the two's complement binary
jaroslav@49
  1138
     *     representation of the specified {@code int} value.
jaroslav@49
  1139
     * @since 1.5
jaroslav@49
  1140
     */
jaroslav@49
  1141
    public static int bitCount(int i) {
jaroslav@49
  1142
        // HD, Figure 5-2
jaroslav@49
  1143
        i = i - ((i >>> 1) & 0x55555555);
jaroslav@49
  1144
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
jaroslav@49
  1145
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
jaroslav@49
  1146
        i = i + (i >>> 8);
jaroslav@49
  1147
        i = i + (i >>> 16);
jaroslav@49
  1148
        return i & 0x3f;
jaroslav@49
  1149
    }
jaroslav@49
  1150
jaroslav@49
  1151
    /**
jaroslav@49
  1152
     * Returns the value obtained by rotating the two's complement binary
jaroslav@49
  1153
     * representation of the specified {@code int} value left by the
jaroslav@49
  1154
     * specified number of bits.  (Bits shifted out of the left hand, or
jaroslav@49
  1155
     * high-order, side reenter on the right, or low-order.)
jaroslav@49
  1156
     *
jaroslav@49
  1157
     * <p>Note that left rotation with a negative distance is equivalent to
jaroslav@49
  1158
     * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
jaroslav@49
  1159
     * distance)}.  Note also that rotation by any multiple of 32 is a
jaroslav@49
  1160
     * no-op, so all but the last five bits of the rotation distance can be
jaroslav@49
  1161
     * ignored, even if the distance is negative: {@code rotateLeft(val,
jaroslav@49
  1162
     * distance) == rotateLeft(val, distance & 0x1F)}.
jaroslav@49
  1163
     *
jaroslav@49
  1164
     * @return the value obtained by rotating the two's complement binary
jaroslav@49
  1165
     *     representation of the specified {@code int} value left by the
jaroslav@49
  1166
     *     specified number of bits.
jaroslav@49
  1167
     * @since 1.5
jaroslav@49
  1168
     */
jaroslav@49
  1169
    public static int rotateLeft(int i, int distance) {
jaroslav@49
  1170
        return (i << distance) | (i >>> -distance);
jaroslav@49
  1171
    }
jaroslav@49
  1172
jaroslav@49
  1173
    /**
jaroslav@49
  1174
     * Returns the value obtained by rotating the two's complement binary
jaroslav@49
  1175
     * representation of the specified {@code int} value right by the
jaroslav@49
  1176
     * specified number of bits.  (Bits shifted out of the right hand, or
jaroslav@49
  1177
     * low-order, side reenter on the left, or high-order.)
jaroslav@49
  1178
     *
jaroslav@49
  1179
     * <p>Note that right rotation with a negative distance is equivalent to
jaroslav@49
  1180
     * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
jaroslav@49
  1181
     * distance)}.  Note also that rotation by any multiple of 32 is a
jaroslav@49
  1182
     * no-op, so all but the last five bits of the rotation distance can be
jaroslav@49
  1183
     * ignored, even if the distance is negative: {@code rotateRight(val,
jaroslav@49
  1184
     * distance) == rotateRight(val, distance & 0x1F)}.
jaroslav@49
  1185
     *
jaroslav@49
  1186
     * @return the value obtained by rotating the two's complement binary
jaroslav@49
  1187
     *     representation of the specified {@code int} value right by the
jaroslav@49
  1188
     *     specified number of bits.
jaroslav@49
  1189
     * @since 1.5
jaroslav@49
  1190
     */
jaroslav@49
  1191
    public static int rotateRight(int i, int distance) {
jaroslav@49
  1192
        return (i >>> distance) | (i << -distance);
jaroslav@49
  1193
    }
jaroslav@49
  1194
jaroslav@49
  1195
    /**
jaroslav@49
  1196
     * Returns the value obtained by reversing the order of the bits in the
jaroslav@49
  1197
     * two's complement binary representation of the specified {@code int}
jaroslav@49
  1198
     * value.
jaroslav@49
  1199
     *
jaroslav@49
  1200
     * @return the value obtained by reversing order of the bits in the
jaroslav@49
  1201
     *     specified {@code int} value.
jaroslav@49
  1202
     * @since 1.5
jaroslav@49
  1203
     */
jaroslav@49
  1204
    public static int reverse(int i) {
jaroslav@49
  1205
        // HD, Figure 7-1
jaroslav@49
  1206
        i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
jaroslav@49
  1207
        i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
jaroslav@49
  1208
        i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
jaroslav@49
  1209
        i = (i << 24) | ((i & 0xff00) << 8) |
jaroslav@49
  1210
            ((i >>> 8) & 0xff00) | (i >>> 24);
jaroslav@49
  1211
        return i;
jaroslav@49
  1212
    }
jaroslav@49
  1213
jaroslav@49
  1214
    /**
jaroslav@49
  1215
     * Returns the signum function of the specified {@code int} value.  (The
jaroslav@49
  1216
     * return value is -1 if the specified value is negative; 0 if the
jaroslav@49
  1217
     * specified value is zero; and 1 if the specified value is positive.)
jaroslav@49
  1218
     *
jaroslav@49
  1219
     * @return the signum function of the specified {@code int} value.
jaroslav@49
  1220
     * @since 1.5
jaroslav@49
  1221
     */
jaroslav@49
  1222
    public static int signum(int i) {
jaroslav@49
  1223
        // HD, Section 2-7
jaroslav@49
  1224
        return (i >> 31) | (-i >>> 31);
jaroslav@49
  1225
    }
jaroslav@49
  1226
jaroslav@49
  1227
    /**
jaroslav@49
  1228
     * Returns the value obtained by reversing the order of the bytes in the
jaroslav@49
  1229
     * two's complement representation of the specified {@code int} value.
jaroslav@49
  1230
     *
jaroslav@49
  1231
     * @return the value obtained by reversing the bytes in the specified
jaroslav@49
  1232
     *     {@code int} value.
jaroslav@49
  1233
     * @since 1.5
jaroslav@49
  1234
     */
jaroslav@49
  1235
    public static int reverseBytes(int i) {
jaroslav@49
  1236
        return ((i >>> 24)           ) |
jaroslav@49
  1237
               ((i >>   8) &   0xFF00) |
jaroslav@49
  1238
               ((i <<   8) & 0xFF0000) |
jaroslav@49
  1239
               ((i << 24));
jaroslav@49
  1240
    }
jaroslav@49
  1241
jaroslav@49
  1242
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
jaroslav@49
  1243
    private static final long serialVersionUID = 1360826667806852920L;
jaroslav@49
  1244
}