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

  1. /*
  2.  * @(#)Integer.java    1.49 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 Integer class wraps a value of the primitive type <code>int</code> 
  19.  * in an object. An object of type <code>Integer</code> contains a 
  20.  * single field whose type is <code>int</code>. 
  21.  * <p>
  22.  * In addition, this class provides several methods for converting 
  23.  * an <code>int</code> to a <code>String</code> and a 
  24.  * <code>String</code> to an <code>int</code>, as well as other 
  25.  * constants and methods useful when dealing with an 
  26.  * <code>int</code>. 
  27.  *
  28.  * @author  Lee Boynton
  29.  * @author  Arthur van Hoff
  30.  * @version 1.49, 03/18/98
  31.  * @since   JDK1.0
  32.  */
  33. public final class Integer extends Number implements Comparable {
  34.     /**
  35.      * The smallest value of type <code>int</code>. 
  36.      */
  37.     public static final int   MIN_VALUE = 0x80000000;
  38.  
  39.     /**
  40.      * The largest value of type <code>int</code>. 
  41.      */
  42.     public static final int   MAX_VALUE = 0x7fffffff;
  43.  
  44.     /**
  45.      * The Class object representing the primitive type int.
  46.      *
  47.      * @since   JDK1.1
  48.      */
  49.     public static final Class    TYPE = Class.getPrimitiveClass("int");
  50.  
  51.     /**
  52.      * All possible chars for representing a number as a String 
  53.      */
  54.     final static char[] digits = { 
  55.     '0' , '1' , '2' , '3' , '4' , '5' , 
  56.     '6' , '7' , '8' , '9' , 'a' , 'b' , 
  57.     'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 
  58.     'i' , 'j' , 'k' , 'l' , 'm' , 'n' , 
  59.     'o' , 'p' , 'q' , 'r' , 's' , 't' , 
  60.     'u' , 'v' , 'w' , 'x' , 'y' , 'z' 
  61.     };
  62.  
  63.     /**
  64.      * Array of chars to lookup the char for the digit in the tenth's 
  65.      * place for a two digit, base ten number.  The char can be got by 
  66.      * using the number as the index.
  67.      */
  68.     private final static char[] radixTenTenths = {
  69.     '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', 
  70.     '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', 
  71.     '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', 
  72.     '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', 
  73.     '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', 
  74.     '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', 
  75.     '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', 
  76.     '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', 
  77.     '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', 
  78.     '9', '9', '9', '9', '9', '9', '9', '9', '9', '9'
  79.     };
  80.     /**
  81.      * Array of chars to lookup the char for the digit in the unit's 
  82.      * place for a two digit, base ten number.  The char can be got by 
  83.      * using the number as the index.
  84.      */
  85.     private final static char[] radixTenUnits = {
  86.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  87.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  88.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  89.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  90.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  91.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  92.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  93.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  94.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 
  95.     '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
  96.     };
  97.  
  98.     /**
  99.      * Creates a string representation of the first argument in the 
  100.      * radix specified by the second argument. 
  101.      * <p>
  102.      * If the radix is smaller than <code>Character.MIN_RADIX</code> or 
  103.      * larger than <code>Character.MAX_RADIX</code>, then the radix 
  104.      * <code>10</code> is used instead. 
  105.      * <p>
  106.      * If the first argument is negative, the first element of the 
  107.      * result is the ASCII minus character <code>'-'</code>. If the first 
  108.      * argument is not negative, no sign character appears in the result. 
  109.      * The following ASCII characters are used as digits: 
  110.      * <blockquote><pre>
  111.      *   0123456789abcdefghijklmnopqrstuvwxyz
  112.      * </pre></blockquote>
  113.      *
  114.      * @param   i       an integer.
  115.      * @param   radix   the radix.
  116.      * @return  a string representation of the argument in the specified radix.
  117.      * @see     java.lang.Character#MAX_RADIX
  118.      * @see     java.lang.Character#MIN_RADIX
  119.      */
  120.     public static String toString(int i, int radix) {
  121.  
  122.         if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  123.         radix = 10;
  124.     
  125.     /* Use the faster version */
  126.     if (radix == 10) {
  127.         return toString(i);
  128.     }
  129.     
  130.     char buf[] = new char[33];
  131.     boolean negative = (i < 0);
  132.     int charPos = 32;
  133.         
  134.     if (!negative) {
  135.         i = -i;
  136.     }
  137.             
  138.     while (i <= -radix) {
  139.         buf[charPos--] = digits[-(i % radix)];
  140.         i = i / radix;
  141.     }
  142.     buf[charPos] = digits[-i];
  143.  
  144.     if (negative) {
  145.         buf[--charPos] = '-';
  146.     }
  147.  
  148.     return new String(buf, charPos, (33 - charPos));
  149.     }
  150.     
  151.     
  152.     /**
  153.      * Creates a string representation of the integer argument as an 
  154.      * unsigned integer in base 16. 
  155.      * <p>
  156.      * The unsigned integer value is the argument plus 2<sup>32</sup> if 
  157.      * the argument is negative; otherwise, it is equal to the argument. 
  158.      * This value is converted to a string of ASCII digits in hexadecimal 
  159.      * (base 16) with no extra leading <code>0</code>s. 
  160.      *
  161.      * @param   i   an integer.
  162.      * @return  the string representation of the unsigned integer value
  163.      *          represented by the argument in hexadecimal (base 16).
  164.      * @since   JDK1.0.2
  165.      */
  166.     public static String toHexString(int i) {
  167.     return toUnsignedString(i, 4);
  168.     }
  169.  
  170.     /**
  171.      * Creates a string representation of the integer argument as an 
  172.      * unsigned integer in base 8. 
  173.      * <p>
  174.      * The unsigned integer value is the argument plus 2<sup>32</sup> if 
  175.      * the argument is negative; otherwise, it is equal to the argument. 
  176.      * This value is converted to a string of ASCII digits in octal 
  177.      * (base 8) with no extra leading <code>0</code>s. 
  178.      *
  179.      * @param   i   an integer
  180.      * @return  the string representation of the unsigned integer value
  181.      *          represented by the argument in octal (base 8).
  182.      * @since   JDK1.0.2
  183.      */
  184.     public static String toOctalString(int i) {
  185.     return toUnsignedString(i, 3);
  186.     }
  187.  
  188.     /**
  189.      * Creates a string representation of the integer argument as an 
  190.      * unsigned integer in base 2. 
  191.      * <p>
  192.      * The unsigned integer value is the argument plus 2<sup>32</sup>if 
  193.      * the argument is negative; otherwise it is equal to the argument. 
  194.      * This value is converted to a string of ASCII digits in binary 
  195.      * (base 2) with no extra leading <code>0</code>s. 
  196.      *
  197.      * @param   i   an integer.
  198.      * @return  the string representation of the unsigned integer value
  199.      *          represented by the argument in binary (base 2).
  200.      * @since   JDK1.0.2
  201.      */
  202.     public static String toBinaryString(int i) {
  203.     return toUnsignedString(i, 1);
  204.     }
  205.  
  206.     /** 
  207.      * Convert the integer to an unsigned number.
  208.      */
  209.     private static String toUnsignedString(int i, int shift) {
  210.     char[] buf = new char[32];
  211.     int charPos = 32;
  212.     int radix = 1 << shift;
  213.     int mask = radix - 1;
  214.     do {
  215.         buf[--charPos] = digits[i & mask];
  216.         i >>>= shift;
  217.     } while (i != 0);
  218.     
  219.     return new String(buf, charPos, (32 - charPos));
  220.     }
  221.  
  222.     /**
  223.      * Returns a new String object representing the specified integer. The radix
  224.      * is assumed to be 10.
  225.      *
  226.      * @param   i   an integer to be converted.
  227.      * @return  a string representation of the argument in base 10.
  228.      */
  229.     public static String toString(int i) {
  230.     /** 
  231.      * Performance improvements -
  232.      *
  233.      * 1) Avoid a method call and radix checks by inlining the code for 
  234.      *    radix = 10 in this method.
  235.      * 2) Use char arrays instead of StringBuffer and avoid calls to
  236.      *    Character.forDigit.
  237.      * 3) Do computations in positive space to avoid negation each time
  238.      *    around the loop.
  239.      * 4) Unroll loop by half and use a static array of chars to look-
  240.      *    up chars for a digit.
  241.      * The other option for 4) was to use a switch statement and assign 
  242.      * the char for the current digit.  That was a little slower than 4)
  243.      * with most jits. 
  244.      * Speed-up = (approximately) 4x on both Solaris and Win32.
  245.      */
  246.     char[] buf = new char[12];
  247.     boolean negative = (i < 0);
  248.     int charPos = 12;
  249.     
  250.     if (i == Integer.MIN_VALUE) {
  251.         return "-2147483648";
  252.     }
  253.         
  254.     if (negative) {
  255.         i = -i;
  256.     }
  257.     
  258.     do {
  259.         int digit = i%100;
  260.         buf[--charPos] = radixTenUnits[digit];
  261.         buf[--charPos] = radixTenTenths[digit];
  262.         i = i / 100;
  263.     } while(i != 0);
  264.     
  265.     if (buf[charPos] == '0') {
  266.         charPos++;
  267.     }
  268.     if (negative) {
  269.         buf[--charPos] = '-';
  270.     }
  271.         
  272.     return new String(buf , charPos , (12 - charPos));
  273.     }
  274.     
  275.     /**
  276.      * Parses the string argument as a signed integer in the radix 
  277.      * specified by the second argument. The characters in the string 
  278.      * must all be digits of the specified radix (as determined by 
  279.      * whether <code>Character.digit</code> returns a 
  280.      * nonnegative value), except that the first character may be an 
  281.      * ASCII minus sign <code>'-'</code> to indicate a negative value. 
  282.      * The resulting integer value is returned. 
  283.      *
  284.      * @param      s   the <code>String</code> containing the integer.
  285.      * @param      radix   the radix to be used.
  286.      * @return     the integer represented by the string argument in the
  287.      *             specified radix.
  288.      * @exception  NumberFormatException  if the string does not contain a
  289.      *               parsable integer.
  290.  
  291.      */
  292.     public static int parseInt(String s, int radix) 
  293.         throws NumberFormatException 
  294.     {
  295.         if (s == null) {
  296.             throw new NumberFormatException("null");
  297.         }
  298.  
  299.     if (radix < Character.MIN_RADIX) {
  300.         throw new NumberFormatException("radix " + radix + 
  301.                         " less than Character.MIN_RADIX");
  302.     }
  303.     
  304.     if (radix > Character.MAX_RADIX) {
  305.         throw new NumberFormatException("radix " + radix + 
  306.                         " greater than Character.MAX_RADIX");
  307.     }
  308.         
  309.     int result = 0;
  310.     boolean negative = false;
  311.     int i = 0, max = s.length();
  312.     int limit;
  313.     int multmin;
  314.     int digit;
  315.  
  316.     if (max > 0) {
  317.         if (s.charAt(0) == '-') {
  318.         negative = true;
  319.         limit = Integer.MIN_VALUE;
  320.         i++;
  321.         } else {
  322.         limit = -Integer.MAX_VALUE;
  323.         }
  324.         multmin = limit / radix;
  325.         if (i < max) {
  326.         digit = Character.digit(s.charAt(i++),radix);
  327.         if (digit < 0) {
  328.             throw new NumberFormatException(s);
  329.         } else {
  330.             result = -digit;
  331.         }
  332.         }
  333.         while (i < max) {
  334.         // Accumulating negatively avoids surprises near MAX_VALUE
  335.         digit = Character.digit(s.charAt(i++),radix);
  336.         if (digit < 0) {
  337.             throw new NumberFormatException(s);
  338.         }
  339.         if (result < multmin) {
  340.             throw new NumberFormatException(s);
  341.         }
  342.         result *= radix;
  343.         if (result < limit + digit) {
  344.             throw new NumberFormatException(s);
  345.         }
  346.         result -= digit;
  347.         }
  348.     } else {
  349.         throw new NumberFormatException(s);
  350.     }
  351.     if (negative) {
  352.         if (i > 1) {
  353.         return result;
  354.         } else {    /* Only got "-" */
  355.         throw new NumberFormatException(s);
  356.         }
  357.     } else {
  358.         return -result;
  359.     }
  360.     }
  361.  
  362.     /**
  363.      * Parses the string argument as a signed decimal integer. The 
  364.      * characters in the string must all be decimal digits, except that 
  365.      * the first character may be an ASCII minus sign <code>'-'</code> to 
  366.      * indicate a negative value. 
  367.      *
  368.      * @param      s   a string.
  369.      * @return     the integer represented by the argument in decimal.
  370.      * @exception  NumberFormatException  if the string does not contain a
  371.      *               parsable integer.
  372.      */
  373.     public static int parseInt(String s) throws NumberFormatException {
  374.     return parseInt(s,10);
  375.     }
  376.  
  377.     /**
  378.      * Returns a new Integer object initialized to the value of the
  379.      * specified String.  Throws an exception if the String cannot be
  380.      * parsed as an int.
  381.      *
  382.      * @param      s   the string to be parsed.
  383.      * @return     a newly constructed <code>Integer</code> initialized to the
  384.      *             value represented by the string argument in the specified
  385.      *             radix.
  386.      * @exception  NumberFormatException  if the <code>String</code> does not
  387.      *               contain a parsable integer.
  388.      */
  389.     public static Integer valueOf(String s, int radix) throws NumberFormatException {
  390.     return new Integer(parseInt(s,radix));
  391.     }
  392.  
  393.     /**
  394.      * Returns a new Integer object initialized to the value of the
  395.      * specified String.  Throws an exception if the String cannot be
  396.      * parsed as an int. The radix is assumed to be 10.
  397.      *
  398.      * @param      s   the string to be parsed.
  399.      * @return     a newly constructed <code>Integer</code> initialized to the
  400.      *             value represented by the string argument.
  401.      * @exception  NumberFormatException  if the string does not contain a
  402.      *               parsable integer.
  403.      */
  404.     public static Integer valueOf(String s) throws NumberFormatException
  405.     {
  406.     return new Integer(parseInt(s, 10));
  407.     }
  408.  
  409.     /**
  410.      * The value of the Integer.
  411.      */
  412.     private int value;
  413.  
  414.     /**
  415.      * Constructs a newly allocated <code>Integer</code> object that 
  416.      * represents the primitive <code>int</code> argument. 
  417.      *
  418.      * @param   value   the value to be represented by the <code>Integer</code>.
  419.      */
  420.     public Integer(int value) {
  421.     this.value = value;
  422.     }
  423.  
  424.     /**
  425.      * Constructs a newly allocated <code>Integer</code> object that 
  426.      * represents the value represented by the string. The string is 
  427.      * converted to an int value as if by the <code>valueOf</code> method. 
  428.      *
  429.      * @param      s   the <code>String</code> to be converted to an
  430.      *                 <code>Integer</code>.
  431.      * @exception  NumberFormatException  if the <code>String</code> does not
  432.      *               contain a parsable integer.
  433.      * @see        java.lang.Integer#valueOf(java.lang.String, int)
  434.      */
  435.     public Integer(String s) throws NumberFormatException {
  436.     this.value = parseInt(s, 10);
  437.     }
  438.  
  439.     /**
  440.      * Returns the value of this Integer as a byte.
  441.      *
  442.      * @since   JDK1.1
  443.      */
  444.     public byte byteValue() {
  445.     return (byte)value;
  446.     }
  447.  
  448.     /**
  449.      * Returns the value of this Integer as a short.
  450.      *
  451.      * @since   JDK1.1
  452.      */
  453.     public short shortValue() {
  454.     return (short)value;
  455.     }
  456.  
  457.     /**
  458.      * Returns the value of this Integer as an int.
  459.      *
  460.      * @return  the <code>int</code> value represented by this object.
  461.      */
  462.     public int intValue() {
  463.     return value;
  464.     }
  465.  
  466.     /**
  467.      * Returns the value of this Integer as a long.
  468.      *
  469.      * @return  the <code>int</code> value represented by this object that is
  470.      *          converted to type <code>long</code> and the result of the
  471.      *          conversion is returned.
  472.      */
  473.     public long longValue() {
  474.     return (long)value;
  475.     }
  476.  
  477.     /**
  478.      * Returns the value of this Integer as a float.
  479.      *
  480.      * @return  the <code>int</code> value represented by this object is
  481.      *          converted to type <code>float</code> and the result of the
  482.      *          conversion is returned.
  483.      */
  484.     public float floatValue() {
  485.     return (float)value;
  486.     }
  487.  
  488.     /**
  489.      * Returns the value of this Integer as a double.
  490.      *
  491.      * @return  the <code>int</code> value represented by this object is
  492.      *          converted to type <code>double</code> and the result of the
  493.      *          conversion is returned.
  494.      */
  495.     public double doubleValue() {
  496.     return (double)value;
  497.     }
  498.  
  499.     /**
  500.      * Returns a String object representing this Integer's value.
  501.      *
  502.      * @return  a string representation of the value of this object in
  503.      *          base 10.
  504.      */
  505.     public String toString() {
  506.     return String.valueOf(value);
  507.     }
  508.  
  509.     /**
  510.      * Returns a hashcode for this Integer.
  511.      *
  512.      * @return  a hash code value for this object. 
  513.      */
  514.     public int hashCode() {
  515.     return value;
  516.     }
  517.  
  518.     /**
  519.      * Compares this object to the specified object.
  520.      * The result is <code>true</code> if and only if the argument is not 
  521.      * <code>null</code> and is an <code>Integer</code> object that contains 
  522.      * the same <code>int</code> value as this object. 
  523.      *
  524.      * @param   obj   the object to compare with.
  525.      * @return  <code>true</code> if the objects are the same;
  526.      *          <code>false</code> otherwise.
  527.      */
  528.     public boolean equals(Object obj) {
  529.     if ((obj != null) && (obj instanceof Integer)) {
  530.         return value == ((Integer)obj).intValue();
  531.     }
  532.     return false;
  533.     }
  534.  
  535.     /**
  536.      * Determines the integer value of the system property with the 
  537.      * specified name. 
  538.      * <p>
  539.      * The first argument is treated as the name of a system property. 
  540.      * System properties are accessible through <code>getProperty</code>  
  541.      * and , a method defined by the <code>System</code> class. The 
  542.      * string value of this property is then interpreted as an integer 
  543.      * value and an <code>Integer</code> object representing this value is 
  544.      * returned. Details of possible numeric formats can be found with 
  545.      * the definition of <code>getProperty</code>. 
  546.      * <p>
  547.      * If there is no property with the specified name, or if the 
  548.      * property does not have the correct numeric format, then 
  549.      * <code>null</code> is returned. 
  550.      *
  551.      * @param   nm   property name.
  552.      * @return  the <code>Integer</code> value of the property.
  553.      * @see     java.lang.System#getProperty(java.lang.String)
  554.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  555.      */
  556.     public static Integer getInteger(String nm) {
  557.     return getInteger(nm, null);
  558.     }
  559.  
  560.     /**
  561.      * Determines the integer value of the system property with the 
  562.      * specified name. 
  563.      * <p>
  564.      * The first argument is treated as the name of a system property. 
  565.      * System properties are accessible through <code>getProperty</code>  
  566.      * and , a method defined by the <code>System</code> class. The 
  567.      * string value of this property is then interpreted as an integer 
  568.      * value and an <code>Integer</code> object representing this value is 
  569.      * returned. Details of possible numeric formats can be found with 
  570.      * the definition of <code>getProperty</code>. 
  571.      * <p>
  572.      * If there is no property with the specified name, or if the 
  573.      * property does not have the correct numeric format, then an 
  574.      * <code>Integer</code> object that represents the value of the 
  575.      * second argument is returned. 
  576.      *
  577.      * @param   nm   property name.
  578.      * @param   val   default value.
  579.      * @return  the <code>Integer</code> value of the property.
  580.      * @see     java.lang.System#getProperty(java.lang.String)
  581.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  582.      */
  583.     public static Integer getInteger(String nm, int val) {
  584.         Integer result = getInteger(nm, null);
  585.         return (result == null) ? new Integer(val) : result;
  586.     }
  587.  
  588.     /**
  589.      * Determines the integer value of the system property with the 
  590.      * specified name. 
  591.      * <p>
  592.      * The first argument is treated as the name of a system property. 
  593.      * System properties are accessible through <code>getProperty</code>  
  594.      * and , a method defined by the <code>System</code> class. The 
  595.      * string value of this property is then interpreted as an integer 
  596.      * value and an <code>Integer</code> object representing this value is 
  597.      * returned. 
  598.      * <p>
  599.      * If the property value begins with "<code>0x</code>" or 
  600.      * "<code>#</code>", not followed by a minus sign, the rest 
  601.      * of it is parsed as a hexadecimal integer exactly as for the method 
  602.      * <code>Integer.valueOf</code> with radix 16. 
  603.      * <p>
  604.      * If the property value begins with "<code>0</code>" then 
  605.      * it is parsed as an octal integer exactly as for the method 
  606.      * <code>Integer.valueOf</code> with radix 8. 
  607.      * <p>
  608.      * Otherwise the property value is parsed as a decimal integer 
  609.      * exactly as for the method <code>Integer.valueOf</code> with radix 10.
  610.      * <p>
  611.      * The second argument is the default value. If there is no property 
  612.      * of the specified name, or if the property does not have the 
  613.      * correct numeric format, then the second argument is returned. 
  614.      *
  615.      * @param   nm   property name.
  616.      * @param   val   default value.
  617.      * @return  the <code>Integer</code> value of the property.
  618.      * @see     java.lang.System#getProperty(java.lang.String)
  619.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  620.      */
  621.     public static Integer getInteger(String nm, Integer val) {
  622.     String v = System.getProperty(nm);
  623.     if (v != null) {
  624.         try {
  625.         return Integer.decode(v);
  626.         } catch (NumberFormatException e) {
  627.         }
  628.     }    
  629.     return val;
  630.     }
  631.  
  632.     /**
  633.      * Decodes a string into an Integer. Deals with decimal, hexadecimal,
  634.      * and octal numbers.
  635.      * @param nm the string to decode
  636.      * @since   JDK1.1
  637.      */
  638.     public static Integer decode(String nm) throws NumberFormatException {
  639.     if (nm.startsWith("0x")) {
  640.         return Integer.valueOf(nm.substring(2), 16);
  641.     }
  642.     if (nm.startsWith("#")) {
  643.         return Integer.valueOf(nm.substring(1), 16);
  644.     }
  645.     if (nm.startsWith("0") && nm.length() > 1) {
  646.         return Integer.valueOf(nm.substring(1), 8);
  647.     }
  648.     
  649.     return Integer.valueOf(nm);
  650.     }
  651.  
  652.     /**
  653.      * Compares two Integers numerically.
  654.      *
  655.      * @param   anotherInteger   the <code>Integer</code> to be compared.
  656.      * @return  the value <code>0</code> if the argument Integer is equal to
  657.      *          this Integer; a value less than <code>0</code> if this Integer
  658.      *          is numerically less than the Integer argument; and a
  659.      *          value greater than <code>0</code> if this Integer is
  660.      *          numerically greater than the Integer argument
  661.      *        (signed comparison).
  662.      * @since   JDK1.2
  663.      */
  664.     public int compareTo(Integer anotherInteger) {
  665.     int thisVal = this.value;
  666.     int anotherVal = anotherInteger.value;
  667.     return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));
  668.     }
  669.  
  670.     /**
  671.      * Compares this Integer to another Object.  If the Object is a Integer,
  672.      * this function behaves like <code>compareTo(Integer)</code>.  Otherwise,
  673.      * it throws a <code>ClassCastException</code> (as Integers are comparable
  674.      * only to other Integers).
  675.      *
  676.      * @param   o the <code>Object</code> to be compared.
  677.      * @return  the value <code>0</code> if the argument is a Integer
  678.      *        numerically equal to this Integer; a value less than
  679.      *        <code>0</code> if the argument is a Integer numerically
  680.      *        greater than this Integer; and a value greater than
  681.      *        <code>0</code> if the argument is a Integer numerically
  682.      *        less than this Integer.
  683.      * @exception <code>ClassCastException</code> if the argument is not an
  684.      *          <code>Integer</code>.
  685.      * @see     java.lang.Comparable
  686.      * @since   JDK1.2
  687.      */
  688.     public int compareTo(Object o) {
  689.     return compareTo((Integer)o);
  690.     }
  691.  
  692.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  693.     private static final long serialVersionUID = 1360826667806852920L;
  694. }
  695.