1. /*
  2. * @(#)Short.java 1.33 03/01/23
  3. *
  4. * Copyright 2003 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.33, 01/23/03
  23. * @see java.lang.Number
  24. * @since JDK1.1
  25. */
  26. public final class Short extends Number implements Comparable {
  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 TYPE = 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. /**
  171. * Decodes a <code>String</code> into a <code>Short</code>.
  172. * Accepts decimal, hexadecimal, and octal numbers given by
  173. * the following grammar:
  174. *
  175. * <blockquote>
  176. * <dl>
  177. * <dt><i>DecodableString:</i>
  178. * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
  179. * <dd><i>Sign<sub>opt</sub></i> <code>0x</code> <i>HexDigits</i>
  180. * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>
  181. * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>
  182. * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>
  183. * <p>
  184. * <dt><i>Sign:</i>
  185. * <dd><code>-</code>
  186. * </dl>
  187. * </blockquote>
  188. *
  189. * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
  190. * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">§3.10.1</a>
  191. * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
  192. * Language Specification</a>.
  193. * <p>
  194. * The sequence of characters following an (optional) negative
  195. * sign and/or radix specifier ("<code>0x</code>",
  196. * "<code>0X</code>", "<code>#</code>", or
  197. * leading zero) is parsed as by the <code>Short.parseShort</code>
  198. * method with the indicated radix (10, 16, or 8). This sequence
  199. * of characters must represent a positive value or a {@link
  200. * NumberFormatException} will be thrown. The result is negated
  201. * if first character of the specified <code>String</code> is the
  202. * minus sign. No whitespace characters are permitted in the
  203. * <code>String</code>.
  204. *
  205. * @param nm the <code>String</code> to decode.
  206. * @return a <code>Short</code> object holding the <code>short</code>
  207. * value represented by <code>nm</code>
  208. * @exception NumberFormatException if the <code>String</code> does not
  209. * contain a parsable <code>short</code>.
  210. * @see java.lang.Short#parseShort(java.lang.String, int)
  211. */
  212. public static Short decode(String nm) throws NumberFormatException {
  213. int radix = 10;
  214. int index = 0;
  215. boolean negative = false;
  216. Short result;
  217. // Handle minus sign, if present
  218. if (nm.startsWith("-")) {
  219. negative = true;
  220. index++;
  221. }
  222. // Handle radix specifier, if present
  223. if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  224. index += 2;
  225. radix = 16;
  226. }
  227. else if (nm.startsWith("#", index)) {
  228. index ++;
  229. radix = 16;
  230. }
  231. else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  232. index ++;
  233. radix = 8;
  234. }
  235. if (nm.startsWith("-", index))
  236. throw new NumberFormatException("Negative sign in wrong position");
  237. try {
  238. result = Short.valueOf(nm.substring(index), radix);
  239. result = negative ? new Short((short)-result.shortValue()) :result;
  240. } catch (NumberFormatException e) {
  241. // If number is Short.MIN_VALUE, we'll end up here. The next line
  242. // handles this case, and causes any genuine format error to be
  243. // rethrown.
  244. String constant = negative ? new String("-" + nm.substring(index))
  245. : nm.substring(index);
  246. result = Short.valueOf(constant, radix);
  247. }
  248. return result;
  249. }
  250. /**
  251. * The value of the <code>Short</code>.
  252. *
  253. * @serial
  254. */
  255. private short value;
  256. /**
  257. * Constructs a newly allocated <code>Short</code> object that
  258. * represents the specified <code>short</code> value.
  259. *
  260. * @param value the value to be represented by the
  261. * <code>Short</code>.
  262. */
  263. public Short(short value) {
  264. this.value = value;
  265. }
  266. /**
  267. * Constructs a newly allocated <code>Short</code> object that
  268. * represents the <code>short</code> value indicated by the
  269. * <code>String</code> parameter. The string is converted to a
  270. * <code>short</code> value in exactly the manner used by the
  271. * <code>parseShort</code> method for radix 10.
  272. *
  273. * @param s the <code>String</code> to be converted to a
  274. * <code>Short</code>
  275. * @exception NumberFormatException If the <code>String</code>
  276. * does not contain a parsable <code>short</code>.
  277. * @see java.lang.Short#parseShort(java.lang.String, int)
  278. */
  279. public Short(String s) throws NumberFormatException {
  280. this.value = parseShort(s, 10);
  281. }
  282. /**
  283. * Returns the value of this <code>Short</code> as a
  284. * <code>byte</code>.
  285. */
  286. public byte byteValue() {
  287. return (byte)value;
  288. }
  289. /**
  290. * Returns the value of this <code>Short</code> as a
  291. * <code>short</code>.
  292. */
  293. public short shortValue() {
  294. return value;
  295. }
  296. /**
  297. * Returns the value of this <code>Short</code> as an
  298. * <code>int</code>.
  299. */
  300. public int intValue() {
  301. return (int)value;
  302. }
  303. /**
  304. * Returns the value of this <code>Short</code> as a
  305. * <code>long</code>.
  306. */
  307. public long longValue() {
  308. return (long)value;
  309. }
  310. /**
  311. * Returns the value of this <code>Short</code> as a
  312. * <code>float</code>.
  313. */
  314. public float floatValue() {
  315. return (float)value;
  316. }
  317. /**
  318. * Returns the value of this <code>Short</code> as a
  319. * <code>double</code>.
  320. */
  321. public double doubleValue() {
  322. return (double)value;
  323. }
  324. /**
  325. * Returns a <code>String</code> object representing this
  326. * <code>Short</code>'s value. The value is converted to signed
  327. * decimal representation and returned as a string, exactly as if
  328. * the <code>short</code> value were given as an argument to the
  329. * {@link java.lang.Short#toString(short)} method.
  330. *
  331. * @return a string representation of the value of this object in
  332. * base 10.
  333. */
  334. public String toString() {
  335. return String.valueOf((int)value);
  336. }
  337. /**
  338. * Returns a hash code for this <code>Short</code>.
  339. */
  340. public int hashCode() {
  341. return (int)value;
  342. }
  343. /**
  344. * Compares this object to the specified object. The result is
  345. * <code>true</code> if and only if the argument is not
  346. * <code>null</code> and is a <code>Short</code> object that
  347. * contains the same <code>short</code> value as this object.
  348. *
  349. * @param obj the object to compare with
  350. * @return <code>true</code> if the objects are the same;
  351. * <code>false</code> otherwise.
  352. */
  353. public boolean equals(Object obj) {
  354. if (obj instanceof Short) {
  355. return value == ((Short)obj).shortValue();
  356. }
  357. return false;
  358. }
  359. /**
  360. * Compares two <code>Short</code> objects numerically.
  361. *
  362. * @param anotherShort the <code>Short</code> to be compared.
  363. * @return the value <code>0</code> if this <code>Short</code> is
  364. * equal to the argument <code>Short</code> a value less than
  365. * <code>0</code> if this <code>Short</code> is numerically less
  366. * than the argument <code>Short</code> and a value greater than
  367. * <code>0</code> if this <code>Short</code> is numerically
  368. * greater than the argument <code>Short</code> (signed
  369. * comparison).
  370. * @since 1.2
  371. */
  372. public int compareTo(Short anotherShort) {
  373. return this.value - anotherShort.value;
  374. }
  375. /**
  376. * Compares this <code>Short</code> object to another object. If
  377. * the object is a <code>Short</code>, this function behaves like
  378. * <code>compareTo(Short)</code>. Otherwise, it throws a
  379. * <code>ClassCastException</code> (as <code>Short</code> objects
  380. * are only comparable to other <code>Short</code> objects).
  381. *
  382. * @param o the <code>Object</code> to be compared.
  383. * @return the value <code>0</code> if the argument is a
  384. * <code>Short</code> numerically equal to this
  385. * <code>Short</code> a value less than <code>0</code> if
  386. * the argument is a <code>Short</code> numerically greater
  387. * than this <code>Short</code> and a value greater than
  388. * <code>0</code> if the argument is a <code>Short</code>
  389. * numerically less than this <code>Short</code>.
  390. * @exception <code>ClassCastException</code> if the argument is not a
  391. * <code>Short</code>.
  392. * @see java.lang.Comparable
  393. * @since 1.2
  394. */
  395. public int compareTo(Object o) {
  396. return compareTo((Short)o);
  397. }
  398. /** use serialVersionUID from JDK 1.1. for interoperability */
  399. private static final long serialVersionUID = 7515723908773894738L;
  400. }