Back to index

plt-scheme  4.2.1
Integer.java
Go to the documentation of this file.
00001 /* Integer.java -- object wrapper for int
00002    Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
00003 
00004 This file is part of GNU Classpath.
00005 
00006 GNU Classpath is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU General Public License as published by
00008 the Free Software Foundation; either version 2, or (at your option)
00009 any later version.
00010 
00011 GNU Classpath is distributed in the hope that it will be useful, but
00012 WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 General Public License for more details.
00015 
00016 You should have received a copy of the GNU General Public License
00017 along with GNU Classpath; see the file COPYING.  If not, write to the
00018 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019 02111-1307 USA.
00020 
00021 Linking this library statically or dynamically with other modules is
00022 making a combined work based on this library.  Thus, the terms and
00023 conditions of the GNU General Public License cover the whole
00024 combination.
00025 
00026 As a special exception, the copyright holders of this library give you
00027 permission to link this library with independent modules to produce an
00028 executable, regardless of the license terms of these independent
00029 modules, and to copy and distribute the resulting executable under
00030 terms of your choice, provided that you also meet, for each linked
00031 independent module, the terms and conditions of the license of that
00032 module.  An independent module is a module which is not derived from
00033 or based on this library.  If you modify this library, you may extend
00034 this exception to your version of the library, but you are not
00035 obligated to do so.  If you do not wish to do so, delete this
00036 exception statement from your version. */
00037 
00038 
00039 package java.lang;
00040 
00055 public final class Integer extends Number implements Comparable
00056 {
00060   private static final long serialVersionUID = 1360826667806852920L;
00061 
00066   public static final int MIN_VALUE = 0x80000000;
00067 
00072   public static final int MAX_VALUE = 0x7fffffff;
00073 
00079   //public static final Class TYPE = VMClassLoader.getPrimitiveClass('I');
00080 
00086   private final int value;
00087 
00094   public Integer(int value)
00095   {
00096     this.value = value;
00097   }
00098 
00107   public Integer(String s)
00108   {
00109     value = parseInt(s, 10, false);
00110   }
00111 
00124   public static String toString(int num, int radix)
00125   {
00126     if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
00127       radix = 10;
00128 
00129     // For negative numbers, print out the absolute value w/ a leading '-'.
00130     // Use an array large enough for a binary number.
00131     char[] buffer = new char[33];
00132     int i = 33;
00133     boolean isNeg = false;
00134     if (num < 0)
00135       {
00136         isNeg = true;
00137         num = -num;
00138 
00139         // When the value is MIN_VALUE, it overflows when made positive
00140         if (num < 0)
00141          {
00142            buffer[--i] = digits[(int) (-(num + radix) % radix)];
00143            num = -(num / radix);
00144          }
00145       }
00146 
00147     do
00148       {
00149         buffer[--i] = digits[num % radix];
00150         num /= radix;
00151       }
00152     while (num > 0);
00153 
00154     if (isNeg)
00155       buffer[--i] = '-';
00156 
00157     // Package constructor avoids an array copy.
00158     return new String(buffer, i, 33 - i);
00159   }
00160 
00168   public static String toHexString(int i)
00169   {
00170     return toUnsignedString(i, 4);
00171   }
00172 
00180   public static String toOctalString(int i)
00181   {
00182     return toUnsignedString(i, 3);
00183   }
00184 
00192   public static String toBinaryString(int i)
00193   {
00194     return toUnsignedString(i, 1);
00195   }
00196 
00205   public static String toString(int i)
00206   {
00207     // This is tricky: in libgcj, String.valueOf(int) is a fast native
00208     // implementation.  In Classpath it just calls back to
00209     // Integer.toString(int, int).
00210     return String.valueOf(i);
00211   }
00212 
00229   public static int parseInt(String str, int radix)
00230   {
00231     return parseInt(str, radix, false);
00232   }
00233 
00244   public static int parseInt(String s)
00245   {
00246     return parseInt(s, 10, false);
00247   }
00248 
00260   public static Integer valueOf(String s, int radix)
00261   {
00262     return new Integer(parseInt(s, radix, false));
00263   }
00264 
00276   public static Integer valueOf(String s)
00277   {
00278     return new Integer(parseInt(s, 10, false));
00279   }
00280 
00286   public byte byteValue()
00287   {
00288     return (byte) value;
00289   }
00290 
00296   public short shortValue()
00297   {
00298     return (short) value;
00299   }
00300 
00305   public int intValue()
00306   {
00307     return value;
00308   }
00309 
00315   public long longValue()
00316   {
00317     return value;
00318   }
00319 
00325   public float floatValue()
00326   {
00327     return value;
00328   }
00329 
00335   public double doubleValue()
00336   {
00337     return value;
00338   }
00339 
00346   public String toString()
00347   {
00348     return String.valueOf(value);
00349   }
00350 
00357   public int hashCode()
00358   {
00359     return value;
00360   }
00361 
00369   public boolean equals(Object obj)
00370   {
00371     return obj instanceof Integer && value == ((Integer) obj).value;
00372   }
00373 
00386   public static Integer getInteger(String nm)
00387   {
00388     return getInteger(nm, null);
00389   }
00390 
00404   public static Integer getInteger(String nm, int val)
00405   {
00406     Integer result = getInteger(nm, null);
00407     return result == null ? new Integer(val) : result;
00408   }
00409 
00423   public static Integer getInteger(String nm, Integer def)
00424   {
00425     if (nm == null || "".equals(nm))
00426       return def;
00427     else
00428       return def;
00429     /*nm = System.getProperty(nm);
00430     if (nm == null)
00431       return def;
00432     try
00433       {
00434         return decode(nm);
00435       }
00436     catch (NumberFormatException e)
00437       {
00438         return def;
00439       }*/
00440   }
00441 
00473   public static Integer decode(String str)
00474   {
00475     return new Integer(parseInt(str, 10, true));
00476   }
00477 
00487   public int compareTo(Integer i)
00488   {
00489     if (value == i.value)
00490       return 0;
00491     // Returns just -1 or 1 on inequality; doing math might overflow.
00492     return value > i.value ? 1 : -1;
00493   }
00494 
00506   public int compareTo(Object o)
00507   {
00508     return compareTo((Integer) o);
00509   }
00510 
00517   // Package visible for use by Long.
00518   static String toUnsignedString(int num, int exp)
00519   {
00520     // Use an array large enough for a binary number.
00521     int mask = (1 << exp) - 1;
00522     char[] buffer = new char[32];
00523     int i = 32;
00524     do
00525       {
00526         buffer[--i] = digits[num & mask];
00527         num >>>= exp;
00528       }
00529     while (num != 0);
00530 
00531     // Package constructor avoids an array copy.
00532     return new String(buffer, i, 32 - i);
00533   }
00534 
00549   static int parseInt(String str, int radix, boolean decode)
00550   {
00551     if (! decode && str == null)
00552       throw new NumberFormatException("parseInt cannot convert an empty String into an int");
00553     int index = 0;
00554     int len = str.length();
00555     boolean isNeg = false;
00556     if (len == 0)
00557       throw new NumberFormatException("parseInt cannot convert and empty String into an int");
00558     int ch = (int) str.charAt(index);
00559     if (ch == '-')
00560       {
00561         if (len == 1)
00562           throw new NumberFormatException("parseInt cannot convert - into an int");
00563         isNeg = true;
00564         ch = str.charAt(++index);
00565       }
00566     if (decode)
00567       {
00568         if (ch == '0')
00569           {
00570             if (++index == len)
00571               return 0;
00572             if ((str.charAt(index) & ~('x' ^ 'X')) == 'X')
00573               {
00574                 radix = 16;
00575                 index++;
00576               }
00577             else
00578               radix = 8;
00579           }
00580         else if (ch == '#')
00581           {
00582             radix = 16;
00583             index++;
00584           }
00585       }
00586     if (index == len)
00587       throw new NumberFormatException("parseInt cannot convert " + str + " into an int");
00588 
00589     int max = MAX_VALUE / radix;
00590     // We can't directly write `max = (MAX_VALUE + 1) / radix'.
00591     // So instead we fake it.
00592     if (isNeg && MAX_VALUE % radix == radix - 1)
00593       ++max;
00594 
00595     int val = 0;
00596     while (index < len)
00597       {
00598        if (val < 0 || val > max)
00599          throw new NumberFormatException("Number given to parseInt exceeded allowed size.");
00600 
00601         ch = Character.digit(str.charAt(index++), radix);
00602         val = val * radix + ch;
00603         if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE)))
00604           throw new NumberFormatException("Input given to parseInt contains non-numeric character");
00605       }
00606     return isNeg ? -val : val;
00607   }
00608 }