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

  1. /*
  2.  * @(#)Integer.java    1.30 96/03/29  
  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 Integer class is a wrapper for integer values.  In Java, integers are not 
  24.  * objects and most of the Java utility classes require the use of objects.  Thus, 
  25.  * if you needed to store an integer in a hashtable, you would have to "wrap" an 
  26.  * Integer instance around it.
  27.  * @version     1.30, 29 Mar 1996
  28.  * @author    Lee Boynton
  29.  * @author    Arthur van Hoff
  30.  */
  31. public final
  32. class Integer extends Number {
  33.     /**
  34.      * The minimum value an Integer can have.  The lowest minimum value an
  35.      * Integer can have is 0x80000000.
  36.      */
  37.     public static final int   MIN_VALUE = 0x80000000;
  38.  
  39.     /**
  40.      * The maximum value an Integer can have.  The greatest maximum value an
  41.      * Integer can have is 0x7fffffff.
  42.      */
  43.     public static final int   MAX_VALUE = 0x7fffffff;
  44.  
  45.     /**
  46.      * Returns a new String object representing the specified integer in
  47.      * the specified radix.
  48.      * @param i        the integer to be converted
  49.      * @param radix    the radix
  50.      * @see Character#MIN_RADIX
  51.      * @see Character#MAX_RADIX
  52.      */
  53.     public static String toString(int i, int radix) {
  54.     if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  55.         radix = 10;
  56.     StringBuffer buf = new StringBuffer(radix >= 8 ? 12 : 33);
  57.     boolean negative = (i < 0);
  58.         if (!negative)
  59.         i = -i;
  60.     while (i <= -radix) {
  61.         buf.append(Character.forDigit(-(i % radix), radix));
  62.         i = i / radix;
  63.     }
  64.     buf.append(Character.forDigit(-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 integer 
  72.      * as unsigned hexidecimal number. 
  73.      */
  74.     public static String toHexString(int i) {
  75.     return toUnsignedString(i, 4);
  76.     }
  77.  
  78.     /**
  79.      * Returns a new String object representing the specified integer 
  80.      * as unsigned octal number. 
  81.      */
  82.     public static String toOctalString(int i) {
  83.     return toUnsignedString(i, 3);
  84.     }
  85.  
  86.     /**
  87.      * Returns a new String object representing the specified integer 
  88.      * as unsigned binary number. 
  89.      */
  90.     public static String toBinaryString(int i) {
  91.     return toUnsignedString(i, 1);
  92.     }
  93.  
  94.     /** 
  95.      * Convert the integer to an unsigned number.
  96.      */
  97.     private static String toUnsignedString(int i, int shift) {
  98.     StringBuffer buf = new StringBuffer(shift >= 3 ? 11 : 32);
  99.     int radix = 1 << shift;
  100.     int mask = radix - 1;
  101.     do {
  102.         buf.append(Character.forDigit(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 integer to be converted
  113.      */
  114.     public static String toString(int i) {
  115.     return toString(i,10);
  116.     }
  117.     
  118.     /**
  119.      * Assuming the specified String represents an integer, returns that integer's
  120.      * value. Throws an exception if the String cannot be parsed as an int.
  121.      * @param s        the String containing the integer
  122.      * @param radix     the radix to be used
  123.      * @exception    NumberFormatException If the String does not contain a parsable 
  124.      *                                        integer.
  125.      */
  126.     public static int parseInt(String s, int radix) throws NumberFormatException {
  127.         if (s == null) {
  128.             throw new NumberFormatException("null");
  129.         }
  130.     int result = 0;
  131.     boolean negative = false;
  132.     int i=0, max = s.length();
  133.     if (max > 0) {
  134.         if (s.charAt(0) == '-') {
  135.         negative = true;
  136.         i++;
  137.         }
  138.         while (i < max) {
  139.         int digit = Character.digit(s.charAt(i++),radix);
  140.         if (digit < 0)
  141.             throw new NumberFormatException(s);
  142.         result = result * radix + digit;
  143.         }
  144.     } else
  145.         throw new NumberFormatException(s);
  146.     if (negative)
  147.         return -result;
  148.     else
  149.         return result;
  150.     }
  151.  
  152.     /**
  153.      * Assuming the specified String represents an integer, returns that integer's
  154.      * value. Throws an exception if the String cannot be parsed as an int.
  155.      * The radix is assumed to be 10.
  156.      * @param s        the String containing the integer
  157.      * @exception    NumberFormatException If the string does not contain a parsable 
  158.      *                                        integer.
  159.      */
  160.     public static int parseInt(String s) throws NumberFormatException {
  161.     return parseInt(s,10);
  162.     }
  163.  
  164.     /**
  165.      * Assuming the specified String represents an integer, returns a new Integer
  166.      * object initialized to that value. Throws an exception if the String cannot be
  167.      * parsed as an int.
  168.      * @param s        the String containing the integer
  169.      * @param radix     the radix to be used
  170.      * @exception    NumberFormatException If the String does not contain a parsable 
  171.      *                                        integer.
  172.      */
  173.     public static Integer valueOf(String s, int radix) throws NumberFormatException {
  174.     return new Integer(parseInt(s,radix));
  175.     }
  176.  
  177.     /**
  178.      * Assuming the specified String represents an integer, returns a new Integer
  179.      * object initialized to that value. Throws an exception if the String cannot be
  180.      * parsed as an int. The radix is assumed to be 10.
  181.      * @param s        the String containing the integer
  182.      * @exception    NumberFormatException If the String does not contain a parsable 
  183.      *                                        integer.
  184.      */
  185.     public static Integer valueOf(String s) throws NumberFormatException
  186.     {
  187.     return new Integer(parseInt(s, 10));
  188.     }
  189.  
  190.     /**
  191.      * The value of the Integer.
  192.      */
  193.     private int value;
  194.  
  195.     /**
  196.      * Constructs an Integer object initialized to the specified int value.
  197.      * @param value    the initial value of the Integer
  198.      */
  199.     public Integer(int value) {
  200.     this.value = value;
  201.     }
  202.  
  203.     /**
  204.      * Constructs an Integer object initialized to the value specified by the
  205.      * String parameter.  The radix is assumed to be 10.
  206.      * @param s        the String to be converted to an Integer
  207.      * @exception    NumberFormatException If the String does not contain a parsable 
  208.      *                                        integer.
  209.      */
  210.     public Integer(String s) throws NumberFormatException {
  211.     this.value = parseInt(s, 10);
  212.     }
  213.  
  214.     /**
  215.      * Returns the value of this Integer as an int.
  216.      */
  217.     public int intValue() {
  218.     return value;
  219.     }
  220.  
  221.     /**
  222.      * Returns the value of this Integer as a long.
  223.      */
  224.     public long longValue() {
  225.     return (long)value;
  226.     }
  227.  
  228.     /**
  229.      * Returns the value of this Integer as a float.
  230.      */
  231.     public float floatValue() {
  232.     return (float)value;
  233.     }
  234.  
  235.     /**
  236.      * Returns the value of this Integer as a double.
  237.      */
  238.     public double doubleValue() {
  239.     return (double)value;
  240.     }
  241.  
  242.     /**
  243.      * Returns a String object representing this Integer's value.
  244.      */
  245.     public String toString() {
  246.     return String.valueOf(value);
  247.     }
  248.  
  249.     /**
  250.      * Returns a hashcode for this Integer.
  251.      */
  252.     public int hashCode() {
  253.     return value;
  254.     }
  255.  
  256.     /**
  257.      * Compares this object to the specified object.
  258.      * @param obj    the object to compare with
  259.      * @return         true if the objects are the same; false otherwise.
  260.      */
  261.     public boolean equals(Object obj) {
  262.     if ((obj != null) && (obj instanceof Integer)) {
  263.         return value == ((Integer)obj).intValue();
  264.     }
  265.     return false;
  266.     }
  267.  
  268.     /**
  269.      * Gets an Integer property. If the property does not
  270.      * exist, it will return 0.
  271.      * @param nm the property name
  272.      */
  273.     public static Integer getInteger(String nm) {
  274.     return getInteger(nm, null);
  275.     }
  276.  
  277.     /**
  278.      * Gets an Integer property. If the property does not
  279.      * exist, it will return val. Deals with hexadecimal
  280.      * and octal numbers.
  281.      * @param nm the String name
  282.      * @param val the Integer value
  283.      */
  284.     public static Integer getInteger(String nm, int val) {
  285.         Integer result = getInteger(nm, null);
  286.         return (result == null) ? new Integer(val) : result;
  287.     }
  288.  
  289.     /**
  290.      * Gets an Integer property. If the property does not
  291.      * exist, it will return val. Deals with hexadecimal
  292.      * and octal numbers.
  293.      * @param nm the property name
  294.      * @param val the integer value
  295.      */
  296.     public static Integer getInteger(String nm, Integer val) {
  297.     String v = System.getProperty(nm);
  298.     if (v != null) {
  299.         try {
  300.         return Integer.decode(v);
  301.         } catch (NumberFormatException e) {
  302.         }
  303.     }    
  304.     return val;
  305.     }
  306.  
  307.     /**
  308.      * Decodes a string into an Integer. Deals with decimal, hexadecimal,
  309.      * and octal numbers.
  310.      * @param nm the string to decode
  311.      */
  312.     private static Integer decode(String nm) throws NumberFormatException {
  313.     if (nm.startsWith("0x")) {
  314.         return Integer.valueOf(nm.substring(2), 16);
  315.     }
  316.     if (nm.startsWith("#")) {
  317.         return Integer.valueOf(nm.substring(1), 16);
  318.     }
  319.     if (nm.startsWith("0") && nm.length() > 1) {
  320.         return Integer.valueOf(nm.substring(1), 8);
  321.     }
  322.     
  323.     return Integer.valueOf(nm);
  324.     }
  325. }
  326.  
  327.  
  328.