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