1 7 8 package java.lang; 9 10 28 public final class Short extends Number implements Comparable <Short > { 29 30 34 public static final short MIN_VALUE = -32768; 35 36 40 public static final short MAX_VALUE = 32767; 41 42 46 public static final Class <Short > TYPE = (Class <Short >) Class.getPrimitiveClass("short"); 47 48 56 public static String toString(short s) { 57 return Integer.toString((int)s, 10); 58 } 59 60 77 public static short parseShort(String s) throws NumberFormatException { 78 return parseShort(s, 10); 79 } 80 81 118 public static short parseShort(String s, int radix) 119 throws NumberFormatException { 120 int i = Integer.parseInt(s, radix); 121 if (i < MIN_VALUE || i > MAX_VALUE) 122 throw new NumberFormatException ( 123 "Value out of range. Value:\"" + s + "\" Radix:" + radix); 124 return (short)i; 125 } 126 127 151 public static Short valueOf(String s, int radix) 152 throws NumberFormatException { 153 return new Short (parseShort(s, radix)); 154 } 155 156 177 public static Short valueOf(String s) throws NumberFormatException { 178 return valueOf(s, 10); 179 } 180 181 private static class ShortCache { 182 private ShortCache(){} 183 184 static final Short cache[] = new Short [-(-128) + 127 + 1]; 185 186 static { 187 for(int i = 0; i < cache.length; i++) 188 cache[i] = new Short ((short)(i - 128)); 189 } 190 } 191 192 205 public static Short valueOf(short s) { 206 final int offset = 128; 207 int sAsInt = s; 208 if (sAsInt >= -128 && sAsInt <= 127) { return ShortCache.cache[sAsInt + offset]; 210 } 211 return new Short (s); 212 } 213 214 256 public static Short decode(String nm) throws NumberFormatException { 257 int radix = 10; 258 int index = 0; 259 boolean negative = false; 260 Short result; 261 262 if (nm.startsWith("-")) { 264 negative = true; 265 index++; 266 } 267 268 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { 270 index += 2; 271 radix = 16; 272 } 273 else if (nm.startsWith("#", index)) { 274 index ++; 275 radix = 16; 276 } 277 else if (nm.startsWith("0", index) && nm.length() > 1 + index) { 278 index ++; 279 radix = 8; 280 } 281 282 if (nm.startsWith("-", index)) 283 throw new NumberFormatException ("Negative sign in wrong position"); 284 285 try { 286 result = Short.valueOf(nm.substring(index), radix); 287 result = negative ? new Short ((short)-result.shortValue()) :result; 288 } catch (NumberFormatException e) { 289 String constant = negative ? new String ("-" + nm.substring(index)) 293 : nm.substring(index); 294 result = Short.valueOf(constant, radix); 295 } 296 return result; 297 } 298 299 304 private final short value; 305 306 313 public Short(short value) { 314 this.value = value; 315 } 316 317 330 public Short(String s) throws NumberFormatException { 331 this.value = parseShort(s, 10); 332 } 333 334 338 public byte byteValue() { 339 return (byte)value; 340 } 341 342 346 public short shortValue() { 347 return value; 348 } 349 350 354 public int intValue() { 355 return (int)value; 356 } 357 358 362 public long longValue() { 363 return (long)value; 364 } 365 366 370 public float floatValue() { 371 return (float)value; 372 } 373 374 378 public double doubleValue() { 379 return (double)value; 380 } 381 382 392 public String toString() { 393 return String.valueOf((int)value); 394 } 395 396 399 public int hashCode() { 400 return (int)value; 401 } 402 403 413 public boolean equals(Object obj) { 414 if (obj instanceof Short ) { 415 return value == ((Short )obj).shortValue(); 416 } 417 return false; 418 } 419 420 433 public int compareTo(Short anotherShort) { 434 return this.value - anotherShort.value; 435 } 436 437 441 public static final int SIZE = 16; 442 443 451 public static short reverseBytes(short i) { 452 return (short) (((i & 0xFF00) >> 8) | (i << 8)); 453 } 454 455 456 private static final long serialVersionUID = 7515723908773894738L; 457 } 458 | Popular Tags |