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