Back to index

plt-scheme  4.2.1
Long.java
Go to the documentation of this file.
00001 /* Long.java -- object wrapper for long
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 Long extends Number implements Comparable
00056 {
00060   private static final long serialVersionUID = 4290774380558885855L;
00061 
00066   public static final long MIN_VALUE = 0x8000000000000000L;
00067 
00072   public static final long MAX_VALUE = 0x7fffffffffffffffL;
00073 
00079   //public static final Class TYPE = VMClassLoader.getPrimitiveClass ('J');
00080 
00086   private final long value;
00087 
00094   public Long(long value)
00095   {
00096     this.value = value;
00097   }
00098 
00107   public Long(String s)
00108   {
00109     value = parseLong(s, 10, false);
00110   }
00111 
00124   public static String toString(long num, int radix)
00125   {
00126     // Use the Integer toString for efficiency if possible.
00127     if ((int) num == num)
00128       return Integer.toString((int) num, radix);
00129 
00130     if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
00131       radix = 10;
00132 
00133     // For negative numbers, print out the absolute value w/ a leading '-'.
00134     // Use an array large enough for a binary number.
00135     char[] buffer = new char[65];
00136     int i = 65;
00137     boolean isNeg = false;
00138     if (num < 0)
00139       {
00140         isNeg = true;
00141         num = -num;
00142 
00143         // When the value is MIN_VALUE, it overflows when made positive
00144         if (num < 0)
00145          {
00146            buffer[--i] = digits[(int) (-(num + radix) % radix)];
00147            num = -(num / radix);
00148          }
00149       }
00150 
00151     do
00152       {
00153         buffer[--i] = digits[(int) (num % radix)];
00154         num /= radix;
00155       }
00156     while (num > 0);
00157 
00158     if (isNeg)
00159       buffer[--i] = '-';
00160 
00161     // Package constructor avoids an array copy.
00162     return new String(buffer, i, 65 - i);
00163   }
00164 
00172   public static String toHexString(long l)
00173   {
00174     return toUnsignedString(l, 4);
00175   }
00176 
00184   public static String toOctalString(long l)
00185   {
00186     return toUnsignedString(l, 3);
00187   }
00188 
00196   public static String toBinaryString(long l)
00197   {
00198     return toUnsignedString(l, 1);
00199   }
00200 
00209   public static String toString(long num)
00210   {
00211     return toString(num, 10);
00212   }
00213 
00232   public static long parseLong(String str, int radix)
00233   {
00234     return parseLong(str, radix, false);
00235   }
00236 
00247   public static long parseLong(String s)
00248   {
00249     return parseLong(s, 10, false);
00250   }
00251 
00263   public static Long valueOf(String s, int radix)
00264   {
00265     return new Long(parseLong(s, radix, false));
00266   }
00267 
00279   public static Long valueOf(String s)
00280   {
00281     return new Long(parseLong(s, 10, false));
00282   }
00283 
00316   public static Long decode(String str)
00317   {
00318     return new Long(parseLong(str, 10, true));
00319   }
00320 
00326   public byte byteValue()
00327   {
00328     return (byte) value;
00329   }
00330 
00336   public short shortValue()
00337   {
00338     return (short) value;
00339   }
00340 
00346   public int intValue()
00347   {
00348     return (int) value;
00349   }
00350 
00356   public long longValue()
00357   {
00358     return value;
00359   }
00360 
00366   public float floatValue()
00367   {
00368     return value;
00369   }
00370 
00376   public double doubleValue()
00377   {
00378     return value;
00379   }
00380 
00387   public String toString()
00388   {
00389     return toString(value, 10);
00390   }
00391 
00398   public int hashCode()
00399   {
00400     return (int) (value ^ (value >>> 32));
00401   }
00402 
00410   public boolean equals(Object obj)
00411   {
00412     return obj instanceof Long && value == ((Long) obj).value;
00413   }
00414 
00427   public static Long getLong(String nm)
00428   {
00429     return getLong(nm, null);
00430   }
00431 
00445   public static Long getLong(String nm, long val)
00446   {
00447     Long result = getLong(nm, null);
00448     return result == null ? new Long(val) : result;
00449   }
00450 
00464   public static Long getLong(String nm, Long def)
00465   {
00466     if (nm == null || "".equals(nm))
00467       return def;
00468     else
00469       return def;
00470     /*nm = System.getProperty(nm);
00471     if (nm == null)
00472       return def;
00473     try
00474       {
00475         return decode(nm);
00476       }
00477     catch (NumberFormatException e)
00478       {
00479         return def;
00480       }*/
00481   }
00482 
00492   public int compareTo(Long l)
00493   {
00494     if (value == l.value)
00495       return 0;
00496     // Returns just -1 or 1 on inequality; doing math might overflow the long.
00497     return value > l.value ? 1 : -1;
00498   }
00499 
00511   public int compareTo(Object o)
00512   {
00513     return compareTo((Long) o);
00514   }
00515 
00522   private static String toUnsignedString(long num, int exp)
00523   {
00524     // Use the Integer toUnsignedString for efficiency if possible.
00525     // If NUM<0 then this particular optimization doesn't work
00526     // properly.
00527     if (num >= 0 && (int) num == num)
00528       return Integer.toUnsignedString((int) num, exp);
00529 
00530     // Use an array large enough for a binary number.
00531     int mask = (1 << exp) - 1;
00532     char[] buffer = new char[64];
00533     int i = 64;
00534     do
00535       {
00536         buffer[--i] = digits[(int) num & mask];
00537         num >>>= exp;
00538       }
00539     while (num != 0);
00540 
00541     // Package constructor avoids an array copy.
00542     return new String(buffer, i, 64 - i);
00543   }
00544 
00557   private static long parseLong(String str, int radix, boolean decode)
00558   {
00559     if (! decode && str == null)
00560       throw new NumberFormatException();
00561     int index = 0;
00562     int len = str.length();
00563     boolean isNeg = false;
00564     if (len == 0)
00565       throw new NumberFormatException();
00566     int ch = str.charAt(index);
00567     if (ch == '-')
00568       {
00569         if (len == 1)
00570           throw new NumberFormatException();
00571         isNeg = true;
00572         ch = str.charAt(++index);
00573       }
00574     if (decode)
00575       {
00576         if (ch == '0')
00577           {
00578             if (++index == len)
00579               return 0;
00580             if ((str.charAt(index) & ~('x' ^ 'X')) == 'X')
00581               {
00582                 radix = 16;
00583                 index++;
00584               }
00585             else
00586               radix = 8;
00587           }
00588         else if (ch == '#')
00589           {
00590             radix = 16;
00591             index++;
00592           }
00593       }
00594     if (index == len)
00595       throw new NumberFormatException();
00596 
00597     long max = MAX_VALUE / radix;
00598     // We can't directly write `max = (MAX_VALUE + 1) / radix'.
00599     // So instead we fake it.
00600     if (isNeg && MAX_VALUE % radix == radix - 1)
00601       ++max;
00602 
00603     long val = 0;
00604     while (index < len)
00605       {
00606        if (val < 0 || val > max)
00607          throw new NumberFormatException();
00608 
00609         ch = Character.digit(str.charAt(index++), radix);
00610         val = val * radix + ch;
00611         if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE)))
00612           throw new NumberFormatException();
00613       }
00614     return isNeg ? -val : val;
00615   }
00616 }