home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / Long.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  19.9 KB  |  590 lines

  1. /*
  2.  * @(#)Long.java    1.37 98/03/18
  3.  *
  4.  * Copyright 1994-1997 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.37, 03/18/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>. If the first 
  61.      * argument is not negative, no sign character appears in the result. 
  62.      * The following ASCII characters are used as digits: 
  63.      * <blockquote><pre>
  64.      *   0123456789abcdefghijklmnopqrstuvwxyz
  65.      * </pre></blockquote>
  66.      *
  67.      * @param   i       a long.
  68.      * @param   radix   the radix.
  69.      * @return  a string representation of the argument in the specified radix.
  70.      * @see     java.lang.Character#MAX_RADIX
  71.      * @see     java.lang.Character#MIN_RADIX
  72.      */
  73.     public static String toString(long i, int radix) {
  74.         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  75.         radix = 10;
  76.     
  77.     char[] buf = new char[65];
  78.     int charPos = 64;
  79.     boolean negative = (i < 0);
  80.        
  81.     if (!negative) {
  82.         i = -i;
  83.     }
  84.     
  85.     while (i <= -radix) {
  86.         buf[charPos--] = Integer.digits[(int)(-(i % radix))];
  87.         i = i / radix;
  88.     }
  89.     buf[charPos] = Integer.digits[(int)(-i)];
  90.     
  91.     if (negative) {
  92.         buf[--charPos] = '-';
  93.     }
  94.  
  95.     return new String(buf, charPos, (65 - charPos));
  96.     }
  97.  
  98.     /**
  99.      * Creates a string representation of the long argument as an 
  100.      * unsigned integer in base 16. 
  101.      * <p>
  102.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  103.      * the argument is negative; otherwise, it is equal to the argument. 
  104.      * This value is converted to a string of ASCII digits in hexadecimal 
  105.      * (base 16) with no extra leading <code>0</code>s. 
  106.      *
  107.      * @param   i   a <code>long</code>.
  108.      * @return  the string representation of the unsigned long value
  109.      *          represented by the argument in hexadecimal (base 16).
  110.      * @since   JDK 1.0.2
  111.      */
  112.     public static String toHexString(long i) {
  113.     return toUnsignedString(i, 4);
  114.     }
  115.  
  116.     /**
  117.      * Creates a string representation of the long argument as an 
  118.      * unsigned integer in base 8. 
  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.      * This value is converted to a string of ASCII digits in octal 
  123.      * (base 8) with no extra leading <code>0</code>s. 
  124.      *
  125.      * @param   i   a <code>long</code>.
  126.      * @return  the string representation of the unsigned long value
  127.      *          represented by the argument in octal (base 8).
  128.      * @since   JDK 1.0.2
  129.      */
  130.     public static String toOctalString(long i) {
  131.     return toUnsignedString(i, 3);
  132.     }
  133.  
  134.     /**
  135.      * Creates a string representation of the long argument as an 
  136.      * unsigned integer in base 2. 
  137.      * <p>
  138.      * The unsigned long value is the argument plus 2<sup>64</sup> if 
  139.      * the argument is negative; otherwise, it is equal to the argument. 
  140.      * This value is converted to a string of ASCII digits in binary 
  141.      * (base 2) with no extra leading <code>0</code>s. 
  142.      *
  143.      * @param   i   a long.
  144.      * @return  the string representation of the unsigned long value
  145.      *          represented by the argument in binary (base 2).
  146.      * @since   JDK 1.0.2
  147.      */
  148.     public static String toBinaryString(long i) {
  149.     return toUnsignedString(i, 1);
  150.     }
  151.  
  152.     /** 
  153.      * Convert the integer to an unsigned number.
  154.      */
  155.     private static String toUnsignedString(long i, int shift) {
  156.     char[] buf = new char[64];
  157.     int charPos = 64;
  158.     int radix = 1 << shift;
  159.     long mask = radix - 1;
  160.     do {
  161.         buf[--charPos] = Integer.digits[(int)(i & mask)];
  162.         i >>>= shift;
  163.     } while (i != 0);
  164.     return new String(buf, charPos, (64 - charPos));
  165.     }
  166.  
  167.     /**
  168.      * Returns a new String object representing the specified integer. The radix
  169.      * is assumed to be 10.
  170.      *
  171.      * @param   i   a <code>long</code> to be converted.
  172.      * @return  a string representation of the argument in base 10.
  173.      */
  174.     public static String toString(long i) {
  175.     return toString(i, 10);
  176.     }
  177.  
  178.     /**
  179.      * Parses the string argument as a signed <code>long</code> in the 
  180.      * radix specified by the second argument. The characters in the 
  181.      * string must all be digits of the specified radix (as determined by 
  182.      * whether <code>Character.digit</code> returns a 
  183.      * nonnegative value), except that the first character may be an 
  184.      * ASCII minus sign <code>'-'</code> to indicate a negative value. 
  185.      * The resulting <code>long</code> value is returned. 
  186.      *
  187.      * @param      s       the <code>String</code> containing the
  188.      *                     <code>long</code>.
  189.      * @param      radix   the radix to be used.
  190.      * @return     the <code>long</code> represented by the string argument in
  191.      *             the specified radix.
  192.      * @exception  NumberFormatException  if the string does not contain a
  193.      *               parsable integer.
  194.      */
  195.     public static long parseLong(String s, int radix) 
  196.               throws NumberFormatException 
  197.     {
  198.         if (s == null) {
  199.             throw new NumberFormatException("null");
  200.         }
  201.  
  202.     if (radix < Character.MIN_RADIX) {
  203.         throw new NumberFormatException("radix " + radix + 
  204.                         " less than Character.MIN_RADIX");
  205.     }
  206.     if (radix > Character.MAX_RADIX) {
  207.         throw new NumberFormatException("radix " + radix + 
  208.                         " greater than Character.MAX_RADIX");
  209.     }
  210.  
  211.     long result = 0;
  212.     boolean negative = false;
  213.     int i = 0, max = s.length();
  214.     long limit;
  215.     long multmin;
  216.     int digit;
  217.  
  218.     if (max > 0) {
  219.         if (s.charAt(0) == '-') {
  220.         negative = true;
  221.         limit = Long.MIN_VALUE;
  222.         i++;
  223.         } else {
  224.         limit = -Long.MAX_VALUE;
  225.         }
  226.         multmin = limit / radix;
  227.             if (i < max) {
  228.                 digit = Character.digit(s.charAt(i++),radix);
  229.         if (digit < 0) {
  230.             throw new NumberFormatException(s);
  231.         } else {
  232.             result = -digit;
  233.         }
  234.         }    
  235.         while (i < max) {
  236.         // Accumulating negatively avoids surprises near MAX_VALUE
  237.         digit = Character.digit(s.charAt(i++),radix);
  238.         if (digit < 0) {
  239.             throw new NumberFormatException(s);
  240.         }
  241.         if (result < multmin) {
  242.             throw new NumberFormatException(s);
  243.         }
  244.         result *= radix;
  245.         if (result < limit + digit) {
  246.             throw new NumberFormatException(s);
  247.         }
  248.         result -= digit;
  249.         }
  250.     } else {
  251.         throw new NumberFormatException(s);
  252.     }
  253.     if (negative) {
  254.         if (i > 1) {
  255.         return result;
  256.         } else {    /* Only got "-" */
  257.         throw new NumberFormatException(s);
  258.         }
  259.     } else {
  260.         return -result;
  261.     }
  262.     }
  263.  
  264.     /**
  265.      * Parses the string argument as a signed decimal <code>long</code>. 
  266.      * The characters in the string must all be decimal digits, except 
  267.      * that the first character may be an ASCII minus sign 
  268.      * <code>'-'</code> to indicate a negative value. 
  269.      *
  270.      * @param      s   a string.
  271.      * @return     the <code>long</code> represented by the argument in decimal.
  272.      * @exception  NumberFormatException  if the string does not contain a
  273.      *               parsable <code>long</code>.
  274.      */
  275.     public static long parseLong(String s) throws NumberFormatException {
  276.     return parseLong(s, 10);
  277.     }
  278.  
  279.     /**
  280.      * Returns a new long object initialized to the value of the
  281.      * specified String. Throws an exception if the String cannot be
  282.      * parsed as a long.
  283.      *
  284.      * @param      s       the <code>String</code> containing the
  285.      *                     <code>long</code>.
  286.      * @param      radix   the radix to be used. 
  287.      * @return     a newly constructed <code>Long</code> initialized to the
  288.      *             value represented by the string argument in the specified
  289.      *             radix.
  290.      * @exception  NumberFormatException  If the <code>String</code> does not
  291.      *               contain a parsable <code>long</code>.
  292.      */
  293.     public static Long valueOf(String s, int radix) throws NumberFormatException {
  294.     return new Long(parseLong(s, radix));
  295.     }
  296.  
  297.     /**
  298.      * Returns a new long object initialized to the value of the
  299.      * specified String. Throws an exception if the String cannot be
  300.      * parsed as a long. The radix is assumed to be 10.
  301.      *
  302.      * @param      s   the string to be parsed.
  303.      * @return     a newly constructed <code>Long</code> initialized to the
  304.      *             value represented by the string argument.
  305.      * @exception  NumberFormatException  If the <code>String</code> does not
  306.      *               contain a parsable <code>long</code>.
  307.      */
  308.     public static Long valueOf(String s) throws NumberFormatException 
  309.     {
  310.     return new Long(parseLong(s, 10));
  311.     }
  312.  
  313.     /**
  314.      * The value of the Long.
  315.      */
  316.     private long value;
  317.  
  318.     /**
  319.      * Constructs a newly allocated <code>Long</code> object that 
  320.      * represents the primitive <code>long</code> argument. 
  321.      *
  322.      * @param   value   the value to be represented by the <code>Long</code>.
  323.      */
  324.     public Long(long value) {
  325.     this.value = value;
  326.     }
  327.  
  328.     /**
  329.      * Constructs a newly allocated <code>Long</code> object that 
  330.      * represents the value represented by the string. The string is 
  331.      * converted to an <code>long</code> value as if by the 
  332.      * <code>valueOf</code> method. 
  333.      *
  334.      * @param      s   the string to be converted to a <code>Long</code>.
  335.      * @exception  NumberFormatException  if the <code>String</code> does not
  336.      *               contain a parsable long integer.
  337.      * @see        java.lang.Long#valueOf(java.lang.String)
  338.      */
  339.     public Long(String s) throws NumberFormatException {
  340.     this.value = parseLong(s, 10);
  341.     }
  342.  
  343.     /**
  344.      * Returns the value of this Long as a byte.
  345.      *
  346.      * @since   JDK1.1
  347.      */
  348.     public byte byteValue() {
  349.     return (byte)value;
  350.     }
  351.  
  352.     /**
  353.      * Returns the value of this Long as a short.
  354.      *
  355.      * @since   JDK1.1
  356.      */
  357.     public short shortValue() {
  358.     return (short)value;
  359.     }
  360.  
  361.     /**
  362.      * Returns the value of this Long as an int.
  363.      *
  364.      * @return  the <code>long</code> value represented by this object is
  365.      *          converted to type <code>int</code> and the result of the
  366.      *          conversion is returned.
  367.      */
  368.     public int intValue() {
  369.     return (int)value;
  370.     }
  371.  
  372.     /**
  373.      * Returns the value of this Long as a long.
  374.      *
  375.      * @return  the <code>long</code> value represented by this object.
  376.      */
  377.     public long longValue() {
  378.     return (long)value;
  379.     }
  380.  
  381.     /**
  382.      * Returns the value of this Long as a float.
  383.      *
  384.      * @return  the <code>long</code> value represented by this object is
  385.      *          converted to type <code>float</code> and the result of
  386.      *          the conversion is returned.
  387.      */
  388.     public float floatValue() {
  389.     return (float)value;
  390.     }
  391.  
  392.     /**
  393.      * Returns the value of this Long as a double.
  394.      *
  395.      * @return  the <code>long</code> value represented by this object that
  396.      *          is converted to type <code>double</code> and the result of
  397.      *          the conversion is returned.
  398.      */
  399.     public double doubleValue() {
  400.     return (double)value;
  401.     }
  402.  
  403.     /**
  404.      * Returns a String object representing this Long's value.
  405.      *
  406.      * @return  a string representation of this object in base 10.
  407.      */
  408.     public String toString() {
  409.     return String.valueOf(value);
  410.     }
  411.  
  412.     /**
  413.      * Computes a hashcode for this Long.
  414.      *
  415.      * @return  a hash code value for this object. 
  416.      */
  417.     public int hashCode() {
  418.     return (int)(value ^ (value >> 32));
  419.     }
  420.  
  421.     /**
  422.      * Compares this object against the specified object.
  423.      * The result is <code>true</code> if and only if the argument is 
  424.      * not <code>null</code> and is a <code>Long</code> object that 
  425.      * contains the same <code>long</code> value as this object. 
  426.      *
  427.      * @param   obj   the object to compare with.
  428.      * @return  <code>true</code> if the objects are the same;
  429.      *          <code>false</code> otherwise.
  430.      */
  431.     public boolean equals(Object obj) {
  432.     if ((obj != null) && (obj instanceof Long)) {
  433.         return value == ((Long)obj).longValue();
  434.     }
  435.     return false;
  436.     }
  437.  
  438.     /**
  439.      * Determines the <code>long</code> value of the system property 
  440.      * with the specified name. 
  441.      * <p>
  442.      * The first argument is treated as the name of a system property. 
  443.      * System properties are accessible through <code>getProperty</code>  
  444.      * and , a method defined by the <code>System</code> class. The 
  445.      * string value of this property is then interpreted as a long value 
  446.      * and a <code>Long</code> object representing this value is returned. 
  447.      * Details of possible numeric formats can be found with the 
  448.      * definition of <code>getProperty</code>. 
  449.      * <p>
  450.      * If there is no property with the specified name, or if the 
  451.      * property does not have the correct numeric format, then 
  452.      * <code>null</code> is returned. 
  453.      *
  454.      * @param   nm   property name.
  455.      * @return  the <code>Long</code> value of the property.
  456.      * @see     java.lang.System#getProperty(java.lang.String)
  457.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  458.      */
  459.     public static Long getLong(String nm) {
  460.     return getLong(nm, null);
  461.     }
  462.  
  463.     /**
  464.      * Determines the <code>long</code> value of the system property 
  465.      * with the specified name. 
  466.      * <p>
  467.      * The first argument is treated as the name of a system property. 
  468.      * System properties are accessible through <code>getProperty</code>  
  469.      * and , a method defined by the <code>System</code> class. The 
  470.      * string value of this property is then interpreted as a long value 
  471.      * and a <code>Long</code> object representing this value is returned. 
  472.      * Details of possible numeric formats can be found with the 
  473.      * definition of <code>getProperty</code>. 
  474.      * <p>
  475.      * If there is no property with the specified name, or if the 
  476.      * property does not have the correct numeric format, then a 
  477.      * <code>Long</code> object that represents the value of the second 
  478.      * argument is returned. 
  479.      *
  480.      * @param   nm    property name.
  481.      * @param   val   default value.
  482.      * @return  the <code>Long</code> value of the property.
  483.      * @see     java.lang.System#getProperty(java.lang.String)
  484.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  485.      */
  486.     public static Long getLong(String nm, long val) {
  487.         Long result = Long.getLong(nm, null);
  488.         return (result == null) ? new Long(val) : result;
  489.     }
  490.  
  491.     /**
  492.      * Determines the <code>long</code> value of the system property 
  493.      * with the specified name. 
  494.      * <p>
  495.      * The first argument is treated as the name of a system property. 
  496.      * System properties are accessible through <code>getProperty</code>  
  497.      * and , a method defined by the <code>System</code> class. The 
  498.      * string value of this property is then interpreted as a long value 
  499.      * and a <code>Long</code> object representing this value is returned. 
  500.      * <p>
  501.      * If the property value begins with "<code>0x</code>" or 
  502.      * "<code>#</code>", not followed by a minus sign, the rest 
  503.      * of it is parsed as a hexadecimal integer exactly as for the method 
  504.      * <code>Long.valueOf</code> with radix 16. 
  505.      * <p>
  506.      * If the property value begins with "<code>0</code>", 
  507.      * then it is parsed as an octal integer exactly as for the method 
  508.      * <code>Long.valueOf</code> with radix 8. 
  509.      * <p>
  510.      * Otherwise the property value is parsed as a decimal integer 
  511.      * exactly as for the method <code>Long.valueOf</code> with radix 10. 
  512.      * <p>
  513.      * Note that, in every case, neither <code>L</code> nor 
  514.      * <code>l</code> is permitted to appear at the end of the string. 
  515.      * <p>
  516.      * The second argument is the default value. If there is no property 
  517.      * of the specified name, or if the property does not have the 
  518.      * correct numeric format, then the second argument is returned. 
  519.      *
  520.      * @param   nm    the property name.
  521.      * @param   val   the default <code>Long</code> value.
  522.      * @return  the <code>long</code> value of the property.
  523.      * @see     java.lang.Long#valueOf(java.lang.String, int)
  524.      * @see     java.lang.System#getProperty(java.lang.String)
  525.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  526.      */
  527.     public static Long getLong(String nm, Long val) {
  528.     String v = System.getProperty(nm);
  529.     if (v != null) {
  530.         try {
  531.         if (v.startsWith("0x")) {
  532.             return Long.valueOf(v.substring(2), 16);
  533.         }
  534.         if (v.startsWith("#")) {
  535.             return Long.valueOf(v.substring(1), 16);
  536.         }
  537.         if (v.startsWith("0") && v.length() > 1) {
  538.             return Long.valueOf(v.substring(1), 8);
  539.         }
  540.         return Long.valueOf(v);
  541.         } catch (NumberFormatException e) {
  542.         }
  543.     }    
  544.     return val;
  545.     }
  546.  
  547.     /**
  548.      * Compares two Longs numerically.
  549.      *
  550.      * @param   anotherLong   the <code>Long</code> to be compared.
  551.      * @return  the value <code>0</code> if the argument Long is equal to
  552.      *          this Long; a value less than <code>0</code> if this Long
  553.      *          is numerically less than the Long argument; and a
  554.      *          value greater than <code>0</code> if this Long is
  555.      *          numerically greater than the Long argument
  556.      *        (signed comparison).
  557.      * @since   JDK1.2
  558.      */
  559.     public int compareTo(Long anotherLong) {
  560.     long thisVal = this.value;
  561.     long anotherVal = anotherLong.value;
  562.     return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
  563.     }
  564.  
  565.     /**
  566.      * Compares this Long to another Object.  If the Object is a Long,
  567.      * this function behaves like <code>compareTo(Long)</code>.  Otherwise,
  568.      * it throws a <code>ClassCastException</code> (as Longs are comparable
  569.      * only to other Longs).
  570.      *
  571.      * @param   o the <code>Object</code> to be compared.
  572.      * @return  the value <code>0</code> if the argument is a Long
  573.      *        numerically equal to this Long; a value less than
  574.      *        <code>0</code> if the argument is a Long numerically
  575.      *        greater than this Long; and a value greater than
  576.      *        <code>0</code> if the argument is a Long numerically
  577.      *        less than this Long.
  578.      * @exception <code>ClassCastException</code> if the argument is not a
  579.      *          <code>Long</code>. 
  580.      * @see     java.lang.Comparable
  581.      * @since   JDK1.2
  582.      */
  583.     public int compareTo(Object o) {
  584.     return compareTo((Long)o);
  585.     }
  586.  
  587.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  588.     private static final long serialVersionUID = 4290774380558885855L;
  589. }
  590.