1. /*
  2. * @(#)Short.java 1.43 04/05/11
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.lang;
  8. /**
  9. * The <code>Short</code> class wraps a value of primitive type
  10. * <code>short</code> in an object. An object of type
  11. * <code>Short</code> contains a single field whose type is
  12. * <code>short</code>.
  13. *
  14. * <p>
  15. *
  16. * In addition, this class provides several methods for converting a
  17. * <code>short</code> to a <code>String</code> and a
  18. * <code>String</code> to a <code>short</code>, as well as other
  19. * constants and methods useful when dealing with a <code>short</code>.
  20. *
  21. * @author Nakul Saraiya
  22. * @version 1.43, 05/11/04
  23. * @see java.lang.Number
  24. * @since JDK1.1
  25. */
  26. public final class Short extends Number implements Comparable<Short> {
  27. /**
  28. * A constant holding the minimum value a <code>short</code> can
  29. * have, -2<sup>15</sup>.
  30. */
  31. public static final short MIN_VALUE = -32768;
  32. /**
  33. * A constant holding the maximum value a <code>short</code> can
  34. * have, 2<sup>15</sup>-1.
  35. */
  36. public static final short MAX_VALUE = 32767;
  37. /**
  38. * The <code>Class</code> instance representing the primitive type
  39. * <code>short</code>.
  40. */
  41. public static final Class<Short> TYPE = (Class<Short>) Class.getPrimitiveClass("short");
  42. /**
  43. * Returns a new <code>String</code> object representing the
  44. * specified <code>short</code>. The radix is assumed to be 10.
  45. *
  46. * @param s the <code>short</code> to be converted
  47. * @return the string representation of the specified <code>short</code>
  48. * @see java.lang.Integer#toString(int)
  49. */
  50. public static String toString(short s) {
  51. return Integer.toString((int)s, 10);
  52. }
  53. /**
  54. * Parses the string argument as a signed decimal
  55. * <code>short</code>. The characters in the string must all be
  56. * decimal digits, except that the first character may be an ASCII
  57. * minus sign <code>'-'</code> (<code>'\u002D'</code>) to
  58. * indicate a negative value. The resulting <code>short</code> value is
  59. * returned, exactly as if the argument and the radix 10 were
  60. * given as arguments to the {@link #parseShort(java.lang.String,
  61. * int)} method.
  62. *
  63. * @param s a <code>String</code> containing the <code>short</code>
  64. * representation to be parsed
  65. * @return the <code>short</code> value represented by the
  66. * argument in decimal.
  67. * @exception NumberFormatException If the string does not
  68. * contain a parsable <code>short</code>.
  69. */
  70. public static short parseShort(String s) throws NumberFormatException {
  71. return parseShort(s, 10);
  72. }
  73. /**
  74. * Parses the string argument as a signed <code>short</code> in
  75. * the radix specified by the second argument. The characters in
  76. * the string must all be digits, of the specified radix (as
  77. * determined by whether {@link java.lang.Character#digit(char,
  78. * int)} returns a nonnegative value) except that the first
  79. * character may be an ASCII minus sign <code>'-'</code>
  80. * (<code>'\u002D'</code>) to indicate a negative value. The
  81. * resulting <code>byte</code> value is returned.
  82. * <p>
  83. * An exception of type <code>NumberFormatException</code> is
  84. * thrown if any of the following situations occurs:
  85. * <ul>
  86. * <li> The first argument is <code>null</code> or is a string of
  87. * length zero.
  88. *
  89. * <li> The radix is either smaller than {@link
  90. * java.lang.Character#MIN_RADIX} or larger than {@link
  91. * java.lang.Character#MAX_RADIX}.
  92. *
  93. * <li> Any character of the string is not a digit of the specified
  94. * radix, except that the first character may be a minus sign
  95. * <code>'-'</code> (<code>'\u002D'</code>) provided that the
  96. * string is longer than length 1.
  97. *
  98. * <li> The value represented by the string is not a value of type
  99. * <code>short</code>.
  100. * </ul>
  101. *
  102. * @param s the <code>String</code> containing the
  103. * <code>short</code> representation to be parsed
  104. * @param radix the radix to be used while parsing <code>s</code>
  105. * @return the <code>short</code> represented by the string
  106. * argument in the specified radix.
  107. * @exception NumberFormatException If the <code>String</code>
  108. * does not contain a parsable <code>short</code>.
  109. */
  110. public static short parseShort(String s, int radix)
  111. throws NumberFormatException {
  112. int i = Integer.parseInt(s, radix);
  113. if (i < MIN_VALUE || i > MAX_VALUE)
  114. throw new NumberFormatException(
  115. "Value out of range. Value:\"" + s + "\" Radix:" + radix);
  116. return (short)i;
  117. }
  118. /**
  119. * Returns a <code>Short</code> object holding the value
  120. * extracted from the specified <code>String</code> when parsed
  121. * with the radix given by the second argument. The first argument
  122. * is interpreted as representing a signed <code>short</code> in
  123. * the radix specified by the second argument, exactly as if the
  124. * argument were given to the {@link #parseShort(java.lang.String,
  125. * int)} method. The result is a <code>Short</code> object that
  126. * represents the <code>short</code> value specified by the string.
  127. * <p> In other words, this method returns a <code>Short</code> object
  128. * equal to the value of:
  129. *
  130. * <blockquote><code>
  131. * new Short(Short.parseShort(s, radix))
  132. * </code></blockquote>
  133. *
  134. * @param s the string to be parsed
  135. * @param radix the radix to be used in interpreting <code>s</code>
  136. * @return a <code>Short</code> object holding the value
  137. * represented by the string argument in the
  138. * specified radix.
  139. * @exception NumberFormatException If the <code>String</code> does
  140. * not contain a parsable <code>short</code>.
  141. */
  142. public static Short valueOf(String s, int radix)
  143. throws NumberFormatException {
  144. return new Short(parseShort(s, radix));
  145. }
  146. /**
  147. * Returns a <code>Short</code> object holding the
  148. * value given by the specified <code>String</code>. The argument
  149. * is interpreted as representing a signed decimal
  150. * <code>short</code>, exactly as if the argument were given to
  151. * the {@link #parseShort(java.lang.String)} method. The result is
  152. * a <code>Short</code> object that represents the
  153. * <code>short</code> value specified by the string. <p> In other
  154. * words, this method returns a <code>Byte</code> object equal to
  155. * the value of:
  156. *
  157. * <blockquote><code>
  158. * new Short(Short.parseShort(s))
  159. * </code></blockquote>
  160. *
  161. * @param s the string to be parsed
  162. * @return a <code>Short</code> object holding the value
  163. * represented by the string argument
  164. * @exception NumberFormatException If the <code>String</code> does
  165. * not contain a parsable <code>short</code>.
  166. */
  167. public static Short valueOf(String s) throws NumberFormatException {
  168. return valueOf(s, 10);
  169. }
  170. private static class ShortCache {
  171. private ShortCache(){}
  172. static final Short cache[] = new Short[-(-128) + 127 + 1];
  173. static {
  174. for(int i = 0; i < cache.length; i++)
  175. cache[i] = new Short((short)(i - 128));
  176. }
  177. }
  178. /**
  179. * Returns a <tt>Short</tt> instance representing the specified
  180. * <tt>short</tt> value.
  181. * If a new <tt>Short</tt> instance is not required, this method
  182. * should generally be used in preference to the constructor
  183. * {@link #Short(short)}, as this method is likely to yield
  184. * significantly better space and time performance by caching
  185. * frequently requested values.
  186. *
  187. * @param s a short value.
  188. * @return a <tt>Short</tt> instance representing <tt>s</tt>.
  189. * @since 1.5
  190. */
  191. public static Short valueOf(short s) {
  192. final int offset = 128;
  193. int sAsInt = s;
  194. if (sAsInt >= -128 && sAsInt <= 127) { // must cache
  195. return ShortCache.cache[sAsInt + offset];
  196. }
  197. return new Short(s);
  198. }
  199. /**
  200. * Decodes a <code>String</code> into a <code>Short</code>.
  201. * Accepts decimal, hexadecimal, and octal numbers given by
  202. * the following grammar:
  203. *
  204. * <blockquote>
  205. * <dl>
  206. * <dt><i>DecodableString:</i>
  207. * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
  208. * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
  209. * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
  210. * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
  211. * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
  212. * <p>
  213. * <dt><i>Sign:</i>
  214. * <dd><code>-</code>
  215. * </dl>
  216. * </blockquote>
  217. *
  218. * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
  219. * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a>
  220. * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
  221. * Language Specification</a>.
  222. * <p>
  223. * The sequence of characters following an (optional) negative
  224. * sign and/or radix specifier ("<code>0x</code>",
  225. * "<code>0X</code>", "<code>#</code>", or
  226. * leading zero) is parsed as by the <code>Short.parseShort</code>
  227. * method with the indicated radix (10, 16, or 8). This sequence
  228. * of characters must represent a positive value or a {@link
  229. * NumberFormatException} will be thrown. The result is negated
  230. * if first character of the specified <code>String</code> is the
  231. * minus sign. No whitespace characters are permitted in the
  232. * <code>String</code>.
  233. *
  234. * @param nm the <code>String</code> to decode.
  235. * @return a <code>Short</code> object holding the <code>short</code>
  236. * value represented by <code>nm</code>
  237. * @exception NumberFormatException if the <code>String</code> does not
  238. * contain a parsable <code>short</code>.
  239. * @see java.lang.Short#parseShort(java.lang.String, int)
  240. */
  241. public static Short decode(String nm) throws NumberFormatException {
  242. int radix = 10;
  243. int index = 0;
  244. boolean negative = false;
  245. Short result;
  246. // Handle minus sign, if present
  247. if (nm.startsWith("-")) {
  248. negative = true;
  249. index++;
  250. }
  251. // Handle radix specifier, if present
  252. if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  253. index += 2;
  254. radix = 16;
  255. }
  256. else if (nm.startsWith("#", index)) {
  257. index ++;
  258. radix = 16;
  259. }
  260. else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  261. index ++;
  262. radix = 8;
  263. }
  264. if (nm.startsWith("-", index))
  265. throw new NumberFormatException("Negative sign in wrong position");
  266. try {
  267. result = Short.valueOf(nm.substring(index), radix);
  268. result = negative ? new Short((short)-result.shortValue()) :result;
  269. } catch (NumberFormatException e) {
  270. // If number is Short.MIN_VALUE, we'll end up here. The next line
  271. // handles this case, and causes any genuine format error to be
  272. // rethrown.
  273. String constant = negative ? new String("-" + nm.substring(index))
  274. : nm.substring(index);
  275. result = Short.valueOf(constant, radix);
  276. }
  277. return result;
  278. }
  279. /**
  280. * The value of the <code>Short</code>.
  281. *
  282. * @serial
  283. */
  284. private final short value;
  285. /**
  286. * Constructs a newly allocated <code>Short</code> object that
  287. * represents the specified <code>short</code> value.
  288. *
  289. * @param value the value to be represented by the
  290. * <code>Short</code>.
  291. */
  292. public Short(short value) {
  293. this.value = value;
  294. }
  295. /**
  296. * Constructs a newly allocated <code>Short</code> object that
  297. * represents the <code>short</code> value indicated by the
  298. * <code>String</code> parameter. The string is converted to a
  299. * <code>short</code> value in exactly the manner used by the
  300. * <code>parseShort</code> method for radix 10.
  301. *
  302. * @param s the <code>String</code> to be converted to a
  303. * <code>Short</code>
  304. * @exception NumberFormatException If the <code>String</code>
  305. * does not contain a parsable <code>short</code>.
  306. * @see java.lang.Short#parseShort(java.lang.String, int)
  307. */
  308. public Short(String s) throws NumberFormatException {
  309. this.value = parseShort(s, 10);
  310. }
  311. /**
  312. * Returns the value of this <code>Short</code> as a
  313. * <code>byte</code>.
  314. */
  315. public byte byteValue() {
  316. return (byte)value;
  317. }
  318. /**
  319. * Returns the value of this <code>Short</code> as a
  320. * <code>short</code>.
  321. */
  322. public short shortValue() {
  323. return value;
  324. }
  325. /**
  326. * Returns the value of this <code>Short</code> as an
  327. * <code>int</code>.
  328. */
  329. public int intValue() {
  330. return (int)value;
  331. }
  332. /**
  333. * Returns the value of this <code>Short</code> as a
  334. * <code>long</code>.
  335. */
  336. public long longValue() {
  337. return (long)value;
  338. }
  339. /**
  340. * Returns the value of this <code>Short</code> as a
  341. * <code>float</code>.
  342. */
  343. public float floatValue() {
  344. return (float)value;
  345. }
  346. /**
  347. * Returns the value of this <code>Short</code> as a
  348. * <code>double</code>.
  349. */
  350. public double doubleValue() {
  351. return (double)value;
  352. }
  353. /**
  354. * Returns a <code>String</code> object representing this
  355. * <code>Short</code>'s value. The value is converted to signed
  356. * decimal representation and returned as a string, exactly as if
  357. * the <code>short</code> value were given as an argument to the
  358. * {@link java.lang.Short#toString(short)} method.
  359. *
  360. * @return a string representation of the value of this object in
  361. * base 10.
  362. */
  363. public String toString() {
  364. return String.valueOf((int)value);
  365. }
  366. /**
  367. * Returns a hash code for this <code>Short</code>.
  368. */
  369. public int hashCode() {
  370. return (int)value;
  371. }
  372. /**
  373. * Compares this object to the specified object. The result is
  374. * <code>true</code> if and only if the argument is not
  375. * <code>null</code> and is a <code>Short</code> object that
  376. * contains the same <code>short</code> value as this object.
  377. *
  378. * @param obj the object to compare with
  379. * @return <code>true</code> if the objects are the same;
  380. * <code>false</code> otherwise.
  381. */
  382. public boolean equals(Object obj) {
  383. if (obj instanceof Short) {
  384. return value == ((Short)obj).shortValue();
  385. }
  386. return false;
  387. }
  388. /**
  389. * Compares two <code>Short</code> objects numerically.
  390. *
  391. * @param anotherShort the <code>Short</code> to be compared.
  392. * @return the value <code>0</code> if this <code>Short</code> is
  393. * equal to the argument <code>Short</code> a value less than
  394. * <code>0</code> if this <code>Short</code> is numerically less
  395. * than the argument <code>Short</code> and a value greater than
  396. * <code>0</code> if this <code>Short</code> is numerically
  397. * greater than the argument <code>Short</code> (signed
  398. * comparison).
  399. * @since 1.2
  400. */
  401. public int compareTo(Short anotherShort) {
  402. return this.value - anotherShort.value;
  403. }
  404. /**
  405. * The number of bits used to represent a <tt>short</tt> value in two's
  406. * complement binary form.
  407. */
  408. public static final int SIZE = 16;
  409. /**
  410. * Returns the value obtained by reversing the order of the bytes in the
  411. * two's complement representation of the specified <tt>short</tt> value.
  412. *
  413. * @return the value obtained by reversing (or, equivalently, swapping)
  414. * the bytes in the specified <tt>short</tt> value.
  415. * @since 1.5
  416. */
  417. public static short reverseBytes(short i) {
  418. return (short) (((i & 0xFF00) >> 8) | (i << 8));
  419. }
  420. /** use serialVersionUID from JDK 1.1. for interoperability */
  421. private static final long serialVersionUID = 7515723908773894738L;
  422. }