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