OSDN Git Service

f3ec40206711573a939d1c3b5406f766daae9e42
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / Byte.java
1 /* Copyright (C) 1998, 1999  Cygnus Solutions
2
3    This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
7 details.  */
8
9 package java.lang;
10
11 /**
12  * @author Per Bothner <bothner@cygnus.com>
13  * @date April 17, 1998.  
14  */
15 /* Written using "Java Class Libraries", 2nd edition, plus online
16  * API docs for JDK 1.2 beta from http://www.javasoft.com.
17  * Status:  Believed complete and correct.
18  *          Includes JDK 1.2 methods.
19  */
20
21 public final class Byte extends Number implements Comparable
22 {
23   byte value;
24
25   public final static byte MIN_VALUE = -128;
26   public final static byte MAX_VALUE = 127;
27
28   // This initialization is seemingly circular, but it is accepted
29   // by javac, and is handled specially by gcc.
30   public static final Class TYPE = byte.class;
31
32   public Byte(byte value)
33   {
34     this.value = value;
35   }
36
37   public Byte(String str) 
38     throws NumberFormatException
39   {
40     this.value = parseByte(str, 10);
41   }
42
43   public byte byteValue()
44   {
45     return value;
46   }
47
48   public short shortValue()
49   {
50     return value;
51   }
52
53   public int intValue()
54   {
55     return value;
56   }
57
58   public long longValue ()
59   {
60     return value;
61   }
62
63   public float floatValue ()
64   {
65     return (float) value;
66   }
67
68   public double doubleValue ()
69   {
70     return (double) value;
71   }
72
73   public static Byte decode(String str)
74     throws NumberFormatException
75   {
76     int i = (Integer.decode(str)).intValue();
77     if (i < MIN_VALUE || i > MAX_VALUE)
78       throw new NumberFormatException();
79     return new Byte((byte) i);
80   }
81
82   public static byte parseByte(String str, int radix)
83     throws NumberFormatException
84   {
85     int i = Integer.parseInt(str, radix);
86     if (i < MIN_VALUE || i > MAX_VALUE)
87       throw new NumberFormatException();
88     return (byte) i;
89   }
90
91   public static byte parseByte(String str)
92     throws NumberFormatException
93   {
94     return parseByte(str, 10);
95   }
96
97   public static Byte valueOf(String str, int radix)
98     throws NumberFormatException
99   {
100     return new Byte(parseByte(str, radix));
101   }
102
103   public static Byte valueOf(String str)
104     throws NumberFormatException
105   {
106     return valueOf(str, 10);
107   }
108
109   // Added in JDK 1.2
110   public int compareTo(Byte anotherByte)
111   {
112     return this.value - anotherByte.value;
113   }
114
115   // Added in JDK 1.2
116   public int compareTo(Object o) throws ClassCastException
117   {
118     if (o instanceof Byte)
119       return this.value - ((Byte) o).value;
120     else
121       throw new ClassCastException();
122   }
123
124   public boolean equals(Object obj)
125   {
126     return obj != null && (obj instanceof Byte) && ((Byte)obj).value == value;
127   }
128
129   // Verified that hashCode is returns plain value (see Boolean_1 test).
130   public int hashCode()
131   {
132     return value;
133   }
134
135   public String toString()
136   {
137     return Integer.toString((int) value);
138   }
139
140   public static String toString(byte value)
141   {
142     return Integer.toString((int) value);
143   }
144 }