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