home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / Integer.java < prev    next >
Text File  |  1997-05-20  |  20KB  |  571 lines

  1. /*
  2.  * @(#)Integer.java    1.40 97/02/03
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  * 
  21.  */
  22.  
  23. package java.lang;
  24.  
  25. /**
  26.  * The Integer class wraps a value of the primitive type <code>int</code> 
  27.  * in an object. An object of type <code>Integer</code> contains a 
  28.  * single field whose type is <code>int</code>. 
  29.  * <p>
  30.  * In addition, this class provides several methods for converting 
  31.  * an <code>int</code> to a <code>String</code> and a 
  32.  * <code>String</code> to an <code>int</code>, as well as other 
  33.  * constants and methods useful when dealing with an 
  34.  * <code>int</code>. 
  35.  *
  36.  * @author  Lee Boynton
  37.  * @author  Arthur van Hoff
  38.  * @version 1.40, 02/03/97
  39.  * @since   JDK1.0
  40.  */
  41. public final
  42. class Integer extends Number {
  43.     /**
  44.      * The smallest value of type <code>int</code>. 
  45.      *
  46.      * @since   JDK1.0
  47.      */
  48.     public static final int   MIN_VALUE = 0x80000000;
  49.  
  50.     /**
  51.      * The largest value of type <code>int</code>. 
  52.      *
  53.      * @since   JDK1.0
  54.      */
  55.     public static final int   MAX_VALUE = 0x7fffffff;
  56.  
  57.     /**
  58.      * The Class object representing the primitive type int.
  59.      *
  60.      * @since   JDK1.1
  61.      */
  62.     public static final Class    TYPE = Class.getPrimitiveClass("int");
  63.  
  64.     /**
  65.      * Creates a string representation of the first argument in the 
  66.      * radix specified by the second argument. 
  67.      * <p>
  68.      * If the radix is smaller than <code>Character.MIN_RADIX</code> or 
  69.      * larger than <code>Character.MAX_RADIX</code>, then the radix 
  70.      * <code>10</code> is used instead. 
  71.      * <p>
  72.      * If the first argument is negative, the first element of the 
  73.      * result is the ASCII minus character <code>'-'</code>. If the first 
  74.      * argument is not negative, no sign character appears in the result. 
  75.      * The following ASCII characters are used as digits: 
  76.      * <ul><code>
  77.      *   0123456789abcdefghijklmnopqrstuvwxyz
  78.      * </code></ul>
  79.      *
  80.      * @param   i       an integer.
  81.      * @param   radix   the radix.
  82.      * @return  a string representation of the argument in the specified radix.
  83.      * @see     java.lang.Character#MAX_RADIX
  84.      * @see     java.lang.Character#MIN_RADIX
  85.      * @since   JDK1.0
  86.      */
  87.     public static String toString(int i, int radix) {
  88.     if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  89.         radix = 10;
  90.     StringBuffer buf = new StringBuffer(radix >= 8 ? 12 : 33);
  91.     boolean negative = (i < 0);
  92.         if (!negative) {
  93.         i = -i;
  94.     }
  95.     while (i <= -radix) {
  96.         buf.append(Character.forDigit(-(i % radix), radix));
  97.         i = i / radix;
  98.     }
  99.     buf.append(Character.forDigit(-i, radix));
  100.     if (negative) {
  101.         buf.append('-');
  102.     }
  103.         return buf.reverse().toString();
  104.     }
  105.  
  106.     /**
  107.      * Creates a string representation of the integer argument as an 
  108.      * unsigned integer in base 16. 
  109.      * <p>
  110.      * The unsigned integer value is the argument plus 2<sup>32</sup> if 
  111.      * the argument is negative; otherwise, it is equal to the argument. 
  112.      * This value is converted to a string of ASCII digits in hexadecimal 
  113.      * (base 16) with no extra leading <code>0</code>s. 
  114.      *
  115.      * @param   i   an integer.
  116.      * @return  the string representation of the unsigned integer value
  117.      *          represented by the argument in hexadecimal (base 16).
  118.      * @since   JDK1.0.2
  119.      */
  120.     public static String toHexString(int i) {
  121.     return toUnsignedString(i, 4);
  122.     }
  123.  
  124.     /**
  125.      * Creates a string representation of the integer argument as an 
  126.      * unsigned integer in base 8. 
  127.      * <p>
  128.      * The unsigned integer value is the argument plus 2<sup>32</sup> if 
  129.      * the argument is negative; otherwise, it is equal to the argument. 
  130.      * This value is converted to a string of ASCII digits in octal 
  131.      * (base 8) with no extra leading <code>0</code>s. 
  132.      *
  133.      * @param   i   an integer
  134.      * @return  the string representation of the unsigned integer value
  135.      *          represented by the argument in octal (base&mnsp;8).
  136.      * @since   JDK1.0.2
  137.      */
  138.     public static String toOctalString(int i) {
  139.     return toUnsignedString(i, 3);
  140.     }
  141.  
  142.     /**
  143.      * Creates a string representation of the integer argument as an 
  144.      * unsigned integer in base 2. 
  145.      * <p>
  146.      * The unsigned integer value is the argument plus 2<sup>32</sup>if 
  147.      * the argument is negative; otherwise it is equal to the argument. 
  148.      * This value is converted to a string of ASCII digits in binary 
  149.      * (base 2) with no extra leading <code>0</code>s. 
  150.      *
  151.      * @param   i   an integer.
  152.      * @return  the string representation of the unsigned integer value
  153.      *          represented by the argument in binary (base 2).
  154.      * @since   JDK1.0.2
  155.      */
  156.     public static String toBinaryString(int i) {
  157.     return toUnsignedString(i, 1);
  158.     }
  159.  
  160.     /** 
  161.      * Convert the integer to an unsigned number.
  162.      */
  163.     private static String toUnsignedString(int i, int shift) {
  164.     StringBuffer buf = new StringBuffer(shift >= 3 ? 11 : 32);
  165.     int radix = 1 << shift;
  166.     int mask = radix - 1;
  167.     do {
  168.         buf.append(Character.forDigit(i & mask, radix));
  169.         i >>>= shift;
  170.     } while (i != 0);
  171.     return buf.reverse().toString();
  172.     }
  173.  
  174.     /**
  175.      * Returns a new String object representing the specified integer. The radix
  176.      * is assumed to be 10.
  177.      *
  178.      * @param   i   an integer to be converted.
  179.      * @return  a string representation of the argument in base 10.
  180.      * @since   JDK1.0
  181.      */
  182.     public static String toString(int i) {
  183.     return toString(i,10);
  184.     }
  185.     
  186.     /**
  187.      * Parses the string argument as a signed integer in the radix 
  188.      * specified by the second argument. The characters in the string 
  189.      * must all be digits of the specified radix (as determined by 
  190.      * whether <code>Character.digit</code> returns a 
  191.      * nonnegative value), except that the first character may be an 
  192.      * ASCII minus sign <code>'-'</code> to indicate a negative value. 
  193.      * The resulting integer value is returned. 
  194.      *
  195.      * @param      s   the <code>String</code> containing the integer.
  196.      * @param      radix   the radix to be used.
  197.      * @return     the integer represented by the string argument in the
  198.      *             specified radix.
  199.      * @exception  NumberFormatException  if the string does not contain a
  200.      *               parsable integer.
  201.  
  202.      * @since      JDK1.0
  203.      */
  204.     public static int parseInt(String s, int radix) 
  205.         throws NumberFormatException 
  206.     {
  207.         if (s == null) {
  208.             throw new NumberFormatException("null");
  209.         }
  210.     int result = 0;
  211.     boolean negative = false;
  212.     int i = 0, max = s.length();
  213.     int limit;
  214.     int multmin;
  215.     int digit;
  216.  
  217.     if (max > 0) {
  218.         if (s.charAt(0) == '-') {
  219.         negative = true;
  220.         limit = Integer.MIN_VALUE;
  221.         i++;
  222.         } else {
  223.         limit = -Integer.MAX_VALUE;
  224.         }
  225.         multmin = limit / radix;
  226.         if (i < max) {
  227.         digit = Character.digit(s.charAt(i++),radix);
  228.         if (digit < 0) {
  229.             throw new NumberFormatException(s);
  230.         } else {
  231.             result = -digit;
  232.         }
  233.         }
  234.         while (i < max) {
  235.         // Accumulating negatively avoids surprises near MAX_VALUE
  236.         digit = Character.digit(s.charAt(i++),radix);
  237.         if (digit < 0) {
  238.             throw new NumberFormatException(s);
  239.         }
  240.         if (result < multmin) {
  241.             throw new NumberFormatException(s);
  242.         }
  243.         result *= radix;
  244.         if (result < limit + digit) {
  245.             throw new NumberFormatException(s);
  246.         }
  247.         result -= digit;
  248.         }
  249.     } else {
  250.         throw new NumberFormatException(s);
  251.     }
  252.     if (negative) {
  253.         if (i > 1) {
  254.         return result;
  255.         } else {    /* Only got "-" */
  256.         throw new NumberFormatException(s);
  257.         }
  258.     } else {
  259.         return -result;
  260.     }
  261.     }
  262.  
  263.     /**
  264.      * Parses the string argument as a signed decimal integer. The 
  265.      * characters in the string must all be decimal digits, except that 
  266.      * the first character may be an ASCII minus sign <code>'-'</code> to 
  267.      * indicate a negative value. 
  268.      *
  269.      * @param      s   a string.
  270.      * @return     the integer represented by the argument in decimal.
  271.      * @exception  NumberFormatException  if the string does not contain a
  272.      *               parsable integer.
  273.      * @since      JDK1.0
  274.      */
  275.     public static int parseInt(String s) throws NumberFormatException {
  276.     return parseInt(s,10);
  277.     }
  278.  
  279.     /**
  280.      * Returns a new Integer object initialized to the value of the
  281.      * specified String.  Throws an exception if the String cannot be
  282.      * parsed as an int.
  283.      *
  284.      * @param      s   the string to be parsed.
  285.      * @return     a newly constructed <code>Integer</code> initialized to the
  286.      *             value represented by the string argument in the specified
  287.      *             radix.
  288.      * @exception  NumberFormatException  if the <code>String</code> does not
  289.      *               contain a parsable integer.
  290.      * @since   JDK1.0
  291.      */
  292.     public static Integer valueOf(String s, int radix) throws NumberFormatException {
  293.     return new Integer(parseInt(s,radix));
  294.     }
  295.  
  296.     /**
  297.      * Returns a new Integer object initialized to the value of the
  298.      * specified String.  Throws an exception if the String cannot be
  299.      * parsed as an int. The radix is assumed to be 10.
  300.      *
  301.      * @param      s   the string to be parsed.
  302.      * @return     a newly constructed <code>Integer</code> initialized to the
  303.      *             value represented by the string argument.
  304.      * @exception  NumberFormatException  if the string does not contain a
  305.      *               parsable integer.
  306.      * @since   JDK1.0
  307.      */
  308.     public static Integer valueOf(String s) throws NumberFormatException
  309.     {
  310.     return new Integer(parseInt(s, 10));
  311.     }
  312.  
  313.     /**
  314.      * The value of the Integer.
  315.      */
  316.     private int value;
  317.  
  318.     /**
  319.      * Constructs a newly allocated <code>Integer</code> object that 
  320.      * represents the primitive <code>int</code> argument. 
  321.      *
  322.      * @param   value   the value to be represented by the <code>Integer</code>.
  323.      * @since   JDK1.0
  324.      */
  325.     public Integer(int value) {
  326.     this.value = value;
  327.     }
  328.  
  329.     /**
  330.      * Constructs a newly allocated <code>Integer</code> object that 
  331.      * represents the value represented by the string. The string is 
  332.      * converted to an int value as if by the <code>valueOf</code> method. 
  333.      *
  334.      * @param      s   the <code>String</code> to be converted to an
  335.      *                 <code>Integer</code>.
  336.      * @exception  NumberFormatException  if the <code>String</code> does not
  337.      *               contain a parsable integer.
  338.      * @see        java.lang.Integer#valueOf(java.lang.String, int)
  339.      * @since      JDK1.0
  340.      */
  341.     public Integer(String s) throws NumberFormatException {
  342.     this.value = parseInt(s, 10);
  343.     }
  344.  
  345.     /**
  346.      * Returns the value of this Integer as a byte.
  347.      *
  348.      * @since   JDK1.1
  349.      */
  350.     public byte byteValue() {
  351.     return (byte)value;
  352.     }
  353.  
  354.     /**
  355.      * Returns the value of this Integer as a short.
  356.      *
  357.      * @since   JDK1.1
  358.      */
  359.     public short shortValue() {
  360.     return (short)value;
  361.     }
  362.  
  363.     /**
  364.      * Returns the value of this Integer as an int.
  365.      *
  366.      * @return  the <code>int</code> value represented by this object.
  367.      * @since   JDK1.0
  368.      */
  369.     public int intValue() {
  370.     return value;
  371.     }
  372.  
  373.     /**
  374.      * Returns the value of this Integer as a long.
  375.      *
  376.      * @return  the <code>int</code> value represented by this object that is
  377.      *          converted to type <code>long</code> and the result of the
  378.      *          conversion is returned.
  379.      * @since   JDK1.0
  380.      */
  381.     public long longValue() {
  382.     return (long)value;
  383.     }
  384.  
  385.     /**
  386.      * Returns the value of this Integer as a float.
  387.      *
  388.      * @return  the <code>int</code> value represented by this object is
  389.      *          converted to type <code>float</code> and the result of the
  390.      *          conversion is returned.
  391.      * @since   JDK1.0
  392.      */
  393.     public float floatValue() {
  394.     return (float)value;
  395.     }
  396.  
  397.     /**
  398.      * Returns the value of this Integer as a double.
  399.      *
  400.      * @return  the <code>int</code> value represented by this object is
  401.      *          converted to type <code>double</code> and the result of the
  402.      *          conversion is returned.
  403.      * @since   JDK1.0
  404.      */
  405.     public double doubleValue() {
  406.     return (double)value;
  407.     }
  408.  
  409.     /**
  410.      * Returns a String object representing this Integer's value.
  411.      *
  412.      * @return  a string representation of the value of this object in
  413.      *          base 10.
  414.      * @since   JDK1.0
  415.      */
  416.     public String toString() {
  417.     return String.valueOf(value);
  418.     }
  419.  
  420.     /**
  421.      * Returns a hashcode for this Integer.
  422.      *
  423.      * @return  a hash code value for this object. 
  424.      * @since   JDK1.0
  425.      */
  426.     public int hashCode() {
  427.     return value;
  428.     }
  429.  
  430.     /**
  431.      * Compares this object to the specified object.
  432.      * The result is <code>true</code> if and only if the argument is not 
  433.      * <code>null</code> and is an <code>Integer</code> object that contains 
  434.      * the same <code>int</code> value as this object. 
  435.      *
  436.      * @param   obj   the object to compare with.
  437.      * @return  <code>true</code> if the objects are the same;
  438.      *          <code>false</code> otherwise.
  439.      * @since   JDK1.0
  440.      */
  441.     public boolean equals(Object obj) {
  442.     if ((obj != null) && (obj instanceof Integer)) {
  443.         return value == ((Integer)obj).intValue();
  444.     }
  445.     return false;
  446.     }
  447.  
  448.     /**
  449.      * Determines the integer value of the system property with the 
  450.      * specified name. 
  451.      * <p>
  452.      * The first argument is treated as the name of a system property. 
  453.      * System properties are accessible through <code>getProperty</code>  
  454.      * and , a method defined by the <code>System</code> class. The 
  455.      * string value of this property is then interpreted as an integer 
  456.      * value and an <code>Integer</code> object representing this value is 
  457.      * returned. Details of possible numeric formats can be found with 
  458.      * the definition of <code>getProperty</code>. 
  459.      * <p>
  460.      * If there is no property with the specified name, or if the 
  461.      * property does not have the correct numeric format, then 
  462.      * <code>null</code> is returned. 
  463.      *
  464.      * @param   nm   property name.
  465.      * @return  the <code>Integer</code> value of the property.
  466.      * @see     java.lang.System#getProperty(java.lang.String)
  467.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  468.      * @since   JDK1.0
  469.      */
  470.     public static Integer getInteger(String nm) {
  471.     return getInteger(nm, null);
  472.     }
  473.  
  474.     /**
  475.      * Determines the integer value of the system property with the 
  476.      * specified name. 
  477.      * <p>
  478.      * The first argument is treated as the name of a system property. 
  479.      * System properties are accessible through <code>getProperty</code>  
  480.      * and , a method defined by the <code>System</code> class. The 
  481.      * string value of this property is then interpreted as an integer 
  482.      * value and an <code>Integer</code> object representing this value is 
  483.      * returned. Details of possible numeric formats can be found with 
  484.      * the definition of <code>getProperty</code>. 
  485.      * <p>
  486.      * If there is no property with the specified name, or if the 
  487.      * property does not have the correct numeric format, then an 
  488.      * <code>Integer</code> object that represents the value of the 
  489.      * second argument is returned. 
  490.      *
  491.      * @param   nm   property name.
  492.      * @param   val   default value.
  493.      * @return  the <code>Integer</code> value of the property.
  494.      * @see     java.lang.System#getProperty(java.lang.String)
  495.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  496.      * @since   JDK1.0
  497.      */
  498.     public static Integer getInteger(String nm, int val) {
  499.         Integer result = getInteger(nm, null);
  500.         return (result == null) ? new Integer(val) : result;
  501.     }
  502.  
  503.     /**
  504.      * Determines the integer value of the system property with the 
  505.      * specified name. 
  506.      * <p>
  507.      * The first argument is treated as the name of a system property. 
  508.      * System properties are accessible through <code>getProperty</code>  
  509.      * and , a method defined by the <code>System</code> class. The 
  510.      * string value of this property is then interpreted as an integer 
  511.      * value and an <code>Integer</code> object representing this value is 
  512.      * returned. 
  513.      * <p>
  514.      * If the property value begins with "<code>0x</code>" or 
  515.      * "<code>#</code>", not followed by a minus sign, the rest 
  516.      * of it is parsed as a hexadecimal integer exactly as for the method 
  517.      * <code>Integer.valueOf</code> with radix 16. 
  518.      * <p>
  519.      * If the property value begins with "<code>0</code>" then 
  520.      * it is parsed as an octal integer exactly as for the method 
  521.      * <code>Integer.valueOf</code> with radix 8. 
  522.      * <p>
  523.      * Otherwise the property value is parsed as a decimal integer 
  524.      * exactly as for the method <code>Integer.valueOf</code> with radix 10.
  525.      * <p>
  526.      * The second argument is the default value. If there is no property 
  527.      * of the specified name, or if the property does not have the 
  528.      * correct numeric format, then the second argument is returned. 
  529.      *
  530.      * @param   nm   property name.
  531.      * @param   val   default value.
  532.      * @return  the <code>Integer</code> value of the property.
  533.      * @see     java.lang.System#getProperty(java.lang.String)
  534.      * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)
  535.      * @since   JDK1.0
  536.      */
  537.     public static Integer getInteger(String nm, Integer val) {
  538.     String v = System.getProperty(nm);
  539.     if (v != null) {
  540.         try {
  541.         return Integer.decode(v);
  542.         } catch (NumberFormatException e) {
  543.         }
  544.     }    
  545.     return val;
  546.     }
  547.  
  548.     /**
  549.      * Decodes a string into an Integer. Deals with decimal, hexadecimal,
  550.      * and octal numbers.
  551.      * @param nm the string to decode
  552.      * @since   JDK1.1
  553.      */
  554.     public static Integer decode(String nm) throws NumberFormatException {
  555.     if (nm.startsWith("0x")) {
  556.         return Integer.valueOf(nm.substring(2), 16);
  557.     }
  558.     if (nm.startsWith("#")) {
  559.         return Integer.valueOf(nm.substring(1), 16);
  560.     }
  561.     if (nm.startsWith("0") && nm.length() > 1) {
  562.         return Integer.valueOf(nm.substring(1), 8);
  563.     }
  564.     
  565.     return Integer.valueOf(nm);
  566.     }
  567.  
  568.     /** use serialVersionUID from JDK 1.0.2 for interoperability */
  569.     private static final long serialVersionUID = 1360826667806852920L;
  570. }
  571.