Source for java.lang.Short

   1: /* Short.java -- object wrapper for short
   2:    Copyright (C) 1998, 2001, 2002, 2005  Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: 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)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.lang;
  40: 
  41: /**
  42:  * Instances of class <code>Short</code> represent primitive
  43:  * <code>short</code> values.
  44:  *
  45:  * Additionally, this class provides various helper functions and variables
  46:  * related to shorts.
  47:  *
  48:  * @author Paul Fisher
  49:  * @author John Keiser
  50:  * @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
  55: {
  56:   /**
  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 (or
  63:    * -2<sup>15</sup>).
  64:    */
  65:   public static final short MIN_VALUE = -32768;
  66: 
  67:   /**
  68:    * 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;
  72: 
  73:   /**
  74:    * The primitive type <code>short</code> is represented by this
  75:    * <code>Class</code> object.
  76:    */
  77:   public static final Class TYPE = VMClassLoader.getPrimitiveClass('S');
  78: 
  79:   /**
  80:    * The immutable value of this Short.
  81:    *
  82:    * @serial the wrapped short
  83:    */
  84:   private final short value;
  85: 
  86:   /**
  87:    * Create a <code>Short</code> object representing the value of the
  88:    * <code>short</code> argument.
  89:    *
  90:    * @param value the value to use
  91:    */
  92:   public Short(short value)
  93:   {
  94:     this.value = value;
  95:   }
  96: 
  97:   /**
  98:    * Create a <code>Short</code> object representing the value of the
  99:    * argument after conversion to a <code>short</code>.
 100:    *
 101:    * @param s the string to convert
 102:    * @throws NumberFormatException if the String cannot be parsed
 103:    */
 104:   public Short(String s)
 105:   {
 106:     value = parseShort(s, 10);
 107:   }
 108: 
 109:   /**
 110:    * Converts the <code>short</code> to a <code>String</code> and assumes
 111:    * a radix of 10.
 112:    *
 113:    * @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 static String toString(short s)
 117:   {
 118:     return String.valueOf(s);
 119:   }
 120: 
 121:   /**
 122:    * Converts the specified <code>String</code> into a <code>short</code>.
 123:    * This function assumes a radix of 10.
 124:    *
 125:    * @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 static short parseShort(String s)
 131:   {
 132:     return parseShort(s, 10);
 133:   }
 134: 
 135:   /**
 136:    * Converts the specified <code>String</code> into a <code>short</code>
 137:    * using the specified radix (base). The string must not be <code>null</code>
 138:    * or empty. It may begin with an optional '-', which will negate the answer,
 139:    * provided that there are also valid digits. Each digit is parsed as if by
 140:    * <code>Character.digit(d, radix)</code>, and must be in the range
 141:    * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
 142:    * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
 143:    * 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 parseShort(String s, int radix)
 152:   {
 153:     int i = Integer.parseInt(s, radix, false);
 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>
 161:    * and specified radix (base).
 162:    *
 163:    * @param s the <code>String</code> to convert
 164:    * @param radix the radix (base) to convert with
 165:    * @return the new <code>Short</code>
 166:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 167:    *         <code>short</code>
 168:    * @see #parseShort(String, int)
 169:    */
 170:   public static Short valueOf(String s, int radix)
 171:   {
 172:     return new Short(parseShort(s, radix));
 173:   }
 174: 
 175:   /**
 176:    * Creates a new <code>Short</code> object using the <code>String</code>,
 177:    * assuming a radix of 10.
 178:    *
 179:    * @param s the <code>String</code> to convert
 180:    * @return the new <code>Short</code>
 181:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 182:    *         <code>short</code>
 183:    * @see #Short(String)
 184:    * @see #parseShort(String)
 185:    */
 186:   public static Short valueOf(String s)
 187:   {
 188:     return new Short(parseShort(s, 10));
 189:   }
 190: 
 191:   /**
 192:    * Convert the specified <code>String</code> into a <code>Short</code>.
 193:    * The <code>String</code> may represent decimal, hexadecimal, or
 194:    * octal numbers.
 195:    *
 196:    * <p>The extended BNF grammar is as follows:<br>
 197:    * <pre>
 198:    * <em>DecodableString</em>:
 199:    *      ( [ <code>-</code> ] <em>DecimalNumber</em> )
 200:    *    | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
 201:    *              | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
 202:    *    | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
 203:    * <em>DecimalNumber</em>:
 204:    *        <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
 205:    * <em>DecimalDigit</em>:
 206:    *        <em>Character.digit(d, 10) has value 0 to 9</em>
 207:    * <em>OctalDigit</em>:
 208:    *        <em>Character.digit(d, 8) has value 0 to 7</em>
 209:    * <em>DecimalDigit</em>:
 210:    *        <em>Character.digit(d, 16) has value 0 to 15</em>
 211:    * </pre>
 212:    * Finally, the value must be in the range <code>MIN_VALUE</code> to
 213:    * <code>MAX_VALUE</code>, or an exception is thrown.
 214:    *
 215:    * @param s the <code>String</code> to interpret
 216:    * @return the value of the String as a <code>Short</code>
 217:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 218:    *         <code>short</code>
 219:    * @throws NullPointerException if <code>s</code> is null
 220:    * @see Integer#decode(String)
 221:    */
 222:   public static Short decode(String s)
 223:   {
 224:     int i = Integer.parseInt(s, 10, true);
 225:     if ((short) i != i)
 226:       throw new NumberFormatException();
 227:     return new Short((short) i);
 228:   }
 229: 
 230:   /**
 231:    * Return the value of this <code>Short</code> as a <code>byte</code>.
 232:    *
 233:    * @return the byte value
 234:    */
 235:   public byte byteValue()
 236:   {
 237:     return (byte) value;
 238:   }
 239: 
 240:   /**
 241:    * Return the value of this <code>Short</code>.
 242:    *
 243:    * @return the short value
 244:    */
 245:   public short shortValue()
 246:   {
 247:     return value;
 248:   }
 249: 
 250:   /**
 251:    * Return the value of this <code>Short</code> as an <code>int</code>.
 252:    *
 253:    * @return the int value
 254:    */
 255:   public int intValue()
 256:   {
 257:     return value;
 258:   }
 259: 
 260:   /**
 261:    * Return the value of this <code>Short</code> as a <code>long</code>.
 262:    *
 263:    * @return the long value
 264:    */
 265:   public long longValue()
 266:   {
 267:     return value;
 268:   }
 269: 
 270:   /**
 271:    * Return the value of this <code>Short</code> as a <code>float</code>.
 272:    *
 273:    * @return the float value
 274:    */
 275:   public float floatValue()
 276:   {
 277:     return value;
 278:   }
 279: 
 280:   /**
 281:    * Return the value of this <code>Short</code> as a <code>double</code>.
 282:    *
 283:    * @return the double value
 284:    */
 285:   public double doubleValue()
 286:   {
 287:     return value;
 288:   }
 289: 
 290:   /**
 291:    * Converts the <code>Short</code> value to a <code>String</code> and
 292:    * assumes a radix of 10.
 293:    *
 294:    * @return the <code>String</code> representation of this <code>Short</code>
 295:    */
 296:   public String toString()
 297:   {
 298:     return String.valueOf(value);
 299:   }
 300: 
 301:   /**
 302:    * Return a hashcode representing this Object. <code>Short</code>'s hash
 303:    * code is simply its value.
 304:    *
 305:    * @return this Object's hash code
 306:    */
 307:   public int hashCode()
 308:   {
 309:     return value;
 310:   }
 311: 
 312:   /**
 313:    * Returns <code>true</code> if <code>obj</code> is an instance of
 314:    * <code>Short</code> and represents the same short value.
 315:    *
 316:    * @param obj the object to compare
 317:    * @return whether these Objects are semantically equal
 318:    */
 319:   public boolean equals(Object obj)
 320:   {
 321:     return obj instanceof Short && value == ((Short) obj).value;
 322:   }
 323: 
 324:   /**
 325:    * Compare two Shorts numerically by comparing their <code>short</code>
 326:    * values. The result is positive if the first is greater, negative if the
 327:    * second is greater, and 0 if the two are equal.
 328:    *
 329:    * @param s the Short to compare
 330:    * @return the comparison
 331:    * @since 1.2
 332:    */
 333:   public int compareTo(Short s)
 334:   {
 335:     return value - s.value;
 336:   }
 337: 
 338:   /**
 339:    * Behaves like <code>compareTo(Short)</code> unless the Object
 340:    * is not a <code>Short</code>.
 341:    *
 342:    * @param o the object to compare
 343:    * @return the comparison
 344:    * @throws ClassCastException if the argument is not a <code>Short</code>
 345:    * @see #compareTo(Short)
 346:    * @see Comparable
 347:    * @since 1.2
 348:    */
 349:   public int compareTo(Object o)
 350:   {
 351:     return compareTo((Short)o);
 352:   }
 353: }