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