Ignore:
Timestamp:
Apr 27, 2004, 8:39:34 PM (22 years ago)
Author:
bird
Message:

GCC v3.3.3 sources.

Location:
branches/GNU/src/gcc
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/GNU/src/gcc

    • Property svn:ignore
      •  

        old new  
        2626configure.vr
        2727configure.vrs
         28
        2829Makefile
        29 dir.info
        3030lost+found
        3131update.out
  • branches/GNU/src/gcc/libjava/java/lang/Integer.java

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.1.1.2
    r1390 r1391  
    1 /* java.lang.Integer
    2    Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
     1/*
     2   Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
    33
    44This file is part of GNU Classpath.
     
    88the Free Software Foundation; either version 2, or (at your option)
    99any later version.
    10  
     10
    1111GNU Classpath is distributed in the hope that it will be useful, but
    1212WITHOUT ANY WARRANTY; without even the implied warranty of
     
    4949 * @author John Keiser
    5050 * @author Warren Levy
    51  * @since JDK 1.0
     51 * @author Eric Blake <[email protected]>
     52 * @since 1.0
     53 * @status updated to 1.4
    5254 */
    5355public final class Integer extends Number implements Comparable
    5456{
    55   // compatible with JDK 1.0.2+
     57  /**
     58   * Compatible with JDK 1.0.2+.
     59   */
    5660  private static final long serialVersionUID = 1360826667806852920L;
    5761
    5862  /**
    59    * The minimum value an <code>int</code> can represent is -2147483648.
     63   * The minimum value an <code>int</code> can represent is -2147483648 (or
     64   * -2<sup>31</sup>).
    6065   */
    6166  public static final int MIN_VALUE = 0x80000000;
    6267
    6368  /**
    64    * The maximum value an <code>int</code> can represent is 2147483647.
     69   * The maximum value an <code>int</code> can represent is 2147483647 (or
     70   * 2<sup>31</sup> - 1).
    6571   */
    6672  public static final int MAX_VALUE = 0x7fffffff;
    6773
    6874  /**
    69    * The primitive type <code>int</code> is represented by this 
     75   * The primitive type <code>int</code> is represented by this
    7076   * <code>Class</code> object.
    71    */
    72   public static final Class TYPE = VMClassLoader.getPrimitiveClass ('I');
     77   * @since 1.1
     78   */
     79  public static final Class TYPE = VMClassLoader.getPrimitiveClass('I');
    7380
    7481  /**
    7582   * The immutable value of this Integer.
     83
     84
    7685   */
    7786  private final int value;
    7887
    7988  /**
    80    * Create an <code>Integer</code> object representing the value of the 
     89   * Create an <code>Integer</code> object representing the value of the
    8190   * <code>int</code> argument.
    8291   *
     
    8998
    9099  /**
    91    * Create an <code>Integer</code> object representing the value of the 
     100   * Create an <code>Integer</code> object representing the value of the
    92101   * argument after conversion to an <code>int</code>.
    93102   *
    94    * @param s the string to convert.
    95    */
    96   public Integer(String s) throws NumberFormatException
    97   {
    98     value = parseInt(s, 10);
    99   }
    100 
    101   /**
    102    * Return a hashcode representing this Object.
    103    *
    104    * <code>Integer</code>'s hash code is calculated by simply returning its
    105    * value.
    106    *
    107    * @return this Object's hash code.
    108    */
    109   public int hashCode()
    110   {
    111     return value;
    112   }
    113 
    114   /**
    115    * If the <code>Object</code> is not <code>null</code>, is an
    116    * <code>instanceof</code> <code>Integer</code>, and represents
    117    * the same primitive <code>int</code> value return
    118    * <code>true</code>.  Otherwise <code>false</code> is returned.
    119    */
    120   public boolean equals(Object obj)
    121   {
    122     return obj instanceof Integer && value == ((Integer)obj).value;
    123   }
    124 
    125   /**
    126    * Get the specified system property as an <code>Integer</code>.
    127    *
    128    * The <code>decode()</code> method will be used to interpret the value of
    129    * the property.
    130    * @param nm the name of the system property
    131    * @return the system property as an <code>Integer</code>, or
    132    *         <code>null</code> if the property is not found or cannot be
    133    *         decoded as an <code>Integer</code>.
    134    * @see java.lang.System#getProperty(java.lang.String)
    135    * @see #decode(int)
    136    */
    137   public static Integer getInteger(String nm)
    138   {
    139     return getInteger(nm, null);
    140   }
    141 
    142   /**
    143    * Get the specified system property as an <code>Integer</code>, or use a
    144    * default <code>int</code> value if the property is not found or is not
    145    * decodable.
    146    *
    147    * The <code>decode()</code> method will be used to interpret the value of
    148    * the property.
    149    *
    150    * @param nm the name of the system property
    151    * @param val the default value to use if the property is not found or not
    152    *        a number.
    153    * @return the system property as an <code>Integer</code>, or the default
    154    *         value if the property is not found or cannot be decoded as an
    155    *         <code>Integer</code>.
    156    * @see java.lang.System#getProperty(java.lang.String)
    157    * @see #decode(int)
    158    * @see #getInteger(java.lang.String,java.lang.Integer)
    159    */
    160   public static Integer getInteger(String nm, int val)
    161   {
    162     Integer result = getInteger(nm, null);
    163     return (result == null) ? new Integer(val) : result;
    164   }
    165 
    166   /**
    167    * Get the specified system property as an <code>Integer</code>, or use a
    168    * default <code>Integer</code> value if the property is not found or is
    169    * not decodable.
    170    *
    171    * The <code>decode()</code> method will be used to interpret the value of
    172    * the property.
    173    *
    174    * @param nm the name of the system property
    175    * @param val the default value to use if the property is not found or not
    176    *        a number.
    177    * @return the system property as an <code>Integer</code>, or the default
    178    *         value if the property is not found or cannot be decoded as an
    179    *         <code>Integer</code>.
    180    * @see java.lang.System#getProperty(java.lang.String)
    181    * @see #decode(int)
    182    * @see #getInteger(java.lang.String,int)
    183    */
    184   public static Integer getInteger(String nm, Integer def)
    185   {
    186     if (nm == null || "".equals(nm))
    187       return def;
    188     nm = System.getProperty(nm);
    189     if (nm == null) return def;
    190     try
    191       {
    192         return decode(nm);
    193       }
    194     catch (NumberFormatException e)
    195       {
    196         return def;
    197       }
    198   }
    199 
    200   private static String toUnsignedString(int num, int exp)
    201   {
    202     // Use an array large enough for a binary number.
    203     int radix = 1 << exp;
    204     int mask = radix - 1;
    205     char[] buffer = new char[32];
    206     int i = 32;
    207     do
    208       {
    209         buffer[--i] = Character.forDigit(num & mask, radix);
    210         num = num >>> exp;
    211       }
    212     while (num != 0);
    213 
    214     return String.valueOf(buffer, i, 32-i);
    215   }
    216 
    217   /**
    218    * Converts the <code>int</code> to a <code>String</code> assuming it is
    219    * unsigned in base 16.
    220    * @param i the <code>int</code> to convert to <code>String</code>
    221    * @return the <code>String</code> representation of the argument.
    222    */
    223   public static String toHexString(int i)
    224   {
    225     return toUnsignedString(i, 4);
    226   }
    227 
    228   /**
    229    * Converts the <code>int</code> to a <code>String</code> assuming it is
    230    * unsigned in base 8.
    231    * @param i the <code>int</code> to convert to <code>String</code>
    232    * @return the <code>String</code> representation of the argument.
    233    */
    234   public static String toOctalString(int i)
    235   {
    236     return toUnsignedString(i, 3);
    237   }
    238 
    239   /**
    240    * Converts the <code>int</code> to a <code>String</code> assuming it is
    241    * unsigned in base 2.
    242    * @param i the <code>int</code> to convert to <code>String</code>
    243    * @return the <code>String</code> representation of the argument.
    244    */
    245   public static String toBinaryString(int i)
    246   {
    247     return toUnsignedString(i, 1);
    248   }
    249 
    250   /**
    251    * Converts the <code>int</code> to a <code>String</code> and assumes
    252    * a radix of 10.
    253    * @param i the <code>int</code> to convert to <code>String</code>
    254    * @return the <code>String</code> representation of the argument.
    255    */
    256   public static String toString(int i)
    257   {
    258     // This is tricky: in libgcj, String.valueOf(int) is a fast native
    259     // implementation.  In Classpath it just calls back to
    260     // Integer.toString(int,int).
    261     return String.valueOf (i);
    262   }
    263 
    264   /**
    265    * Converts the <code>Integer</code> value to a <code>String</code> and
    266    * assumes a radix of 10.
    267    * @return the <code>String</code> representation of this <code>Integer</code>.
    268    */   
    269   public String toString()
    270   {
    271     return toString (value);
     103   * @param s the string to convert
     104   * @throws NumberFormatException if the String does not contain an int
     105   * @see #valueOf(String)
     106   */
     107  public Integer(String s)
     108  {
     109    value = parseInt(s, 10, false);
    272110  }
    273111
    274112  /**
    275113   * Converts the <code>int</code> to a <code>String</code> using
    276    * the specified radix (base).
    277    * @param i the <code>int</code> to convert to <code>String</code>.
    278    * @param radix the radix (base) to use in the conversion.
    279    * @return the <code>String</code> representation of the argument.
     114   * the specified radix (base). If the radix exceeds
     115   * <code>Character.MIN_RADIX</code> or <code>Character.MAX_RADIX</code>, 10
     116   * is used instead. If the result is negative, the leading character is
     117   * '-' ('\\u002D'). The remaining characters come from
     118   * <code>Character.forDigit(digit, radix)</code> ('0'-'9','a'-'z').
     119   *
     120   * @param num the <code>int</code> to convert to <code>String</code>
     121   * @param radix the radix (base) to use in the conversion
     122   * @return the <code>String</code> representation of the argument
    280123   */
    281124  public static String toString(int num, int radix)
     
    288131    char[] buffer = new char[33];
    289132    int i = 33;
    290     boolean isNeg;
     133    boolean isNeg;
    291134    if (num < 0)
    292135      {
    293136        isNeg = true;
    294         num = -(num);
     137        num = -;
    295138
    296139        // When the value is MIN_VALUE, it overflows when made positive
    297140        if (num < 0)
    298           {
    299             buffer[--i] = Character.forDigit(-(num + radix) % radix, radix);
    300             num = -(num / radix);
    301           }
    302       }
    303     else
    304       isNeg = false;
     141          {
     142            buffer[--i] = digits[(int) (-(num + radix) % radix)];
     143            num = -(num / radix);
     144          }
     145      }
    305146
    306147    do
    307148      {
    308         buffer[--i] = Character.forDigit(num % radix, radix);
     149        buffer[--i] = ;
    309150        num /= radix;
    310151      }
     
    314155      buffer[--i] = '-';
    315156
    316     return String.valueOf(buffer, i, 33-i);
     157    // Package constructor avoids an array copy.
     158    return new String(buffer, i, 33 - i, true);
     159  }
     160
     161  /**
     162   * Converts the <code>int</code> to a <code>String</code> assuming it is
     163   * unsigned in base 16.
     164   *
     165   * @param i the <code>int</code> to convert to <code>String</code>
     166   * @return the <code>String</code> representation of the argument
     167   */
     168  public static String toHexString(int i)
     169  {
     170    return toUnsignedString(i, 4);
     171  }
     172
     173  /**
     174   * Converts the <code>int</code> to a <code>String</code> assuming it is
     175   * unsigned in base 8.
     176   *
     177   * @param i the <code>int</code> to convert to <code>String</code>
     178   * @return the <code>String</code> representation of the argument
     179   */
     180  public static String toOctalString(int i)
     181  {
     182    return toUnsignedString(i, 3);
     183  }
     184
     185  /**
     186   * Converts the <code>int</code> to a <code>String</code> assuming it is
     187   * unsigned in base 2.
     188   *
     189   * @param i the <code>int</code> to convert to <code>String</code>
     190   * @return the <code>String</code> representation of the argument
     191   */
     192  public static String toBinaryString(int i)
     193  {
     194    return toUnsignedString(i, 1);
     195  }
     196
     197  /**
     198   * Converts the <code>int</code> to a <code>String</code> and assumes
     199   * a radix of 10.
     200   *
     201   * @param i the <code>int</code> to convert to <code>String</code>
     202   * @return the <code>String</code> representation of the argument
     203   * @see #toString(int, int)
     204   */
     205  public static String toString(int i)
     206  {
     207    // This is tricky: in libgcj, String.valueOf(int) is a fast native
     208    // implementation.  In Classpath it just calls back to
     209    // Integer.toString(int, int).
     210    return String.valueOf(i);
     211  }
     212
     213  /**
     214   * Converts the specified <code>String</code> into an <code>int</code>
     215   * using the specified radix (base). The string must not be <code>null</code>
     216   * or empty. It may begin with an optional '-', which will negate the answer,
     217   * provided that there are also valid digits. Each digit is parsed as if by
     218   * <code>Character.digit(d, radix)</code>, and must be in the range
     219   * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
     220   * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
     221   * Unlike Double.parseDouble, you may not have a leading '+'.
     222   *
     223   * @param s the <code>String</code> to convert
     224   * @param radix the radix (base) to use in the conversion
     225   * @return the <code>String</code> argument converted to </code>int</code>
     226   * @throws NumberFormatException if <code>s</code> cannot be parsed as an
     227   *         <code>int</code>
     228   */
     229  public static int parseInt(String str, int radix)
     230  {
     231    return parseInt(str, radix, false);
     232  }
     233
     234  /**
     235   * Converts the specified <code>String</code> into an <code>int</code>.
     236   * This function assumes a radix of 10.
     237   *
     238   * @param s the <code>String</code> to convert
     239   * @return the <code>int</code> value of <code>s</code>
     240   * @throws NumberFormatException if <code>s</code> cannot be parsed as an
     241   *         <code>int</code>
     242   * @see #parseInt(String, int)
     243   */
     244  public static int parseInt(String s)
     245  {
     246    return parseInt(s, 10, false);
     247  }
     248
     249  /**
     250   * Creates a new <code>Integer</code> object using the <code>String</code>
     251   * and specified radix (base).
     252   *
     253   * @param s the <code>String</code> to convert
     254   * @param radix the radix (base) to convert with
     255   * @return the new <code>Integer</code>
     256   * @throws NumberFormatException if <code>s</code> cannot be parsed as an
     257   *         <code>int</code>
     258   * @see #parseInt(String, int)
     259   */
     260  public static Integer valueOf(String s, int radix)
     261  {
     262    return new Integer(parseInt(s, radix, false));
    317263  }
    318264
     
    320266   * Creates a new <code>Integer</code> object using the <code>String</code>,
    321267   * assuming a radix of 10.
    322    * @param s the <code>String</code> to convert.
    323    * @return the new <code>Integer</code>.
    324    * @see #Integer(java.lang.String)
    325    * @see #parseInt(java.lang.String)
    326    * @exception NumberFormatException thrown if the <code>String</code>
    327    * cannot be parsed as an <code>int</code>.
    328    */
    329   public static Integer valueOf(String s) throws NumberFormatException
    330   {
    331     return new Integer(parseInt(s));
    332   }
    333 
    334   /**
    335    * Creates a new <code>Integer</code> object using the <code>String</code>
    336    * and specified radix (base).
    337    * @param s the <code>String</code> to convert.
    338    * @param radix the radix (base) to convert with.
    339    * @return the new <code>Integer</code>.
    340    * @see #parseInt(java.lang.String,int)
    341    * @exception NumberFormatException thrown if the <code>String</code>
    342    * cannot be parsed as an <code>int</code>.
    343    */
    344   public static Integer valueOf(String s, int radix)
    345     throws NumberFormatException
    346   {
    347     return new Integer(parseInt(s, radix));
    348   }
    349 
    350   /**
    351    * Converts the specified <code>String</code> into an <code>int</code>.
    352    * This function assumes a radix of 10.
    353268   *
    354269   * @param s the <code>String</code> to convert
    355    * @return the <code>int</code> value of the <code>String</code>
    356    *         argument.
    357    * @exception NumberFormatException thrown if the <code>String</code>
    358    * cannot be parsed as an <code>int</code>.
    359    */
    360   public static int parseInt(String s) throws NumberFormatException
    361   {
    362     return parseInt(s, 10);
    363   }
    364 
    365   /**
    366    * Converts the specified <code>String</code> into an <code>int</code>
    367    * using the specified radix (base).
    368    *
    369    * @param s the <code>String</code> to convert
    370    * @param radix the radix (base) to use in the conversion
    371    * @return the <code>String</code> argument converted to </code>int</code>.
    372    * @exception NumberFormatException thrown if the <code>String</code>
    373    * cannot be parsed as a <code>int</code>.   
    374    */
    375   public static int parseInt(String str, int radix)
    376     throws NumberFormatException
    377   {
    378     final int len;
    379 
    380     if (str == null)
    381       throw new NumberFormatException ();
    382 
    383     if ((len = str.length()) == 0 ||
    384         radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
     270   * @return the new <code>Integer</code>
     271   * @throws NumberFormatException if <code>s</code> cannot be parsed as an
     272   *         <code>int</code>
     273   * @see #Integer(String)
     274   * @see #parseInt(String)
     275   */
     276  public static Integer valueOf(String s)
     277  {
     278    return new Integer(parseInt(s, 10, false));
     279  }
     280
     281  /**
     282   * Return the value of this <code>Integer</code> as a <code>byte</code>.
     283   *
     284   * @return the byte value
     285   */
     286  public byte byteValue()
     287  {
     288    return (byte) value;
     289  }
     290
     291  /**
     292   * Return the value of this <code>Integer</code> as a <code>short</code>.
     293   *
     294   * @return the short value
     295   */
     296  public short shortValue()
     297  {
     298    return (short) value;
     299  }
     300
     301  /**
     302   * Return the value of this <code>Integer</code>.
     303   * @return the int value
     304   */
     305  public int intValue()
     306  {
     307    return value;
     308  }
     309
     310  /**
     311   * Return the value of this <code>Integer</code> as a <code>long</code>.
     312   *
     313   * @return the long value
     314   */
     315  public long longValue()
     316  {
     317    return value;
     318  }
     319
     320  /**
     321   * Return the value of this <code>Integer</code> as a <code>float</code>.
     322   *
     323   * @return the float value
     324   */
     325  public float floatValue()
     326  {
     327    return value;
     328  }
     329
     330  /**
     331   * Return the value of this <code>Integer</code> as a <code>double</code>.
     332   *
     333   * @return the double value
     334   */
     335  public double doubleValue()
     336  {
     337    return value;
     338  }
     339
     340  /**
     341   * Converts the <code>Integer</code> value to a <code>String</code> and
     342   * assumes a radix of 10.
     343   *
     344   * @return the <code>String</code> representation
     345   */
     346  public String toString()
     347  {
     348    return String.valueOf(value);
     349  }
     350
     351  /**
     352   * Return a hashcode representing this Object. <code>Integer</code>'s hash
     353   * code is simply its value.
     354   *
     355   * @return this Object's hash code
     356   */
     357  public int hashCode()
     358  {
     359    return value;
     360  }
     361
     362  /**
     363   * Returns <code>true</code> if <code>obj</code> is an instance of
     364   * <code>Integer</code> and represents the same int value.
     365   *
     366   * @param obj the object to compare
     367   * @return whether these Objects are semantically equal
     368   */
     369  public boolean equals(Object obj)
     370  {
     371    return obj instanceof Integer && value == ((Integer) obj).value;
     372  }
     373
     374  /**
     375   * Get the specified system property as an <code>Integer</code>. The
     376   * <code>decode()</code> method will be used to interpret the value of
     377   * the property.
     378   *
     379   * @param nm the name of the system property
     380   * @return the system property as an <code>Integer</code>, or null if the
     381   *         property is not found or cannot be decoded
     382   * @throws SecurityException if accessing the system property is forbidden
     383   * @see System#getProperty(String)
     384   * @see #decode(String)
     385   */
     386  public static Integer getInteger(String nm)
     387  {
     388    return getInteger(nm, null);
     389  }
     390
     391  /**
     392   * Get the specified system property as an <code>Integer</code>, or use a
     393   * default <code>int</code> value if the property is not found or is not
     394   * decodable. The <code>decode()</code> method will be used to interpret
     395   * the value of the property.
     396   *
     397   * @param nm the name of the system property
     398   * @param val the default value
     399   * @return the value of the system property, or the default
     400   * @throws SecurityException if accessing the system property is forbidden
     401   * @see System#getProperty(String)
     402   * @see #decode(String)
     403   */
     404  public static Integer getInteger(String nm, int val)
     405  {
     406    Integer result = getInteger(nm, null);
     407    return result == null ? new Integer(val) : result;
     408  }
     409
     410  /**
     411   * Get the specified system property as an <code>Integer</code>, or use a
     412   * default <code>Integer</code> value if the property is not found or is
     413   * not decodable. The <code>decode()</code> method will be used to
     414   * interpret the value of the property.
     415   *
     416   * @param nm the name of the system property
     417   * @param val the default value
     418   * @return the value of the system property, or the default
     419   * @throws SecurityException if accessing the system property is forbidden
     420   * @see System#getProperty(String)
     421   * @see #decode(String)
     422   */
     423  public static Integer getInteger(String nm, Integer def)
     424  {
     425    if (nm == null || "".equals(nm))
     426      return def;
     427    nm = System.getProperty(nm);
     428    if (nm == null)
     429      return def;
     430    try
     431      {
     432        return decode(nm);
     433      }
     434    catch (NumberFormatException e)
     435      {
     436        return def;
     437      }
     438  }
     439
     440  /**
     441   * Convert the specified <code>String</code> into an <code>Integer</code>.
     442   * The <code>String</code> may represent decimal, hexadecimal, or
     443   * octal numbers.
     444   *
     445   * <p>The extended BNF grammar is as follows:<br>
     446   * <pre>
     447   * <em>DecodableString</em>:
     448   *      ( [ <code>-</code> ] <em>DecimalNumber</em> )
     449   *    | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
     450   *              | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
     451   *    | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
     452   * <em>DecimalNumber</em>:
     453   *        <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
     454   * <em>DecimalDigit</em>:
     455   *        <em>Character.digit(d, 10) has value 0 to 9</em>
     456   * <em>OctalDigit</em>:
     457   *        <em>Character.digit(d, 8) has value 0 to 7</em>
     458   * <em>DecimalDigit</em>:
     459   *        <em>Character.digit(d, 16) has value 0 to 15</em>
     460   * </pre>
     461   * Finally, the value must be in the range <code>MIN_VALUE</code> to
     462   * <code>MAX_VALUE</code>, or an exception is thrown.
     463   *
     464   * @param s the <code>String</code> to interpret
     465   * @return the value of the String as an <code>Integer</code>
     466   * @throws NumberFormatException if <code>s</code> cannot be parsed as a
     467   *         <code>int</code>
     468   * @throws NullPointerException if <code>s</code> is null
     469   * @since 1.2
     470   */
     471  public static Integer decode(String str)
     472  {
     473    return new Integer(parseInt(str, 10, true));
     474  }
     475
     476  /**
     477   * Compare two Integers numerically by comparing their <code>int</code>
     478   * values. The result is positive if the first is greater, negative if the
     479   * second is greater, and 0 if the two are equal.
     480   *
     481   * @param i the Integer to compare
     482   * @return the comparison
     483   * @since 1.2
     484   */
     485  public int compareTo(Integer i)
     486  {
     487    if (value == i.value)
     488      return 0;
     489    // Returns just -1 or 1 on inequality; doing math might overflow.
     490    return value > i.value ? 1 : -1;
     491  }
     492
     493  /**
     494   * Behaves like <code>compareTo(Integer)</code> unless the Object
     495   * is not an <code>Integer</code>.
     496   *
     497   * @param o the object to compare
     498   * @return the comparison
     499   * @throws ClassCastException if the argument is not an <code>Integer</code>
     500   * @see #compareTo(Integer)
     501   * @see Comparable
     502   * @since 1.2
     503   */
     504  public int compareTo(Object o)
     505  {
     506    return compareTo((Integer) o);
     507  }
     508
     509  /**
     510   * Helper for converting unsigned numbers to String.
     511   *
     512   * @param num the number
     513   * @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex)
     514   */
     515  // Package visible for use by Long.
     516  static String toUnsignedString(int num, int exp)
     517  {
     518    // Use an array large enough for a binary number.
     519    int mask = (1 << exp) - 1;
     520    char[] buffer = new char[32];
     521    int i = 32;
     522    do
     523      {
     524        buffer[--i] = digits[num & mask];
     525        num >>>= exp;
     526      }
     527    while (num != 0);
     528
     529    // Package constructor avoids an array copy.
     530    return new String(buffer, i, 32 - i, true);
     531  }
     532
     533  /**
     534   * Helper for parsing ints, used by Integer, Short, and Byte.
     535   *
     536   * @param str the string to parse
     537   * @param radix the radix to use, must be 10 if decode is true
     538   * @param decode if called from decode
     539   * @return the parsed int value
     540   * @throws NumberFormatException if there is an error
     541   * @throws NullPointerException if decode is true and str if null
     542   * @see #parseInt(String, int)
     543   * @see #decode(String)
     544   * @see Byte#parseInt(String, int)
     545   * @see Short#parseInt(String, int)
     546   */
     547  static int parseInt(String str, int radix, boolean decode)
     548  {
     549    if (! decode && str == null)
    385550      throw new NumberFormatException();
    386 
     551    int index = 0;
     552    int len = str.length();
    387553    boolean isNeg = false;
    388     int index = 0;
    389     if (str.charAt(index) == '-')
    390       if (len > 1)
    391         {
    392           isNeg = true;
    393           index++;
    394         }
    395       else
    396         throw new NumberFormatException();
    397 
    398     return parseInt(str, index, len, isNeg, radix);
    399   }
    400 
    401   private static int parseInt(String str, int index, int len, boolean isNeg,
    402                               int radix)
    403     throws NumberFormatException
    404   {
    405     int val = 0;
    406     int digval;
     554    if (len == 0)
     555      throw new NumberFormatException();
     556    int ch = str.charAt(index);
     557    if (ch == '-')
     558      {
     559        if (len == 1)
     560          throw new NumberFormatException();
     561        isNeg = true;
     562        ch = str.charAt(++index);
     563      }
     564    if (decode)
     565      {
     566        if (ch == '0')
     567          {
     568            if (++index == len)
     569              return 0;
     570            if ((str.charAt(index) & ~('x' ^ 'X')) == 'X')
     571              {
     572                radix = 16;
     573                index++;
     574              }
     575            else
     576              radix = 8;
     577          }
     578        else if (ch == '#')
     579          {
     580            radix = 16;
     581            index++;
     582          }
     583      }
     584    if (index == len)
     585      throw new NumberFormatException();
    407586
    408587    int max = MAX_VALUE / radix;
     
    412591      ++max;
    413592
    414     for ( ; index < len; index++)
     593    int val = 0;
     594    while (index < len)
    415595      {
    416596        if (val < 0 || val > max)
    417597          throw new NumberFormatException();
    418598
    419         if ((digval = Character.digit(str.charAt(index), radix)) < 0)
     599        ch = Character.digit(str.charAt(index++), radix);
     600        val = val * radix + ch;
     601        if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE)))
    420602          throw new NumberFormatException();
    421 
    422         // Throw an exception for overflow if result is negative.
    423         // However, we special-case the most negative value.
    424         val = val * radix + digval;
    425         if (val < 0 && (! isNeg || val != MIN_VALUE))
    426           throw new NumberFormatException();
    427       }
    428 
    429     return isNeg ? -(val) : val;
    430   }
    431 
    432   /**
    433    * Convert the specified <code>String</code> into an <code>Integer</code>.
    434    * The <code>String</code> may represent decimal, hexadecimal, or
    435    * octal numbers.
    436    *
    437    * The <code>String</code> argument is interpreted based on the leading
    438    * characters.  Depending on what the String begins with (after an optional
    439    * minus sign), the base will be interpreted differently:
    440    *
    441    * <table border=1>
    442    * <tr><th>Leading<br>Characters</th><th>Base</th></tr>
    443    * <tr><td>#</td><td>16</td></tr>
    444    * <tr><td>0x</td><td>16</td></tr>
    445    * <tr><td>0X</td><td>16</td></tr>
    446    * <tr><td>0</td><td>8</td></tr>
    447    * <tr><td>Anything<br>Else</td><td>10</td></tr>
    448    * </table>
    449    *
    450    * If the String starts with a minus sign the result is negated.
    451    *
    452    * @param str the <code>String</code> to interpret.
    453    * @return the value of the String as an <code>Integer</code>.
    454    * @exception NumberFormatException thrown if the <code>String</code>
    455    * cannot be parsed as an <code>int</code>.   
    456    */
    457   public static Integer decode(String str) throws NumberFormatException
    458   {
    459     boolean isNeg = false;
    460     int index = 0;
    461     int radix = 10;
    462     final int len;
    463 
    464     if ((len = str.length()) == 0)
    465       throw new NumberFormatException("empty string");
    466 
    467     if (str.charAt(index) == '-')
    468       {
    469         // The minus sign should be followed by at least one more char
    470         if (len > 1)
    471           {
    472             isNeg = true;
    473             index++;
    474           }
    475         else
    476           throw new NumberFormatException();
    477       }
    478 
    479     if (str.charAt(index) == '#')
    480       {
    481         radix = 16;
    482         index++;
    483       }
    484     else if (str.charAt(index) == '0')
    485       {
    486         index++;
    487 
    488         // Check if str is just "0" or "-0"
    489         if (len == index)
    490           return new Integer(0);
    491 
    492         if (str.charAt(index) == 'x' || str.charAt(index) == 'X')
    493           {
    494             radix = 16;
    495             index++;
    496           }
    497         else
    498           radix = 8;
    499       }
    500 
    501     if (index >= len)
    502       throw new NumberFormatException("empty value");
    503 
    504     return new Integer(parseInt(str, index, len, isNeg, radix));
    505   }
    506 
    507   /** Return the value of this <code>Integer</code> as a <code>byte</code>.
    508    ** @return the value of this <code>Integer</code> as a <code>byte</code>.
    509    **/
    510   public byte byteValue()
    511   {
    512     return (byte) value;
    513   }
    514 
    515   /** Return the value of this <code>Integer</code> as a <code>short</code>.
    516    ** @return the value of this <code>Integer</code> as a <code>short</code>.
    517    **/
    518   public short shortValue()
    519   {
    520     return (short) value;
    521   }
    522 
    523   /** Return the value of this <code>Integer</code> as an <code>int</code>.
    524    ** @return the value of this <code>Integer</code> as an <code>int</code>.
    525    **/
    526   public int intValue()
    527   {
    528     return value;
    529   }
    530 
    531   /** Return the value of this <code>Integer</code> as a <code>long</code>.
    532    ** @return the value of this <code>Integer</code> as a <code>long</code>.
    533    **/
    534   public long longValue()
    535   {
    536     return value;
    537   }
    538 
    539   /** Return the value of this <code>Integer</code> as a <code>float</code>.
    540    ** @return the value of this <code>Integer</code> as a <code>float</code>.
    541    **/
    542   public float floatValue()
    543   {
    544     return value;
    545   }
    546 
    547   /** Return the value of this <code>Integer</code> as a <code>double</code>.
    548    ** @return the value of this <code>Integer</code> as a <code>double</code>.
    549    **/
    550   public double doubleValue()
    551   {
    552     return value;
    553   }
    554 
    555   /**
    556    * Compare two Integers numerically by comparing their
    557    * <code>int</code> values.
    558    * @return a positive value if this <code>Integer</code> is greater
    559    * in value than the argument <code>Integer</code>; a negative value
    560    * if this <code>Integer</code> is smaller in value than the argument
    561    * <code>Integer</code>; and <code>0</code>, zero, if this
    562    * <code>Integer</code> is equal in value to the argument
    563    * <code>Integer</code>. 
    564    *
    565    * @since 1.2
    566    */
    567   public int compareTo(Integer i)
    568   {
    569     if (this.value == i.value)
    570       return 0;
    571 
    572     // Returns just -1 or 1 on inequality; doing math might overflow.
    573     if (this.value > i.value)
    574       return 1;
    575 
    576     return -1;
    577   }
    578 
    579   /**
    580    * Behaves like <code>compareTo(java.lang.Integer)</code> unless the Object
    581    * is not a <code>Integer</code>.  Then it throws a
    582    * <code>ClassCastException</code>.
    583    * @exception ClassCastException if the argument is not a
    584    * <code>Integer</code>.
    585    *
    586    * @since 1.2
    587    */
    588   public int compareTo(Object o)
    589   {
    590     return compareTo((Integer)o);
     603      }
     604    return isNeg ? -val : val;
    591605  }
    592606}
Note: See TracChangeset for help on using the changeset viewer.