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