Source for java.lang.Short

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