/libjava/classpath/java/lang/Short.java

https://bitbucket.org/Underworld79/toolchain_gcc-4.8 · Java · 400 lines · 111 code · 30 blank · 259 comment · 9 complexity · 72d51f9ce505695c5fa4162af3bdf5c8 MD5 · raw file

  1. /* Short.java -- object wrapper for short
  2. Copyright (C) 1998, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.lang;
  32. /**
  33. * Instances of class <code>Short</code> represent primitive
  34. * <code>short</code> values.
  35. *
  36. * Additionally, this class provides various helper functions and variables
  37. * related to shorts.
  38. *
  39. * @author Paul Fisher
  40. * @author John Keiser
  41. * @author Eric Blake (ebb9@email.byu.edu)
  42. * @author Tom Tromey (tromey@redhat.com)
  43. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  44. * @since 1.1
  45. * @status updated to 1.5
  46. */
  47. public final class Short extends Number implements Comparable<Short>
  48. {
  49. /**
  50. * Compatible with JDK 1.1+.
  51. */
  52. private static final long serialVersionUID = 7515723908773894738L;
  53. /**
  54. * The minimum value a <code>short</code> can represent is -32768 (or
  55. * -2<sup>15</sup>).
  56. */
  57. public static final short MIN_VALUE = -32768;
  58. /**
  59. * The minimum value a <code>short</code> can represent is 32767 (or
  60. * 2<sup>15</sup>).
  61. */
  62. public static final short MAX_VALUE = 32767;
  63. /**
  64. * The primitive type <code>short</code> is represented by this
  65. * <code>Class</code> object.
  66. */
  67. public static final Class<Short> TYPE = (Class<Short>) VMClassLoader.getPrimitiveClass('S');
  68. /**
  69. * The number of bits needed to represent a <code>short</code>.
  70. * @since 1.5
  71. */
  72. public static final int SIZE = 16;
  73. // This caches some Short values, and is used by boxing conversions
  74. // via valueOf(). We must cache at least -128..127; these constants
  75. // control how much we actually cache.
  76. private static final int MIN_CACHE = -128;
  77. private static final int MAX_CACHE = 127;
  78. private static Short[] shortCache = new Short[MAX_CACHE - MIN_CACHE + 1];
  79. static
  80. {
  81. for (short i=MIN_CACHE; i <= MAX_CACHE; i++)
  82. shortCache[i - MIN_CACHE] = new Short(i);
  83. }
  84. /**
  85. * The immutable value of this Short.
  86. *
  87. * @serial the wrapped short
  88. */
  89. private final short value;
  90. /**
  91. * Create a <code>Short</code> object representing the value of the
  92. * <code>short</code> argument.
  93. *
  94. * @param value the value to use
  95. */
  96. public Short(short value)
  97. {
  98. this.value = value;
  99. }
  100. /**
  101. * Create a <code>Short</code> object representing the value of the
  102. * argument after conversion to a <code>short</code>.
  103. *
  104. * @param s the string to convert
  105. * @throws NumberFormatException if the String cannot be parsed
  106. */
  107. public Short(String s)
  108. {
  109. value = parseShort(s, 10);
  110. }
  111. /**
  112. * Converts the <code>short</code> to a <code>String</code> and assumes
  113. * a radix of 10.
  114. *
  115. * @param s the <code>short</code> to convert to <code>String</code>
  116. * @return the <code>String</code> representation of the argument
  117. */
  118. public static String toString(short s)
  119. {
  120. return String.valueOf(s);
  121. }
  122. /**
  123. * Converts the specified <code>String</code> into a <code>short</code>.
  124. * This function assumes a radix of 10.
  125. *
  126. * @param s the <code>String</code> to convert
  127. * @return the <code>short</code> value of <code>s</code>
  128. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  129. * <code>short</code>
  130. */
  131. public static short parseShort(String s)
  132. {
  133. return parseShort(s, 10);
  134. }
  135. /**
  136. * Converts the specified <code>String</code> into a <code>short</code>
  137. * using the specified radix (base). The string must not be <code>null</code>
  138. * or empty. It may begin with an optional '-', which will negate the answer,
  139. * provided that there are also valid digits. Each digit is parsed as if by
  140. * <code>Character.digit(d, radix)</code>, and must be in the range
  141. * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
  142. * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
  143. * Unlike Double.parseDouble, you may not have a leading '+'.
  144. *
  145. * @param s the <code>String</code> to convert
  146. * @param radix the radix (base) to use in the conversion
  147. * @return the <code>String</code> argument converted to <code>short</code>
  148. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  149. * <code>short</code>
  150. */
  151. public static short parseShort(String s, int radix)
  152. {
  153. int i = Integer.parseInt(s, radix, false);
  154. if ((short) i != i)
  155. throw new NumberFormatException();
  156. return (short) i;
  157. }
  158. /**
  159. * Creates a new <code>Short</code> object using the <code>String</code>
  160. * and specified radix (base).
  161. *
  162. * @param s the <code>String</code> to convert
  163. * @param radix the radix (base) to convert with
  164. * @return the new <code>Short</code>
  165. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  166. * <code>short</code>
  167. * @see #parseShort(String, int)
  168. */
  169. public static Short valueOf(String s, int radix)
  170. {
  171. return valueOf(parseShort(s, radix));
  172. }
  173. /**
  174. * Creates a new <code>Short</code> object using the <code>String</code>,
  175. * assuming a radix of 10.
  176. *
  177. * @param s the <code>String</code> to convert
  178. * @return the new <code>Short</code>
  179. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  180. * <code>short</code>
  181. * @see #Short(String)
  182. * @see #parseShort(String)
  183. */
  184. public static Short valueOf(String s)
  185. {
  186. return valueOf(parseShort(s, 10));
  187. }
  188. /**
  189. * Returns a <code>Short</code> object wrapping the value.
  190. * In contrast to the <code>Short</code> constructor, this method
  191. * will cache some values. It is used by boxing conversion.
  192. *
  193. * @param val the value to wrap
  194. * @return the <code>Short</code>
  195. * @since 1.5
  196. */
  197. public static Short valueOf(short val)
  198. {
  199. if (val < MIN_CACHE || val > MAX_CACHE)
  200. return new Short(val);
  201. else
  202. return shortCache[val - MIN_CACHE];
  203. }
  204. /**
  205. * Convert the specified <code>String</code> into a <code>Short</code>.
  206. * The <code>String</code> may represent decimal, hexadecimal, or
  207. * octal numbers.
  208. *
  209. * <p>The extended BNF grammar is as follows:<br>
  210. * <pre>
  211. * <em>DecodableString</em>:
  212. * ( [ <code>-</code> ] <em>DecimalNumber</em> )
  213. * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
  214. * | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
  215. * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
  216. * <em>DecimalNumber</em>:
  217. * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
  218. * <em>DecimalDigit</em>:
  219. * <em>Character.digit(d, 10) has value 0 to 9</em>
  220. * <em>OctalDigit</em>:
  221. * <em>Character.digit(d, 8) has value 0 to 7</em>
  222. * <em>DecimalDigit</em>:
  223. * <em>Character.digit(d, 16) has value 0 to 15</em>
  224. * </pre>
  225. * Finally, the value must be in the range <code>MIN_VALUE</code> to
  226. * <code>MAX_VALUE</code>, or an exception is thrown.
  227. *
  228. * @param s the <code>String</code> to interpret
  229. * @return the value of the String as a <code>Short</code>
  230. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  231. * <code>short</code>
  232. * @throws NullPointerException if <code>s</code> is null
  233. * @see Integer#decode(String)
  234. */
  235. public static Short decode(String s)
  236. {
  237. int i = Integer.parseInt(s, 10, true);
  238. if ((short) i != i)
  239. throw new NumberFormatException();
  240. return valueOf((short) i);
  241. }
  242. /**
  243. * Return the value of this <code>Short</code> as a <code>byte</code>.
  244. *
  245. * @return the byte value
  246. */
  247. public byte byteValue()
  248. {
  249. return (byte) value;
  250. }
  251. /**
  252. * Return the value of this <code>Short</code>.
  253. *
  254. * @return the short value
  255. */
  256. public short shortValue()
  257. {
  258. return value;
  259. }
  260. /**
  261. * Return the value of this <code>Short</code> as an <code>int</code>.
  262. *
  263. * @return the int value
  264. */
  265. public int intValue()
  266. {
  267. return value;
  268. }
  269. /**
  270. * Return the value of this <code>Short</code> as a <code>long</code>.
  271. *
  272. * @return the long value
  273. */
  274. public long longValue()
  275. {
  276. return value;
  277. }
  278. /**
  279. * Return the value of this <code>Short</code> as a <code>float</code>.
  280. *
  281. * @return the float value
  282. */
  283. public float floatValue()
  284. {
  285. return value;
  286. }
  287. /**
  288. * Return the value of this <code>Short</code> as a <code>double</code>.
  289. *
  290. * @return the double value
  291. */
  292. public double doubleValue()
  293. {
  294. return value;
  295. }
  296. /**
  297. * Converts the <code>Short</code> value to a <code>String</code> and
  298. * assumes a radix of 10.
  299. *
  300. * @return the <code>String</code> representation of this <code>Short</code>
  301. */
  302. public String toString()
  303. {
  304. return String.valueOf(value);
  305. }
  306. /**
  307. * Return a hashcode representing this Object. <code>Short</code>'s hash
  308. * code is simply its value.
  309. *
  310. * @return this Object's hash code
  311. */
  312. public int hashCode()
  313. {
  314. return value;
  315. }
  316. /**
  317. * Returns <code>true</code> if <code>obj</code> is an instance of
  318. * <code>Short</code> and represents the same short value.
  319. *
  320. * @param obj the object to compare
  321. * @return whether these Objects are semantically equal
  322. */
  323. public boolean equals(Object obj)
  324. {
  325. return obj instanceof Short && value == ((Short) obj).value;
  326. }
  327. /**
  328. * Compare two Shorts numerically by comparing their <code>short</code>
  329. * values. The result is positive if the first is greater, negative if the
  330. * second is greater, and 0 if the two are equal.
  331. *
  332. * @param s the Short to compare
  333. * @return the comparison
  334. * @since 1.2
  335. */
  336. public int compareTo(Short s)
  337. {
  338. return value - s.value;
  339. }
  340. /**
  341. * Compares two unboxed short values.
  342. * The result is positive if the first is greater, negative if the second
  343. * is greater, and 0 if the two are equal.
  344. *
  345. * @param x First value to compare.
  346. * @param y Second value to compare.
  347. *
  348. * @return positive int if the first value is greater, negative if the second
  349. * is greater, and 0 if the two are equal.
  350. * @since 1.7
  351. */
  352. public static int compare(short x, short y)
  353. {
  354. return Short.valueOf(x).compareTo(Short.valueOf(y));
  355. }
  356. /**
  357. * Reverse the bytes in val.
  358. * @since 1.5
  359. */
  360. public static short reverseBytes(short val)
  361. {
  362. return (short) (((val >> 8) & 0xff) | ((val << 8) & 0xff00));
  363. }
  364. }