home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / lang / Short.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  7.5 KB  |  268 lines

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