Byte.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Byte.java -- object wrapper for byte
  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>Byte</code> represent primitive <code>byte</code>
  34. * values.
  35. *
  36. * Additionally, this class provides various helper functions and variables
  37. * useful to bytes.
  38. *
  39. * @author Paul Fisher
  40. * @author John Keiser
  41. * @author Per Bothner
  42. * @author Eric Blake (ebb9@email.byu.edu)
  43. * @author Tom Tromey (tromey@redhat.com)
  44. * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
  45. * @since 1.1
  46. * @status updated to 1.5
  47. */
  48. public final class Byte extends Number implements Comparable<Byte>
  49. {
  50. /**
  51. * Compatible with JDK 1.1+.
  52. */
  53. private static final long serialVersionUID = -7183698231559129828L;
  54. /**
  55. * The minimum value a <code>byte</code> can represent is -128 (or
  56. * -2<sup>7</sup>).
  57. */
  58. public static final byte MIN_VALUE = -128;
  59. /**
  60. * The maximum value a <code>byte</code> can represent is 127 (or
  61. * 2<sup>7</sup> - 1).
  62. */
  63. public static final byte MAX_VALUE = 127;
  64. /**
  65. * The primitive type <code>byte</code> is represented by this
  66. * <code>Class</code> object.
  67. */
  68. public static final Class<Byte> TYPE = (Class<Byte>) VMClassLoader.getPrimitiveClass('B');
  69. /**
  70. * The number of bits needed to represent a <code>byte</code>.
  71. * @since 1.5
  72. */
  73. public static final int SIZE = 8;
  74. // This caches Byte values, and is used by boxing conversions via
  75. // valueOf(). We're required to cache all possible values here.
  76. private static Byte[] byteCache = new Byte[MAX_VALUE - MIN_VALUE + 1];
  77. static
  78. {
  79. for (int i=MIN_VALUE; i <= MAX_VALUE; i++)
  80. byteCache[i - MIN_VALUE] = new Byte((byte) i);
  81. }
  82. /**
  83. * The immutable value of this Byte.
  84. *
  85. * @serial the wrapped byte
  86. */
  87. private final byte value;
  88. /**
  89. * Create a <code>Byte</code> object representing the value of the
  90. * <code>byte</code> argument.
  91. *
  92. * @param value the value to use
  93. */
  94. public Byte(byte value)
  95. {
  96. this.value = value;
  97. }
  98. /**
  99. * Create a <code>Byte</code> object representing the value specified
  100. * by the <code>String</code> argument
  101. *
  102. * @param s the string to convert
  103. * @throws NumberFormatException if the String does not contain a byte
  104. * @see #valueOf(String)
  105. */
  106. public Byte(String s)
  107. {
  108. value = parseByte(s, 10);
  109. }
  110. /**
  111. * Converts the <code>byte</code> to a <code>String</code> and assumes
  112. * a radix of 10.
  113. *
  114. * @param b the <code>byte</code> to convert to <code>String</code>
  115. * @return the <code>String</code> representation of the argument
  116. */
  117. public static String toString(byte b)
  118. {
  119. return String.valueOf(b);
  120. }
  121. /**
  122. * Converts the specified <code>String</code> into a <code>byte</code>.
  123. * This function assumes a radix of 10.
  124. *
  125. * @param s the <code>String</code> to convert
  126. * @return the <code>byte</code> value of <code>s</code>
  127. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  128. * <code>byte</code>
  129. * @see #parseByte(String)
  130. */
  131. public static byte parseByte(String s)
  132. {
  133. return parseByte(s, 10);
  134. }
  135. /**
  136. * Converts the specified <code>String</code> into an <code>int</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>byte</code>
  148. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  149. * <code>byte</code>
  150. */
  151. public static byte parseByte(String s, int radix)
  152. {
  153. int i = Integer.parseInt(s, radix, false);
  154. if ((byte) i != i)
  155. throw new NumberFormatException();
  156. return (byte) i;
  157. }
  158. /**
  159. * Creates a new <code>Byte</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>Byte</code>
  165. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  166. * <code>byte</code>
  167. * @see #parseByte(String, int)
  168. */
  169. public static Byte valueOf(String s, int radix)
  170. {
  171. return valueOf(parseByte(s, radix));
  172. }
  173. /**
  174. * Creates a new <code>Byte</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>Byte</code>
  179. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  180. * <code>byte</code>
  181. * @see #Byte(String)
  182. * @see #parseByte(String)
  183. */
  184. public static Byte valueOf(String s)
  185. {
  186. return valueOf(parseByte(s, 10));
  187. }
  188. /**
  189. * Returns a <code>Byte</code> object wrapping the value.
  190. * In contrast to the <code>Byte</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>Byte</code>
  195. */
  196. public static Byte valueOf(byte val)
  197. {
  198. return byteCache[val - MIN_VALUE];
  199. }
  200. /**
  201. * Convert the specified <code>String</code> into a <code>Byte</code>.
  202. * The <code>String</code> may represent decimal, hexadecimal, or
  203. * octal numbers.
  204. *
  205. * <p>The extended BNF grammar is as follows:<br>
  206. * <pre>
  207. * <em>DecodableString</em>:
  208. * ( [ <code>-</code> ] <em>DecimalNumber</em> )
  209. * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
  210. * | <code>#</code> ) { <em>HexDigit</em> }+ )
  211. * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
  212. * <em>DecimalNumber</em>:
  213. * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
  214. * <em>DecimalDigit</em>:
  215. * <em>Character.digit(d, 10) has value 0 to 9</em>
  216. * <em>OctalDigit</em>:
  217. * <em>Character.digit(d, 8) has value 0 to 7</em>
  218. * <em>DecimalDigit</em>:
  219. * <em>Character.digit(d, 16) has value 0 to 15</em>
  220. * </pre>
  221. * Finally, the value must be in the range <code>MIN_VALUE</code> to
  222. * <code>MAX_VALUE</code>, or an exception is thrown.
  223. *
  224. * @param s the <code>String</code> to interpret
  225. * @return the value of the String as a <code>Byte</code>
  226. * @throws NumberFormatException if <code>s</code> cannot be parsed as a
  227. * <code>byte</code>
  228. * @throws NullPointerException if <code>s</code> is null
  229. * @see Integer#decode(String)
  230. */
  231. public static Byte decode(String s)
  232. {
  233. int i = Integer.parseInt(s, 10, true);
  234. if ((byte) i != i)
  235. throw new NumberFormatException();
  236. return valueOf((byte) i);
  237. }
  238. /**
  239. * Return the value of this <code>Byte</code>.
  240. *
  241. * @return the byte value
  242. */
  243. public byte byteValue()
  244. {
  245. return value;
  246. }
  247. /**
  248. * Return the value of this <code>Byte</code> as a <code>short</code>.
  249. *
  250. * @return the short value
  251. */
  252. public short shortValue()
  253. {
  254. return value;
  255. }
  256. /**
  257. * Return the value of this <code>Byte</code> as an <code>int</code>.
  258. *
  259. * @return the int value
  260. */
  261. public int intValue()
  262. {
  263. return value;
  264. }
  265. /**
  266. * Return the value of this <code>Byte</code> as a <code>long</code>.
  267. *
  268. * @return the long value
  269. */
  270. public long longValue()
  271. {
  272. return value;
  273. }
  274. /**
  275. * Return the value of this <code>Byte</code> as a <code>float</code>.
  276. *
  277. * @return the float value
  278. */
  279. public float floatValue()
  280. {
  281. return value;
  282. }
  283. /**
  284. * Return the value of this <code>Byte</code> as a <code>double</code>.
  285. *
  286. * @return the double value
  287. */
  288. public double doubleValue()
  289. {
  290. return value;
  291. }
  292. /**
  293. * Converts the <code>Byte</code> value to a <code>String</code> and
  294. * assumes a radix of 10.
  295. *
  296. * @return the <code>String</code> representation of this <code>Byte</code>
  297. * @see Integer#toString()
  298. */
  299. public String toString()
  300. {
  301. return String.valueOf(value);
  302. }
  303. /**
  304. * Return a hashcode representing this Object. <code>Byte</code>'s hash
  305. * code is simply its value.
  306. *
  307. * @return this Object's hash code
  308. */
  309. public int hashCode()
  310. {
  311. return value;
  312. }
  313. /**
  314. * Returns <code>true</code> if <code>obj</code> is an instance of
  315. * <code>Byte</code> and represents the same byte value.
  316. *
  317. * @param obj the object to compare
  318. * @return whether these Objects are semantically equal
  319. */
  320. public boolean equals(Object obj)
  321. {
  322. return obj instanceof Byte && value == ((Byte) obj).value;
  323. }
  324. /**
  325. * Compare two Bytes numerically by comparing their <code>byte</code> values.
  326. * The result is positive if the first is greater, negative if the second
  327. * is greater, and 0 if the two are equal.
  328. *
  329. * @param b the Byte to compare
  330. * @return the comparison
  331. * @since 1.2
  332. */
  333. public int compareTo(Byte b)
  334. {
  335. return value - b.value;
  336. }
  337. /**
  338. * Compares two unboxed byte values.
  339. * The result is positive if the first is greater, negative if the second
  340. * is greater, and 0 if the two are equal.
  341. *
  342. * @param x First value to compare.
  343. * @param y Second value to compare.
  344. *
  345. * @return positive int if the first value is greater, negative if the second
  346. * is greater, and 0 if the two are equal.
  347. * @since 1.7
  348. */
  349. public static int compare(byte x, byte y)
  350. {
  351. return Byte.valueOf(x).compareTo(Byte.valueOf(y));
  352. }
  353. }