2001-07-23 22:01:29 +02:00
|
|
|
/* java.lang.Integer
|
|
|
|
Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
This file is part of GNU Classpath.
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
|
|
|
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
02111-1307 USA.
|
|
|
|
|
2002-01-22 23:40:42 +01:00
|
|
|
Linking this library statically or dynamically with other modules is
|
|
|
|
making a combined work based on this library. Thus, the terms and
|
|
|
|
conditions of the GNU General Public License cover the whole
|
|
|
|
combination.
|
|
|
|
|
|
|
|
As a special exception, the copyright holders of this library give you
|
|
|
|
permission to link this library with independent modules to produce an
|
|
|
|
executable, regardless of the license terms of these independent
|
|
|
|
modules, and to copy and distribute the resulting executable under
|
|
|
|
terms of your choice, provided that you also meet, for each linked
|
|
|
|
independent module, the terms and conditions of the license of that
|
|
|
|
module. An independent module is a module which is not derived from
|
|
|
|
or based on this library. If you modify this library, you may extend
|
|
|
|
this exception to your version of the library, but you are not
|
|
|
|
obligated to do so. If you do not wish to do so, delete this
|
|
|
|
exception statement from your version. */
|
2001-07-23 22:01:29 +02:00
|
|
|
|
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
package java.lang;
|
|
|
|
|
|
|
|
/**
|
2001-07-23 22:01:29 +02:00
|
|
|
* Instances of class <code>Integer</code> represent primitive
|
|
|
|
* <code>int</code> values.
|
|
|
|
*
|
|
|
|
* Additionally, this class provides various helper functions and variables
|
|
|
|
* related to ints.
|
|
|
|
*
|
|
|
|
* @author Paul Fisher
|
|
|
|
* @author John Keiser
|
|
|
|
* @author Warren Levy
|
|
|
|
* @since JDK 1.0
|
1999-04-07 16:42:40 +02:00
|
|
|
*/
|
|
|
|
public final class Integer extends Number implements Comparable
|
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
// compatible with JDK 1.0.2+
|
2000-09-08 21:37:09 +02:00
|
|
|
private static final long serialVersionUID = 1360826667806852920L;
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* The minimum value an <code>int</code> can represent is -2147483648.
|
|
|
|
*/
|
|
|
|
public static final int MIN_VALUE = 0x80000000;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The maximum value an <code>int</code> can represent is 2147483647.
|
|
|
|
*/
|
|
|
|
public static final int MAX_VALUE = 0x7fffffff;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The primitive type <code>int</code> is represented by this
|
|
|
|
* <code>Class</code> object.
|
|
|
|
*/
|
2001-10-26 03:51:04 +02:00
|
|
|
public static final Class TYPE = VMClassLoader.getPrimitiveClass ('I');
|
2001-07-23 22:01:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The immutable value of this Integer.
|
|
|
|
*/
|
|
|
|
private final int value;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an <code>Integer</code> object representing the value of the
|
|
|
|
* <code>int</code> argument.
|
|
|
|
*
|
|
|
|
* @param value the value to use
|
|
|
|
*/
|
|
|
|
public Integer(int value)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
this.value = value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Create an <code>Integer</code> object representing the value of the
|
|
|
|
* argument after conversion to an <code>int</code>.
|
|
|
|
*
|
|
|
|
* @param s the string to convert.
|
|
|
|
*/
|
|
|
|
public Integer(String s) throws NumberFormatException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
value = parseInt(s, 10);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Return a hashcode representing this Object.
|
|
|
|
*
|
|
|
|
* <code>Integer</code>'s hash code is calculated by simply returning its
|
|
|
|
* value.
|
|
|
|
*
|
|
|
|
* @return this Object's hash code.
|
|
|
|
*/
|
|
|
|
public int hashCode()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* If the <code>Object</code> is not <code>null</code>, is an
|
|
|
|
* <code>instanceof</code> <code>Integer</code>, and represents
|
|
|
|
* the same primitive <code>int</code> value return
|
|
|
|
* <code>true</code>. Otherwise <code>false</code> is returned.
|
|
|
|
*/
|
|
|
|
public boolean equals(Object obj)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return obj instanceof Integer && value == ((Integer)obj).value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Get the specified system property as an <code>Integer</code>.
|
|
|
|
*
|
|
|
|
* The <code>decode()</code> method will be used to interpret the value of
|
|
|
|
* the property.
|
|
|
|
* @param nm the name of the system property
|
|
|
|
* @return the system property as an <code>Integer</code>, or
|
|
|
|
* <code>null</code> if the property is not found or cannot be
|
|
|
|
* decoded as an <code>Integer</code>.
|
|
|
|
* @see java.lang.System#getProperty(java.lang.String)
|
|
|
|
* @see #decode(int)
|
|
|
|
*/
|
|
|
|
public static Integer getInteger(String nm)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return getInteger(nm, null);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Get the specified system property as an <code>Integer</code>, or use a
|
|
|
|
* default <code>int</code> value if the property is not found or is not
|
|
|
|
* decodable.
|
|
|
|
*
|
|
|
|
* The <code>decode()</code> method will be used to interpret the value of
|
|
|
|
* the property.
|
|
|
|
*
|
|
|
|
* @param nm the name of the system property
|
|
|
|
* @param val the default value to use if the property is not found or not
|
|
|
|
* a number.
|
|
|
|
* @return the system property as an <code>Integer</code>, or the default
|
|
|
|
* value if the property is not found or cannot be decoded as an
|
|
|
|
* <code>Integer</code>.
|
|
|
|
* @see java.lang.System#getProperty(java.lang.String)
|
|
|
|
* @see #decode(int)
|
|
|
|
* @see #getInteger(java.lang.String,java.lang.Integer)
|
|
|
|
*/
|
|
|
|
public static Integer getInteger(String nm, int val)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
Integer result = getInteger(nm, null);
|
|
|
|
return (result == null) ? new Integer(val) : result;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Get the specified system property as an <code>Integer</code>, or use a
|
|
|
|
* default <code>Integer</code> value if the property is not found or is
|
|
|
|
* not decodable.
|
|
|
|
*
|
|
|
|
* The <code>decode()</code> method will be used to interpret the value of
|
|
|
|
* the property.
|
|
|
|
*
|
|
|
|
* @param nm the name of the system property
|
|
|
|
* @param val the default value to use if the property is not found or not
|
|
|
|
* a number.
|
|
|
|
* @return the system property as an <code>Integer</code>, or the default
|
|
|
|
* value if the property is not found or cannot be decoded as an
|
|
|
|
* <code>Integer</code>.
|
|
|
|
* @see java.lang.System#getProperty(java.lang.String)
|
|
|
|
* @see #decode(int)
|
|
|
|
* @see #getInteger(java.lang.String,int)
|
|
|
|
*/
|
|
|
|
public static Integer getInteger(String nm, Integer def)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
String val = System.getProperty(nm);
|
|
|
|
if (val == null) return def;
|
|
|
|
try
|
|
|
|
{
|
2001-11-16 20:52:24 +01:00
|
|
|
return decode(val);
|
2001-07-23 22:01:29 +02:00
|
|
|
}
|
|
|
|
catch (NumberFormatException e)
|
|
|
|
{
|
|
|
|
return def;
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
private static String toUnsignedString(int num, int exp)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
// Use an array large enough for a binary number.
|
|
|
|
int radix = 1 << exp;
|
|
|
|
int mask = radix - 1;
|
|
|
|
char[] buffer = new char[32];
|
|
|
|
int i = 32;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
buffer[--i] = Character.forDigit(num & mask, radix);
|
|
|
|
num = num >>> exp;
|
|
|
|
}
|
|
|
|
while (num != 0);
|
|
|
|
|
|
|
|
return String.valueOf(buffer, i, 32-i);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the <code>int</code> to a <code>String</code> assuming it is
|
|
|
|
* unsigned in base 16.
|
|
|
|
* @param i the <code>int</code> to convert to <code>String</code>
|
|
|
|
* @return the <code>String</code> representation of the argument.
|
|
|
|
*/
|
|
|
|
public static String toHexString(int i)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return toUnsignedString(i, 4);
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the <code>int</code> to a <code>String</code> assuming it is
|
|
|
|
* unsigned in base 8.
|
|
|
|
* @param i the <code>int</code> to convert to <code>String</code>
|
|
|
|
* @return the <code>String</code> representation of the argument.
|
|
|
|
*/
|
|
|
|
public static String toOctalString(int i)
|
|
|
|
{
|
|
|
|
return toUnsignedString(i, 3);
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the <code>int</code> to a <code>String</code> assuming it is
|
|
|
|
* unsigned in base 2.
|
|
|
|
* @param i the <code>int</code> to convert to <code>String</code>
|
|
|
|
* @return the <code>String</code> representation of the argument.
|
|
|
|
*/
|
|
|
|
public static String toBinaryString(int i)
|
|
|
|
{
|
|
|
|
return toUnsignedString(i, 1);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the <code>int</code> to a <code>String</code> and assumes
|
|
|
|
* a radix of 10.
|
|
|
|
* @param i the <code>int</code> to convert to <code>String</code>
|
|
|
|
* @return the <code>String</code> representation of the argument.
|
|
|
|
*/
|
|
|
|
public static String toString(int i)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
// This is tricky: in libgcj, String.valueOf(int) is a fast native
|
|
|
|
// implementation. In Classpath it just calls back to
|
|
|
|
// Integer.toString(int,int).
|
|
|
|
return String.valueOf (i);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the <code>Integer</code> value to a <code>String</code> and
|
|
|
|
* assumes a radix of 10.
|
|
|
|
* @return the <code>String</code> representation of this <code>Integer</code>.
|
|
|
|
*/
|
|
|
|
public String toString()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return toString (value);
|
|
|
|
}
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the <code>int</code> to a <code>String</code> using
|
|
|
|
* the specified radix (base).
|
|
|
|
* @param i the <code>int</code> to convert to <code>String</code>.
|
|
|
|
* @param radix the radix (base) to use in the conversion.
|
|
|
|
* @return the <code>String</code> representation of the argument.
|
|
|
|
*/
|
|
|
|
public static String toString(int num, int radix)
|
|
|
|
{
|
2001-07-30 19:58:32 +02:00
|
|
|
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
|
|
|
|
radix = 10;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
// For negative numbers, print out the absolute value w/ a leading '-'.
|
|
|
|
// Use an array large enough for a binary number.
|
|
|
|
char[] buffer = new char[33];
|
|
|
|
int i = 33;
|
|
|
|
boolean isNeg;
|
|
|
|
if (num < 0)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
|
|
|
isNeg = true;
|
2001-07-23 22:01:29 +02:00
|
|
|
num = -(num);
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
// When the value is MIN_VALUE, it overflows when made positive
|
|
|
|
if (num < 0)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
buffer[--i] = Character.forDigit(-(num + radix) % radix, radix);
|
|
|
|
num = -(num / radix);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
}
|
2001-07-23 22:01:29 +02:00
|
|
|
else
|
|
|
|
isNeg = false;
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
buffer[--i] = Character.forDigit(num % radix, radix);
|
|
|
|
num /= radix;
|
|
|
|
}
|
|
|
|
while (num > 0);
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
if (isNeg)
|
|
|
|
buffer[--i] = '-';
|
1999-04-07 16:42:40 +02:00
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
return String.valueOf(buffer, i, 33-i);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Creates a new <code>Integer</code> object using the <code>String</code>,
|
|
|
|
* assuming a radix of 10.
|
|
|
|
* @param s the <code>String</code> to convert.
|
|
|
|
* @return the new <code>Integer</code>.
|
|
|
|
* @see #Integer(java.lang.String)
|
|
|
|
* @see #parseInt(java.lang.String)
|
|
|
|
* @exception NumberFormatException thrown if the <code>String</code>
|
|
|
|
* cannot be parsed as an <code>int</code>.
|
|
|
|
*/
|
|
|
|
public static Integer valueOf(String s) throws NumberFormatException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return new Integer(parseInt(s));
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Creates a new <code>Integer</code> object using the <code>String</code>
|
|
|
|
* and specified radix (base).
|
|
|
|
* @param s the <code>String</code> to convert.
|
|
|
|
* @param radix the radix (base) to convert with.
|
|
|
|
* @return the new <code>Integer</code>.
|
|
|
|
* @see #parseInt(java.lang.String,int)
|
|
|
|
* @exception NumberFormatException thrown if the <code>String</code>
|
|
|
|
* cannot be parsed as an <code>int</code>.
|
|
|
|
*/
|
|
|
|
public static Integer valueOf(String s, int radix)
|
|
|
|
throws NumberFormatException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return new Integer(parseInt(s, radix));
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the specified <code>String</code> into an <code>int</code>.
|
|
|
|
* This function assumes a radix of 10.
|
|
|
|
*
|
|
|
|
* @param s the <code>String</code> to convert
|
|
|
|
* @return the <code>int</code> value of the <code>String</code>
|
|
|
|
* argument.
|
|
|
|
* @exception NumberFormatException thrown if the <code>String</code>
|
|
|
|
* cannot be parsed as an <code>int</code>.
|
|
|
|
*/
|
|
|
|
public static int parseInt(String s) throws NumberFormatException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return parseInt(s, 10);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Converts the specified <code>String</code> into an <code>int</code>
|
|
|
|
* using the specified radix (base).
|
|
|
|
*
|
|
|
|
* @param s the <code>String</code> to convert
|
|
|
|
* @param radix the radix (base) to use in the conversion
|
|
|
|
* @return the <code>String</code> argument converted to </code>int</code>.
|
|
|
|
* @exception NumberFormatException thrown if the <code>String</code>
|
|
|
|
* cannot be parsed as a <code>int</code>.
|
|
|
|
*/
|
|
|
|
public static int parseInt(String str, int radix)
|
|
|
|
throws NumberFormatException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
|
|
|
final int len;
|
|
|
|
|
2001-11-16 20:52:24 +01:00
|
|
|
if (str == null)
|
|
|
|
throw new NumberFormatException ();
|
|
|
|
|
2001-02-09 03:56:38 +01:00
|
|
|
if ((len = str.length()) == 0 ||
|
1999-04-07 16:42:40 +02:00
|
|
|
radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
|
|
|
|
throw new NumberFormatException();
|
|
|
|
|
|
|
|
boolean isNeg = false;
|
|
|
|
int index = 0;
|
|
|
|
if (str.charAt(index) == '-')
|
|
|
|
if (len > 1)
|
|
|
|
{
|
|
|
|
isNeg = true;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
throw new NumberFormatException();
|
|
|
|
|
|
|
|
return parseInt(str, index, len, isNeg, radix);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int parseInt(String str, int index, int len, boolean isNeg,
|
2001-07-23 22:01:29 +02:00
|
|
|
int radix)
|
|
|
|
throws NumberFormatException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
|
|
|
int val = 0;
|
|
|
|
int digval;
|
|
|
|
|
1999-04-08 13:57:28 +02:00
|
|
|
int max = MAX_VALUE / radix;
|
|
|
|
// We can't directly write `max = (MAX_VALUE + 1) / radix'.
|
|
|
|
// So instead we fake it.
|
|
|
|
if (isNeg && MAX_VALUE % radix == radix - 1)
|
|
|
|
++max;
|
|
|
|
|
1999-04-07 16:42:40 +02:00
|
|
|
for ( ; index < len; index++)
|
|
|
|
{
|
1999-04-08 13:57:28 +02:00
|
|
|
if (val < 0 || val > max)
|
1999-04-07 16:42:40 +02:00
|
|
|
throw new NumberFormatException();
|
|
|
|
|
|
|
|
if ((digval = Character.digit(str.charAt(index), radix)) < 0)
|
|
|
|
throw new NumberFormatException();
|
|
|
|
|
|
|
|
// Throw an exception for overflow if result is negative.
|
|
|
|
// However, we special-case the most negative value.
|
1999-04-08 13:57:28 +02:00
|
|
|
val = val * radix + digval;
|
|
|
|
if (val < 0 && (! isNeg || val != MIN_VALUE))
|
|
|
|
throw new NumberFormatException();
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return isNeg ? -(val) : val;
|
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Convert the specified <code>String</code> into an <code>Integer</code>.
|
|
|
|
* The <code>String</code> may represent decimal, hexadecimal, or
|
|
|
|
* octal numbers.
|
|
|
|
*
|
|
|
|
* The <code>String</code> argument is interpreted based on the leading
|
|
|
|
* characters. Depending on what the String begins with, the base will be
|
|
|
|
* interpreted differently:
|
|
|
|
*
|
|
|
|
* <table border=1>
|
|
|
|
* <tr><th>Leading<br>Characters</th><th>Base</th></tr>
|
|
|
|
* <tr><td>#</td><td>16</td></tr>
|
|
|
|
* <tr><td>0x</td><td>16</td></tr>
|
|
|
|
* <tr><td>0X</td><td>16</td></tr>
|
|
|
|
* <tr><td>0</td><td>8</td></tr>
|
|
|
|
* <tr><td>Anything<br>Else</td><td>10</td></tr>
|
|
|
|
* </table>
|
|
|
|
*
|
|
|
|
* @param str the <code>String</code> to interpret.
|
|
|
|
* @return the value of the String as an <code>Integer</code>.
|
|
|
|
* @exception NumberFormatException thrown if the <code>String</code>
|
|
|
|
* cannot be parsed as an <code>int</code>.
|
|
|
|
*/
|
|
|
|
public static Integer decode(String str) throws NumberFormatException
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
boolean isNeg = false;
|
|
|
|
int index = 0;
|
|
|
|
int radix = 10;
|
|
|
|
final int len;
|
|
|
|
|
|
|
|
if (str == null || (len = str.length()) == 0)
|
|
|
|
throw new NumberFormatException("string null or empty");
|
|
|
|
|
|
|
|
// Negative numbers are always radix 10.
|
|
|
|
if (str.charAt(index) == '-')
|
|
|
|
{
|
|
|
|
radix = 10;
|
|
|
|
index++;
|
|
|
|
isNeg = true;
|
|
|
|
}
|
|
|
|
else if (str.charAt(index) == '#')
|
|
|
|
{
|
|
|
|
radix = 16;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
else if (str.charAt(index) == '0')
|
|
|
|
{
|
|
|
|
// Check if str is just "0"
|
|
|
|
if (len == 1)
|
|
|
|
return new Integer(0);
|
|
|
|
|
|
|
|
index++;
|
|
|
|
if (str.charAt(index) == 'x' || str.charAt(index) == 'X')
|
|
|
|
{
|
|
|
|
radix = 16;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
radix = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (index >= len)
|
|
|
|
throw new NumberFormatException("empty value");
|
|
|
|
|
|
|
|
return new Integer(parseInt(str, index, len, isNeg, radix));
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/** Return the value of this <code>Integer</code> as a <code>byte</code>.
|
|
|
|
** @return the value of this <code>Integer</code> as a <code>byte</code>.
|
|
|
|
**/
|
|
|
|
public byte byteValue()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return (byte) value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/** Return the value of this <code>Integer</code> as a <code>short</code>.
|
|
|
|
** @return the value of this <code>Integer</code> as a <code>short</code>.
|
|
|
|
**/
|
|
|
|
public short shortValue()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return (short) value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/** Return the value of this <code>Integer</code> as an <code>int</code>.
|
|
|
|
** @return the value of this <code>Integer</code> as an <code>int</code>.
|
|
|
|
**/
|
|
|
|
public int intValue()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/** Return the value of this <code>Integer</code> as a <code>long</code>.
|
|
|
|
** @return the value of this <code>Integer</code> as a <code>long</code>.
|
|
|
|
**/
|
|
|
|
public long longValue()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/** Return the value of this <code>Integer</code> as a <code>float</code>.
|
|
|
|
** @return the value of this <code>Integer</code> as a <code>float</code>.
|
|
|
|
**/
|
|
|
|
public float floatValue()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/** Return the value of this <code>Integer</code> as a <code>double</code>.
|
|
|
|
** @return the value of this <code>Integer</code> as a <code>double</code>.
|
|
|
|
**/
|
|
|
|
public double doubleValue()
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return value;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Compare two Integers numerically by comparing their
|
|
|
|
* <code>int</code> values.
|
|
|
|
* @return a positive value if this <code>Integer</code> is greater
|
|
|
|
* in value than the argument <code>Integer</code>; a negative value
|
|
|
|
* if this <code>Integer</code> is smaller in value than the argument
|
|
|
|
* <code>Integer</code>; and <code>0</code>, zero, if this
|
|
|
|
* <code>Integer</code> is equal in value to the argument
|
|
|
|
* <code>Integer</code>.
|
|
|
|
*
|
|
|
|
* @since 1.2
|
|
|
|
*/
|
|
|
|
public int compareTo(Integer i)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
if (this.value == i.value)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Returns just -1 or 1 on inequality; doing math might overflow.
|
|
|
|
if (this.value > i.value)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return -1;
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
|
2001-07-23 22:01:29 +02:00
|
|
|
/**
|
|
|
|
* Behaves like <code>compareTo(java.lang.Integer)</code> unless the Object
|
|
|
|
* is not a <code>Integer</code>. Then it throws a
|
|
|
|
* <code>ClassCastException</code>.
|
|
|
|
* @exception ClassCastException if the argument is not a
|
|
|
|
* <code>Integer</code>.
|
|
|
|
*
|
|
|
|
* @since 1.2
|
|
|
|
*/
|
|
|
|
public int compareTo(Object o)
|
1999-04-07 16:42:40 +02:00
|
|
|
{
|
2001-07-23 22:01:29 +02:00
|
|
|
return compareTo((Integer)o);
|
1999-04-07 16:42:40 +02:00
|
|
|
}
|
|
|
|
}
|