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

  1. /*
  2.  * @(#)Short.java    1.7 97/01/20
  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 Short class is the standard wrapper for short values.
  27.  *
  28.  * @author  Nakul Saraiya
  29.  * @version 1.7, 01/20/97
  30.  * @see     java.lang.Number
  31.  * @since   JDK1.1
  32.  */
  33. public final
  34. class Short extends Number {
  35.     /**
  36.      * The minimum value a Short can have.
  37.      *
  38.      * @since   JDK1.1
  39.      */
  40.     public static final short   MIN_VALUE = -32768;
  41.  
  42.     /**
  43.      * The maximum value a Short can have.
  44.      *
  45.      * @since   JDK1.1
  46.      */
  47.     public static final short   MAX_VALUE = 32767;
  48.  
  49.     /**
  50.      * The Class object representing the primitive type short.
  51.      *
  52.      * @since   JDK1.1
  53.      */
  54.     public static final Class    TYPE = Class.getPrimitiveClass("short");
  55.  
  56.     /**
  57.      * Returns a new String object representing the specified
  58.      * Short. The radix is assumed to be 10.
  59.      *
  60.      * @param s the short to be converted
  61.      * @since   JDK1.1
  62.      */
  63.     public static String toString(short s) {
  64.     return Integer.toString((int)s, 10);
  65.     }
  66.  
  67.     /**
  68.      * Assuming the specified String represents a short, returns
  69.      * that short's value. Throws an exception if the String cannot
  70.      * be parsed as a short.  The radix is assumed to be 10.
  71.      *
  72.      * @param s        the String containing the short
  73.      * @exception    NumberFormatException If the string does not
  74.      *            contain a parsable short.
  75.      * @since   JDK1.1
  76.      */
  77.     public static short parseShort(String s) throws NumberFormatException {
  78.     return parseShort(s, 10);
  79.     }
  80.  
  81.     /**
  82.      * Assuming the specified String represents a short, returns
  83.      * that short's value. Throws an exception if the String cannot
  84.      * be parsed as a short.
  85.      *
  86.      * @param s        the String containing the short
  87.      * @param radix    the radix to be used
  88.      * @exception    NumberFormatException If the String does not
  89.      *            contain a parsable short.
  90.      * @since   JDK1.1
  91.      */
  92.     public static short parseShort(String s, int radix)
  93.     throws NumberFormatException {
  94.     int i = Integer.parseInt(s, radix);
  95.     if (i < MIN_VALUE || i > MAX_VALUE)
  96.         throw new NumberFormatException();
  97.     return (short)i;
  98.     }
  99.  
  100.     /**
  101.      * Assuming the specified String represents a short, returns a
  102.      * new Short object initialized to that value.  Throws an
  103.      * exception if the String cannot be parsed as a short.
  104.      *
  105.      * @param s        the String containing the integer
  106.      * @param radix     the radix to be used
  107.      * @exception    NumberFormatException If the String does not
  108.      *            contain a parsable short.
  109.      * @since   JDK1.1
  110.      */
  111.     public static Short valueOf(String s, int radix)
  112.     throws NumberFormatException {
  113.     return new Short(parseShort(s, radix));
  114.     }
  115.  
  116.     /**
  117.      * Assuming the specified String represents a short, returns a
  118.      * new Short object initialized to that value.  Throws an
  119.      * exception if the String cannot be parsed as a short.
  120.      *
  121.      * @param s        the String containing the integer
  122.      * @exception    NumberFormatException If the String does not
  123.      *            contain a parsable short.
  124.      * @since   JDK1.1
  125.      */
  126.     public static Short valueOf(String s) throws NumberFormatException {
  127.     return valueOf(s, 10);
  128.     }
  129.  
  130.     /**
  131.      * Decodes a String into a Short.  The String may represent
  132.      * decimal, hexadecimal, and octal numbers.
  133.      *
  134.      * @param nm the string to decode
  135.      * @since   JDK1.1
  136.      */
  137.     public static Short decode(String nm) throws NumberFormatException {
  138.     if (nm.startsWith("0x")) {
  139.         return Short.valueOf(nm.substring(2), 16);
  140.     }
  141.     if (nm.startsWith("#")) {
  142.         return Short.valueOf(nm.substring(1), 16);
  143.     }
  144.     if (nm.startsWith("0") && nm.length() > 1) {
  145.         return Short.valueOf(nm.substring(1), 8);
  146.     }
  147.  
  148.     return Short.valueOf(nm);
  149.     }
  150.  
  151.     /**
  152.      * The value of the Short.
  153.      */
  154.     private short value;
  155.  
  156.     /**
  157.      * Constructs a Short object initialized to the specified short value.
  158.      *
  159.      * @param value    the initial value of the Short
  160.      * @since   JDK1.1
  161.      */
  162.     public Short(short value) {
  163.     this.value = value;
  164.     }
  165.  
  166.     /**
  167.      * Constructs a Short object initialized to the value specified by the
  168.      * String parameter.  The radix is assumed to be 10.
  169.      *
  170.      * @param s        the String to be converted to a Short
  171.      * @exception    NumberFormatException If the String does not
  172.      *            contain a parsable short.
  173.      * @since   JDK1.1
  174.      */
  175.     public Short(String s) throws NumberFormatException {
  176.     this.value = parseShort(s);
  177.     }
  178.  
  179.     /**
  180.      * Returns the value of this Short as a byte.
  181.      *
  182.      * @since   JDK1.1
  183.      */
  184.     public byte byteValue() {
  185.     return (byte)value;
  186.     }
  187.  
  188.     /**
  189.      * Returns the value of this Short as a short.
  190.      *
  191.      * @since   JDK1.1
  192.      */
  193.     public short shortValue() {
  194.     return value;
  195.     }
  196.  
  197.     /**
  198.      * Returns the value of this Short as an int.
  199.      *
  200.      * @since   JDK1.1
  201.      */
  202.     public int intValue() {
  203.     return (int)value;
  204.     }
  205.  
  206.     /**
  207.      * Returns the value of this Short as a long.
  208.      *
  209.      * @since   JDK1.1
  210.      */
  211.     public long longValue() {
  212.     return (long)value;
  213.     }
  214.  
  215.     /**
  216.      * Returns the value of this Short as a float.
  217.      *
  218.      * @since   JDK1.1
  219.      */
  220.     public float floatValue() {
  221.     return (float)value;
  222.     }
  223.  
  224.     /**
  225.      * Returns the value of this Short as a double.
  226.      *
  227.      * @since   JDK1.1
  228.      */
  229.     public double doubleValue() {
  230.     return (double)value;
  231.     }
  232.  
  233.     /**
  234.      * Returns a String object representing this Short's value.
  235.      *
  236.      * @since   JDK1.1
  237.      */
  238.     public String toString() {
  239.     return String.valueOf((int)value);
  240.     }
  241.  
  242.     /**
  243.      * Returns a hashcode for this Short.
  244.      *
  245.      * @since   JDK1.1
  246.      */
  247.     public int hashCode() {
  248.     return (int)value;
  249.     }
  250.  
  251.     /**
  252.      * Compares this object to the specified object.
  253.      *
  254.      * @param obj    the object to compare with
  255.      * @return         true if the objects are the same; false otherwise.
  256.      * @since   JDK1.1
  257.      */
  258.     public boolean equals(Object obj) {
  259.     if ((obj != null) && (obj instanceof Short)) {
  260.         return value == ((Short)obj).shortValue();
  261.     }
  262.     return false;
  263.     }
  264. }
  265.