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