home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / sybase / starbuck / java.z / Long.java < prev    next >
Text File  |  1996-05-03  |  9KB  |  320 lines

  1. /*
  2.  * @(#)Long.java    1.23 96/01/02  
  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.23, 02 Jan 1996
  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.         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  55.         radix = 10;
  56.     StringBuffer buf = new StringBuffer(radix >= 8 ? 23 : 65);
  57.     boolean negative = (i < 0);
  58.         if (!negative)
  59.         i = -i;
  60.     while (i <= -radix) {
  61.         buf.append(Character.forDigit((int)(-(i % radix)), radix));
  62.         i = i / radix;
  63.     }
  64.     buf.append(Character.forDigit((int)(-i), radix));
  65.     if (negative)
  66.         buf.append('-');
  67.         return buf.reverse().toString();
  68.     }
  69.  
  70.     /**
  71.      * Returns a new String object representing the specified long 
  72.      * as unsigned hexidecimal number. 
  73.      */
  74.     public static String toHexString(long i) {
  75.     return toUnsignedString(i, 4);
  76.     }
  77.  
  78.     /**
  79.      * Returns a new String object representing the specified long 
  80.      * as unsigned octal number. 
  81.      */
  82.     public static String toOctalString(long i) {
  83.     return toUnsignedString(i, 3);
  84.     }
  85.  
  86.     /**
  87.      * Returns a new String object representing the specified long 
  88.      * as unsigned octal number. 
  89.      */
  90.     public static String toBinaryString(long i) {
  91.     return toUnsignedString(i, 1);
  92.     }
  93.  
  94.     /** 
  95.      * Convert the integer to an unsigned number.
  96.      */
  97.     private static String toUnsignedString(long i, int shift) {
  98.     StringBuffer buf = new StringBuffer(shift >= 3 ? 22 : 64);
  99.     int radix = 1 << shift;
  100.     long mask = radix - 1;
  101.     do {
  102.         buf.append(Character.forDigit((int)(i & mask), radix));
  103.         i >>>= shift;
  104.     } while (i != 0);
  105.         return buf.reverse().toString();
  106.     }
  107.  
  108.  
  109.     /**
  110.      * Returns a new String object representing the specified integer. The radix
  111.      * is assumed to be 10.
  112.      * @param i    the long to be converted
  113.      */
  114.     public static String toString(long i) {
  115.     return toString(i, 10);
  116.     }
  117.  
  118.  
  119.     /**
  120.      * Assuming the specified String represents a long, returns that long's
  121.      * value. Throws an exception if the String cannot be parsed as a long.
  122.      * @param s        the String containing the integer
  123.      * @param radix     the radix to be used
  124.      * @exception    NumberFormatException If the String does not 
  125.      *                  contain a parsable integer.
  126.      */
  127.     public static long parseLong(String s, int radix) 
  128.               throws NumberFormatException 
  129.    {
  130.         if (s == null) {
  131.             throw new NumberFormatException("null");
  132.         }
  133.     long result = 0;
  134.     boolean negative = false;
  135.     int i=0, max = s.length();
  136.     if (max > 0) {
  137.         if (s.charAt(0) == '-') {
  138.         negative = true;
  139.         i++;
  140.         }
  141.         while (i < max) {
  142.         int digit = Character.digit(s.charAt(i++),radix);
  143.         if (digit < 0)
  144.             throw new NumberFormatException(s);
  145.         result = result * radix + digit;
  146.         }
  147.     } else
  148.         throw new NumberFormatException(s);
  149.     if (negative)
  150.         return -result;
  151.     else
  152.         return result;
  153.     }
  154.  
  155.  
  156.     /**
  157.      * Assuming the specified String represents a long, return that long's
  158.      * value. Throws an exception if the String cannot be parsed as a long.
  159.      * The radix is assumed to be 10.
  160.      * @param s        the String containing the long
  161.      * @exception    NumberFormatException If the string does not contain
  162.      *                   a parsable long.
  163.      */
  164.     public static long parseLong(String s) throws NumberFormatException {
  165.     return parseLong(s, 10);
  166.     }
  167.  
  168.     /**
  169.      * Assuming the specified String represents a long, returns a new Long
  170.      * object initialized to that value. Throws an exception if the String cannot be
  171.      * parsed as a long.
  172.      * @param s        the String containing the long.
  173.      * @param radix     the radix to be used
  174.      * @exception    NumberFormatException If the String does not contain a parsable 
  175.      *                                        long.
  176.      */
  177.     public static Long valueOf(String s, int radix) throws NumberFormatException {
  178.     return new Long(parseLong(s, radix));
  179.     }
  180.  
  181.     /**
  182.      * Assuming the specified String represents a long, returns a new Long
  183.      * object initialized to that value. Throws an exception if the String cannot be
  184.      * parsed as a long. The radix is assumed to be 10.
  185.      * @param s        the String containing the long
  186.      * @exception    NumberFormatException If the String does not contain a parsable 
  187.      *                                        long.
  188.      */
  189.     public static Long valueOf(String s) throws NumberFormatException 
  190.     {
  191.     return new Long(parseLong(s, 10));
  192.     }
  193.  
  194.  
  195.     /**
  196.      * The value of the Long.
  197.      */
  198.     private long value;
  199.  
  200.     /**
  201.      * Constructs a Long object initialized to the specified value.
  202.      * @param value    the initial value of the Long
  203.      */
  204.     public Long(long value) {
  205.     this.value = value;
  206.     }
  207.  
  208.     /**
  209.      * Constructs a Long object initialized to the value specified by the
  210.      * String parameter.  The radix is assumed to be 10.
  211.      * @param s        the String to be converted to a Long
  212.      * @exception    NumberFormatException If the String does not contain a parsable 
  213.      *                                        long.
  214.      */
  215.     public Long(String s) throws NumberFormatException {
  216.     this.value = parseLong(s, 10);
  217.     }
  218.  
  219.     /**
  220.      * Returns the value of this Long as an int.
  221.      */
  222.     public int intValue() {
  223.     return (int)value;
  224.     }
  225.  
  226.     /**
  227.      * Returns the value of this Long as a long.
  228.      */
  229.     public long longValue() {
  230.     return (long)value;
  231.     }
  232.  
  233.     /**
  234.      * Returns the value of this Long as a float.
  235.      */
  236.     public float floatValue() {
  237.     return (float)value;
  238.     }
  239.  
  240.     /**
  241.      * Returns the value of this Long as a double.
  242.      */
  243.     public double doubleValue() {
  244.     return (double)value;
  245.     }
  246.  
  247.     /**
  248.      * Returns a String object representing this Long's value.
  249.      */
  250.     public String toString() {
  251.     return String.valueOf(value);
  252.     }
  253.  
  254.     /**
  255.      * Computes a hashcode for this Long.
  256.      */
  257.     public int hashCode() {
  258.     return (int)(value ^ (value >> 32));
  259.     }
  260.  
  261.     /**
  262.      * Compares this object against the specified object.
  263.      * @param obj        the object to compare with
  264.      * @return         true if the objects are the same; false otherwise.
  265.      */
  266.     public boolean equals(Object obj) {
  267.     if ((obj != null) && (obj instanceof Long)) {
  268.         return value == ((Long)obj).longValue();
  269.     }
  270.     return false;
  271.     }
  272.  
  273.     /**
  274.      * Gets a Long property. If the property does not
  275.      * exist, it will return 0.
  276.      * @param nm the property name
  277.      */
  278.     public static Long getLong(String nm) {
  279.     return getLong(nm, null);
  280.     }
  281.  
  282.     /**
  283.      * Gets a Long property. If the property does not
  284.      * exist, it will return val. Deals with Hexadecimal and octal numbers.
  285.      * @param nm the String name
  286.      * @param val the Long value
  287.      */
  288.     public static Long getLong(String nm, long val) {
  289.         Long result = Long.getLong(nm, null);
  290.         return (result == null) ? new Long(val) : result;
  291.     }
  292.  
  293.     /**
  294.      * Gets a Long property. If the property does not
  295.      * exist, it will return val. Deals with Hexadecimal and octal numbers.
  296.      * @param nm the property name
  297.      * @param val the Long value
  298.      */
  299.     public static Long getLong(String nm, Long val) {
  300.     String v = System.getProperty(nm);
  301.     if (v != null) {
  302.         try {
  303.         if (v.startsWith("0x")) {
  304.             return Long.valueOf(v.substring(2), 16);
  305.         }
  306.         if (v.startsWith("#")) {
  307.             return Long.valueOf(v.substring(1), 16);
  308.         }
  309.         if (v.startsWith("0") && v.length() > 1) {
  310.             return Long.valueOf(v.substring(1), 8);
  311.         }
  312.         return Long.valueOf(v);
  313.         } catch (NumberFormatException e) {
  314.         }
  315.     }    
  316.     return val;
  317.     }
  318. }
  319.  
  320.