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 number of bits needed to represent a <code>short</code>.
  81:    * @since 1.5
  82:    */
  83:   public static final int SIZE = 16;
  84: 
  85:   // This caches some Short values, and is used by boxing conversions
  86:   // via valueOf().  We must cache at least -128..127; these constants
  87:   // control how much we actually cache.
  88:   private static final int MIN_CACHE = -128;
  89:   private static final int MAX_CACHE = 127;
  90:   private static Short[] shortCache = new Short[MAX_CACHE - MIN_CACHE + 1];
  91: 
  92:   /**
  93:    * The immutable value of this Short.
  94:    *
  95:    * @serial the wrapped short
  96:    */
  97:   private final short value;
  98: 
  99:   /**
 100:    * Create a <code>Short</code> object representing the value of the
 101:    * <code>short</code> argument.
 102:    *
 103:    * @param value the value to use
 104:    */
 105:   public Short(short value)
 106:   {
 107:     this.value = value;
 108:   }
 109: 
 110:   /**
 111:    * Create a <code>Short</code> object representing the value of the
 112:    * argument after conversion to a <code>short</code>.
 113:    *
 114:    * @param s the string to convert
 115:    * @throws NumberFormatException if the String cannot be parsed
 116:    */
 117:   public Short(String s)
 118:   {
 119:     value = parseShort(s, 10);
 120:   }
 121: 
 122:   /**
 123:    * Converts the <code>short</code> to a <code>String</code> and assumes
 124:    * a radix of 10.
 125:    *
 126:    * @param s the <code>short</code> to convert to <code>String</code>
 127:    * @return the <code>String</code> representation of the argument
 128:    */
 129:   public static String toString(short s)
 130:   {
 131:     return String.valueOf(s);
 132:   }
 133: 
 134:   /**
 135:    * Converts the specified <code>String</code> into a <code>short</code>.
 136:    * This function assumes a radix of 10.
 137:    *
 138:    * @param s the <code>String</code> to convert
 139:    * @return the <code>short</code> value of <code>s</code>
 140:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 141:    *         <code>short</code>
 142:    */
 143:   public static short parseShort(String s)
 144:   {
 145:     return parseShort(s, 10);
 146:   }
 147: 
 148:   /**
 149:    * Converts the specified <code>String</code> into a <code>short</code>
 150:    * using the specified radix (base). The string must not be <code>null</code>
 151:    * or empty. It may begin with an optional '-', which will negate the answer,
 152:    * provided that there are also valid digits. Each digit is parsed as if by
 153:    * <code>Character.digit(d, radix)</code>, and must be in the range
 154:    * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
 155:    * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
 156:    * Unlike Double.parseDouble, you may not have a leading '+'.
 157:    *
 158:    * @param s the <code>String</code> to convert
 159:    * @param radix the radix (base) to use in the conversion
 160:    * @return the <code>String</code> argument converted to <code>short</code>
 161:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 162:    *         <code>short</code>
 163:    */
 164:   public static short parseShort(String s, int radix)
 165:   {
 166:     int i = Integer.parseInt(s, radix, false);
 167:     if ((short) i != i)
 168:       throw new NumberFormatException();
 169:     return (short) i;
 170:   }
 171: 
 172:   /**
 173:    * Creates a new <code>Short</code> object using the <code>String</code>
 174:    * and specified radix (base).
 175:    *
 176:    * @param s the <code>String</code> to convert
 177:    * @param radix the radix (base) to convert with
 178:    * @return the new <code>Short</code>
 179:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 180:    *         <code>short</code>
 181:    * @see #parseShort(String, int)
 182:    */
 183:   public static Short valueOf(String s, int radix)
 184:   {
 185:     return new Short(parseShort(s, radix));
 186:   }
 187: 
 188:   /**
 189:    * Creates a new <code>Short</code> object using the <code>String</code>,
 190:    * assuming a radix of 10.
 191:    *
 192:    * @param s the <code>String</code> to convert
 193:    * @return the new <code>Short</code>
 194:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 195:    *         <code>short</code>
 196:    * @see #Short(String)
 197:    * @see #parseShort(String)
 198:    */
 199:   public static Short valueOf(String s)
 200:   {
 201:     return new Short(parseShort(s, 10));
 202:   }
 203: 
 204:   /**
 205:    * Returns a <code>Short</code> object wrapping the value.
 206:    * In contrast to the <code>Short</code> constructor, this method
 207:    * will cache some values.  It is used by boxing conversion.
 208:    *
 209:    * @param val the value to wrap
 210:    * @return the <code>Short</code>
 211:    *
 212:    * @since 1.5
 213:    */
 214:   public static Short valueOf(short val)
 215:   {
 216:     if (val < MIN_CACHE || val > MAX_CACHE)
 217:       return new Short(val);
 218:     synchronized (shortCache)
 219:       {
 220:     if (shortCache[val - MIN_CACHE] == null)
 221:       shortCache[val - MIN_CACHE] = new Short(val);
 222:     return shortCache[val - MIN_CACHE];
 223:       }
 224:   }
 225: 
 226:   /**
 227:    * Convert the specified <code>String</code> into a <code>Short</code>.
 228:    * The <code>String</code> may represent decimal, hexadecimal, or
 229:    * octal numbers.
 230:    *
 231:    * <p>The extended BNF grammar is as follows:<br>
 232:    * <pre>
 233:    * <em>DecodableString</em>:
 234:    *      ( [ <code>-</code> ] <em>DecimalNumber</em> )
 235:    *    | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
 236:    *              | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
 237:    *    | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
 238:    * <em>DecimalNumber</em>:
 239:    *        <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
 240:    * <em>DecimalDigit</em>:
 241:    *        <em>Character.digit(d, 10) has value 0 to 9</em>
 242:    * <em>OctalDigit</em>:
 243:    *        <em>Character.digit(d, 8) has value 0 to 7</em>
 244:    * <em>DecimalDigit</em>:
 245:    *        <em>Character.digit(d, 16) has value 0 to 15</em>
 246:    * </pre>
 247:    * Finally, the value must be in the range <code>MIN_VALUE</code> to
 248:    * <code>MAX_VALUE</code>, or an exception is thrown.
 249:    *
 250:    * @param s the <code>String</code> to interpret
 251:    * @return the value of the String as a <code>Short</code>
 252:    * @throws NumberFormatException if <code>s</code> cannot be parsed as a
 253:    *         <code>short</code>
 254:    * @throws NullPointerException if <code>s</code> is null
 255:    * @see Integer#decode(String)
 256:    */
 257:   public static Short decode(String s)
 258:   {
 259:     int i = Integer.parseInt(s, 10, true);
 260:     if ((short) i != i)
 261:       throw new NumberFormatException();
 262:     return new Short((short) i);
 263:   }
 264: 
 265:   /**
 266:    * Return the value of this <code>Short</code> as a <code>byte</code>.
 267:    *
 268:    * @return the byte value
 269:    */
 270:   public byte byteValue()
 271:   {
 272:     return (byte) value;
 273:   }
 274: 
 275:   /**
 276:    * Return the value of this <code>Short</code>.
 277:    *
 278:    * @return the short value
 279:    */
 280:   public short shortValue()
 281:   {
 282:     return value;
 283:   }
 284: 
 285:   /**
 286:    * Return the value of this <code>Short</code> as an <code>int</code>.
 287:    *
 288:    * @return the int value
 289:    */
 290:   public int intValue()
 291:   {
 292:     return value;
 293:   }
 294: 
 295:   /**
 296:    * Return the value of this <code>Short</code> as a <code>long</code>.
 297:    *
 298:    * @return the long value
 299:    */
 300:   public long longValue()
 301:   {
 302:     return value;
 303:   }
 304: 
 305:   /**
 306:    * Return the value of this <code>Short</code> as a <code>float</code>.
 307:    *
 308:    * @return the float value
 309:    */
 310:   public float floatValue()
 311:   {
 312:     return value;
 313:   }
 314: 
 315:   /**
 316:    * Return the value of this <code>Short</code> as a <code>double</code>.
 317:    *
 318:    * @return the double value
 319:    */
 320:   public double doubleValue()
 321:   {
 322:     return value;
 323:   }
 324: 
 325:   /**
 326:    * Converts the <code>Short</code> value to a <code>String</code> and
 327:    * assumes a radix of 10.
 328:    *
 329:    * @return the <code>String</code> representation of this <code>Short</code>
 330:    */
 331:   public String toString()
 332:   {
 333:     return String.valueOf(value);
 334:   }
 335: 
 336:   /**
 337:    * Return a hashcode representing this Object. <code>Short</code>'s hash
 338:    * code is simply its value.
 339:    *
 340:    * @return this Object's hash code
 341:    */
 342:   public int hashCode()
 343:   {
 344:     return value;
 345:   }
 346: 
 347:   /**
 348:    * Returns <code>true</code> if <code>obj</code> is an instance of
 349:    * <code>Short</code> and represents the same short value.
 350:    *
 351:    * @param obj the object to compare
 352:    * @return whether these Objects are semantically equal
 353:    */
 354:   public boolean equals(Object obj)
 355:   {
 356:     return obj instanceof Short && value == ((Short) obj).value;
 357:   }
 358: 
 359:   /**
 360:    * Compare two Shorts numerically by comparing their <code>short</code>
 361:    * values. The result is positive if the first is greater, negative if the
 362:    * second is greater, and 0 if the two are equal.
 363:    *
 364:    * @param s the Short to compare
 365:    * @return the comparison
 366:    * @since 1.2
 367:    */
 368:   public int compareTo(Short s)
 369:   {
 370:     return value - s.value;
 371:   }
 372: 
 373:   /**
 374:    * Behaves like <code>compareTo(Short)</code> unless the Object
 375:    * is not a <code>Short</code>.
 376:    *
 377:    * @param o the object to compare
 378:    * @return the comparison
 379:    * @throws ClassCastException if the argument is not a <code>Short</code>
 380:    * @see #compareTo(Short)
 381:    * @see Comparable
 382:    * @since 1.2
 383:    */
 384:   public int compareTo(Object o)
 385:   {
 386:     return compareTo((Short)o);
 387:   }
 388: 
 389:   /**
 390:    * Reverse the bytes in val.
 391:    * @since 1.5
 392:    */
 393:   public static short reverseBytes(short val)
 394:   {
 395:     return (short) (((val >> 8) & 0xff) | ((val << 8) & 0xff00));
 396:   }
 397: }