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

  1. package java.lang;
  2.  
  3. public final class Integer extends Number {
  4.    public static final int MIN_VALUE = -2147483648;
  5.    public static final int MAX_VALUE = 2147483647;
  6.    private int value;
  7.  
  8.    public static String toString(int i, int radix) {
  9.       if (radix < 2 || radix > 36) {
  10.          radix = 10;
  11.       }
  12.  
  13.       StringBuffer buf = new StringBuffer(radix >= 8 ? 12 : 33);
  14.       boolean negative = i < 0;
  15.       if (!negative) {
  16.          i = -i;
  17.       }
  18.  
  19.       while(i <= -radix) {
  20.          buf.append(Character.forDigit(-(i % radix), radix));
  21.          i /= radix;
  22.       }
  23.  
  24.       buf.append(Character.forDigit(-i, radix));
  25.       if (negative) {
  26.          buf.append('-');
  27.       }
  28.  
  29.       return buf.reverse().toString();
  30.    }
  31.  
  32.    public static String toHexString(int i) {
  33.       return toUnsignedString(i, 4);
  34.    }
  35.  
  36.    public static String toOctalString(int i) {
  37.       return toUnsignedString(i, 3);
  38.    }
  39.  
  40.    public static String toBinaryString(int i) {
  41.       return toUnsignedString(i, 1);
  42.    }
  43.  
  44.    private static String toUnsignedString(int i, int shift) {
  45.       StringBuffer buf = new StringBuffer(shift >= 3 ? 11 : 32);
  46.       int radix = 1 << shift;
  47.       int mask = radix - 1;
  48.  
  49.       do {
  50.          buf.append(Character.forDigit(i & mask, radix));
  51.          i >>>= shift;
  52.       } while(i != 0);
  53.  
  54.       return buf.reverse().toString();
  55.    }
  56.  
  57.    public static String toString(int i) {
  58.       return toString(i, 10);
  59.    }
  60.  
  61.    public static int parseInt(String s, int radix) throws NumberFormatException {
  62.       if (s == null) {
  63.          throw new NumberFormatException("null");
  64.       } else {
  65.          int result = 0;
  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 * radix + digit;
  84.             }
  85.  
  86.             return negative ? -result : result;
  87.          }
  88.       }
  89.    }
  90.  
  91.    public static int parseInt(String s) throws NumberFormatException {
  92.       return parseInt(s, 10);
  93.    }
  94.  
  95.    public static Integer valueOf(String s, int radix) throws NumberFormatException {
  96.       return new Integer(parseInt(s, radix));
  97.    }
  98.  
  99.    public static Integer valueOf(String s) throws NumberFormatException {
  100.       return new Integer(parseInt(s, 10));
  101.    }
  102.  
  103.    public Integer(int value) {
  104.       this.value = value;
  105.    }
  106.  
  107.    public Integer(String s) throws NumberFormatException {
  108.       this.value = parseInt(s, 10);
  109.    }
  110.  
  111.    public int intValue() {
  112.       return this.value;
  113.    }
  114.  
  115.    public long longValue() {
  116.       return (long)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 this.value;
  133.    }
  134.  
  135.    public boolean equals(Object obj) {
  136.       if (obj != null && obj instanceof Integer) {
  137.          return this.value == (Integer)obj;
  138.       } else {
  139.          return false;
  140.       }
  141.    }
  142.  
  143.    public static Integer getInteger(String nm) {
  144.       return getInteger(nm, (Integer)null);
  145.    }
  146.  
  147.    public static Integer getInteger(String nm, int val) {
  148.       Integer result = getInteger(nm, (Integer)null);
  149.       return result == null ? new Integer(val) : result;
  150.    }
  151.  
  152.    public static Integer getInteger(String nm, Integer val) {
  153.       SecurityManager.setScopePermission();
  154.       String v = System.getProperty(nm);
  155.       if (v != null) {
  156.          try {
  157.             return decode(v);
  158.          } catch (NumberFormatException var3) {
  159.          }
  160.       }
  161.  
  162.       return val;
  163.    }
  164.  
  165.    private static Integer decode(String nm) throws NumberFormatException {
  166.       if (nm.startsWith("0x")) {
  167.          return valueOf(nm.substring(2), 16);
  168.       } else if (nm.startsWith("#")) {
  169.          return valueOf(nm.substring(1), 16);
  170.       } else {
  171.          return nm.startsWith("0") && nm.length() > 1 ? valueOf(nm.substring(1), 8) : valueOf(nm);
  172.       }
  173.    }
  174. }
  175.