home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / Short.java < prev    next >
Text File  |  1998-09-22  |  7KB  |  257 lines

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