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