X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=libjava%2Fjava%2Flang%2FInteger.java;fp=libjava%2Fjava%2Flang%2FInteger.java;h=0000000000000000000000000000000000000000;hb=6fed43773c9b0ce596dca5686f37ac3fc0fa11c0;hp=d513f348184525d840baacde61a7c833e18b78d4;hpb=27b11d56b743098deb193d510b337ba22dc52e5c;p=msp430-gcc.git diff --git a/libjava/java/lang/Integer.java b/libjava/java/lang/Integer.java deleted file mode 100644 index d513f348..00000000 --- a/libjava/java/lang/Integer.java +++ /dev/null @@ -1,592 +0,0 @@ -/* java.lang.Integer - Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc. - -This file is part of GNU Classpath. - -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. - -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. - -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. */ - - -package java.lang; - -/** - * Instances of class Integer represent primitive - * int 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 - */ -public final class Integer extends Number implements Comparable -{ - // compatible with JDK 1.0.2+ - private static final long serialVersionUID = 1360826667806852920L; - - /** - * The minimum value an int can represent is -2147483648. - */ - public static final int MIN_VALUE = 0x80000000; - - /** - * The maximum value an int can represent is 2147483647. - */ - public static final int MAX_VALUE = 0x7fffffff; - - /** - * The primitive type int is represented by this - * Class object. - */ - public static final Class TYPE = VMClassLoader.getPrimitiveClass ('I'); - - /** - * The immutable value of this Integer. - */ - private final int value; - - /** - * Create an Integer object representing the value of the - * int argument. - * - * @param value the value to use - */ - public Integer(int value) - { - this.value = value; - } - - /** - * Create an Integer object representing the value of the - * argument after conversion to an int. - * - * @param s the string to convert. - */ - public Integer(String s) throws NumberFormatException - { - value = parseInt(s, 10); - } - - /** - * Return a hashcode representing this Object. - * - * Integer's hash code is calculated by simply returning its - * value. - * - * @return this Object's hash code. - */ - public int hashCode() - { - return value; - } - - /** - * If the Object is not null, is an - * instanceof Integer, and represents - * the same primitive int value return - * true. Otherwise false is returned. - */ - public boolean equals(Object obj) - { - return obj instanceof Integer && value == ((Integer)obj).value; - } - - /** - * Get the specified system property as an Integer. - * - * The decode() 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 Integer, or - * null if the property is not found or cannot be - * decoded as an Integer. - * @see java.lang.System#getProperty(java.lang.String) - * @see #decode(int) - */ - public static Integer getInteger(String nm) - { - return getInteger(nm, null); - } - - /** - * Get the specified system property as an Integer, or use a - * default int value if the property is not found or is not - * decodable. - * - * The decode() 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 Integer, or the default - * value if the property is not found or cannot be decoded as an - * Integer. - * @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) - { - Integer result = getInteger(nm, null); - return (result == null) ? new Integer(val) : result; - } - - /** - * Get the specified system property as an Integer, or use a - * default Integer value if the property is not found or is - * not decodable. - * - * The decode() 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 Integer, or the default - * value if the property is not found or cannot be decoded as an - * Integer. - * @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) - { - if (nm == null || "".equals(nm)) - return def; - nm = System.getProperty(nm); - if (nm == null) return def; - try - { - return decode(nm); - } - catch (NumberFormatException e) - { - return def; - } - } - - private static String toUnsignedString(int num, int exp) - { - // 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); - } - - /** - * Converts the int to a String assuming it is - * unsigned in base 16. - * @param i the int to convert to String - * @return the String representation of the argument. - */ - public static String toHexString(int i) - { - return toUnsignedString(i, 4); - } - - /** - * Converts the int to a String assuming it is - * unsigned in base 8. - * @param i the int to convert to String - * @return the String representation of the argument. - */ - public static String toOctalString(int i) - { - return toUnsignedString(i, 3); - } - - /** - * Converts the int to a String assuming it is - * unsigned in base 2. - * @param i the int to convert to String - * @return the String representation of the argument. - */ - public static String toBinaryString(int i) - { - return toUnsignedString(i, 1); - } - - /** - * Converts the int to a String and assumes - * a radix of 10. - * @param i the int to convert to String - * @return the String representation of the argument. - */ - public static String toString(int i) - { - // 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); - } - - /** - * Converts the Integer value to a String and - * assumes a radix of 10. - * @return the String representation of this Integer. - */ - public String toString() - { - return toString (value); - } - - /** - * Converts the int to a String using - * the specified radix (base). - * @param i the int to convert to String. - * @param radix the radix (base) to use in the conversion. - * @return the String representation of the argument. - */ - public static String toString(int num, int radix) - { - if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) - radix = 10; - - // 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) - { - isNeg = true; - num = -(num); - - // When the value is MIN_VALUE, it overflows when made positive - if (num < 0) - { - buffer[--i] = Character.forDigit(-(num + radix) % radix, radix); - num = -(num / radix); - } - } - else - isNeg = false; - - do - { - buffer[--i] = Character.forDigit(num % radix, radix); - num /= radix; - } - while (num > 0); - - if (isNeg) - buffer[--i] = '-'; - - return String.valueOf(buffer, i, 33-i); - } - - /** - * Creates a new Integer object using the String, - * assuming a radix of 10. - * @param s the String to convert. - * @return the new Integer. - * @see #Integer(java.lang.String) - * @see #parseInt(java.lang.String) - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. - */ - public static Integer valueOf(String s) throws NumberFormatException - { - return new Integer(parseInt(s)); - } - - /** - * Creates a new Integer object using the String - * and specified radix (base). - * @param s the String to convert. - * @param radix the radix (base) to convert with. - * @return the new Integer. - * @see #parseInt(java.lang.String,int) - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. - */ - public static Integer valueOf(String s, int radix) - throws NumberFormatException - { - return new Integer(parseInt(s, radix)); - } - - /** - * Converts the specified String into an int. - * This function assumes a radix of 10. - * - * @param s the String to convert - * @return the int value of the String - * argument. - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. - */ - public static int parseInt(String s) throws NumberFormatException - { - return parseInt(s, 10); - } - - /** - * Converts the specified String into an int - * using the specified radix (base). - * - * @param s the String to convert - * @param radix the radix (base) to use in the conversion - * @return the String argument converted to int. - * @exception NumberFormatException thrown if the String - * cannot be parsed as a int. - */ - public static int parseInt(String str, int radix) - throws NumberFormatException - { - final int len; - - if (str == null) - throw new NumberFormatException (); - - if ((len = str.length()) == 0 || - 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, - int radix) - throws NumberFormatException - { - int val = 0; - int digval; - - 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; - - for ( ; index < len; index++) - { - if (val < 0 || val > max) - 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. - val = val * radix + digval; - if (val < 0 && (! isNeg || val != MIN_VALUE)) - throw new NumberFormatException(); - } - - return isNeg ? -(val) : val; - } - - /** - * Convert the specified String into an Integer. - * The String may represent decimal, hexadecimal, or - * octal numbers. - * - * The String argument is interpreted based on the leading - * characters. Depending on what the String begins with (after an optional - * minus sign), the base will be interpreted differently: - * - * - * - * - * - * - * - * - *
Leading
Characters
Base
#16
0x16
0X16
08
Anything
Else
10
- * - * If the String starts with a minus sign the result is negated. - * - * @param str the String to interpret. - * @return the value of the String as an Integer. - * @exception NumberFormatException thrown if the String - * cannot be parsed as an int. - */ - public static Integer decode(String str) throws NumberFormatException - { - boolean isNeg = false; - int index = 0; - int radix = 10; - final int len; - - if ((len = str.length()) == 0) - throw new NumberFormatException("empty string"); - - if (str.charAt(index) == '-') - { - // The minus sign should be followed by at least one more char - if (len > 1) - { - isNeg = true; - index++; - } - else - throw new NumberFormatException(); - } - - if (str.charAt(index) == '#') - { - radix = 16; - index++; - } - else if (str.charAt(index) == '0') - { - index++; - - // Check if str is just "0" or "-0" - if (len == index) - return new Integer(0); - - 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)); - } - - /** Return the value of this Integer as a byte. - ** @return the value of this Integer as a byte. - **/ - public byte byteValue() - { - return (byte) value; - } - - /** Return the value of this Integer as a short. - ** @return the value of this Integer as a short. - **/ - public short shortValue() - { - return (short) value; - } - - /** Return the value of this Integer as an int. - ** @return the value of this Integer as an int. - **/ - public int intValue() - { - return value; - } - - /** Return the value of this Integer as a long. - ** @return the value of this Integer as a long. - **/ - public long longValue() - { - return value; - } - - /** Return the value of this Integer as a float. - ** @return the value of this Integer as a float. - **/ - public float floatValue() - { - return value; - } - - /** Return the value of this Integer as a double. - ** @return the value of this Integer as a double. - **/ - public double doubleValue() - { - return value; - } - - /** - * Compare two Integers numerically by comparing their - * int values. - * @return a positive value if this Integer is greater - * in value than the argument Integer; a negative value - * if this Integer is smaller in value than the argument - * Integer; and 0, zero, if this - * Integer is equal in value to the argument - * Integer. - * - * @since 1.2 - */ - public int compareTo(Integer i) - { - 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; - } - - /** - * Behaves like compareTo(java.lang.Integer) unless the Object - * is not a Integer. Then it throws a - * ClassCastException. - * @exception ClassCastException if the argument is not a - * Integer. - * - * @since 1.2 - */ - public int compareTo(Object o) - { - return compareTo((Integer)o); - } -}