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

Diff of /classpath/java/lang/Short.java

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

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

Legend:
Removed from v.1.12  
changed lines
  Added in v.1.13

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