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

  1. /*
  2.  * @(#)Byte.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.  *
  19.  * The Byte class is the standard wrapper for byte values.
  20.  *
  21.  * @author  Nakul Saraiya
  22.  * @version 1.11, 03/18/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.     private byte value;
  137.  
  138.     /**
  139.      * Constructs a Byte object initialized to the specified byte value.
  140.      *
  141.      * @param value    the initial value of the Byte
  142.      */
  143.     public Byte(byte value) {
  144.     this.value = value;
  145.     }
  146.  
  147.     /**
  148.      * Constructs a Byte object initialized to the value specified by the
  149.      * String parameter.  The radix is assumed to be 10.
  150.      *
  151.      * @param s        the String to be converted to a Byte
  152.      * @exception    NumberFormatException If the String does not
  153.      *            contain a parsable byte.
  154.      */
  155.     public Byte(String s) throws NumberFormatException {
  156.     this.value = parseByte(s);
  157.     }
  158.  
  159.     /**
  160.      * Returns the value of this Byte as a byte.
  161.      */
  162.     public byte byteValue() {
  163.     return value;
  164.     }
  165.  
  166.     /**
  167.      * Returns the value of this Byte as a short.
  168.      */
  169.     public short shortValue() {
  170.     return (short)value;
  171.     }
  172.  
  173.     /**
  174.      * Returns the value of this Byte as an int.
  175.      */
  176.     public int intValue() {
  177.     return (int)value;
  178.     }
  179.  
  180.     /**
  181.      * Returns the value of this Byte as a long.
  182.      */
  183.     public long longValue() {
  184.     return (long)value;
  185.     }
  186.  
  187.     /**
  188.      * Returns the value of this Byte as a float.
  189.      */
  190.     public float floatValue() {
  191.     return (float)value;
  192.     }
  193.  
  194.     /**
  195.      * Returns the value of this Byte as a double.
  196.      */
  197.     public double doubleValue() {
  198.     return (double)value;
  199.     }
  200.  
  201.     /**
  202.      * Returns a String object representing this Byte's value.
  203.      */
  204.     public String toString() {
  205.     return String.valueOf((int)value);
  206.     }
  207.  
  208.     /**
  209.      * Returns a hashcode for this Byte.
  210.      */
  211.     public int hashCode() {
  212.     return (int)value;
  213.     }
  214.  
  215.     /**
  216.      * Compares this object to the specified object.
  217.      *
  218.      * @param obj    the object to compare with
  219.      * @return         true if the objects are the same; false otherwise.
  220.      */
  221.     public boolean equals(Object obj) {
  222.     if ((obj != null) && (obj instanceof Byte)) {
  223.         return value == ((Byte)obj).byteValue();
  224.     }
  225.     return false;
  226.     }
  227.  
  228.     /**
  229.      * Compares two Bytes numerically.
  230.      *
  231.      * @param   anotherByte   the <code>Byte</code> to be compared.
  232.      * @return  the value <code>0</code> if the argument Byte is equal to
  233.      *          this Byte; a value less than <code>0</code> if this Byte
  234.      *          is numerically less than the Byte argument; and a
  235.      *          value greater than <code>0</code> if this Byte is
  236.      *          numerically greater than the Byte argument (signed comparison).
  237.      * @since   JDK1.2
  238.      */
  239.     public int compareTo(Byte anotherByte) {
  240.     return this.value - anotherByte.value;
  241.     }
  242.  
  243.     /**
  244.      * Compares this Byte to another Object.  If the Object is a Byte,
  245.      * this function behaves like <code>compareTo(Byte)</code>.  Otherwise,
  246.      * it throws a <code>ClassCastException</code> (as Bytes are comparable
  247.      * only to other Bytes).
  248.      *
  249.      * @param   o the <code>Object</code> to be compared.
  250.      * @return  the value <code>0</code> if the argument is a Byte
  251.      *        numerically equal to this Byte; a value less than
  252.      *        <code>0</code> if the argument is a Byte numerically
  253.      *        greater than this Byte; and a value greater than
  254.      *        <code>0</code> if the argument is a Byte numerically
  255.      *        less than this Byte.
  256.      * @exception <code>ClassCastException</code> if the argument is not a
  257.      *          <code>Byte</code>. 
  258.      * @see     java.lang.Comparable
  259.      * @since   JDK1.2
  260.      */
  261.     public int compareTo(Object o) {
  262.     return compareTo((Byte)o);
  263.     }
  264.  
  265.     /** use serialVersionUID from JDK 1.1. for interoperability */
  266.     private static final long serialVersionUID = -7183698231559129828L;
  267.  
  268. }
  269.