src/java.base/share/classes/java/lang/Integer.java
author vromero
Sun, 09 Dec 2018 12:36:24 -0500
changeset 52914 4fa75d8ad418
parent 52626 991fe09c698c
child 53018 8bf9268df0e2
permissions -rw-r--r--
8210031: implementation for JVM Constants API Reviewed-by: jrose, mcimadamore, darcy, mchung, rriggs, dholmes, forax Contributed-by: brian.goetz@oracle.com, vicente.romero@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
49251
3c0a12972165 8189230: JDK method:java.lang.Integer.numberOfLeadingZeros(int) can be optimized
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2018, 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
15136
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 14507
diff changeset
    28
import java.lang.annotation.Native;
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
    29
import java.lang.invoke.MethodHandles;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
    30
import java.lang.constant.Constable;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
    31
import java.lang.constant.ConstantDesc;
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
    32
import java.util.Objects;
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
    33
import java.util.Optional;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
    34
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
    35
import jdk.internal.HotSpotIntrinsicCandidate;
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 34331
diff changeset
    36
import jdk.internal.misc.VM;
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
    37
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    38
import static java.lang.String.COMPACT_STRINGS;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    39
import static java.lang.String.LATIN1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    40
import static java.lang.String.UTF16;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
    41
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * The {@code Integer} class wraps a value of the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * {@code int} in an object. An object of type {@code Integer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * contains a single field whose type is {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p>In addition, this class provides several methods for converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * an {@code int} to a {@code String} and a {@code String} to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * {@code int}, as well as other constants and methods useful when
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * dealing with an {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p>Implementation note: The implementations of the "bit twiddling"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * methods (such as {@link #highestOneBit(int) highestOneBit} and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * based on material from Henry S. Warren, Jr.'s <i>Hacker's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Delight</i>, (Addison Wesley, 2002).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @author  Lee Boynton
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @author  Joseph D. Darcy
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    62
 * @since 1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 */
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
    64
public final class Integer extends Number
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
    65
        implements Comparable<Integer>, Constable, ConstantDesc {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * A constant holding the minimum value an {@code int} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * have, -2<sup>31</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
15136
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 14507
diff changeset
    70
    @Native public static final int   MIN_VALUE = 0x80000000;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * A constant holding the maximum value an {@code int} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * have, 2<sup>31</sup>-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
15136
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 14507
diff changeset
    76
    @Native public static final int   MAX_VALUE = 0x7fffffff;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * The {@code Class} instance representing the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    82
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
11275
7cb0861d512f 7117612: Miscellaneous warnings in java.lang
omajid
parents: 10602
diff changeset
    84
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * All possible chars for representing a number as a String
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31671
diff changeset
    90
    static final char[] digits = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        '0' , '1' , '2' , '3' , '4' , '5' ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        '6' , '7' , '8' , '9' , 'a' , 'b' ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        'o' , 'p' , 'q' , 'r' , 's' , 't' ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        'u' , 'v' , 'w' , 'x' , 'y' , 'z'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * Returns a string representation of the first argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * radix specified by the second argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <p>If the radix is smaller than {@code Character.MIN_RADIX}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * or larger than {@code Character.MAX_RADIX}, then the radix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * {@code 10} is used instead.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p>If the first argument is negative, the first element of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * result is the ASCII minus character {@code '-'}
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   109
     * ({@code '\u005Cu002D'}). If the first argument is not
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * negative, no sign character appears in the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <p>The remaining characters of the result represent the magnitude
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * of the first argument. If the magnitude is zero, it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * represented by a single zero character {@code '0'}
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   115
     * ({@code '\u005Cu0030'}); otherwise, the first character of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * the representation of the magnitude will not be the zero
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * character.  The following ASCII characters are used as digits:
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 0123456789abcdefghijklmnopqrstuvwxyz}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   123
     * These are {@code '\u005Cu0030'} through
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   124
     * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   125
     * {@code '\u005Cu007A'}. If {@code radix} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * <var>N</var>, then the first <var>N</var> of these characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * are used as radix-<var>N</var> digits in the order shown. Thus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * the digits for hexadecimal (radix 16) are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * {@code 0123456789abcdef}. If uppercase letters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * desired, the {@link java.lang.String#toUpperCase()} method may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * be called on the result:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *  {@code Integer.toString(n, 16).toUpperCase()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param   i       an integer to be converted to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * @param   radix   the radix to use in the string representation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @return  a string representation of the argument in the specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see     java.lang.Character#MAX_RADIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @see     java.lang.Character#MIN_RADIX
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static String toString(int i, int radix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            radix = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        /* Use the faster version */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (radix == 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            return toString(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   152
        if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   153
            byte[] buf = new byte[33];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   154
            boolean negative = (i < 0);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   155
            int charPos = 32;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   156
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   157
            if (!negative) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   158
                i = -i;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   159
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   160
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   161
            while (i <= -radix) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   162
                buf[charPos--] = (byte)digits[-(i % radix)];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   163
                i = i / radix;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   164
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   165
            buf[charPos] = (byte)digits[-i];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   166
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   167
            if (negative) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   168
                buf[--charPos] = '-';
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   169
            }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   170
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   171
            return StringLatin1.newString(buf, charPos, (33 - charPos));
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   172
        }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   173
        return toStringUTF16(i, radix);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   174
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   175
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   176
    private static String toStringUTF16(int i, int radix) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   177
        byte[] buf = new byte[33 * 2];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        boolean negative = (i < 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int charPos = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        if (!negative) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            i = -i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        while (i <= -radix) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   184
            StringUTF16.putChar(buf, charPos--, digits[-(i % radix)]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            i = i / radix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   187
        StringUTF16.putChar(buf, charPos, digits[-i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (negative) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   190
            StringUTF16.putChar(buf, --charPos, '-');
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   192
        return StringUTF16.newString(buf, charPos, (33 - charPos));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   196
     * Returns a string representation of the first argument as an
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   197
     * unsigned integer value in the radix specified by the second
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   198
     * argument.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   199
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   200
     * <p>If the radix is smaller than {@code Character.MIN_RADIX}
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   201
     * or larger than {@code Character.MAX_RADIX}, then the radix
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   202
     * {@code 10} is used instead.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   203
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   204
     * <p>Note that since the first argument is treated as an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   205
     * value, no leading sign character is printed.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   206
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   207
     * <p>If the magnitude is zero, it is represented by a single zero
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   208
     * character {@code '0'} ({@code '\u005Cu0030'}); otherwise,
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   209
     * the first character of the representation of the magnitude will
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   210
     * not be the zero character.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   211
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   212
     * <p>The behavior of radixes and the characters used as digits
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   213
     * are the same as {@link #toString(int, int) toString}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   214
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   215
     * @param   i       an integer to be converted to an unsigned string.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   216
     * @param   radix   the radix to use in the string representation.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   217
     * @return  an unsigned string representation of the argument in the specified radix.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   218
     * @see     #toString(int, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   219
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   220
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   221
    public static String toUnsignedString(int i, int radix) {
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   222
        return Long.toUnsignedString(toUnsignedLong(i), radix);
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   223
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   224
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   225
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Returns a string representation of the integer argument as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * unsigned integer in base&nbsp;16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * if the argument is negative; otherwise, it is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * argument.  This value is converted to a string of ASCII digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * in hexadecimal (base&nbsp;16) with no extra leading
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   233
     * {@code 0}s.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   234
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   235
     * <p>The value of the argument can be recovered from the returned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   236
     * string {@code s} by calling {@link
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   237
     * Integer#parseUnsignedInt(String, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   238
     * Integer.parseUnsignedInt(s, 16)}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   239
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   240
     * <p>If the unsigned magnitude is zero, it is represented by a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   241
     * single zero character {@code '0'} ({@code '\u005Cu0030'});
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   242
     * otherwise, the first character of the representation of the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   243
     * unsigned magnitude will not be the zero character. The
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   244
     * following characters are used as hexadecimal digits:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *  {@code 0123456789abcdef}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   250
     * These are the characters {@code '\u005Cu0030'} through
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   251
     * {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   252
     * {@code '\u005Cu0066'}. If uppercase letters are
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * desired, the {@link java.lang.String#toUpperCase()} method may
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * be called on the result:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *  {@code Integer.toHexString(n).toUpperCase()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param   i   an integer to be converted to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @return  the string representation of the unsigned integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *          represented by the argument in hexadecimal (base&nbsp;16).
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   263
     * @see #parseUnsignedInt(String, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   264
     * @see #toUnsignedString(int, int)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   265
     * @since   1.0.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    public static String toHexString(int i) {
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   268
        return toUnsignedString0(i, 4);
2
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
     * Returns a string representation of the integer argument as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * unsigned integer in base&nbsp;8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * if the argument is negative; otherwise, it is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * argument.  This value is converted to a string of ASCII digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * in octal (base&nbsp;8) with no extra leading {@code 0}s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   280
     * <p>The value of the argument can be recovered from the returned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   281
     * string {@code s} by calling {@link
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   282
     * Integer#parseUnsignedInt(String, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   283
     * Integer.parseUnsignedInt(s, 8)}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   284
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <p>If the unsigned magnitude is zero, it is represented by a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   286
     * single zero character {@code '0'} ({@code '\u005Cu0030'});
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   287
     * otherwise, the first character of the representation of the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   288
     * unsigned magnitude will not be the zero character. The
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   289
     * following characters are used as octal digits:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * {@code 01234567}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   295
     * These are the characters {@code '\u005Cu0030'} through
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   296
     * {@code '\u005Cu0037'}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * @param   i   an integer to be converted to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @return  the string representation of the unsigned integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *          represented by the argument in octal (base&nbsp;8).
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   301
     * @see #parseUnsignedInt(String, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   302
     * @see #toUnsignedString(int, int)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   303
     * @since   1.0.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public static String toOctalString(int i) {
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   306
        return toUnsignedString0(i, 3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Returns a string representation of the integer argument as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * unsigned integer in base&nbsp;2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * <p>The unsigned integer value is the argument plus 2<sup>32</sup>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * if the argument is negative; otherwise it is equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * argument.  This value is converted to a string of ASCII digits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * in binary (base&nbsp;2) with no extra leading {@code 0}s.
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   317
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   318
     * <p>The value of the argument can be recovered from the returned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   319
     * string {@code s} by calling {@link
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   320
     * Integer#parseUnsignedInt(String, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   321
     * Integer.parseUnsignedInt(s, 2)}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   322
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   323
     * <p>If the unsigned magnitude is zero, it is represented by a
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   324
     * single zero character {@code '0'} ({@code '\u005Cu0030'});
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   325
     * otherwise, the first character of the representation of the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   326
     * unsigned magnitude will not be the zero character. The
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   327
     * characters {@code '0'} ({@code '\u005Cu0030'}) and {@code
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   328
     * '1'} ({@code '\u005Cu0031'}) are used as binary digits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @param   i   an integer to be converted to a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @return  the string representation of the unsigned integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *          represented by the argument in binary (base&nbsp;2).
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   333
     * @see #parseUnsignedInt(String, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   334
     * @see #toUnsignedString(int, int)
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   335
     * @since   1.0.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public static String toBinaryString(int i) {
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   338
        return toUnsignedString0(i, 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * Convert the integer to an unsigned number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     */
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   344
    private static String toUnsignedString0(int val, int shift) {
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   345
        // assert shift > 0 && shift <=5 : "Illegal shift value";
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   346
        int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val);
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   347
        int chars = Math.max(((mag + (shift - 1)) / shift), 1);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   348
        if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   349
            byte[] buf = new byte[chars];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   350
            formatUnsignedInt(val, shift, buf, 0, chars);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   351
            return new String(buf, LATIN1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   352
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   353
            byte[] buf = new byte[chars * 2];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   354
            formatUnsignedIntUTF16(val, shift, buf, 0, chars);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   355
            return new String(buf, UTF16);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   356
        }
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   357
    }
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   358
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   359
    /**
25660
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   360
     * Format an {@code int} (treated as unsigned) into a character buffer. If
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   361
     * {@code len} exceeds the formatted ASCII representation of {@code val},
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   362
     * {@code buf} will be padded with leading zeroes.
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   363
     *
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   364
     * @param val the unsigned int to format
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   365
     * @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary)
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   366
     * @param buf the character buffer to write to
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   367
     * @param offset the offset in the destination buffer to start at
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   368
     * @param len the number of characters to write
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   369
     */
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   370
    static void formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) {
25660
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   371
        // assert shift > 0 && shift <=5 : "Illegal shift value";
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   372
        // assert offset >= 0 && offset < buf.length : "illegal offset";
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   373
        // assert len > 0 && (offset + len) <= buf.length : "illegal length";
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   374
        int charPos = offset + len;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        int radix = 1 << shift;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        int mask = radix - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        do {
25660
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   378
            buf[--charPos] = Integer.digits[val & mask];
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
   379
            val >>>= shift;
25660
01fa3ccedf50 8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents: 25653
diff changeset
   380
        } while (charPos > offset);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   383
    /** byte[]/LATIN1 version    */
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   384
    static void formatUnsignedInt(int val, int shift, byte[] buf, int offset, int len) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   385
        int charPos = offset + len;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   386
        int radix = 1 << shift;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   387
        int mask = radix - 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   388
        do {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   389
            buf[--charPos] = (byte)Integer.digits[val & mask];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   390
            val >>>= shift;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   391
        } while (charPos > offset);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   392
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   393
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   394
    /** byte[]/UTF16 version    */
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 37880
diff changeset
   395
    private static void formatUnsignedIntUTF16(int val, int shift, byte[] buf, int offset, int len) {
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   396
        int charPos = offset + len;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   397
        int radix = 1 << shift;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   398
        int mask = radix - 1;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   399
        do {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   400
            StringUTF16.putChar(buf, --charPos, Integer.digits[val & mask]);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   401
            val >>>= shift;
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   402
        } while (charPos > offset);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   403
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   404
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   405
    static final byte[] DigitTens = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        '0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        '2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        '3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        '4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        '5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        '6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        '8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        '9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   418
    static final byte[] DigitOnes = {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        } ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * Returns a {@code String} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * specified integer. The argument is converted to signed decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * representation and returned as a string, exactly as if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * argument and radix 10 were given as arguments to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * #toString(int, int)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * @param   i   an integer to be converted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @return  a string representation of the argument in base&nbsp;10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
   442
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    public static String toString(int i) {
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   444
        int size = stringSize(i);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   445
        if (COMPACT_STRINGS) {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   446
            byte[] buf = new byte[size];
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   447
            getChars(i, size, buf);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   448
            return new String(buf, LATIN1);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   449
        } else {
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   450
            byte[] buf = new byte[size * 2];
44642
331e669007f7 8158168: Missing bounds checks for some String intrinsics
dlong
parents: 37880
diff changeset
   451
            StringUTF16.getChars(i, size, buf);
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   452
            return new String(buf, UTF16);
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   453
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    /**
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   457
     * Returns a string representation of the argument as an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   458
     * decimal value.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   459
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   460
     * The argument is converted to unsigned decimal representation
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   461
     * and returned as a string exactly as if the argument and radix
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   462
     * 10 were given as arguments to the {@link #toUnsignedString(int,
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   463
     * int)} method.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   464
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   465
     * @param   i  an integer to be converted to an unsigned string.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   466
     * @return  an unsigned string representation of the argument.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   467
     * @see     #toUnsignedString(int, int)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   468
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   469
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   470
    public static String toUnsignedString(int i) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   471
        return Long.toString(toUnsignedLong(i));
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   472
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   473
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   474
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Places characters representing the integer i into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * character array buf. The characters are placed into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * the buffer backwards starting with the least significant
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * digit at the specified index (exclusive), and working
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * backwards from there.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   481
     * @implNote This method converts positive inputs into negative
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   482
     * values, to cover the Integer.MIN_VALUE case. Converting otherwise
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   483
     * (negative to positive) will expose -Integer.MIN_VALUE that overflows
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   484
     * integer.
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents: 35302
diff changeset
   485
     *
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents: 35302
diff changeset
   486
     * @param i     value to convert
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents: 35302
diff changeset
   487
     * @param index next index, after the least significant digit
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents: 35302
diff changeset
   488
     * @param buf   target buffer, Latin1-encoded
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents: 35302
diff changeset
   489
     * @return index of the most significant digit or minus sign, if present
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     */
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents: 35302
diff changeset
   491
    static int getChars(int i, int index, byte[] buf) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        int q, r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        int charPos = index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   495
        boolean negative = i < 0;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   496
        if (!negative) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            i = -i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        // Generate two digits per iteration
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   501
        while (i <= -100) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            q = i / 100;
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   503
            r = (q * 100) - i;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            i = q;
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   505
            buf[--charPos] = DigitOnes[r];
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   506
            buf[--charPos] = DigitTens[r];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   509
        // We know there are at most two digits left at this point.
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   510
        q = i / 10;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   511
        r = (q * 10) - i;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   512
        buf[--charPos] = (byte)('0' + r);
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   513
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   514
        // Whatever left is the remaining digit.
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   515
        if (q < 0) {
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   516
            buf[--charPos] = (byte)('0' - q);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   518
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   519
        if (negative) {
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   520
            buf[--charPos] = (byte)'-';
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   521
        }
35388
a33a232cf7b7 8148483: JEP 280: Indify String Concatenation
shade
parents: 35302
diff changeset
   522
        return charPos;
33663
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   523
    }
2cd62a4bd471 8141132: JEP 254: Compact Strings
thartmann
parents: 32649
diff changeset
   524
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   525
    // Left here for compatibility reasons, see JDK-8143900.
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31671
diff changeset
   526
    static final int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                                      99999999, 999999999, Integer.MAX_VALUE };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   529
    /**
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   530
     * Returns the string representation size for a given int value.
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   531
     *
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   532
     * @param x int value
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   533
     * @return string size
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   534
     *
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   535
     * @implNote There are other ways to compute this: e.g. binary search,
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   536
     * but values are biased heavily towards zero, and therefore linear search
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   537
     * wins. The iteration results are also routinely inlined in the generated
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   538
     * code after loop unrolling.
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   539
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    static int stringSize(int x) {
34331
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   541
        int d = 1;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   542
        if (x >= 0) {
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   543
            d = 0;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   544
            x = -x;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   545
        }
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   546
        int p = -10;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   547
        for (int i = 1; i < 10; i++) {
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   548
            if (x > p)
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   549
                return i + d;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   550
            p = 10 * p;
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   551
        }
7b1fea58eefe 8136500: Integer/Long getChars and stringSize should be more idiomatic
shade
parents: 33663
diff changeset
   552
        return 10 + d;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * Parses the string argument as a signed integer in the radix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * specified by the second argument. The characters in the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * must all be digits of the specified radix (as determined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * whether {@link java.lang.Character#digit(char, int)} returns a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * nonnegative value), except that the first character may be an
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   561
     * ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * indicate a negative value or an ASCII plus sign {@code '+'}
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   563
     * ({@code '\u005Cu002B'}) to indicate a positive value. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * resulting integer value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <p>An exception of type {@code NumberFormatException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * thrown if any of the following situations occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * <li>The first argument is {@code null} or is a string of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * length zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <li>The radix is either smaller than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * {@link java.lang.Character#MIN_RADIX} or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * larger than {@link java.lang.Character#MAX_RADIX}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * <li>Any character of the string is not a digit of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * radix, except that the first character may be a minus sign
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   578
     * {@code '-'} ({@code '\u005Cu002D'}) or plus sign
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   579
     * {@code '+'} ({@code '\u005Cu002B'}) provided that the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * string is longer than length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <li>The value represented by the string is not a value of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <p>Examples:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * parseInt("0", 10) returns 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * parseInt("473", 10) returns 473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * parseInt("+42", 10) returns 42
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * parseInt("-0", 10) returns 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * parseInt("-FF", 16) returns -255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * parseInt("1100110", 2) returns 102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * parseInt("2147483647", 10) returns 2147483647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * parseInt("-2147483648", 10) returns -2147483648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * parseInt("2147483648", 10) throws a NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * parseInt("99", 8) throws a NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * parseInt("Kona", 10) throws a NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * parseInt("Kona", 27) returns 411787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @param      s   the {@code String} containing the integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *                  representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param      radix   the radix to be used while parsing {@code s}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * @return     the integer represented by the string argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *             specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @exception  NumberFormatException if the {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *             does not contain a parsable {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public static int parseInt(String s, int radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                throws NumberFormatException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    {
2425
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
         * WARNING: This method may be invoked early during VM initialization
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   615
         * before IntegerCache is initialized. Care must be taken to not use
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   616
         * the valueOf method.
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   617
         */
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   618
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            throw new NumberFormatException("null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (radix < Character.MIN_RADIX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            throw new NumberFormatException("radix " + radix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                            " less than Character.MIN_RADIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (radix > Character.MAX_RADIX) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            throw new NumberFormatException("radix " + radix +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                                            " greater than Character.MAX_RADIX");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        boolean negative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        int i = 0, len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        int limit = -Integer.MAX_VALUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            char firstChar = s.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            if (firstChar < '0') { // Possible leading "+" or "-"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                if (firstChar == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                    negative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    limit = Integer.MIN_VALUE;
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   643
                } else if (firstChar != '+') {
51412
8f7e3f9ddbc0 8176425: Add radix indication in NumberFormatException message for Integer.decode
darcy
parents: 51388
diff changeset
   644
                    throw NumberFormatException.forInputString(s, radix);
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   645
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   647
                if (len == 1) { // Cannot have lone "+" or "-"
51412
8f7e3f9ddbc0 8176425: Add radix indication in NumberFormatException message for Integer.decode
darcy
parents: 51388
diff changeset
   648
                    throw NumberFormatException.forInputString(s, radix);
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   649
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            }
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   652
            int multmin = limit / radix;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   653
            int result = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            while (i < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                // Accumulating negatively avoids surprises near MAX_VALUE
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   656
                int digit = Character.digit(s.charAt(i++), radix);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   657
                if (digit < 0 || result < multmin) {
51412
8f7e3f9ddbc0 8176425: Add radix indication in NumberFormatException message for Integer.decode
darcy
parents: 51388
diff changeset
   658
                    throw NumberFormatException.forInputString(s, radix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                result *= radix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                if (result < limit + digit) {
51412
8f7e3f9ddbc0 8176425: Add radix indication in NumberFormatException message for Integer.decode
darcy
parents: 51388
diff changeset
   662
                    throw NumberFormatException.forInputString(s, radix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
                result -= digit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            }
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   666
            return negative ? result : -result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        } else {
51412
8f7e3f9ddbc0 8176425: Add radix indication in NumberFormatException message for Integer.decode
darcy
parents: 51388
diff changeset
   668
            throw NumberFormatException.forInputString(s, radix);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
        }
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   670
    }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   671
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   672
    /**
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   673
     * Parses the {@link CharSequence} argument as a signed {@code int} in the
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   674
     * specified {@code radix}, beginning at the specified {@code beginIndex}
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   675
     * and extending to {@code endIndex - 1}.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   676
     *
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   677
     * <p>The method does not take steps to guard against the
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   678
     * {@code CharSequence} being mutated while parsing.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   679
     *
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   680
     * @param      s   the {@code CharSequence} containing the {@code int}
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   681
     *                  representation to be parsed
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   682
     * @param      beginIndex   the beginning index, inclusive.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   683
     * @param      endIndex     the ending index, exclusive.
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   684
     * @param      radix   the radix to be used while parsing {@code s}.
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   685
     * @return     the signed {@code int} represented by the subsequence in
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   686
     *             the specified radix.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   687
     * @throws     NullPointerException  if {@code s} is null.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   688
     * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   689
     *             negative, or if {@code beginIndex} is greater than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   690
     *             {@code endIndex} or if {@code endIndex} is greater than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   691
     *             {@code s.length()}.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   692
     * @throws     NumberFormatException  if the {@code CharSequence} does not
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   693
     *             contain a parsable {@code int} in the specified
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   694
     *             {@code radix}, or if {@code radix} is either smaller than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   695
     *             {@link java.lang.Character#MIN_RADIX} or larger than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   696
     *             {@link java.lang.Character#MAX_RADIX}.
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34882
diff changeset
   697
     * @since  9
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   698
     */
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   699
    public static int parseInt(CharSequence s, int beginIndex, int endIndex, int radix)
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   700
                throws NumberFormatException {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   701
        s = Objects.requireNonNull(s);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   702
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   703
        if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   704
            throw new IndexOutOfBoundsException();
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   705
        }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   706
        if (radix < Character.MIN_RADIX) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   707
            throw new NumberFormatException("radix " + radix +
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   708
                                            " less than Character.MIN_RADIX");
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   709
        }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   710
        if (radix > Character.MAX_RADIX) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   711
            throw new NumberFormatException("radix " + radix +
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   712
                                            " greater than Character.MAX_RADIX");
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   713
        }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   714
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   715
        boolean negative = false;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   716
        int i = beginIndex;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   717
        int limit = -Integer.MAX_VALUE;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   718
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   719
        if (i < endIndex) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   720
            char firstChar = s.charAt(i);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   721
            if (firstChar < '0') { // Possible leading "+" or "-"
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   722
                if (firstChar == '-') {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   723
                    negative = true;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   724
                    limit = Integer.MIN_VALUE;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   725
                } else if (firstChar != '+') {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   726
                    throw NumberFormatException.forCharSequence(s, beginIndex,
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   727
                            endIndex, i);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   728
                }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   729
                i++;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   730
                if (i == endIndex) { // Cannot have lone "+" or "-"
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   731
                    throw NumberFormatException.forCharSequence(s, beginIndex,
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   732
                            endIndex, i);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   733
                }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   734
            }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   735
            int multmin = limit / radix;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   736
            int result = 0;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   737
            while (i < endIndex) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   738
                // Accumulating negatively avoids surprises near MAX_VALUE
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   739
                int digit = Character.digit(s.charAt(i), radix);
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   740
                if (digit < 0 || result < multmin) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   741
                    throw NumberFormatException.forCharSequence(s, beginIndex,
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   742
                            endIndex, i);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   743
                }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   744
                result *= radix;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   745
                if (result < limit + digit) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   746
                    throw NumberFormatException.forCharSequence(s, beginIndex,
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   747
                            endIndex, i);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   748
                }
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   749
                i++;
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   750
                result -= digit;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   751
            }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   752
            return negative ? result : -result;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   753
        } else {
51412
8f7e3f9ddbc0 8176425: Add radix indication in NumberFormatException message for Integer.decode
darcy
parents: 51388
diff changeset
   754
            throw NumberFormatException.forInputString("", radix);
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   755
        }
2
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
     * Parses the string argument as a signed decimal integer. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     * characters in the string must all be decimal digits, except
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
     * that the first character may be an ASCII minus sign {@code '-'}
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   762
     * ({@code '\u005Cu002D'}) to indicate a negative value or an
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   763
     * ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * indicate a positive value. The resulting integer value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * returned, exactly as if the argument and the radix 10 were
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * given as arguments to the {@link #parseInt(java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * int)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * @param s    a {@code String} containing the {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     *             representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     * @return     the integer value represented by the argument in decimal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @exception  NumberFormatException  if the string does not contain a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *               parsable integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    public static int parseInt(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        return parseInt(s,10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    /**
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   780
     * Parses the string argument as an unsigned integer in the radix
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   781
     * specified by the second argument.  An unsigned integer maps the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   782
     * values usually associated with negative numbers to positive
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   783
     * numbers larger than {@code MAX_VALUE}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   784
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   785
     * The characters in the string must all be digits of the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   786
     * specified radix (as determined by whether {@link
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   787
     * java.lang.Character#digit(char, int)} returns a nonnegative
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   788
     * value), except that the first character may be an ASCII plus
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   789
     * sign {@code '+'} ({@code '\u005Cu002B'}). The resulting
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   790
     * integer value is returned.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   791
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   792
     * <p>An exception of type {@code NumberFormatException} is
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   793
     * thrown if any of the following situations occurs:
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   794
     * <ul>
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   795
     * <li>The first argument is {@code null} or is a string of
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   796
     * length zero.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   797
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   798
     * <li>The radix is either smaller than
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   799
     * {@link java.lang.Character#MIN_RADIX} or
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   800
     * larger than {@link java.lang.Character#MAX_RADIX}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   801
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   802
     * <li>Any character of the string is not a digit of the specified
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   803
     * radix, except that the first character may be a plus sign
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   804
     * {@code '+'} ({@code '\u005Cu002B'}) provided that the
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   805
     * string is longer than length 1.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   806
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   807
     * <li>The value represented by the string is larger than the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   808
     * largest unsigned {@code int}, 2<sup>32</sup>-1.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   809
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   810
     * </ul>
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   811
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   812
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   813
     * @param      s   the {@code String} containing the unsigned integer
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   814
     *                  representation to be parsed
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   815
     * @param      radix   the radix to be used while parsing {@code s}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   816
     * @return     the integer represented by the string argument in the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   817
     *             specified radix.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   818
     * @throws     NumberFormatException if the {@code String}
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   819
     *             does not contain a parsable {@code int}.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   820
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   821
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   822
    public static int parseUnsignedInt(String s, int radix)
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   823
                throws NumberFormatException {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   824
        if (s == null)  {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   825
            throw new NumberFormatException("null");
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   826
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   827
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   828
        int len = s.length();
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   829
        if (len > 0) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   830
            char firstChar = s.charAt(0);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   831
            if (firstChar == '-') {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   832
                throw new
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   833
                    NumberFormatException(String.format("Illegal leading minus sign " +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   834
                                                       "on unsigned string %s.", s));
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   835
            } else {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   836
                if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   837
                    (radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digits
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   838
                    return parseInt(s, radix);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   839
                } else {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   840
                    long ell = Long.parseLong(s, radix);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   841
                    if ((ell & 0xffff_ffff_0000_0000L) == 0) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   842
                        return (int) ell;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   843
                    } else {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   844
                        throw new
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   845
                            NumberFormatException(String.format("String value %s exceeds " +
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   846
                                                                "range of unsigned int.", s));
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   847
                    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   848
                }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   849
            }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   850
        } else {
51412
8f7e3f9ddbc0 8176425: Add radix indication in NumberFormatException message for Integer.decode
darcy
parents: 51388
diff changeset
   851
            throw NumberFormatException.forInputString(s, radix);
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   852
        }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   853
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   854
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   855
    /**
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   856
     * Parses the {@link CharSequence} argument as an unsigned {@code int} in
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   857
     * the specified {@code radix}, beginning at the specified
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   858
     * {@code beginIndex} and extending to {@code endIndex - 1}.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   859
     *
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   860
     * <p>The method does not take steps to guard against the
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   861
     * {@code CharSequence} being mutated while parsing.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   862
     *
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   863
     * @param      s   the {@code CharSequence} containing the unsigned
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   864
     *                 {@code int} representation to be parsed
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   865
     * @param      beginIndex   the beginning index, inclusive.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   866
     * @param      endIndex     the ending index, exclusive.
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   867
     * @param      radix   the radix to be used while parsing {@code s}.
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   868
     * @return     the unsigned {@code int} represented by the subsequence in
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   869
     *             the specified radix.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   870
     * @throws     NullPointerException  if {@code s} is null.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   871
     * @throws     IndexOutOfBoundsException  if {@code beginIndex} is
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   872
     *             negative, or if {@code beginIndex} is greater than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   873
     *             {@code endIndex} or if {@code endIndex} is greater than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   874
     *             {@code s.length()}.
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   875
     * @throws     NumberFormatException  if the {@code CharSequence} does not
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   876
     *             contain a parsable unsigned {@code int} in the specified
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   877
     *             {@code radix}, or if {@code radix} is either smaller than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   878
     *             {@link java.lang.Character#MIN_RADIX} or larger than
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   879
     *             {@link java.lang.Character#MAX_RADIX}.
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 34882
diff changeset
   880
     * @since  9
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   881
     */
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   882
    public static int parseUnsignedInt(CharSequence s, int beginIndex, int endIndex, int radix)
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   883
                throws NumberFormatException {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   884
        s = Objects.requireNonNull(s);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   885
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   886
        if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   887
            throw new IndexOutOfBoundsException();
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   888
        }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   889
        int start = beginIndex, len = endIndex - beginIndex;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   890
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   891
        if (len > 0) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   892
            char firstChar = s.charAt(start);
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   893
            if (firstChar == '-') {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   894
                throw new
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   895
                    NumberFormatException(String.format("Illegal leading minus sign " +
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   896
                                                       "on unsigned string %s.", s));
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   897
            } else {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   898
                if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   899
                        (radix == 10 && len <= 9)) { // Integer.MAX_VALUE in base 10 is 10 digits
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   900
                    return parseInt(s, start, start + len, radix);
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   901
                } else {
26462
d6d34934be12 8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents: 25859
diff changeset
   902
                    long ell = Long.parseLong(s, start, start + len, radix);
25653
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   903
                    if ((ell & 0xffff_ffff_0000_0000L) == 0) {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   904
                        return (int) ell;
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   905
                    } else {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   906
                        throw new
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   907
                            NumberFormatException(String.format("String value %s exceeds " +
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   908
                                                                "range of unsigned int.", s));
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   909
                    }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   910
                }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   911
            }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   912
        } else {
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   913
            throw new NumberFormatException("");
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   914
        }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   915
    }
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   916
41e5fa7ce490 8041972: Additional parse methods for Long/Integer
redestad
parents: 24865
diff changeset
   917
    /**
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   918
     * Parses the string argument as an unsigned decimal integer. The
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   919
     * characters in the string must all be decimal digits, except
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 26462
diff changeset
   920
     * that the first character may be an ASCII plus sign {@code
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11672
diff changeset
   921
     * '+'} ({@code '\u005Cu002B'}). The resulting integer value
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   922
     * is returned, exactly as if the argument and the radix 10 were
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   923
     * given as arguments to the {@link
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   924
     * #parseUnsignedInt(java.lang.String, int)} method.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   925
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   926
     * @param s   a {@code String} containing the unsigned {@code int}
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   927
     *            representation to be parsed
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   928
     * @return    the unsigned integer value represented by the argument in decimal.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   929
     * @throws    NumberFormatException  if the string does not contain a
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   930
     *            parsable unsigned integer.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   931
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   932
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   933
    public static int parseUnsignedInt(String s) throws NumberFormatException {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   934
        return parseUnsignedInt(s, 10);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   935
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   936
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
   937
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * Returns an {@code Integer} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     * extracted from the specified {@code String} when parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * with the radix given by the second argument. The first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     * is interpreted as representing a signed integer in the radix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * specified by the second argument, exactly as if the arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     * were given to the {@link #parseInt(java.lang.String, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     * method. The result is an {@code Integer} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * represents the integer value specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
     * <p>In other words, this method returns an {@code Integer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
     * object equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
     *  {@code new Integer(Integer.parseInt(s, radix))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
     * @param      s   the string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
     * @param      radix the radix to be used in interpreting {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
     * @return     an {@code Integer} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     *             represented by the string argument in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     *             radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     * @exception NumberFormatException if the {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     *            does not contain a parsable {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public static Integer valueOf(String s, int radix) throws NumberFormatException {
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   963
        return Integer.valueOf(parseInt(s,radix));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * Returns an {@code Integer} object holding the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * value of the specified {@code String}. The argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * interpreted as representing a signed decimal integer, exactly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     * as if the argument were given to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * #parseInt(java.lang.String)} method. The result is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     * {@code Integer} object that represents the integer value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     * specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     * <p>In other words, this method returns an {@code Integer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
     * object equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
     *  {@code new Integer(Integer.parseInt(s))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
     * @param      s   the string to be parsed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * @return     an {@code Integer} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     *             represented by the string argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     * @exception  NumberFormatException  if the string cannot be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     *             as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     */
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   988
    public static Integer valueOf(String s) throws NumberFormatException {
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   989
        return Integer.valueOf(parseInt(s, 10));
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   990
    }
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   991
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   992
    /**
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   993
     * 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
   994
     * -128 and 127 (inclusive) as required by JLS.
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
   995
     *
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   996
     * The cache is initialized on first usage.  The size of the cache
14014
da3648e13e67 8000269: Cleanup javadoc warnings
alanb
parents: 12858
diff changeset
   997
     * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
   998
     * 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
   999
     * may be set and saved in the private system properties in the
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 34331
diff changeset
  1000
     * jdk.internal.misc.VM class.
52402
72d4e10305b9 8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region.
jiangli
parents: 51509
diff changeset
  1001
     *
72d4e10305b9 8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region.
jiangli
parents: 51509
diff changeset
  1002
     * WARNING: The cache is archived with CDS and reloaded from the shared
72d4e10305b9 8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region.
jiangli
parents: 51509
diff changeset
  1003
     * archive at runtime. The archived cache (Integer[]) and Integer objects
72d4e10305b9 8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region.
jiangli
parents: 51509
diff changeset
  1004
     * reside in the closed archive heap regions. Care should be taken when
72d4e10305b9 8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region.
jiangli
parents: 51509
diff changeset
  1005
     * changing the implementation and the cache array should not be assigned
72d4e10305b9 8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region.
jiangli
parents: 51509
diff changeset
  1006
     * with new Integer object(s) after initialization.
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1007
     */
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1008
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    private static class IntegerCache {
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1010
        static final int low = -128;
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1011
        static final int high;
51388
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1012
        static final Integer[] cache;
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1013
        static Integer[] archivedCache;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
        static {
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1016
            // high value may be configured by property
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1017
            int h = 127;
6882
637546039be3 6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents: 5506
diff changeset
  1018
            String integerCacheHighPropValue =
34882
ce2a8ec851c1 8145544: Move sun.misc.VM to jdk.internal.misc
chegar
parents: 34331
diff changeset
  1019
                VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1020
            if (integerCacheHighPropValue != null) {
18278
4b4381313a3c 8015395: NumberFormatException during startup if JDK-internal property java.lang.Integer.IntegerCache.high set to bad value
bpb
parents: 17929
diff changeset
  1021
                try {
52626
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 52402
diff changeset
  1022
                    h = Math.max(parseInt(integerCacheHighPropValue), 127);
18278
4b4381313a3c 8015395: NumberFormatException during startup if JDK-internal property java.lang.Integer.IntegerCache.high set to bad value
bpb
parents: 17929
diff changeset
  1023
                    // Maximum array size is Integer.MAX_VALUE
52626
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 52402
diff changeset
  1024
                    h = Math.min(h, Integer.MAX_VALUE - (-low) -1);
18278
4b4381313a3c 8015395: NumberFormatException during startup if JDK-internal property java.lang.Integer.IntegerCache.high set to bad value
bpb
parents: 17929
diff changeset
  1025
                } catch( NumberFormatException nfe) {
4b4381313a3c 8015395: NumberFormatException during startup if JDK-internal property java.lang.Integer.IntegerCache.high set to bad value
bpb
parents: 17929
diff changeset
  1026
                    // If the property cannot be parsed into an int, ignore it.
4b4381313a3c 8015395: NumberFormatException during startup if JDK-internal property java.lang.Integer.IntegerCache.high set to bad value
bpb
parents: 17929
diff changeset
  1027
                }
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1028
            }
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1029
            high = h;
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1030
51388
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1031
            // Load IntegerCache.archivedCache from archive, if possible
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1032
            VM.initializeFromArchive(IntegerCache.class);
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1033
            int size = (high - low) + 1;
12668
9a70f2230e3b 7165102: Only run assertion on Integer autoboxing cache size once
forax
parents: 11676
diff changeset
  1034
51388
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1035
            // Use the archived cache if it exists and is large enough
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1036
            if (archivedCache == null || size > archivedCache.length) {
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1037
                Integer[] c = new Integer[size];
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1038
                int j = low;
52626
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 52402
diff changeset
  1039
                for(int i = 0; i < c.length; i++) {
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 52402
diff changeset
  1040
                    c[i] = new Integer(j++);
991fe09c698c 8213033: Archive remaining primitive box caches
redestad
parents: 52402
diff changeset
  1041
                }
51388
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1042
                archivedCache = c;
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1043
            }
0dcd27526967 8209120: Archive the Integer.IntegerCache
redestad
parents: 50211
diff changeset
  1044
            cache = archivedCache;
12668
9a70f2230e3b 7165102: Only run assertion on Integer autoboxing cache size once
forax
parents: 11676
diff changeset
  1045
            // range [-128, 127] must be interned (JLS7 5.1.7)
9a70f2230e3b 7165102: Only run assertion on Integer autoboxing cache size once
forax
parents: 11676
diff changeset
  1046
            assert IntegerCache.high >= 127;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
        }
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1048
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1049
        private IntegerCache() {}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
     * Returns an {@code Integer} instance representing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
     * {@code int} value.  If a new {@code Integer} instance is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     * required, this method should generally be used in preference to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
     * the constructor {@link #Integer(int)}, as this method is likely
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * to yield significantly better space and time performance by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
     * caching frequently requested values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
     *
3224
3aa65803ae07 6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents: 2425
diff changeset
  1060
     * 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
  1061
     * 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
  1062
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * @param  i an {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     * @return an {@code Integer} instance representing {@code i}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1067
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    public static Integer valueOf(int i) {
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1069
        if (i >= IntegerCache.low && i <= IntegerCache.high)
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1070
            return IntegerCache.cache[i + (-IntegerCache.low)];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
        return new Integer(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     * The value of the {@code Integer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    private final int value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
     * Constructs a newly allocated {@code Integer} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * represents the specified {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * @param   value   the value to be represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     *                  {@code Integer} object.
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1087
     *
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1088
     * @deprecated
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1089
     * It is rarely appropriate to use this constructor. The static factory
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1090
     * {@link #valueOf(int)} is generally a better choice, as it is
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1091
     * likely to yield significantly better space and time performance.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1093
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
    public Integer(int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     * Constructs a newly allocated {@code Integer} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     * represents the {@code int} value indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * {@code String} parameter. The string is converted to an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     * {@code int} value in exactly the manner used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
     * {@code parseInt} method for radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
     *
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1105
     * @param   s   the {@code String} to be converted to an {@code Integer}.
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1106
     * @throws      NumberFormatException if the {@code String} does not
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1107
     *              contain a parsable integer.
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1108
     *
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1109
     * @deprecated
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1110
     * It is rarely appropriate to use this constructor.
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1111
     * Use {@link #parseInt(String)} to convert a string to a
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1112
     * {@code int} primitive, or use {@link #valueOf(String)}
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1113
     * to convert a string to an {@code Integer} object.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     */
37521
b6e0f285c998 8145468: update java.lang APIs with new deprecations
smarks
parents: 35388
diff changeset
  1115
    @Deprecated(since="9")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
    public Integer(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
        this.value = parseInt(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1121
     * 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
  1122
     * after a narrowing primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1123
     * @jls 5.1.3 Narrowing Primitive Conversions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    public byte byteValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        return (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1130
     * 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
  1131
     * after a narrowing primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1132
     * @jls 5.1.3 Narrowing Primitive Conversions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
    public short shortValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
        return (short)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
     * Returns the value of this {@code Integer} as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
     * {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1142
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1148
     * 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
  1149
     * after a widening primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1150
     * @jls 5.1.2 Widening Primitive Conversions
17929
5ef41523c723 8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents: 15311
diff changeset
  1151
     * @see Integer#toUnsignedLong(int)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
    public long longValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        return (long)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1158
     * 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
  1159
     * after a widening primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1160
     * @jls 5.1.2 Widening Primitive Conversions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    public float floatValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
        return (float)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
    /**
10067
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1167
     * 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
  1168
     * after a widening primitive conversion.
1263ecd22db6 6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents: 9266
diff changeset
  1169
     * @jls 5.1.2 Widening Primitive Conversions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
    public double doubleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        return (double)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
     * Returns a {@code String} object representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
     * {@code Integer}'s value. The value is converted to signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
     * decimal representation and returned as a string, exactly as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
     * the integer value were given as an argument to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
     * java.lang.Integer#toString(int)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
     * @return  a string representation of the value of this object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
     *          base&nbsp;10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
    public String toString() {
3964
cf913644be58 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents: 3943
diff changeset
  1186
        return toString(value);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
     * Returns a hash code for this {@code Integer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
     * @return  a hash code value for this object, equal to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
     *          primitive {@code int} value represented by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
     *          {@code Integer} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
     */
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1196
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
    public int hashCode() {
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1198
        return Integer.hashCode(value);
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1199
    }
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1200
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1201
    /**
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 37877
diff changeset
  1202
     * Returns a hash code for an {@code int} value; compatible with
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1203
     * {@code Integer.hashCode()}.
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1204
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1205
     * @param value the value to hash
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1206
     * @since 1.8
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1207
     *
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 37877
diff changeset
  1208
     * @return a hash code value for an {@code int} value.
14503
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1209
     */
0729d9e57ed5 7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents: 14014
diff changeset
  1210
    public static int hashCode(int value) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     * Compares this object to the specified object.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
     * {@code true} if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
     * {@code null} and is an {@code Integer} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
     * contains the same {@code int} value as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
     * @param   obj   the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
     * @return  {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        if (obj instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
            return value == ((Integer)obj).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
     * Determines the integer value of the system property with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
     * specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
     *
10335
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1235
     * <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
  1236
     * property.  System properties are accessible through the {@link
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1237
     * java.lang.System#getProperty(java.lang.String)} method. The
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
     * 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
  1239
     * value using the grammar supported by {@link Integer#decode decode} and
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1240
     * an {@code Integer} object representing this value is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
     *
10335
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1242
     * <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
  1243
     * specified name is empty or {@code null}, or if the property
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1244
     * does not have the correct numeric format, then {@code null} is
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1245
     * returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
     * <p>In other words, this method returns an {@code Integer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
     * object equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
     *  {@code getInteger(nm, null)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
     * @param   nm   property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * @return  the {@code Integer} value of the property.
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 10335
diff changeset
  1256
     * @throws  SecurityException for the same reasons as
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 10335
diff changeset
  1257
     *          {@link System#getProperty(String) System.getProperty}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    public static Integer getInteger(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        return getInteger(nm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * Determines the integer value of the system property with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
     * specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
     *
10335
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1269
     * <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
  1270
     * property.  System properties are accessible through the {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * java.lang.System#getProperty(java.lang.String)} method. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
     * 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
  1273
     * value using the grammar supported by {@link Integer#decode decode} and
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1274
     * an {@code Integer} object representing this value is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
     * <p>The second argument is the default value. An {@code Integer} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
     * that represents the value of the second argument is returned if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
     * is no property of the specified name, if the property does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
     * the correct numeric format, or if the specified name is empty or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
     * {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
     * <p>In other words, this method returns an {@code Integer} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
     * equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
     *  {@code getInteger(nm, new Integer(val))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     * but in practice it may be implemented in a manner such as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     * Integer result = getInteger(nm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * return (result == null) ? new Integer(val) : result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * to avoid the unnecessary allocation of an {@code Integer}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * object when the default value is not needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
     * @param   nm   property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * @param   val   default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * @return  the {@code Integer} value of the property.
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 10335
diff changeset
  1302
     * @throws  SecurityException for the same reasons as
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 10335
diff changeset
  1303
     *          {@link System#getProperty(String) System.getProperty}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    public static Integer getInteger(String nm, int val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        Integer result = getInteger(nm, null);
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1309
        return (result == null) ? Integer.valueOf(val) : result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * Returns the integer value of the system property with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     * specified name.  The first argument is treated as the name of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * system property.  System properties are accessible through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * {@link java.lang.System#getProperty(java.lang.String)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
     * 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
  1318
     * integer value, as per the {@link Integer#decode decode} method,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     * and an {@code Integer} object representing this value is
10335
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1320
     * returned; in summary:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
     * <ul><li>If the property value begins with the two ASCII characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     *         {@code 0x} or the ASCII character {@code #}, not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     *      followed by a minus sign, then the rest of it is parsed as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
     *      hexadecimal integer exactly as by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
     *      {@link #valueOf(java.lang.String, int)} with radix 16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * <li>If the property value begins with the ASCII character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
     *     {@code 0} followed by another character, it is parsed as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
     *     octal integer exactly as by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
     *     {@link #valueOf(java.lang.String, int)} with radix 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * <li>Otherwise, the property value is parsed as a decimal integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
     * exactly as by the method {@link #valueOf(java.lang.String, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     * with radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
     * <p>The second argument is the default value. The default value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * returned if there is no property of the specified name, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
     * property does not have the correct numeric format, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
     * specified name is empty or {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * @param   nm   property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
     * @param   val   default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
     * @return  the {@code Integer} value of the property.
10602
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 10335
diff changeset
  1344
     * @throws  SecurityException for the same reasons as
ab8c1e3b237b 6268216: Boolean.getBoolean() throws SecurityException
darcy
parents: 10335
diff changeset
  1345
     *          {@link System#getProperty(String) System.getProperty}
10335
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1346
     * @see     System#getProperty(java.lang.String)
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1347
     * @see     System#getProperty(java.lang.String, java.lang.String)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
    public static Integer getInteger(String nm, Integer val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
        String v = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
            v = System.getProperty(nm);
10335
3c7eda3ab2f5 4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents: 10067
diff changeset
  1353
        } catch (IllegalArgumentException | NullPointerException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
                return Integer.decode(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
     * Decodes a {@code String} into an {@code Integer}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
     * Accepts decimal, hexadecimal, and octal numbers given
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
     * by the following grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
     * <dt><i>DecodableString:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18546
diff changeset
  1377
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * <dt><i>Sign:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * <dd>{@code -}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
     * <dd>{@code +}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * <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
  1385
     * 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
  1386
     * <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
  1387
     * except that underscores are not accepted between digits.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * <p>The sequence of characters following an optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * "{@code #}", or leading zero) is parsed as by the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * Integer.parseInt} method with the indicated radix (10, 16, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * 8).  This sequence of characters must represent a positive
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * value or a {@link NumberFormatException} will be thrown.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     * result is negated if first character of the specified {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * String} is the minus sign.  No whitespace characters are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * permitted in the {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * @param     nm the {@code String} to decode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * @return    an {@code Integer} object holding the {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     *             value represented by {@code nm}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * @exception NumberFormatException  if the {@code String} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     *            contain a parsable integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * @see java.lang.Integer#parseInt(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    public static Integer decode(String nm) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
        int radix = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
        boolean negative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
        Integer result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
        if (nm.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
            throw new NumberFormatException("Zero length string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
        char firstChar = nm.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        // Handle sign, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        if (firstChar == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
            negative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
        } else if (firstChar == '+')
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
        // Handle radix specifier, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
            index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            radix = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
        else if (nm.startsWith("#", index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
            index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
            radix = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
        else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
            radix = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
        if (nm.startsWith("-", index) || nm.startsWith("+", index))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
            throw new NumberFormatException("Sign character in wrong position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
            result = Integer.valueOf(nm.substring(index), radix);
2425
99a19a90813e 6807702: Integer.valueOf cache should be configurable
alanb
parents: 2
diff changeset
  1441
            result = negative ? Integer.valueOf(-result.intValue()) : result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
            // If number is Integer.MIN_VALUE, we'll end up here. The next line
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
            // handles this case, and causes any genuine format error to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
            // rethrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
            String constant = negative ? ("-" + nm.substring(index))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
                                       : nm.substring(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
            result = Integer.valueOf(constant, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
     * Compares two {@code Integer} objects numerically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     * @param   anotherInteger   the {@code Integer} to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * @return  the value {@code 0} if this {@code Integer} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
     *          equal to the argument {@code Integer}; a value less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
     *          {@code 0} if this {@code Integer} is numerically less
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
     *          than the argument {@code Integer}; and a value greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
     *          than {@code 0} if this {@code Integer} is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     *           greater than the argument {@code Integer} (signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *           comparison).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
    public int compareTo(Integer anotherInteger) {
3943
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1467
        return compare(this.value, anotherInteger.value);
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1468
    }
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1469
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1470
    /**
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1471
     * Compares two {@code int} values numerically.
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1472
     * 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
  1473
     * <pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1474
     *    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
  1475
     * </pre>
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1476
     *
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1477
     * @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
  1478
     * @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
  1479
     * @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
  1480
     *         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
  1481
     *         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
  1482
     * @since 1.7
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1483
     */
11abf5578222 6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents: 3288
diff changeset
  1484
    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
  1485
        return (x < y) ? -1 : ((x == y) ? 0 : 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
11672
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1488
    /**
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1489
     * Compares two {@code int} values numerically treating the values
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1490
     * as unsigned.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1491
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1492
     * @param  x the first {@code int} to compare
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1493
     * @param  y the second {@code int} to compare
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1494
     * @return the value {@code 0} if {@code x == y}; a value less
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1495
     *         than {@code 0} if {@code x < y} as unsigned values; and
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1496
     *         a value greater than {@code 0} if {@code x > y} as
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1497
     *         unsigned values
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1498
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1499
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1500
    public static int compareUnsigned(int x, int y) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1501
        return compare(x + MIN_VALUE, y + MIN_VALUE);
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1502
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1503
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1504
    /**
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1505
     * Converts the argument to a {@code long} by an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1506
     * conversion.  In an unsigned conversion to a {@code long}, the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1507
     * high-order 32 bits of the {@code long} are zero and the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1508
     * low-order 32 bits are equal to the bits of the integer
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1509
     * argument.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1510
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1511
     * Consequently, zero and positive {@code int} values are mapped
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1512
     * to a numerically equal {@code long} value and negative {@code
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1513
     * int} values are mapped to a {@code long} value equal to the
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1514
     * input plus 2<sup>32</sup>.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1515
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1516
     * @param  x the value to convert to an unsigned {@code long}
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1517
     * @return the argument converted to {@code long} by an unsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1518
     *         conversion
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1519
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1520
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1521
    public static long toUnsignedLong(int x) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1522
        return ((long) x) & 0xffffffffL;
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1523
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1524
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1525
    /**
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1526
     * Returns the unsigned quotient of dividing the first argument by
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1527
     * the second where each argument and the result is interpreted as
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1528
     * an unsigned value.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1529
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1530
     * <p>Note that in two's complement arithmetic, the three other
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1531
     * basic arithmetic operations of add, subtract, and multiply are
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1532
     * bit-wise identical if the two operands are regarded as both
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1533
     * being signed or both being unsigned.  Therefore separate {@code
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1534
     * addUnsigned}, etc. methods are not provided.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1535
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1536
     * @param dividend the value to be divided
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1537
     * @param divisor the value doing the dividing
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1538
     * @return the unsigned quotient of the first argument divided by
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1539
     * the second argument
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1540
     * @see #remainderUnsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1541
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1542
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1543
    public static int divideUnsigned(int dividend, int divisor) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1544
        // In lieu of tricky code, for now just use long arithmetic.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1545
        return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor));
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1546
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1547
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1548
    /**
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1549
     * Returns the unsigned remainder from dividing the first argument
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1550
     * by the second where each argument and the result is interpreted
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1551
     * as an unsigned value.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1552
     *
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1553
     * @param dividend the value to be divided
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1554
     * @param divisor the value doing the dividing
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1555
     * @return the unsigned remainder of the first argument divided by
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1556
     * the second argument
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1557
     * @see #divideUnsigned
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1558
     * @since 1.8
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1559
     */
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1560
    public static int remainderUnsigned(int dividend, int divisor) {
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1561
        // In lieu of tricky code, for now just use long arithmetic.
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1562
        return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1563
    }
a5fa8c844b54 4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents: 11275
diff changeset
  1564
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
    // Bit twiddling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
     * The number of bits used to represent an {@code int} value in two's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
     * complement binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
     */
15136
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 14507
diff changeset
  1574
    @Native public static final int SIZE = 32;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    /**
37880
60ec48925dc6 8156661: Handful of typos in javadoc
igerasim
parents: 37877
diff changeset
  1577
     * The number of bytes used to represent an {@code int} value in two's
14507
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
  1578
     * complement binary form.
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
  1579
     *
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
  1580
     * @since 1.8
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
  1581
     */
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
  1582
    public static final int BYTES = SIZE / Byte.SIZE;
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
  1583
066419d1e732 7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents: 14503
diff changeset
  1584
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * Returns an {@code int} value with at most a single one-bit, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * position of the highest-order ("leftmost") one-bit in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * {@code int} value.  Returns zero if the specified value has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * one-bits in its two's complement binary representation, that is, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1591
     * @param i the value whose highest one bit is to be computed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * @return an {@code int} value with a single one-bit, in the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     *     of the highest-order one-bit in the specified value, or zero if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     *     the specified value is itself equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
    public static int highestOneBit(int i) {
49426
7f3986bad197 8199843: Optimize Integer/Long.highestOneBit()
igerasim
parents: 49251
diff changeset
  1598
        return i & (MIN_VALUE >>> numberOfLeadingZeros(i));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * Returns an {@code int} value with at most a single one-bit, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     * position of the lowest-order ("rightmost") one-bit in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     * {@code int} value.  Returns zero if the specified value has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
     * one-bits in its two's complement binary representation, that is, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     * is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1608
     * @param i the value whose lowest one bit is to be computed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * @return an {@code int} value with a single one-bit, in the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     *     of the lowest-order one-bit in the specified value, or zero if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     *     the specified value is itself equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
    public static int lowestOneBit(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
        // HD, Section 2-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
        return i & -i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
     * Returns the number of zero bits preceding the highest-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
     * ("leftmost") one-bit in the two's complement binary representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
     * of the specified {@code int} value.  Returns 32 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
     * specified value has no one-bits in its two's complement representation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
     * in other words if it is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
     * <p>Note that this method is closely related to the logarithm base 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
     * For all positive {@code int} values x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
     * <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
     * <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1633
     * @param i the value whose number of leading zeros is to be computed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
     * @return the number of zero bits preceding the highest-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
     *     ("leftmost") one-bit in the two's complement binary representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
     *     of the specified {@code int} value, or 32 if the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
     *     is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1640
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
    public static int numberOfLeadingZeros(int i) {
50211
5afedc9e4662 8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents: 49426
diff changeset
  1642
        // HD, Count leading 0's
49251
3c0a12972165 8189230: JDK method:java.lang.Integer.numberOfLeadingZeros(int) can be optimized
bpb
parents: 47216
diff changeset
  1643
        if (i <= 0)
3c0a12972165 8189230: JDK method:java.lang.Integer.numberOfLeadingZeros(int) can be optimized
bpb
parents: 47216
diff changeset
  1644
            return i == 0 ? 32 : 0;
50211
5afedc9e4662 8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents: 49426
diff changeset
  1645
        int n = 31;
5afedc9e4662 8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents: 49426
diff changeset
  1646
        if (i >= 1 << 16) { n -= 16; i >>>= 16; }
5afedc9e4662 8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents: 49426
diff changeset
  1647
        if (i >= 1 <<  8) { n -=  8; i >>>=  8; }
5afedc9e4662 8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents: 49426
diff changeset
  1648
        if (i >= 1 <<  4) { n -=  4; i >>>=  4; }
5afedc9e4662 8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents: 49426
diff changeset
  1649
        if (i >= 1 <<  2) { n -=  2; i >>>=  2; }
5afedc9e4662 8203352: Improve java implementation of Integer/Long.numberOfLeadingZeros
redestad
parents: 49426
diff changeset
  1650
        return n - (i >>> 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
     * Returns the number of zero bits following the lowest-order ("rightmost")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
     * one-bit in the two's complement binary representation of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
     * {@code int} value.  Returns 32 if the specified value has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
     * one-bits in its two's complement representation, in other words if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
     * equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1660
     * @param i the value whose number of trailing zeros is to be computed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
     * @return the number of zero bits following the lowest-order ("rightmost")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
     *     one-bit in the two's complement binary representation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
     *     specified {@code int} value, or 32 if the value is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
     *     to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1667
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
    public static int numberOfTrailingZeros(int i) {
51509
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1669
        // HD, Count trailing 0's
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1670
        i = ~i & (i - 1);
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1671
        if (i <= 0) return i & 32;
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1672
        int n = 1;
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1673
        if (i > 1 << 16) { n += 16; i >>>= 16; }
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1674
        if (i > 1 <<  8) { n +=  8; i >>>=  8; }
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1675
        if (i > 1 <<  4) { n +=  4; i >>>=  4; }
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1676
        if (i > 1 <<  2) { n +=  2; i >>>=  2; }
758b3f3f3a8d 8209171: Simplify Java implementation of Integer/Long.numberOfTrailingZeros()
igerasim
parents: 51412
diff changeset
  1677
        return n + (i >>> 1);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
     * Returns the number of one-bits in the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
     * representation of the specified {@code int} value.  This function is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
     * sometimes referred to as the <i>population count</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1685
     * @param i the value whose bits are to be counted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
     * @return the number of one-bits in the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
     *     representation of the specified {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1690
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    public static int bitCount(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        // HD, Figure 5-2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
        i = i - ((i >>> 1) & 0x55555555);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
        i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        i = (i + (i >>> 4)) & 0x0f0f0f0f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        i = i + (i >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
        i = i + (i >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
        return i & 0x3f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * Returns the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * representation of the specified {@code int} value left by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * specified number of bits.  (Bits shifted out of the left hand, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     * high-order, side reenter on the right, or low-order.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
     * <p>Note that left rotation with a negative distance is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
     * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
     * distance)}.  Note also that rotation by any multiple of 32 is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
     * no-op, so all but the last five bits of the rotation distance can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
     * ignored, even if the distance is negative: {@code rotateLeft(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
     * distance) == rotateLeft(val, distance & 0x1F)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1714
     * @param i the value whose bits are to be rotated left
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1715
     * @param distance the number of bit positions to rotate left
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
     * @return the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
     *     representation of the specified {@code int} value left by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
     *     specified number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
    public static int rotateLeft(int i, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
        return (i << distance) | (i >>> -distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
     * Returns the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     * representation of the specified {@code int} value right by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     * specified number of bits.  (Bits shifted out of the right hand, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
     * low-order, side reenter on the left, or high-order.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
     * <p>Note that right rotation with a negative distance is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
     * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
     * distance)}.  Note also that rotation by any multiple of 32 is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
     * no-op, so all but the last five bits of the rotation distance can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
     * ignored, even if the distance is negative: {@code rotateRight(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
     * distance) == rotateRight(val, distance & 0x1F)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1738
     * @param i the value whose bits are to be rotated right
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1739
     * @param distance the number of bit positions to rotate right
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     * @return the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     *     representation of the specified {@code int} value right by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
     *     specified number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
    public static int rotateRight(int i, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
        return (i >>> distance) | (i << -distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
     * Returns the value obtained by reversing the order of the bits in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
     * two's complement binary representation of the specified {@code int}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1754
     * @param i the value to be reversed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
     * @return the value obtained by reversing order of the bits in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
     *     specified {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
    public static int reverse(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
        // HD, Figure 7-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
        i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
        i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
        i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f;
37877
dae28a12fb38 8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents: 37521
diff changeset
  1764
dae28a12fb38 8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents: 37521
diff changeset
  1765
        return reverseBytes(i);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
     * Returns the signum function of the specified {@code int} value.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
     * return value is -1 if the specified value is negative; 0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
     * specified value is zero; and 1 if the specified value is positive.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1773
     * @param i the value whose signum is to be computed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
     * @return the signum function of the specified {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
    public static int signum(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
        // HD, Section 2-7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
        return (i >> 31) | (-i >>> 31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
     * Returns the value obtained by reversing the order of the bytes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
     * two's complement representation of the specified {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
     *
18546
862067c6481c 8017550: Fix doclint issues in java.lang and subpackages
darcy
parents: 18278
diff changeset
  1786
     * @param i the value whose bytes are to be reversed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
     * @return the value obtained by reversing the bytes in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
     *     {@code int} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
     */
31671
362e0c0acece 8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents: 28059
diff changeset
  1791
    @HotSpotIntrinsicCandidate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    public static int reverseBytes(int i) {
37877
dae28a12fb38 8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents: 37521
diff changeset
  1793
        return (i << 24)            |
dae28a12fb38 8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents: 37521
diff changeset
  1794
               ((i & 0xff00) << 8)  |
dae28a12fb38 8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents: 37521
diff changeset
  1795
               ((i >>> 8) & 0xff00) |
dae28a12fb38 8155795: Optimize Integer/Long.reverse by using reverseBytes
redestad
parents: 37521
diff changeset
  1796
               (i >>> 24);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
15311
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1799
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1800
     * Adds two integers together as per the + operator.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1801
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1802
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1803
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1804
     * @return the sum of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1805
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1806
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1807
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1808
    public static int sum(int a, int b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1809
        return a + b;
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1810
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1811
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1812
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1813
     * Returns the greater of two {@code int} values
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1814
     * as if by calling {@link Math#max(int, int) Math.max}.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1815
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1816
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1817
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1818
     * @return the greater of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1819
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1820
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1821
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1822
    public static int max(int a, int b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1823
        return Math.max(a, b);
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1824
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1825
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1826
    /**
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1827
     * Returns the smaller of two {@code int} values
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1828
     * as if by calling {@link Math#min(int, int) Math.min}.
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1829
     *
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1830
     * @param a the first operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1831
     * @param b the second operand
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1832
     * @return the smaller of {@code a} and {@code b}
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1833
     * @see java.util.function.BinaryOperator
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1834
     * @since 1.8
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1835
     */
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1836
    public static int min(int a, int b) {
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1837
        return Math.min(a, b);
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1838
    }
be0ff4a719bf 8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents: 15140
diff changeset
  1839
52914
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1840
    /**
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1841
     * Returns a nominal descriptor for this instance, which is the instance
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1842
     * itself.
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1843
     *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1844
     * @return an {@link Optional} describing the {@linkplain Integer} instance
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1845
     * @since 12
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1846
     */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1847
    @Override
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1848
    public Optional<Integer> describeConstable() {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1849
        return Optional.of(this);
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1850
    }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1851
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1852
    /**
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1853
     * Resolves this instance as a {@link ConstantDesc}, the result of which is
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1854
     * the instance itself.
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1855
     *
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1856
     * @param lookup ignored
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1857
     * @return the {@linkplain Integer} instance
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1858
     * @since 12
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1859
     */
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1860
    @Override
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1861
    public Integer resolveConstantDesc(MethodHandles.Lookup lookup) {
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1862
        return this;
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1863
    }
4fa75d8ad418 8210031: implementation for JVM Constants API
vromero
parents: 52626
diff changeset
  1864
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
15136
c17824042364 8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents: 14507
diff changeset
  1866
    @Native private static final long serialVersionUID = 1360826667806852920L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
}