home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / Long.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  29.2 KB  |  797 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)Long.java    1.42 98/08/26
  3.  *
  4.  * Copyright 1994-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.lang;
  16.  
  17. /**
  18.  * The Long class wraps a value of the primitive type <code>long</code>
  19.  * in an object. An object of type <code>Long</code> contains a single
  20.  * field whose type is <code>long</code>.
  21.  * <p>
  22.  * In addition, this class provides several methods for converting a
  23.  * <code>long</code> to a <code>String</code> and a
  24.  * <code>String</code> to a <code>long</code>, as well as other
  25.  * constants and methods useful when dealing with a
  26.  * <code>long</code>.
  27.  *
  28.  * @author  Lee Boynton
  29.  * @author  Arthur van Hoff
  30.  * @version 1.42, 08/26/98
  31.  * @since   JDK1.0
  32.  */
  33. public final class Long extends Number implements Comparable {
  34.     /**
  35.      * The smallest value of type <code>long</code>.
  36.      */
  37.     public static final long MIN_VALUE = 0x8000000000000000L;
  38.  
  39.     /**
  40.      * The largest value of type <code>long</code>.
  41.      */
  42.     public static final long MAX_VALUE = 0x7fffffffffffffffL;
  43.  
  44.     /**
  45.      * The Class object representing the primitive type long.
  46.      *
  47.      * @since   JDK1.1
  48.      */
  49.     public static final Class    TYPE = Class.getPrimitiveClass("long");
  50.  
  51.     /**
  52.      * Creates a string representation of the first argument in the
  53.      * radix specified by the second argument.
  54.      * <p>
  55.      * If the radix is smaller than <code>Character.MIN_RADIX</code> or
  56.      * larger than <code>Character.MAX_RADIX</code>, then the radix
  57.      * <code>10</code> is used instead.
  58.      * <p>
  59.      * If the first argument is negative, the first element of the 
  60.      * result is the ASCII minus sign <code>'-'</code> 
  61.      * (<code>'\u002d'</code>. If the first argument is not negative, 
  62.      * no sign character appears in the result. 
  63.      * <p>
  64.      * The remaining characters of the result represent the magnitude of 
  65.      * the first argument. If the magnitude is zero, it is represented by 
  66.      * a single zero character <code>'0'</code> 
  67.      * (<code>'\u0030'</code>); otherwise, the first character of the
  68.      * representation of the magnitude will not be the zero character.
  69.      * The following ASCII characters are used as digits: 
  70.      * <blockquote><pre>
  71.      *   0123456789abcdefghijklmnopqrstuvwxyz
  72.      * </pre></blockquote>
  73.      * These are <tt>'\u0030'</tt> through <tt>'\u0039'</tt> 
  74.      * and <tt>'\u0061'</tt> through <tt>'\u007a'</tt>. If the 
  75.      * radix is <var>N</var>, then the first <var>N</var> of these 
  76.      * characters are used as radix-<var>N</var> digits in the order 
  77.      * shown. Thus, the digits for hexadecimal (radix 16) are 
  78.      * <tt>0123456789abcdef</tt>. If uppercase letters
  79.      * are desired, the {@link java.lang.String#toUpperCase()} method 
  80.      * may be called on the result: 
  81.      * <blockquote><pre>
  82.      * Long.toString(n, 16).toUpperCase()
  83.      * </pre></blockquote>
  84.      * 
  85.      * @param   i       a long.
  86.      * @param   radix   the radix.
  87.      * @return  a string representation of the argument in the specified radix.
  88.      * @see     java.lang.Character#MAX_RADIX
  89.      * @see     java.lang.Character#MIN_RADIX
  90.      */
  91.     public static String toString(long i, int radix) {
  92.         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  93.         radix = 10;
  94.  
  95.     char[] buf = new char[65];
  96.     int charPos = 64;
  97.     boolean negative = (i < 0);
  98.  
  99.     if (!negative) {
  100.         i = -i;
  101.     }
  102.  
  103.     while (i <= -radix) {
  104.         buf[charPos--] = Integer.digits[(int)(-(i % radix))];
  105.         i = i / radix;
  106.     }
  107.     buf[charPos] = Integer.digits[(int)(-i)];
  108.  
  109.     if (negative) {
  110.         buf[--charPos] = '-';
  111.     }
  112.  
  113.     return new String(buf, charPos, (65 - charPos));
  114.     }
  115.  
  116.     /**
  117.      * Creates a string representation of the long argument as an
  118.      * unsigned integer in base 16.
  119.      * <p>
  120.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  121.      * the argument is negative; otherwise, it is equal to the argument. 
  122.      * <p>
  123.      * If the unsigned magnitude is zero, it is represented by a single 
  124.      * zero character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, 
  125.      * the first character of the representation of the unsigned magnitude 
  126.      * will not be the zero character. The following characters are used 
  127.      * as hexadecimal digits:
  128.      * <blockquote><pre>
  129.      * 0123456789abcdef
  130.      * </pre></blockquote>
  131.      * These are the characters <tt>'\u0030'</tt> through 
  132.      * <tt>'\u0039'</tt> and '\u0061' through <tt>'\u0066'</tt>. 
  133.      * If uppercase letters are desired, the 
  134.      * {@link java.lang.String#toUpperCase()} method may be called on 
  135.      * the result: 
  136.      * <blockquote><pre>
  137.      * Long.toHexString(n).toUpperCase()
  138.      * </pre></blockquote>
  139.      *
  140.      * @param   i   a <code>long</code>.
  141.      * @return  the string representation of the unsigned long value
  142.      *          represented by the argument in hexadecimal (base 16).
  143.      * @since   JDK 1.0.2
  144.      */
  145.     public static String toHexString(long i) {
  146.     return toUnsignedString(i, 4);
  147.     }
  148.  
  149.     /**
  150.      * Creates a string representation of the long argument as an
  151.      * unsigned integer in base 8.
  152.      * <p>
  153.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  154.      * the argument is negative; otherwise, it is equal to the argument. 
  155.      * <p>
  156.      * If the unsigned magnitude is zero, it is represented by a single zero
  157.      * character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, the 
  158.      * first character of the representation of the unsigned magnitude 
  159.      * will not be the zero character. The following characters are used 
  160.      * as octal digits:
  161.      * <blockquote><pre>
  162.      * 01234567
  163.      * </pre></blockquote>
  164.      * These are the characters <tt>'\u0030'</tt> through 
  165.      * <tt>'\u0037'</tt>. 
  166.      *
  167.      * @param   i   a <code>long</code>.
  168.      * @return  the string representation of the unsigned long value
  169.      *          represented by the argument in octal (base 8).
  170.      * @since   JDK 1.0.2
  171.      */
  172.     public static String toOctalString(long i) {
  173.     return toUnsignedString(i, 3);
  174.     }
  175.  
  176.     /**
  177.      * Creates a string representation of the long argument as an
  178.      * unsigned integer in base 2.
  179.      * <p>
  180.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  181.      * the argument is negative; otherwise, it is equal to the argument. 
  182.      * <p>
  183.      * If the unsigned magnitude is zero, it is represented by a single zero
  184.      * character <tt>'0'</tt> (<tt>'&392;u0030'</tt>); otherwise, the 
  185.      * first character of the representation of the unsigned magnitude 
  186.      * will not be the zero character. The characters <tt>'0'</tt> 
  187.      * (<tt>'\u0030'</tt>) and <tt>'1'</tt> (<tt>'\u0031'</tt>) 
  188.      * are used as binary digits. 
  189.      *
  190.      * @param   i   a long.
  191.      * @return  the string representation of the unsigned long value
  192.      *          represented by the argument in binary (base 2).
  193.      * @since   JDK 1.0.2
  194.      */
  195.     public static String toBinaryString(long i) {
  196.     return toUnsignedString(i, 1);
  197.     }
  198.  
  199.     /**
  200.      * Convert the integer to an unsigned number.
  201.      */
  202.     private static String toUnsignedString(long i, int shift) {
  203.     char[] buf = new char[64];
  204.     int charPos = 64;
  205.     int radix = 1 << shift;
  206.     long mask = radix - 1;
  207.     do {
  208.         buf[--charPos] = Integer.digits[(int)(i & mask)];
  209.         i >>>= shift;
  210.     } while (i != 0);
  211.     return new String(buf, charPos, (64 - charPos));
  212.     }
  213.  
  214.     /**
  215.      * Returns a new String object representing the specified integer. 
  216.      * The argument is converted to signed decimal representation and 
  217.      * returned as a string, exactly as if the argument and the radix 
  218.      * 10 were given as arguments to the 
  219.      * {@link #toString(java.lang.String, int)} method that takes two 
  220.      * arguments.
  221.      *
  222.      * @param   i   a <code>long</code> to be converted.
  223.      * @return  a string representation of the argument in base 10.
  224.      */
  225.     public static String toString(long i) {
  226.     return toString(i, 10);
  227.     }
  228.  
  229.     /**
  230.      * Parses the string argument as a signed <code>long</code> in the 
  231.      * radix specified by the second argument. The characters in the 
  232.      * string must all be digits of the specified radix (as determined by 
  233.      * whether <code>Character.digit</code> returns a 
  234.      * nonnegative value), except that the first character may be an 
  235.      * ASCII minus sign <code>'-'</code> (<tt>'\u002d'</tt> to indicate 
  236.      * a negative value. The resulting <code>long</code> value is returned. 
  237.      * <p>
  238.      * Note that neither <tt>L</tt> nor <tt>l</tt> is permitted to appear at 
  239.      * the end of the string as a type indicator, as would be permitted in 
  240.      * Java programming language source code - except that either <tt>L</tt> 
  241.      * or <tt>l</tt> may appear as a digit for a radix greater than 22.
  242.      * <p>
  243.      * An exception of type <tt>NumberFormatException</tt> is thrown if any of
  244.      * the following situations occurs:
  245.      * <ul>
  246.      * <li>The first argument is <tt>null</tt> or is a string of length zero. 
  247.      * <li>The <tt>radix</tt> is either smaller than 
  248.      *     {@link java.lang.Character#MIN_RADIX} or larger than 
  249.      *     {@link java.lang.Character#MAX_RADIX}. 
  250.      * <li>The first character of the string is not a digit of the
  251.      *     specified <tt>radix</tt> and is not a minus sign <tt>'-'</tt> 
  252.      *     (<tt>'\u002d'</tt>). 
  253.      * <li>The first character of the string is a minus sign and the
  254.      *     string is of length 1. 
  255.      * <li>Any character of the string after the first is not a digit of
  256.      *     the specified <tt>radix</tt>. 
  257.      * <li>The integer value represented by the string cannot be
  258.      *     represented as a value of type <tt>long</tt>. 
  259.      * </ul><p>
  260.      * Examples:
  261.      * <blockquote><pre>
  262.      * parseLong("0", 10) returns 0L
  263.      * parseLong("473", 10) returns 473L
  264.      * parseLong("-0", 10) returns 0L
  265.      * parseLong("-FF", 16) returns -255L
  266.      * parseLong("1100110", 2) returns 102L
  267.      * parseLong("99", 8) throws a NumberFormatException
  268.      * parseLong("Hazelnut", 10) throws a NumberFormatException
  269.      * parseLong("Hazelnut", 36) returns 1356099454469L
  270.      * </pre></blockquote>
  271.      * 
  272.      * @param      s       the <code>String</code> containing the
  273.      *                     <code>long</code>.
  274.      * @param      radix   the radix to be used.
  275.      * @return     the <code>long</code> represented by the string argument in
  276.      *             the specified radix.
  277.      * @exception  NumberFormatException  if the string does not contain a
  278.      *               parsable integer.
  279.      */
  280.     public static long parseLong(String s, int radix)
  281.               throws NumberFormatException
  282.     {
  283.         if (s == null) {
  284.             throw new NumberFormatException("null");
  285.         }
  286.  
  287.     if (radix < Character.MIN_RADIX) {
  288.         throw new NumberFormatException("radix " + radix +
  289.                         " less than Character.MIN_RADIX");
  290.     }
  291.     if (radix > Character.MAX_RADIX) {
  292.         throw new NumberFormatException("radix " + radix +
  293.                         " greater than Character.MAX_RADIX");
  294.     }
  295.  
  296.     long result = 0;
  297.     boolean negative = false;
  298.     int i = 0, max = s.length();
  299.     long limit;
  300.     long multmin;
  301.     int digit;
  302.  
  303.     if (max > 0) {
  304.         if (s.charAt(0) == '-') {
  305.         negative = true;
  306.         limit = Long.MIN_VALUE;
  307.         i++;
  308.         } else {
  309.         limit = -Long.MAX_VALUE;
  310.         }
  311.         multmin = limit / radix;
  312.             if (i < max) {
  313.                 digit = Character.digit(s.charAt(i++),radix);
  314.         if (digit < 0) {
  315.             throw new NumberFormatException(s);
  316.         } else {
  317.             result = -digit;
  318.         }
  319.         }
  320.         while (i < max) {
  321.         // Accumulating negatively avoids surprises near MAX_VALUE
  322.         digit = Character.digit(s.charAt(i++),radix);
  323.         if (digit < 0) {
  324.             throw new NumberFormatException(s);
  325.         }
  326.         if (result < multmin) {
  327.             throw new NumberFormatException(s);
  328.         }
  329.         result *= radix;
  330.         if (result < limit + digit) {
  331.             throw new NumberFormatException(s);
  332.         }
  333.         result -= digit;
  334.         }
  335.     } else {
  336.         throw new NumberFormatException(s);
  337.     }
  338.     if (negative) {
  339.         if (i > 1) {
  340.         return result;
  341.         } else {    /* Only got "-" */
  342.         throw new NumberFormatException(s);
  343.         }
  344.     } else {
  345.         return -result;
  346.     }
  347.     }
  348.  
  349.     /**
  350.      * Parses the string argument as a signed decimal <code>long</code>. 
  351.      * The characters in the string must all be decimal digits, except 
  352.      * that the first character may be an ASCII minus sign 
  353.      * <code>'-'</code> (<code>\u002d'</code>) to indicate a negative 
  354.      * value. The resulting long value is returned, exactly as if the
  355.      * argument and the radix <tt>10</tt> were given as arguments to the
  356.      * {@link #parseLong(String, int)} method that takes two arguments. 
  357.      * <p>
  358.      * Note that neither <tt>L</tt> nor <tt>l</tt> is permitted to appear 
  359.      * at the end of the string as a type indicator, as would be permitted
  360.      * in Java programming language source code.
  361.      *
  362.      * @param      s   a string.
  363.      * @return     the <code>long</code> represented by the argument in decimal.
  364.      * @exception  NumberFormatException  if the string does not contain a
  365.      *               parsable <code>long</code>.
  366.      */
  367.     public static long parseLong(String s) throws NumberFormatException {
  368.     return parseLong(s, 10);
  369.     }
  370.  
  371.     /**
  372.      * Returns a new long object initialized to the value of the
  373.      * specified String. Throws an exception if the String cannot be
  374.      * parsed as a long.
  375.      * <p>
  376.      * The first argument is interpreted as representing a signed integer
  377.      * in the radix specified by the second argument, exactly as if the
  378.      * arguments were given to the {@link #parseLong(java.lang.String, int)} 
  379.      * method that takes two arguments. The result is a <tt>Long</tt> object 
  380.      * that represents the integer value specified by the string. 
  381.      * <p>
  382.      * In other words, this method returns a <tt>Long</tt> object equal 
  383.      * to the value of:
  384.      * <blockquote><pre>
  385.      * new Long(Long.parseLong(s, radix))
  386.      * </pre></blockquote>
  387.      *
  388.      * @param      s       the <code>String</code> containing the
  389.      *                     <code>long</code>.
  390.      * @param      radix   the radix to be used.
  391.      * @return     a newly constructed <code>Long</code> initialized to the
  392.      *             value represented by the string argument in the specified
  393.      *             radix.
  394.      * @exception  NumberFormatException  If the <code>String</code> does not
  395.      *               contain a parsable <code>long</code>.
  396.      */
  397.     public static Long valueOf(String s, int radix) throws NumberFormatException {
  398.     return new Long(parseLong(s, radix));
  399.     }
  400.  
  401.     /**
  402.      * Returns a new long object initialized to the value of the
  403.      * specified String. Throws an exception if the String cannot be
  404.      * parsed as a long. The radix is assumed to be 10.
  405.      * <p>
  406.      * The argument is interpreted as representing a signed decimal
  407.      * integer, exactly as if the argument were given to the 
  408.      * {@link #parseLong(java.lang.String)} method that takes one argument). 
  409.      * The result is a <code>Long</code> object that represents the integer 
  410.      * value specified by the string. 
  411.      * <p>
  412.      * In other words, this method returns a <tt>Long</tt> object equal to 
  413.      * the value of:
  414.      * <blockquote><pre>
  415.      * new Long(Long.parseLong(s))
  416.      * </pre></blockquote>
  417.      *
  418.      * @param      s   the string to be parsed.
  419.      * @return     a newly constructed <code>Long</code> initialized to the
  420.      *             value represented by the string argument.
  421.      * @exception  NumberFormatException  If the <code>String</code> does not
  422.      *               contain a parsable <code>long</code>.
  423.      */
  424.     public static Long valueOf(String s) throws NumberFormatException
  425.     {
  426.     return new Long(parseLong(s, 10));
  427.     }
  428.  
  429.     /**
  430.      * Decodes a <code>String</code> into a <code>Long</code>.  Accepts
  431.      * decimal, hexadecimal, and octal numbers, in the following formats:
  432.      * <pre>
  433.      *     [-]    <decimal constant>
  434.      *     [-] 0x     <hex constant>
  435.      *     [-] #      <hex constant>
  436.      *     [-] 0    <octal constant>
  437.      * </pre>
  438.      *
  439.      * The constant following an (optional) negative sign and/or "radix
  440.      * specifier" is parsed as by the <code>Long.parseLong</code> method
  441.      * with the specified radix (10, 8 or 16).  This constant must be positive
  442.      * or a NumberFormatException will result.  The result is made negative if
  443.      * first character of the specified <code>String</code> is the negative
  444.      * sign.  No whitespace characters are permitted in the
  445.      * <code>String</code>.
  446.      *
  447.      * @param     nm the <code>String</code> to decode.
  448.      * @return    the <code>Long</code> represented by the specified string.
  449.      * @exception NumberFormatException  if the <code>String</code> does not
  450.      *            contain a parsable long.
  451.      * @see java.lang.Long#parseLong(String, int)
  452.      */
  453.     public static Long decode(String nm) throws NumberFormatException {
  454.         int radix = 10;
  455.         int index = 0;
  456.         boolean negative = false;
  457.         Long result;
  458.  
  459.         // Handle minus sign, if present
  460.         if (nm.startsWith("-")) {
  461.             negative = true;
  462.             index++;
  463.         }
  464.  
  465.         // Handle radix specifier, if present
  466.     if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  467.         index += 2;
  468.             radix = 16;
  469.     }
  470.     else if (nm.startsWith("#", index)) {
  471.         index ++;
  472.             radix = 16;
  473.     }
  474.     else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  475.         index ++;
  476.             radix = 8;
  477.     }
  478.  
  479.         if (nm.startsWith("-", index))
  480.             throw new NumberFormatException("Negative sign in wrong position");
  481.  
  482.         try {
  483.             result = Long.valueOf(nm.substring(index), radix);
  484.             result = negative ? new Long((long)-result.longValue()) : result;
  485.         } catch (NumberFormatException e) {
  486.             // If number is Long.MIN_VALUE, we'll end up here. The next line
  487.             // handles this case, and causes any genuine format error to be
  488.             // rethrown.
  489.             String constant = negative ? new String("-" + nm.substring(index))
  490.                                        : nm.substring(index);
  491.             result = Long.valueOf(constant, radix);
  492.         }
  493.         return result;
  494.     }
  495.  
  496.     /**
  497.      * The value of the Long.
  498.      *
  499.      * @serial
  500.      */
  501.     private long value;
  502.  
  503.     /**
  504.      * Constructs a newly allocated <code>Long</code> object that
  505.      * represents the primitive <code>long</code> argument.
  506.      *
  507.      * @param   value   the value to be represented by the 
  508.      *          <code>Long</code> object.
  509.      */
  510.     public Long(long value) {
  511.     this.value = value;
  512.     }
  513.  
  514.     /**
  515.      * Constructs a newly allocated <code>Long</code> object that 
  516.      * represents the value represented by the string in decimal form. 
  517.      * The string is converted to an <code>long</code> value as if by the 
  518.      * {@link #parseLong(java.lang.String, int)} method for radix 10. 
  519.      *
  520.      * @param      s   the string to be converted to a <code>Long</code>.
  521.      * @exception  NumberFormatException  if the <code>String</code> does not
  522.      *               contain a parsable long integer.
  523.      * @see        java.lang.Long#valueOf(java.lang.String)
  524.      */
  525.     public Long(String s) throws NumberFormatException {
  526.     this.value = parseLong(s, 10);
  527.     }
  528.  
  529.     /**
  530.      * Returns the value of this Long as a byte.
  531.      *
  532.      * @since   JDK1.1
  533.      */
  534.     public byte byteValue() {
  535.     return (byte)value;
  536.     }
  537.  
  538.     /**
  539.      * Returns the value of this Long as a short.
  540.      *
  541.      * @since   JDK1.1
  542.      */
  543.     public short shortValue() {
  544.     return (short)value;
  545.     }
  546.  
  547.     /**
  548.      * Returns the value of this Long as an int.
  549.      *
  550.      * @return  the <code>long</code> value represented by this object is
  551.      *          converted to type <code>int</code> and the result of the
  552.      *          conversion is returned.
  553.      */
  554.     public int intValue() {
  555.     return (int)value;
  556.     }
  557.  
  558.     /**
  559.      * Returns the value of this Long as a long value.
  560.      *
  561.      * @return  the <code>long</code> value represented by this object.
  562.      */
  563.     public long longValue() {
  564.     return (long)value;
  565.     }
  566.  
  567.     /**
  568.      * Returns the value of this Long as a float.
  569.      *
  570.      * @return  the <code>long</code> value represented by this object is
  571.      *          converted to type <code>float</code> and the result of
  572.      *          the conversion is returned.
  573.      */
  574.     public float floatValue() {
  575.     return (float)value;
  576.     }
  577.  
  578.     /**
  579.      * Returns the value of this Long as a double.
  580.      *
  581.      * @return  the <code>long</code> value represented by this object that
  582.      *          is converted to type <code>double</code> and the result of
  583.      *          the conversion is returned.
  584.      */
  585.     public double doubleValue() {
  586.     return (double)value;
  587.     }
  588.  
  589.     /**
  590.      * Returns a String object representing this Long's value. 
  591.      * The long integer value represented by this Long object is converted 
  592.      * to signed decimal representation and returned as a string, exactly 
  593.      * as if the long value were given as an argument to the 
  594.      * {@link #toString(long)} method that takes one argument.
  595.      *
  596.      * @return  a string representation of this object in base 10.
  597.      */
  598.     public String toString() {
  599.     return String.valueOf(value);
  600.     }
  601.  
  602.     /**
  603.      * Computes a hashcode for this Long. The result is the exclusive 
  604.      * OR of the two halves of the primitive <code>long</code> value 
  605.      * represented by this <code>Long</code> object. That is, the hashcode 
  606.      * is the value of the expression: 
  607.      * <blockquote><pre>
  608.      * (int)(this.longValue()^(this.longValue()>>>32))
  609.      * </pre></blockquote>
  610.      *
  611.      * @return  a hash code value for this object.
  612.      */
  613.     public int hashCode() {
  614.     return (int)(value ^ (value >> 32));
  615.     }
  616.  
  617.     /**
  618.      * Compares this object against the specified object.
  619.      * The result is <code>true</code> if and only if the argument is
  620.      * not <code>null</code> and is a <code>Long</code> object that
  621.      * contains the same <code>long</code> value as this object.
  622.      *
  623.      * @param   obj   the object to compare with.
  624.      * @return  <code>true</code> if the objects are the same;
  625.      *          <code>false</code> otherwise.
  626.      */
  627.     public boolean equals(Object obj) {
  628.     if ((obj != null) && (obj instanceof Long)) {
  629.         return value == ((Long)obj).longValue();
  630.     }
  631.     return false;
  632.     }
  633.  
  634.     /**
  635.      * Determines the <code>long</code> value of the system property
  636.      * with the specified name.
  637.      * <p>
  638.      * The first argument is treated as the name of a system property. 
  639.      * System properties are accessible through the 
  640.      * {@link java.lang.System#getProperty(java.lang.String)} method. The 
  641.      * string value of this property is then interpreted as a long value 
  642.      * and a <code>Long</code> object representing this value is returned. 
  643.      * Details of possible numeric formats can be found with the 
  644.      * definition of <code>getProperty</code>. 
  645.      * <p>
  646.      * If there is no property with the specified name, or if the
  647.      * property does not have the correct numeric format, then
  648.      * <code>null</code> is returned.
  649.      * <p>
  650.      * In other words, this method returns a <tt>Long</tt> object equal to 
  651.      * the value of:
  652.      * <blockquote><pre>
  653.      * getLong(nm, null)
  654.      * </pre></blockquote>
  655.      *
  656.      * @param   nm   property name.
  657.      * @return  the <code>Long</code> value of the property.
  658.      * @see     java.lang.System#getProperty(java.lang.String)
  659.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  660.      */
  661.     public static Long getLong(String nm) {
  662.     return getLong(nm, null);
  663.     }
  664.  
  665.     /**
  666.      * Determines the <code>long</code> value of the system property
  667.      * with the specified name.
  668.      * <p>
  669.      * The first argument is treated as the name of a system property. 
  670.      * System properties are accessible through the 
  671.      * {@link java.lang.System#getProperty()} method. The 
  672.      * string value of this property is then interpreted as a long value 
  673.      * and a <code>Long</code> object representing this value is returned. 
  674.      * Details of possible numeric formats can be found with the 
  675.      * definition of <code>getProperty</code>. 
  676.      * <p>
  677.      * If there is no property with the specified name, or if the 
  678.      * property does not have the correct numeric format, then a 
  679.      * <code>Long</code> object that represents the value of the second 
  680.      * argument is returned. 
  681.      * <p>
  682.      * In other words, this method returns a <tt>Long</tt> object equal 
  683.      * to the value of:
  684.      * <blockquote><pre>
  685.      * getLong(nm, new Long(val))
  686.      * </pre></blockquote>
  687.      * but in practice it may be implemented in a manner such as: 
  688.      * <blockquote><pre>
  689.      * Long result = getLong(nm, null);
  690.      * return (result == null) ? new Long(val) : result;
  691.      * </pre></blockquote>
  692.      * to avoid the unnecessary allocation of a <tt>Long</tt> object when 
  693.      * the default value is not needed. 
  694.      *
  695.      * @param   nm    property name.
  696.      * @param   val   default value.
  697.      * @return  the <code>Long</code> value of the property.
  698.      * @see     java.lang.System#getProperty(java.lang.String)
  699.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  700.      */
  701.     public static Long getLong(String nm, long val) {
  702.         Long result = Long.getLong(nm, null);
  703.         return (result == null) ? new Long(val) : result;
  704.     }
  705.  
  706.     /**
  707.      * Returns the long value of the system property with the specified
  708.      * name.  The first argument is treated as the name of a system property.
  709.      * System properties are accessible through the 
  710.      * {@link java.lang.System#getProperty(java.lang.String)} method. 
  711.      * The string value of this property is then interpreted as a long 
  712.      * value, as per the <code>Long.decode</code> method, and a 
  713.      * <code>Long</code> object representing this value is returned.
  714.      * <p><ul>
  715.      * <li>If the property value begins with the two ASCII characters
  716.      * <tt>0x</tt> or the ASCII character <tt>#</tt>, not followed by a 
  717.      * minus sign, then the rest of it is parsed as a hexadecimal integer
  718.      * exactly as for the method {@link #valueOf(java.lang.String, int)} 
  719.      * with radix 16. 
  720.      * <li>If the property value begins with the character <tt>0</tt> followed
  721.      * by another character, it is parsed as an octal integer exactly
  722.      * as for the method {@link #valueOf(java.lang.String, int)} with radix 8. 
  723.      * <li>Otherwise the property value is parsed as a decimal
  724.      * integer exactly as for the method 
  725.      * {@link #valueOf(java.lang.String, int)} with radix 10. 
  726.      * <p>
  727.      * Note that, in every case, neither <tt>L</tt> nor <tt>l</tt> is 
  728.      * permitted to appear at the end of the property value as a type 
  729.      * indicator, as would be permitted in Java programming language 
  730.      * source code.
  731.      * <p>
  732.      * The second argument is the default value. If there is no property 
  733.      * of the specified name, or if the property does not have the 
  734.      * correct numeric format, then the second argument is returned. 
  735.      *
  736.      * @param   nm   property name.
  737.      * @param   val   default value.
  738.      * @return  the <code>Long</code> value of the property.
  739.      * @see     java.lang.System#getProperty(java.lang.String)
  740.      * @see java.lang.System#getProperty(java.lang.String, java.lang.String)
  741.      * @see java.lang.Long#decode
  742.      */
  743.     public static Long getLong(String nm, Long val) {
  744.     String v = System.getProperty(nm);
  745.     if (v != null) {
  746.         try {
  747.         return Long.decode(v);
  748.         } catch (NumberFormatException e) {
  749.         }
  750.     }
  751.     return val;
  752.     }
  753.  
  754.     /**
  755.      * Compares two Longs numerically.
  756.      *
  757.      * @param   anotherLong   the <code>Long</code> to be compared.
  758.      * @return  the value <code>0</code> if the argument Long is equal to
  759.      *          this Long; a value less than <code>0</code> if this Long
  760.      *          is numerically less than the Long argument; and a
  761.      *          value greater than <code>0</code> if this Long is
  762.      *          numerically greater than the Long argument
  763.      *        (signed comparison).
  764.      * @since   JDK1.2
  765.      */
  766.     public int compareTo(Long anotherLong) {
  767.     long thisVal = this.value;
  768.     long anotherVal = anotherLong.value;
  769.     return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
  770.     }
  771.  
  772.     /**
  773.      * Compares this Long to another Object.  If the Object is a Long,
  774.      * this function behaves like <code>compareTo(Long)</code>.  Otherwise,
  775.      * it throws a <code>ClassCastException</code> (as Longs are comparable
  776.      * only to other Longs).
  777.      *
  778.      * @param   o the <code>Object</code> to be compared.
  779.      * @return  the value <code>0</code> if the argument is a Long
  780.      *        numerically equal to this Long; a value less than
  781.      *        <code>0</code> if the argument is a Long numerically
  782.      *        greater than this Long; and a value greater than
  783.      *        <code>0</code> if the argument is a Long numerically
  784.      *        less than this Long.
  785.      * @exception <code>ClassCastException</code> if the argument is not a
  786.      *          <code>Long</code>.
  787.      * @see     java.lang.Comparable
  788.      * @since   JDK1.2
  789.      */
  790.     public int compareTo(Object o) {
  791.     return compareTo((Long)o);
  792.     }
  793.  
  794.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  795.     private static final long serialVersionUID = 4290774380558885855L;
  796. }
  797.