home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1998 January / PCO0198.ISO / 1&1 / java.z / java_301 / java / lang / Long.class (.txt) < prev    next >
Encoding:
Java Class File  |  1996-10-20  |  4.0 KB  |  178 lines

  1. package java.lang;
  2.  
  3. public final class Long extends Number {
  4.    public static final long MIN_VALUE = -9223372036854775808L;
  5.    public static final long MAX_VALUE = 9223372036854775807L;
  6.    private long value;
  7.  
  8.    public static String toString(long i, int radix) {
  9.       if (radix < 2 || radix > 36) {
  10.          radix = 10;
  11.       }
  12.  
  13.       StringBuffer buf = new StringBuffer(radix >= 8 ? 23 : 65);
  14.       boolean negative = i < 0L;
  15.       if (!negative) {
  16.          i = -i;
  17.       }
  18.  
  19.       while(i <= (long)(-radix)) {
  20.          buf.append(Character.forDigit((int)(-(i % (long)radix)), radix));
  21.          i /= (long)radix;
  22.       }
  23.  
  24.       buf.append(Character.forDigit((int)(-i), radix));
  25.       if (negative) {
  26.          buf.append('-');
  27.       }
  28.  
  29.       return buf.reverse().toString();
  30.    }
  31.  
  32.    public static String toHexString(long i) {
  33.       return toUnsignedString(i, 4);
  34.    }
  35.  
  36.    public static String toOctalString(long i) {
  37.       return toUnsignedString(i, 3);
  38.    }
  39.  
  40.    public static String toBinaryString(long i) {
  41.       return toUnsignedString(i, 1);
  42.    }
  43.  
  44.    private static String toUnsignedString(long i, int shift) {
  45.       StringBuffer buf = new StringBuffer(shift >= 3 ? 22 : 64);
  46.       int radix = 1 << shift;
  47.       long mask = (long)(radix - 1);
  48.  
  49.       do {
  50.          buf.append(Character.forDigit((int)(i & mask), radix));
  51.          i >>>= shift;
  52.       } while(i != 0L);
  53.  
  54.       return buf.reverse().toString();
  55.    }
  56.  
  57.    public static String toString(long i) {
  58.       return toString(i, 10);
  59.    }
  60.  
  61.    public static long parseLong(String s, int radix) throws NumberFormatException {
  62.       if (s == null) {
  63.          throw new NumberFormatException("null");
  64.       } else {
  65.          long result = 0L;
  66.          boolean negative = false;
  67.          int i = 0;
  68.          int max = s.length();
  69.          if (max <= 0) {
  70.             throw new NumberFormatException(s);
  71.          } else {
  72.             if (s.charAt(0) == '-') {
  73.                negative = true;
  74.                ++i;
  75.             }
  76.  
  77.             while(i < max) {
  78.                int digit = Character.digit(s.charAt(i++), radix);
  79.                if (digit < 0) {
  80.                   throw new NumberFormatException(s);
  81.                }
  82.  
  83.                result = result * (long)radix + (long)digit;
  84.             }
  85.  
  86.             return negative ? -result : result;
  87.          }
  88.       }
  89.    }
  90.  
  91.    public static long parseLong(String s) throws NumberFormatException {
  92.       return parseLong(s, 10);
  93.    }
  94.  
  95.    public static Long valueOf(String s, int radix) throws NumberFormatException {
  96.       return new Long(parseLong(s, radix));
  97.    }
  98.  
  99.    public static Long valueOf(String s) throws NumberFormatException {
  100.       return new Long(parseLong(s, 10));
  101.    }
  102.  
  103.    public Long(long value) {
  104.       this.value = value;
  105.    }
  106.  
  107.    public Long(String s) throws NumberFormatException {
  108.       this.value = parseLong(s, 10);
  109.    }
  110.  
  111.    public int intValue() {
  112.       return (int)this.value;
  113.    }
  114.  
  115.    public long longValue() {
  116.       return this.value;
  117.    }
  118.  
  119.    public float floatValue() {
  120.       return (float)this.value;
  121.    }
  122.  
  123.    public double doubleValue() {
  124.       return (double)this.value;
  125.    }
  126.  
  127.    public String toString() {
  128.       return String.valueOf(this.value);
  129.    }
  130.  
  131.    public int hashCode() {
  132.       return (int)(this.value ^ this.value >> 32);
  133.    }
  134.  
  135.    public boolean equals(Object obj) {
  136.       if (obj != null && obj instanceof Long) {
  137.          return this.value == (Long)obj;
  138.       } else {
  139.          return false;
  140.       }
  141.    }
  142.  
  143.    public static Long getLong(String nm) {
  144.       return getLong(nm, (Long)null);
  145.    }
  146.  
  147.    public static Long getLong(String nm, long val) {
  148.       Long result = getLong(nm, (Long)null);
  149.       return result == null ? new Long(val) : result;
  150.    }
  151.  
  152.    public static Long getLong(String nm, Long val) {
  153.       SecurityManager.setScopePermission();
  154.       String v = System.getProperty(nm);
  155.       SecurityManager.resetScopePermission();
  156.       if (v != null) {
  157.          try {
  158.             if (v.startsWith("0x")) {
  159.                return valueOf(v.substring(2), 16);
  160.             }
  161.  
  162.             if (v.startsWith("#")) {
  163.                return valueOf(v.substring(1), 16);
  164.             }
  165.  
  166.             if (v.startsWith("0") && v.length() > 1) {
  167.                return valueOf(v.substring(1), 8);
  168.             }
  169.  
  170.             return valueOf(v);
  171.          } catch (NumberFormatException var3) {
  172.          }
  173.       }
  174.  
  175.       return val;
  176.    }
  177. }
  178.