jdk/src/share/classes/java/lang/Long.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 51 6fe31bc95bbc
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1994-2006 Sun Microsystems, Inc.  All Rights Reserved.
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        return new Long(parseLong(s, radix));
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
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
        return new Long(parseLong(s, 10));
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
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * @param  l a long value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * @return a {@code Long} instance representing {@code l}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public static Long valueOf(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        final int offset = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        if (l >= -128 && l <= 127) { // will cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            return LongCache.cache[(int)l + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        return new Long(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * Decodes a {@code String} into a {@code Long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * Accepts decimal, hexadecimal, and octal numbers given by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * following grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * <dt><i>DecodableString:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * <dt><i>Sign:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * <dd>{@code -}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <dd>{@code +}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">&sect;3.10.1</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Language Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <p>The sequence of characters following an optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * "{@code #}", or leading zero) is parsed as by the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * Long.parseLong} method with the indicated radix (10, 16, or 8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * This sequence of characters must represent a positive value or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * a {@link NumberFormatException} will be thrown.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * negated if first character of the specified {@code String} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * the minus sign.  No whitespace characters are permitted in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @param     nm the {@code String} to decode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @return    a {@code Long} object holding the {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *            value represented by {@code nm}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @throws    NumberFormatException  if the {@code String} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     *            contain a parsable {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * @see java.lang.Long#parseLong(String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public static Long decode(String nm) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        int radix = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        boolean negative = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        Long result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
        if (nm.length() == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            throw new NumberFormatException("Zero length string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        char firstChar = nm.charAt(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        // Handle sign, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        if (firstChar == '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            negative = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        } else if (firstChar == '+')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            index++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        // Handle radix specifier, if present
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            index += 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            radix = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        else if (nm.startsWith("#", index)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            radix = 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            index ++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            radix = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (nm.startsWith("-", index) || nm.startsWith("+", index))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            throw new NumberFormatException("Sign character in wrong position");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            result = Long.valueOf(nm.substring(index), radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            result = negative ? new Long((long)-result.longValue()) : result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
        } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            // If number is Long.MIN_VALUE, we'll end up here. The next line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            // handles this case, and causes any genuine format error to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            // rethrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            String constant = negative ? ("-" + nm.substring(index))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                                       : nm.substring(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            result = Long.valueOf(constant, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * The value of the {@code Long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    private final long value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * Constructs a newly allocated {@code Long} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * represents the specified {@code long} argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @param   value   the value to be represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     *          {@code Long} object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    public Long(long value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     * Constructs a newly allocated {@code Long} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * represents the {@code long} value indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * {@code String} parameter. The string is converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * {@code long} value in exactly the manner used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * {@code parseLong} method for radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * @param      s   the {@code String} to be converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     *             {@code Long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * @throws     NumberFormatException  if the {@code String} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *             contain a parsable {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @see        java.lang.Long#parseLong(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
    public Long(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        this.value = parseLong(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     * Returns the value of this {@code Long} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    public byte byteValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return (byte)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Returns the value of this {@code Long} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    public short shortValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        return (short)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * Returns the value of this {@code Long} as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Returns the value of this {@code Long} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public long longValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        return (long)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * Returns the value of this {@code Long} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * {@code float}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    public float floatValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        return (float)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
     * Returns the value of this {@code Long} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     * {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
    public double doubleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return (double)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Returns a {@code String} object representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * {@code Long}'s value.  The value is converted to signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * decimal representation and returned as a string, exactly as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * the {@code long} value were given as an argument to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * {@link java.lang.Long#toString(long)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @return  a string representation of the value of this object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *          base&nbsp;10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        return String.valueOf(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * Returns a hash code for this {@code Long}. The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * the exclusive OR of the two halves of the primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * {@code long} value held by this {@code Long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * object. That is, the hashcode is the value of the expression:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     *  {@code (int)(this.longValue()^(this.longValue()>>>32))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * @return  a hash code value for this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        return (int)(value ^ (value >>> 32));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Compares this object to the specified object.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * {@code true} if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     * {@code null} and is a {@code Long} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * contains the same {@code long} value as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * @param   obj   the object to compare with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     * @return  {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     *          {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        if (obj instanceof Long) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return value == ((Long)obj).longValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * Determines the {@code long} value of the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     * with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
     * <p>The first argument is treated as the name of a system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * System properties are accessible through the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     * java.lang.System#getProperty(java.lang.String)} method. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * string value of this property is then interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * {@code long} value and a {@code Long} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * representing this value is returned.  Details of possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * numeric formats can be found with the definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * {@code getProperty}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * <p>If there is no property with the specified name, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * specified name is empty or {@code null}, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * property does not have the correct numeric format, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * {@code null} is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * <p>In other words, this method returns a {@code Long} object equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     * the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *  {@code getLong(nm, null)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @param   nm   property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @return  the {@code Long} value of the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public static Long getLong(String nm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        return getLong(nm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
     * Determines the {@code long} value of the system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * with the specified name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * <p>The first argument is treated as the name of a system property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * System properties are accessible through the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * java.lang.System#getProperty(java.lang.String)} method. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * string value of this property is then interpreted as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * {@code long} value and a {@code Long} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * representing this value is returned.  Details of possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * numeric formats can be found with the definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * {@code getProperty}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * <p>The second argument is the default value. A {@code Long} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * that represents the value of the second argument is returned if there
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * is no property of the specified name, if the property does not have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * the correct numeric format, or if the specified name is empty or null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     * <p>In other words, this method returns a {@code Long} object equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     *  {@code getLong(nm, new Long(val))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * but in practice it may be implemented in a manner such as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * <blockquote><pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * Long result = getLong(nm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * return (result == null) ? new Long(val) : result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * </pre></blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * to avoid the unnecessary allocation of a {@code Long} object when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * the default value is not needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * @param   nm    property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @param   val   default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * @return  the {@code Long} value of the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    public static Long getLong(String nm, long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        Long result = Long.getLong(nm, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        return (result == null) ? new Long(val) : result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * Returns the {@code long} value of the system property with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * the specified name.  The first argument is treated as the name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * of a system property.  System properties are accessible through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * the {@link java.lang.System#getProperty(java.lang.String)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * method. The string value of this property is then interpreted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * as a {@code long} value, as per the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * {@code Long.decode} method, and a {@code Long} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * representing this value is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * <li>If the property value begins with the two ASCII characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * {@code 0x} or the ASCII character {@code #}, not followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * a minus sign, then the rest of it is parsed as a hexadecimal integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * exactly as for the method {@link #valueOf(java.lang.String, int)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * with radix 16.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     * <li>If the property value begins with the ASCII character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
     * {@code 0} followed by another character, it is parsed as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * an octal integer exactly as by the method {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * #valueOf(java.lang.String, int)} with radix 8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * <li>Otherwise the property value is parsed as a decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * integer exactly as by the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * {@link #valueOf(java.lang.String, int)} with radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * <p>Note that, in every case, neither {@code L}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * (<code>'&#92;u004C'</code>) nor {@code l}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * (<code>'&#92;u006C'</code>) is permitted to appear at the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * of the property value as a type indicator, as would be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * permitted in Java programming language source code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * <p>The second argument is the default value. The default value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * returned if there is no property of the specified name, if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * property does not have the correct numeric format, or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * specified name is empty or {@code null}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * @param   nm   property name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * @param   val   default value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @return  the {@code Long} value of the property.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @see     java.lang.System#getProperty(java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @see java.lang.Long#decode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    public static Long getLong(String nm, Long val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        String v = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            v = System.getProperty(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        } catch (IllegalArgumentException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        } catch (NullPointerException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        if (v != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
                return Long.decode(v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
            } catch (NumberFormatException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        return val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     * Compares two {@code Long} objects numerically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * @param   anotherLong   the {@code Long} to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * @return  the value {@code 0} if this {@code Long} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *          equal to the argument {@code Long}; a value less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     *          {@code 0} if this {@code Long} is numerically less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *          than the argument {@code Long}; and a value greater
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     *          than {@code 0} if this {@code Long} is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     *           greater than the argument {@code Long} (signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
     *           comparison).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
    public int compareTo(Long anotherLong) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        long thisVal = this.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
        long anotherVal = anotherLong.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
        return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
    // Bit Twiddling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
     * The number of bits used to represent a {@code long} value in two's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
     * complement binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
    public static final int SIZE = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
     * Returns a {@code long} value with at most a single one-bit, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
     * position of the highest-order ("leftmost") one-bit in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
     * {@code long} value.  Returns zero if the specified value has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
     * one-bits in its two's complement binary representation, that is, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
     * is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
     * @return a {@code long} value with a single one-bit, in the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
     *     of the highest-order one-bit in the specified value, or zero if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
     *     the specified value is itself equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
    public static long highestOneBit(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
        // HD, Figure 3-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        i |= (i >>  1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        i |= (i >>  2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
        i |= (i >>  4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
        i |= (i >>  8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
        i |= (i >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        i |= (i >> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        return i - (i >>> 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * Returns a {@code long} value with at most a single one-bit, in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     * position of the lowest-order ("rightmost") one-bit in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * {@code long} value.  Returns zero if the specified value has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * one-bits in its two's complement binary representation, that is, if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     * is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
     * @return a {@code long} value with a single one-bit, in the position
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
     *     of the lowest-order one-bit in the specified value, or zero if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     *     the specified value is itself equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    public static long lowestOneBit(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        // HD, Section 2-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
        return i & -i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
     * Returns the number of zero bits preceding the highest-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
     * ("leftmost") one-bit in the two's complement binary representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
     * of the specified {@code long} value.  Returns 64 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
     * specified value has no one-bits in its two's complement representation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
     * in other words if it is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
     * <p>Note that this method is closely related to the logarithm base 2.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
     * For all positive {@code long} values x:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
     * <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
     * <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
     * @return the number of zero bits preceding the highest-order
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
     *     ("leftmost") one-bit in the two's complement binary representation
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
     *     of the specified {@code long} value, or 64 if the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     *     is equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    public static int numberOfLeadingZeros(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
        // HD, Figure 5-6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
         if (i == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
            return 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        int n = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        int x = (int)(i >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
        if (x == 0) { n += 32; x = (int)i; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
        if (x >>> 16 == 0) { n += 16; x <<= 16; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
        if (x >>> 24 == 0) { n +=  8; x <<=  8; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        if (x >>> 28 == 0) { n +=  4; x <<=  4; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
        if (x >>> 30 == 0) { n +=  2; x <<=  2; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        n -= x >>> 31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     * Returns the number of zero bits following the lowest-order ("rightmost")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     * one-bit in the two's complement binary representation of the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * {@code long} value.  Returns 64 if the specified value has no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
     * one-bits in its two's complement representation, in other words if it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
     * equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
     * @return the number of zero bits following the lowest-order ("rightmost")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
     *     one-bit in the two's complement binary representation of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
     *     specified {@code long} value, or 64 if the value is equal
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *     to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
    public static int numberOfTrailingZeros(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
        // HD, Figure 5-14
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
        int x, y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        if (i == 0) return 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
        int n = 63;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        y = x <<16; if (y != 0) { n = n -16; x = y; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
        y = x << 8; if (y != 0) { n = n - 8; x = y; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        y = x << 4; if (y != 0) { n = n - 4; x = y; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        y = x << 2; if (y != 0) { n = n - 2; x = y; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        return n - ((x << 1) >>> 31);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     * Returns the number of one-bits in the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
     * representation of the specified {@code long} value.  This function is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
     * sometimes referred to as the <i>population count</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
     * @return the number of one-bits in the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
     *     representation of the specified {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     public static int bitCount(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        // HD, Figure 5-14
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
        i = i - ((i >>> 1) & 0x5555555555555555L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
        i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
        i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        i = i + (i >>> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        i = i + (i >>> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        i = i + (i >>> 32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        return (int)i & 0x7f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     * Returns the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
     * representation of the specified {@code long} value left by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
     * specified number of bits.  (Bits shifted out of the left hand, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
     * high-order, side reenter on the right, or low-order.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
     * <p>Note that left rotation with a negative distance is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * right rotation: {@code rotateLeft(val, -distance) == rotateRight(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * distance)}.  Note also that rotation by any multiple of 64 is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     * no-op, so all but the last six bits of the rotation distance can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * ignored, even if the distance is negative: {@code rotateLeft(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * distance) == rotateLeft(val, distance & 0x3F)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
     * @return the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
     *     representation of the specified {@code long} value left by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
     *     specified number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public static long rotateLeft(long i, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        return (i << distance) | (i >>> -distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Returns the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * representation of the specified {@code long} value right by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     * specified number of bits.  (Bits shifted out of the right hand, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * low-order, side reenter on the left, or high-order.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     * <p>Note that right rotation with a negative distance is equivalent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
     * left rotation: {@code rotateRight(val, -distance) == rotateLeft(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
     * distance)}.  Note also that rotation by any multiple of 64 is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
     * no-op, so all but the last six bits of the rotation distance can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
     * ignored, even if the distance is negative: {@code rotateRight(val,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
     * distance) == rotateRight(val, distance & 0x3F)}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
     * @return the value obtained by rotating the two's complement binary
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
     *     representation of the specified {@code long} value right by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
     *     specified number of bits.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
    public static long rotateRight(long i, int distance) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
        return (i >>> distance) | (i << -distance);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
     * Returns the value obtained by reversing the order of the bits in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
     * two's complement binary representation of the specified {@code long}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
     * value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
     * @return the value obtained by reversing order of the bits in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
     *     specified {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
    public static long reverse(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
        // HD, Figure 7-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
        i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        i = (i << 48) | ((i & 0xffff0000L) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
            ((i >>> 16) & 0xffff0000L) | (i >>> 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
     * Returns the signum function of the specified {@code long} value.  (The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
     * return value is -1 if the specified value is negative; 0 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
     * specified value is zero; and 1 if the specified value is positive.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
     * @return the signum function of the specified {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
    public static int signum(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        // HD, Section 2-7
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        return (int) ((i >> 63) | (-i >>> 63));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * Returns the value obtained by reversing the order of the bytes in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * two's complement representation of the specified {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * @return the value obtained by reversing the bytes in the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *     {@code long} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    public static long reverseBytes(long i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        return (i << 48) | ((i & 0xffff0000L) << 16) |
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
            ((i >>> 16) & 0xffff0000L) | (i >>> 48);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
    /** use serialVersionUID from JDK 1.0.2 for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
    private static final long serialVersionUID = 4290774380558885855L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
}