1. /*
  2. * @(#)Short.java 1.22 00/02/02
  3. *
  4. * Copyright 1996-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.lang;
  11. /**
  12. * The Short class is the standard wrapper for short values.
  13. *
  14. * @author Nakul Saraiya
  15. * @version 1.22, 02/02/00
  16. * @see java.lang.Number
  17. * @since JDK1.1
  18. */
  19. public final
  20. class Short extends Number implements Comparable {
  21. /**
  22. * The minimum value a Short can have.
  23. */
  24. public static final short MIN_VALUE = -32768;
  25. /**
  26. * The maximum value a Short can have.
  27. */
  28. public static final short MAX_VALUE = 32767;
  29. /**
  30. * The Class object representing the primitive type short.
  31. */
  32. public static final Class TYPE = Class.getPrimitiveClass("short");
  33. /**
  34. * Returns a new String object representing the specified
  35. * Short. The radix is assumed to be 10.
  36. *
  37. * @param s the short to be converted
  38. * @return The String that represents the specified short in radix 10.
  39. */
  40. public static String toString(short s) {
  41. return Integer.toString((int)s, 10);
  42. }
  43. /**
  44. * Assuming the specified String represents a short, returns
  45. * that short's value. Throws an exception if the String cannot
  46. * be parsed as a short. The radix is assumed to be 10.
  47. *
  48. * @param s the String containing the short
  49. * @return short the value represented by the specified string
  50. * @exception NumberFormatException If the string does not
  51. * contain a parsable short.
  52. */
  53. public static short parseShort(String s) throws NumberFormatException {
  54. return parseShort(s, 10);
  55. }
  56. /**
  57. * Assuming the specified String represents a short, returns
  58. * that short's value. Throws an exception if the String cannot
  59. * be parsed as a short.
  60. *
  61. * @param s the String containing the short
  62. * @param radix the radix to be used
  63. * @return The short value represented by the specified string in
  64. * the specified radix.
  65. * @exception NumberFormatException If the String does not
  66. * contain a parsable short.
  67. */
  68. public static short parseShort(String s, int radix)
  69. throws NumberFormatException {
  70. int i = Integer.parseInt(s, radix);
  71. if (i < MIN_VALUE || i > MAX_VALUE)
  72. throw new NumberFormatException();
  73. return (short)i;
  74. }
  75. /**
  76. * Assuming the specified String represents a short, returns a
  77. * new Short object initialized to that value. Throws an
  78. * exception if the String cannot be parsed as a short.
  79. *
  80. * @param s the String containing the integer
  81. * @param radix the radix to be used
  82. * @return The Short value represented by the specified string in
  83. * the specified radix.
  84. * @exception NumberFormatException If the String does not
  85. * contain a parsable short.
  86. */
  87. public static Short valueOf(String s, int radix)
  88. throws NumberFormatException {
  89. return new Short(parseShort(s, radix));
  90. }
  91. /**
  92. * Assuming the specified String represents a short, returns a
  93. * new Short object initialized to that value. Throws an
  94. * exception if the String cannot be parsed as a short.
  95. *
  96. * @param s the String containing the integer
  97. * @return Short of the value represented by the specified string in
  98. * radix 10.
  99. * @exception NumberFormatException If the String does not
  100. * contain a parsable short.
  101. */
  102. public static Short valueOf(String s) throws NumberFormatException {
  103. return valueOf(s, 10);
  104. }
  105. /**
  106. * Decodes a <code>String</code> into a <code>Short</code>. Accepts
  107. * decimal, hexadecimal, and octal numbers, in the following formats:
  108. * <pre>
  109. * [-] decimal constant
  110. * [-] 0x hex constant
  111. * [-] # hex constant
  112. * [-] 0 octal constant
  113. * </pre>
  114. *
  115. * The constant following an (optional) negative sign and/or "radix
  116. * specifier" is parsed as by the <code>Short.parseShort</code> method
  117. * with the specified radix (10, 8 or 16). This constant must be positive
  118. * or a NumberFormatException will result. The result is made negative if
  119. * first character of the specified <code>String</code> is the negative
  120. * sign. No whitespace characters are permitted in the
  121. * <code>String</code>.
  122. *
  123. * @param nm the <code>String</code> to decode.
  124. * @return the <code>Short</code> represented by the specified string.
  125. * @exception NumberFormatException if the <code>String</code> does not
  126. * contain a parsable short.
  127. * @see java.lang.Short#parseShort(String, int)
  128. */
  129. public static Short decode(String nm) throws NumberFormatException {
  130. int radix = 10;
  131. int index = 0;
  132. boolean negative = false;
  133. Short result;
  134. // Handle minus sign, if present
  135. if (nm.startsWith("-")) {
  136. negative = true;
  137. index++;
  138. }
  139. // Handle radix specifier, if present
  140. if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
  141. index += 2;
  142. radix = 16;
  143. }
  144. else if (nm.startsWith("#", index)) {
  145. index ++;
  146. radix = 16;
  147. }
  148. else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
  149. index ++;
  150. radix = 8;
  151. }
  152. if (nm.startsWith("-", index))
  153. throw new NumberFormatException("Negative sign in wrong position");
  154. try {
  155. result = Short.valueOf(nm.substring(index), radix);
  156. result = negative ? new Short((short)-result.shortValue()) :result;
  157. } catch (NumberFormatException e) {
  158. // If number is Short.MIN_VALUE, we'll end up here. The next line
  159. // handles this case, and causes any genuine format error to be
  160. // rethrown.
  161. String constant = negative ? new String("-" + nm.substring(index))
  162. : nm.substring(index);
  163. result = Short.valueOf(constant, radix);
  164. }
  165. return result;
  166. }
  167. /**
  168. * The value of the Short.
  169. *
  170. * @serial
  171. */
  172. private short value;
  173. /**
  174. * Constructs a Short object initialized to the specified short value.
  175. *
  176. * @param value the initial value of the Short
  177. */
  178. public Short(short value) {
  179. this.value = value;
  180. }
  181. /**
  182. * Constructs a Short object initialized to the value specified by the
  183. * String parameter. The radix is assumed to be 10.
  184. *
  185. * @param s the String to be converted to a Short
  186. * @exception NumberFormatException If the String does not
  187. * contain a parsable short.
  188. */
  189. public Short(String s) throws NumberFormatException {
  190. this.value = parseShort(s);
  191. }
  192. /**
  193. * Returns the value of this Short as a byte.
  194. */
  195. public byte byteValue() {
  196. return (byte)value;
  197. }
  198. /**
  199. * Returns the value of this Short as a short.
  200. */
  201. public short shortValue() {
  202. return value;
  203. }
  204. /**
  205. * Returns the value of this Short as an int.
  206. */
  207. public int intValue() {
  208. return (int)value;
  209. }
  210. /**
  211. * Returns the value of this Short as a long.
  212. */
  213. public long longValue() {
  214. return (long)value;
  215. }
  216. /**
  217. * Returns the value of this Short as a float.
  218. */
  219. public float floatValue() {
  220. return (float)value;
  221. }
  222. /**
  223. * Returns the value of this Short as a double.
  224. */
  225. public double doubleValue() {
  226. return (double)value;
  227. }
  228. /**
  229. * Returns a String object representing this Short's value.
  230. */
  231. public String toString() {
  232. return String.valueOf((int)value);
  233. }
  234. /**
  235. * Returns a hashcode for this Short.
  236. */
  237. public int hashCode() {
  238. return (int)value;
  239. }
  240. /**
  241. * Compares this object to the specified object.
  242. *
  243. * @param obj the object to compare with
  244. * @return true if the objects are the same; false otherwise.
  245. */
  246. public boolean equals(Object obj) {
  247. if (obj instanceof Short) {
  248. return value == ((Short)obj).shortValue();
  249. }
  250. return false;
  251. }
  252. /**
  253. * Compares two Shorts numerically.
  254. *
  255. * @param anotherShort the <code>Short</code> to be compared.
  256. * @return the value <code>0</code> if the argument Short is equal to
  257. * this Short; a value less than <code>0</code> if this Short
  258. * is numerically less than the Short argument; and a
  259. * value greater than <code>0</code> if this Short is
  260. * numerically greater than the Short argument
  261. * (signed comparison).
  262. * @since 1.2
  263. */
  264. public int compareTo(Short anotherShort) {
  265. return this.value - anotherShort.value;
  266. }
  267. /**
  268. * Compares this Short to another Object. If the Object is a Short,
  269. * this function behaves like <code>compareTo(Short)</code>. Otherwise,
  270. * it throws a <code>ClassCastException</code> (as Shorts are comparable
  271. * only to other Shorts).
  272. *
  273. * @param o the <code>Object</code> to be compared.
  274. * @return the value <code>0</code> if the argument is a Short
  275. * numerically equal to this Short; a value less than
  276. * <code>0</code> if the argument is a Short numerically
  277. * greater than this Short; and a value greater than
  278. * <code>0</code> if the argument is a Short numerically
  279. * less than this Short.
  280. * @exception <code>ClassCastException</code> if the argument is not a
  281. * <code>Short</code>.
  282. * @see java.lang.Comparable
  283. * @since 1.2
  284. */
  285. public int compareTo(Object o) {
  286. return compareTo((Short)o);
  287. }
  288. /** use serialVersionUID from JDK 1.1. for interoperability */
  289. private static final long serialVersionUID = 7515723908773894738L;
  290. }