home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / src / java / lang / long~1.jav < prev    next >
Encoding:
Text File  |  1996-01-12  |  8.9 KB  |  319 lines

  1. /*
  2.  * @(#)Long.java    1.20 95/10/04  
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. package java.lang;
  21.  
  22. /**
  23.  * The Long class provides an object wrapper for Long data values and serves as 
  24.  * a place for long-oriented operations.  A wrapper is useful because most of Java's
  25.  * utility classes require the use of objects.  Since longs are not objects in Java,
  26.  * they need to be "wrapped" in a Long instance.
  27.  * @version     1.20, 10/04/95
  28.  * @author    Lee Boynton
  29.  * @author    Arthur van Hoff
  30.  */
  31. public final
  32. class Long extends Number {
  33.     /**
  34.      * The minimum value a Long can have.  The lowest minimum value that a
  35.      * Long can have is 0x8000000000000000.
  36.      */
  37.     public static final long MIN_VALUE = 0x8000000000000000L;
  38.  
  39.     /**
  40.      * The maximum value a Long can have.  The larget maximum value that a
  41.      * Long can have is 0x7fffffffffffffff.
  42.      */
  43.     public static final long MAX_VALUE = 0x7fffffffffffffffL;
  44.  
  45.     /**
  46.      * Returns a new String object representing the specified long in
  47.      * the specified radix.
  48.      * @param i        the long to be converted
  49.      * @param radix    the radix
  50.      * @see Character#MIN_RADIX
  51.      * @see Character#MAX_RADIX
  52.      */
  53.     public static String toString(long i, int radix) {
  54.     StringBuffer buf;
  55.     int digitCount = 0;
  56.     boolean minval = false;
  57.     boolean negative = false;
  58.     if (i == 0x8000000000000000L) {
  59.             switch (radix) {
  60.                 case 2:  return "-1000000000000000000000000000000000000000000000000000000000000000";
  61.                 case 4:  return "-20000000000000000000000000000000";
  62.                 case 8:  return "-1000000000000000000000";
  63.                 case 16: return "-8000000000000000";
  64.                 default: 
  65.                     minval = negative = true;
  66.                     i = MAX_VALUE;
  67.             }
  68.     } else if (i < 0) {
  69.         i = -i;
  70.         negative = true;
  71.     }
  72.     buf = new StringBuffer(45);
  73.     while (i > 0) {
  74.         if (i < radix) {
  75.         buf.append(Character.forDigit((int)i, radix));
  76.         i = 0;
  77.         } else {
  78.         int j = (int)(i % radix);
  79.         i /= radix;
  80.         buf.append(Character.forDigit(j,radix));
  81.         }
  82.         digitCount++;
  83.     }
  84.     if (digitCount > 0) {
  85.         int j = buf.length();
  86.         int k = 0;
  87.             int ii;
  88.         char tmp[];
  89.         if (negative) {
  90.         tmp = new char[j + 1];
  91.         tmp[0] = '-';
  92.         k = 1;
  93.         } else {
  94.         tmp = new char[j];
  95.         }
  96.         ii = 0;
  97.         while (j-- > 0) {
  98.         tmp[j+k] = buf.charAt(ii++);
  99.         }
  100.         if (minval) {
  101.         tmp[tmp.length-1]++;
  102.         }
  103.         return String.valueOf(tmp);
  104.     } else
  105.         return "0";
  106.     }
  107.  
  108.     /**
  109.      * Returns a new String object representing the specified integer. The radix
  110.      * is assumed to be 10.
  111.      * @param i    the long to be converted
  112.      */
  113.     public static String toString(long i) {
  114.     return toString(i, 10);
  115.     }
  116.  
  117.  
  118.     /**
  119.      * Assuming the specified String represents a long, returns that long's
  120.      * value. Throws an exception if the String cannot be parsed as a long.
  121.      * @param s        the String containing the integer
  122.      * @param radix     the radix to be used
  123.      * @exception    NumberFormatException If the String does not 
  124.      *                  contain a parsable integer.
  125.      */
  126.     public static long parseLong(String s, int radix) 
  127.               throws NumberFormatException 
  128.    {
  129.         if (s == null) {
  130.             throw new NumberFormatException("null");
  131.         }
  132.     long result = 0;
  133.     boolean negative = false;
  134.     int i=0, max = s.length();
  135.     if (max > 0) {
  136.         if (s.charAt(0) == '-') {
  137.         negative = true;
  138.         i++;
  139.         }
  140.         while (i < max) {
  141.         int digit = Character.digit(s.charAt(i++),radix);
  142.         if (digit < 0)
  143.             throw new NumberFormatException(s);
  144.         result = result * radix + digit;
  145.         }
  146.     } else
  147.         throw new NumberFormatException(s);
  148.     if (negative)
  149.         return -result;
  150.     else
  151.         return result;
  152.     }
  153.  
  154.  
  155.     /**
  156.      * Assuming the specified String represents a long, return that long's
  157.      * value. Throws an exception if the String cannot be parsed as a long.
  158.      * The radix is assumed to be 10.
  159.      * @param s        the String containing the long
  160.      * @exception    NumberFormatException If the string does not contain
  161.      *                   a parsable long.
  162.      */
  163.     public static long parseLong(String s) throws NumberFormatException {
  164.     return parseLong(s, 10);
  165.     }
  166.  
  167.     /**
  168.      * Assuming the specified String represents a long, returns a new Long
  169.      * object initialized to that value. Throws an exception if the String cannot be
  170.      * parsed as a long.
  171.      * @param s        the String containing the long.
  172.      * @param radix     the radix to be used
  173.      * @exception    NumberFormatException If the String does not contain a parsable 
  174.      *                                        long.
  175.      */
  176.     public static Long valueOf(String s, int radix) throws NumberFormatException {
  177.     return new Long(parseLong(s, radix));
  178.     }
  179.  
  180.     /**
  181.      * Assuming the specified String represents a long, returns a new Long
  182.      * object initialized to that value. Throws an exception if the String cannot be
  183.      * parsed as a long. The radix is assumed to be 10.
  184.      * @param s        the String containing the long
  185.      * @exception    NumberFormatException If the String does not contain a parsable 
  186.      *                                        long.
  187.      */
  188.     public static Long valueOf(String s) throws NumberFormatException 
  189.     {
  190.     return new Long(parseLong(s, 10));
  191.     }
  192.  
  193.  
  194.     /**
  195.      * The value of the Long.
  196.      */
  197.     private long value;
  198.  
  199.     /**
  200.      * Constructs a Long object initialized to the specified value.
  201.      * @param value    the initial value of the Long
  202.      */
  203.     public Long(long value) {
  204.     this.value = value;
  205.     }
  206.  
  207.     /**
  208.      * Constructs a Long object initialized to the value specified by the
  209.      * String parameter.  The radix is assumed to be 10.
  210.      * @param s        the String to be converted to a Long
  211.      * @exception    NumberFormatException If the String does not contain a parsable 
  212.      *                                        long.
  213.      */
  214.     public Long(String s) throws NumberFormatException {
  215.     this.value = parseLong(s, 10);
  216.     }
  217.  
  218.     /**
  219.      * Returns the value of this Long as an int.
  220.      */
  221.     public int intValue() {
  222.     return (int)value;
  223.     }
  224.  
  225.     /**
  226.      * Returns the value of this Long as a long.
  227.      */
  228.     public long longValue() {
  229.     return (long)value;
  230.     }
  231.  
  232.     /**
  233.      * Returns the value of this Long as a float.
  234.      */
  235.     public float floatValue() {
  236.     return (float)value;
  237.     }
  238.  
  239.     /**
  240.      * Returns the value of this Long as a double.
  241.      */
  242.     public double doubleValue() {
  243.     return (double)value;
  244.     }
  245.  
  246.     /**
  247.      * Returns a String object representing this Long's value.
  248.      */
  249.     public String toString() {
  250.     return String.valueOf(value);
  251.     }
  252.  
  253.     /**
  254.      * Computes a hashcode for this Long.
  255.      */
  256.     public int hashCode() {
  257.     return (int)value;
  258.     }
  259.  
  260.     /**
  261.      * Compares this object against the specified object.
  262.      * @param obj        the object to compare with
  263.      * @return         true if the objects are the same; false otherwise.
  264.      */
  265.     public boolean equals(Object obj) {
  266.     if ((obj != null) && (obj instanceof Long)) {
  267.         return value == ((Long)obj).longValue();
  268.     }
  269.     return false;
  270.     }
  271.  
  272.     /**
  273.      * Get a Long property. If the property does not
  274.      * exist, it will return 0.
  275.      * @param nm the property name
  276.      */
  277.     public static Long getLong(String nm) {
  278.     return getLong(nm, null);
  279.     }
  280.  
  281.     /**
  282.      * Get a Long property. If the property does not
  283.      * exist, it will return val. Deals with Hexadecimal and octal numbers.
  284.      * @param nm the String name
  285.      * @param val the Long value
  286.      */
  287.     public static Long getLong(String nm, long val) {
  288.         Long result = Long.getLong(nm, null);
  289.         return (result == null) ? new Long(val) : result;
  290.     }
  291.  
  292.     /**
  293.      * Get a Long property. If the property does not
  294.      * exist, it will return val. Deals with Hexadecimal and octal numbers.
  295.      * @param nm the property name
  296.      * @param val the Long value
  297.      */
  298.     public static Long getLong(String nm, Long val) {
  299.     String v = System.getProperty(nm);
  300.     if (v != null) {
  301.         try {
  302.         if (v.startsWith("0x")) {
  303.             return Long.valueOf(v.substring(2), 16);
  304.         }
  305.         if (v.startsWith("#")) {
  306.             return Long.valueOf(v.substring(1), 16);
  307.         }
  308.         if (v.startsWith("0")) {
  309.             return Long.valueOf(v.substring(1), 8);
  310.         }
  311.         return Long.valueOf(v);
  312.         } catch (NumberFormatException e) {
  313.         }
  314.     }    
  315.     return val;
  316.     }
  317. }
  318.  
  319.