/[classpath]/classpath/java/lang/Byte.java
ViewVC logotype

Diff of /classpath/java/lang/Byte.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.19 by mark, Tue Jan 22 22:26:59 2002 UTC revision 1.20 by ericb, Mon Feb 25 01:36:03 2002 UTC
# Line 1  Line 1 
1  /* Byte.java -- object wrapper for byte  /* Byte.java -- object wrapper for byte
2     Copyright (C) 1998, 2001 Free Software Foundation, Inc.     Copyright (C) 1998, 2001, 2002 Free Software Foundation, Inc.
3    
4  This file is part of GNU Classpath.  This file is part of GNU Classpath.
5    
# Line 7  GNU Classpath is free software; you can Line 7  GNU Classpath is free software; you can
7  it under the terms of the GNU General Public License as published by  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2, or (at your option)  the Free Software Foundation; either version 2, or (at your option)
9  any later version.  any later version.
10    
11  GNU Classpath is distributed in the hope that it will be useful, but  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Line 48  package java.lang; Line 48  package java.lang;
48   * @author Paul Fisher   * @author Paul Fisher
49   * @author John Keiser   * @author John Keiser
50   * @author Per Bothner   * @author Per Bothner
51   * @since JDK 1.0   * @author Eric Blake <ebb9@email.byu.edu>
52     * @since 1.1
53     * @status updated to 1.4
54   */   */
55  public final class Byte extends Number implements Comparable  public final class Byte extends Number implements Comparable
56  {  {
57    static final long serialVersionUID = -7183698231559129828L;    /**
58       * Compatible with JDK 1.1+.
59       */
60      private static final long serialVersionUID = -7183698231559129828L;
61    
62    /**    /**
63     * The minimum value a <code>byte</code> can represent is -128.     * The minimum value a <code>byte</code> can represent is -128 (or
64       * -2<sup>7</sup>).
65     */     */
66    public static final byte MIN_VALUE = -128;    public static final byte MIN_VALUE = -128;
67    
68    /**    /**
69     * The maximum value a <code>byte</code> can represent is 127.     * The maximum value a <code>byte</code> can represent is 127 (or
70       * 2<sup>7</sup> - 1).
71     */     */
72    public static final byte MAX_VALUE = 127;    public static final byte MAX_VALUE = 127;
73    
74    /**    /**
75     * The primitive type <code>byte</code> is represented by this     * The primitive type <code>byte</code> is represented by this
76     * <code>Class</code> object.     * <code>Class</code> object.
77     */     */
78    public static final Class TYPE = VMClassLoader.getPrimitiveClass('B');    public static final Class TYPE = VMClassLoader.getPrimitiveClass('B');
79    
80    /**    /**
81     * The immutable value of this Byte.     * The immutable value of this Byte.
82       *
83       * @serial the wrapped byte
84     */     */
85    private final byte value;    private final byte value;
86    
87    /**    /**
88     * Create a <code>Byte</code> object representing the value of the     * Create a <code>Byte</code> object representing the value of the
89     * <code>byte</code> argument.     * <code>byte</code> argument.
90     *     *
91     * @param value the value to use     * @param value the value to use
92     */         */
93    public Byte(byte value)    public Byte(byte value)
94    {    {
95      this.value = value;      this.value = value;
96    }    }
97    
98    /**    /**
99     * Create a <code>Byte</code> object representing the value specified     * Create a <code>Byte</code> object representing the value specified
100     * by the <code>String</code> argument.     * by the <code>String</code> argument
101     *     *
102     * @param s the string to convert.     * @param s the string to convert
103       * @throws NumberFormatException if the String does not contain a byte
104       * @see #valueOf(String)
105     */     */
106    public Byte(String s) throws NumberFormatException    public Byte(String s)
107    {    {
108      value = parseByte(s, 10);      value = parseByte(s, 10);
109    }    }
110    
111    /**    /**
112     * Return a hashcode representing this Object.     * Converts the <code>byte</code> to a <code>String</code> and assumes
113     *     * a radix of 10.
    * <code>Byte</code>'s hash code is calculated by simply returning its  
    * value.  
114     *     *
115     * @return this Object's hash code.     * @param b the <code>byte</code> to convert to <code>String</code>
116       * @return the <code>String</code> representation of the argument
117     */     */
118    public int hashCode()    public static String toString(byte b)
119    {    {
120      return value;      return String.valueOf(b);
121    }    }
122    
123    /**    /**
124     * Returns <code>true</code> if <code>obj</code> is an instance of     * Converts the specified <code>String</code> into a <code>byte</code>.
125     * <code>Byte</code> and represents the same byte value.     * This function assumes a radix of 10.
126     * @return whether these Objects are semantically equal.     *
127     */         * @param s the <code>String</code> to convert
128    public boolean equals(Object obj)     * @return the <code>byte</code> value of <code>s</code>
129    {     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
130      return ((obj instanceof Byte) && (value == ((Byte)obj).byteValue()));     *         <code>byte</code>
131    }     * @see #parseByte(String)
132       */
133    /**    public static byte parseByte(String s)
    * Converts the <code>byte</code> to a <code>String</code> and assumes  
    * a radix of 10.  
    * @param i the <code>byte</code> to convert to <code>String</code>  
    * @return the <code>String</code> representation of the argument.  
    */      
   public static String toString(byte i)  
134    {    {
135      return Integer.toString ((int) i);      return parseByte(s, 10);
136    }    }
137    
138    /**    /**
139     * Converts the <code>Byte</code> value to a <code>String</code> and     * Converts the specified <code>String</code> into an <code>int</code>
140     * assumes a radix of 10.     * using the specified radix (base). The string must not be <code>null</code>
141     * @return the <code>String</code> representation of this <code>Byte</code>.     * or empty. It may begin with an optional '-', which will negate the answer,
142     */         * provided that there are also valid digits. Each digit is parsed as if by
143    public String toString()     * <code>Character.digit(d, radix)</code>, and must be in the range
144    {     * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
145      return Integer.toString ((int) value);     * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
146    }     * Unlike Double.parseDouble, you may not have a leading '+'.
147           *
148    /**     * @param s the <code>String</code> to convert
149     * Creates a new <code>Byte</code> object using the <code>String</code>,     * @param radix the radix (base) to use in the conversion
150     * assuming a radix of 10.     * @return the <code>String</code> argument converted to </code>byte</code>
151     * @param s the <code>String</code> to convert.     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
152     * @return the new <code>Byte</code>.     *         <code>byte</code>
    * @see #Byte(java.lang.String)  
    * @see #parseByte(java.lang.String)  
    * @exception NumberFormatException thrown if the <code>String</code>  
    * cannot be parsed as a <code>byte</code>.  
153     */     */
154    public static Byte valueOf(String s) throws NumberFormatException    public static byte parseByte(String s, int radix)
155    {    {
156      return new Byte(parseByte(s));      int i = Integer.parseInt(s, radix);
157        if ((byte) i != i)
158          throw new NumberFormatException();
159        return (byte) i;
160    }    }
161    
162    /**    /**
163     * Creates a new <code>Byte</code> object using the <code>String</code>     * Creates a new <code>Byte</code> object using the <code>String</code>
164     * and specified radix (base).     * and specified radix (base).
    * @param s the <code>String</code> to convert.  
    * @param radix the radix (base) to convert with.  
    * @return the new <code>Byte</code>.  
    * @see #parseByte(java.lang.String,int)  
    * @exception NumberFormatException thrown if the <code>String</code>  
    * cannot be parsed as a <code>byte</code>.  
    */  
   public static Byte valueOf(String s, int radix)  
     throws NumberFormatException  
   {  
     return new Byte(parseByte(s, radix));  
   }  
   
   /**  
    * Converts the specified <code>String</code> into a <code>byte</code>.  
    * This function assumes a radix of 10.  
165     *     *
166     * @param s the <code>String</code> to convert     * @param s the <code>String</code> to convert
167     * @return the <code>byte</code> value of the <code>String</code>     * @param radix the radix (base) to convert with
168     *         argument.     * @return the new <code>Byte</code>
169     * @exception NumberFormatException thrown if the <code>String</code>     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
170     * cannot be parsed as a <code>byte</code>.     *         <code>byte</code>
171       * @see #parseByte(String, int)
172     */     */
173    public static byte parseByte(String s) throws NumberFormatException    public static Byte valueOf(String s, int radix)
174    {    {
175      return parseByte(s, 10);      return new Byte(parseByte(s, radix));
176    }    }
177    
178    /**    /**
179     * Converts the specified <code>String</code> into a <code>byte</code>     * Creates a new <code>Byte</code> object using the <code>String</code>,
180     * using the specified radix (base).     * assuming a radix of 10.
181     *     *
182     * @param str the <code>String</code> to convert     * @param s the <code>String</code> to convert
183     * @param radix the radix (base) to use in the conversion     * @return the new <code>Byte</code>
184     * @return the <code>String</code> argument converted to </code>byte</code>.     * @throws NumberFormatException if <code>s</code> cannot be parsed as a
185     * @exception NumberFormatException thrown if the <code>String</code>     *         <code>byte</code>
186     * cannot be parsed as a <code>byte</code>.         * @see #Byte(String)
187       * @see #parseByte(String)
188     */     */
189    public static byte parseByte(String str, int radix)    public static Byte valueOf(String s)
     throws NumberFormatException  
190    {    {
191      int i = Integer.parseInt(str, radix);      return new Byte(parseByte(s, 10));
     if (i < MIN_VALUE || i > MAX_VALUE)  
       throw new NumberFormatException();  
     return (byte) i;  
192    }    }
193    
194    /**    /**
195     * Convert the specified <code>String</code> into a <code>Byte</code>.     * Convert the specified <code>String</code> into a <code>Byte</code>.
196     * The <code>String</code> may represent decimal, hexadecimal, or     * The <code>String</code> may represent decimal, hexadecimal, or
197     * octal numbers.     * octal numbers.
198     *     *
199     * The <code>String</code> argument is interpreted based on the leading     * <p>The extended BNF grammar is as follows:<br>
200     * characters.  Depending on what the String begins with, the base will be     * <pre>
201     * interpreted differently:     * <em>DecodableString</em>:
202     *     *      ( [ <code>-</code> ] <em>DecimalNumber</em> )
203     * <table>     *    | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
204     * <tr><th>Leading<br>Characters</th><th>Base</th></tr>     *              | <code>#</code> ) { <em>HexDigit</em> }+ )
205     * <tr><td>#</td><td>16</td></tr>     *    | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
206     * <tr><td>0x</td><td>16</td></tr>     * <em>DecimalNumber</em>:
207     * <tr><td>0X</td><td>16</td></tr>     *        <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
208     * <tr><td>0</td><td>8</td></tr>     * <em>DecimalDigit</em>:
209     * <tr><td>Anything<br>Else</td><td>10</td></tr>     *        <em>Character.digit(d, 10) has value 0 to 9</em>
210     * </table>     * <em>OctalDigit</em>:
211     *     *        <em>Character.digit(d, 8) has value 0 to 7</em>
212     * @param str the <code>String</code> to interpret.     * <em>DecimalDigit</em>:
213     * @return the value of the String as a <code>Byte</code>.     *        <em>Character.digit(d, 16) has value 0 to 15</em>
214     * @exception NumberFormatException thrown if the <code>String</code>     * </pre>
215     * cannot be parsed as a <code>byte</code>.         * Note that you cannot decode MIN_VALUE, as the specification requires
216       * that the digits be parsed before negating the result, but 128 will not
217       * fit in a byte.
218       *
219       * @param s the <code>String</code> to interpret
220       * @return the value of the String as a <code>Byte</code>
221       * @throws NumberFormatException if <code>s</code> cannot be parsed as a
222       *         <code>byte</code>
223       * @throws NullPointerException if s is null
224       * @see Integer#decode(String)
225     */     */
226    public static Byte decode(String str) throws NumberFormatException    public static Byte decode(String s)
227    {    {
228      int i = (Integer.decode(str)).intValue();      int i = (Integer.decode(str)).intValue();
229      if (i < MIN_VALUE || i > MAX_VALUE)      if ((byte) i != i)
230        throw new NumberFormatException();        throw new NumberFormatException();
231      return new Byte((byte) i);      return new Byte((byte) i);
232    }    }
233        
234    /** Return the value of this <code>Byte</code> as an <code>short</code>.    /**
235     ** @return the value of this <code>Byte</code> as an <code>short</code>.     * Return the value of this <code>Byte</code>.
236     **/     *
237       * @return the byte value
238       */
239    public byte byteValue()    public byte byteValue()
240    {    {
241      return value;      return value;
242    }    }
243    
244    /** Return the value of this <code>Byte</code> as an <code>short</code>.    /**
245     ** @return the value of this <code>Byte</code> as an <code>short</code>.     * Return the value of this <code>Byte</code> as a <code>short</code>.
246     **/     *
247       * @return the short value
248       */
249    public short shortValue()    public short shortValue()
250    {    {
251      return value;      return value;
252    }    }
253    
254    /** Return the value of this <code>Byte</code> as an <code>int</code>.    /**
255     ** @return the value of this <code>Byte</code> as an <code>int</code>.     * Return the value of this <code>Byte</code> as an <code>int</code>.
256     **/     *
257       * @return the int value
258       */
259    public int intValue()    public int intValue()
260    {    {
261      return value;      return value;
262    }    }
263    
264    /** Return the value of this <code>Byte</code> as a <code>long</code>.    /**
265     ** @return the value of this <code>Byte</code> as a <code>long</code>.     * Return the value of this <code>Byte</code> as a <code>long</code>.
266     **/     *
267       * @return the long value
268       */
269    public long longValue()    public long longValue()
270    {    {
271      return value;      return value;
272    }    }
273    
274    /** Return the value of this <code>Byte</code> as a <code>float</code>.    /**
275     ** @return the value of this <code>Byte</code> as a <code>float</code>.     * Return the value of this <code>Byte</code> as a <code>float</code>.
276     **/     *
277       * @return the float value
278       */
279    public float floatValue()    public float floatValue()
280    {    {
281      return value;      return value;
282    }    }
283    
284    /** Return the value of this <code>Byte</code> as a <code>double</code>.    /**
285     ** @return the value of this <code>Byte</code> as a <code>double</code>.     * Return the value of this <code>Byte</code> as a <code>double</code>.
286     **/     *
287       * @return the double value
288       */
289    public double doubleValue()    public double doubleValue()
290    {    {
291      return value;      return value;
292    }    }
293        
294    /**    /**
295     * Compare two Bytes numerically by comparing their     * Converts the <code>Byte</code> value to a <code>String</code> and
296     * <code>byte</code> values.     * assumes a radix of 10.
297     * @return a positive value if this <code>Byte</code> is greater     *
298     * in value than the argument <code>Byte</code>; a negative value     * @return the <code>String</code> representation of this <code>Byte</code>
299     * if this <code>Byte</code> is smaller in value than the argument     * @see Integer#toString()
300     * <code>Byte</code>; and <code>0</code>, zero, if this     */
301     * <code>Byte</code> is equal in value to the argument    public String toString()
302     * <code>Byte</code>.      {
303        return String.valueOf(value);
304      }
305    
306      /**
307       * Return a hashcode representing this Object. <code>Byte</code>'s hash
308       * code is simply its value.
309       *
310       * @return this Object's hash code
311       */
312      public int hashCode()
313      {
314        return value;
315      }
316    
317      /**
318       * Returns <code>true</code> if <code>obj</code> is an instance of
319       * <code>Byte</code> and represents the same byte value.
320       *
321       * @param obj the object to compare
322       * @return whether these Objects are semantically equal
323       */
324      public boolean equals(Object obj)
325      {
326        return obj instanceof Byte && value == ((Byte) obj).value;
327      }
328    
329      /**
330       * Compare two Bytes numerically by comparing their <code>byte</code> values.
331       * The result is positive if the first is greater, negative if the second
332       * is greater, and 0 if the two are equal.
333       *
334       * @param b the Byte to compare
335       * @return the comparison
336       * @since 1.2
337     */     */
338    public int compareTo(Byte b)    public int compareTo(Byte b)
339    {    {
340      return (int)(value - b.byteValue());      return value - b.value;
341    }    }
342        
343    /**    /**
344     * Behaves like <code>compareTo(java.lang.Byte)</code> unless the Object     * Behaves like <code>compareTo(Byte)</code> unless the Object
345     * is not a <code>Byte</code>.  Then it throws a     * is not a <code>Byte</code>.
346     * <code>ClassCastException</code>.     *
347     * @exception ClassCastException if the argument is not a     * @param o the object to compare
348     * <code>Byte</code>.       * @return the comparison
349       * @throws ClassCastException if the argument is not a <code>Byte</code>
350       * @see #compareTo(Byte)
351       * @see Comparable
352       * @since 1.2
353     */     */
354    public int compareTo(Object o)    public int compareTo(Object o)
355    {    {
356      return compareTo((Byte)o);      return compareTo((Byte) o);
357    }    }
358  }  }

Legend:
Removed from v.1.19  
changed lines
  Added in v.1.20

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26