home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / lang / Byte.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  7.5 KB  |  271 lines  |  [TEXT/CWIE]

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