PageRenderTime 61ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/cldc/src/javaapi/cldc1.0/java/lang/Long.java

https://github.com/borman/phoneme-qtopia
Java | 378 lines | 120 code | 22 blank | 236 comment | 27 complexity | 3acb4f22509270153dc7bb5544b71f6a MD5 | raw file
  1. /*
  2. *
  3. *
  4. * Copyright 1990-2009 Sun Microsystems, Inc. All Rights Reserved.
  5. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 only, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License version 2 for more details (a copy is
  15. * included at /legal/license.txt).
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * version 2 along with this work; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA
  21. *
  22. * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
  23. * Clara, CA 95054 or visit www.sun.com if you need additional
  24. * information or have any questions.
  25. */
  26. package java.lang;
  27. /**
  28. * The Long class wraps a value of the primitive type <code>long</code>
  29. * in an object. An object of type <code>Long</code> contains a single
  30. * field whose type is <code>long</code>.
  31. * <p>
  32. * In addition, this class provides several methods for converting a
  33. * <code>long</code> to a <code>String</code> and a
  34. * <code>String</code> to a <code>long</code>, as well as other
  35. * constants and methods useful when dealing with a
  36. * <code>long</code>.
  37. *
  38. * @version 1.51, 12/04/99 (CLDC 1.0, Spring 2000)
  39. * @since JDK1.0
  40. */
  41. public final class Long {
  42. /**
  43. * The smallest value of type <code>long</code>.
  44. */
  45. public static final long MIN_VALUE = 0x8000000000000000L;
  46. /**
  47. * The largest value of type <code>long</code>.
  48. */
  49. public static final long MAX_VALUE = 0x7fffffffffffffffL;
  50. /**
  51. * Creates a string representation of the first argument in the
  52. * radix specified by the second argument.
  53. * <p>
  54. * If the radix is smaller than <code>Character.MIN_RADIX</code> or
  55. * larger than <code>Character.MAX_RADIX</code>, then the radix
  56. * <code>10</code> is used instead.
  57. * <p>
  58. * If the first argument is negative, the first element of the
  59. * result is the ASCII minus sign <code>'-'</code>
  60. * (<code>'&#92;u002d'</code>. If the first argument is not negative,
  61. * no sign character appears in the result.
  62. * <p>
  63. * The remaining characters of the result represent the magnitude of
  64. * the first argument. If the magnitude is zero, it is represented by
  65. * a single zero character <code>'0'</code>
  66. * (<code>'&#92;u0030'</code>); otherwise, the first character of the
  67. * representation of the magnitude will not be the zero character.
  68. * The following ASCII characters are used as digits:
  69. * <blockquote><pre>
  70. * 0123456789abcdefghijklmnopqrstuvwxyz
  71. * </pre></blockquote>
  72. * These are <tt>'&#92;u0030'</tt> through <tt>'&#92;u0039'</tt>
  73. * and <tt>'&#92;u0061'</tt> through <tt>'&#92;u007a'</tt>. If the
  74. * radix is <var>N</var>, then the first <var>N</var> of these
  75. * characters are used as radix-<var>N</var> digits in the order
  76. * shown. Thus, the digits for hexadecimal (radix 16) are
  77. * <blockquote><pre>
  78. * <tt>0123456789abcdef</tt>.
  79. * </pre></blockquote>
  80. *
  81. * @param i a long.
  82. * @param radix the radix.
  83. * @return a string representation of the argument in the specified radix.
  84. * @see java.lang.Character#MAX_RADIX
  85. * @see java.lang.Character#MIN_RADIX
  86. */
  87. public static String toString(long i, int radix) {
  88. if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
  89. radix = 10;
  90. char[] buf = new char[65];
  91. int charPos = 64;
  92. boolean negative = (i < 0);
  93. if (!negative) {
  94. i = -i;
  95. }
  96. while (i <= -radix) {
  97. buf[charPos--] = Integer.digits[(int)(-(i % radix))];
  98. i = i / radix;
  99. }
  100. buf[charPos] = Integer.digits[(int)(-i)];
  101. if (negative) {
  102. buf[--charPos] = '-';
  103. }
  104. return new String(buf, charPos, (65 - charPos));
  105. }
  106. /**
  107. * Returns a new String object representing the specified integer.
  108. * The argument is converted to signed decimal representation and
  109. * returned as a string, exactly as if the argument and the radix
  110. * 10 were given as arguments to the
  111. * {@link #toString(long, int)} method that takes two arguments.
  112. *
  113. * @param i a <code>long</code> to be converted.
  114. * @return a string representation of the argument in base&nbsp;10.
  115. */
  116. public static String toString(long i) {
  117. return toString(i, 10);
  118. }
  119. /**
  120. * Parses the string argument as a signed <code>long</code> in the
  121. * radix specified by the second argument. The characters in the
  122. * string must all be digits of the specified radix (as determined by
  123. * whether <code>Character.digit</code> returns a
  124. * nonnegative value), except that the first character may be an
  125. * ASCII minus sign <code>'-'</code> (<tt>'&#92;u002d'</tt> to indicate
  126. * a negative value. The resulting <code>long</code> value is returned.
  127. * <p>
  128. * Note that neither <tt>L</tt> nor <tt>l</tt> is permitted to appear at
  129. * the end of the string as a type indicator, as would be permitted in
  130. * Java programming language source code - except that either <tt>L</tt>
  131. * or <tt>l</tt> may appear as a digit for a radix greater than 22.
  132. * <p>
  133. * An exception of type <tt>NumberFormatException</tt> is thrown if any of
  134. * the following situations occurs:
  135. * <ul>
  136. * <li>The first argument is <tt>null</tt> or is a string of length zero.
  137. * <li>The <tt>radix</tt> is either smaller than
  138. * {@link java.lang.Character#MIN_RADIX} or larger than
  139. * {@link java.lang.Character#MAX_RADIX}.
  140. * <li>The first character of the string is not a digit of the
  141. * specified <tt>radix</tt> and is not a minus sign <tt>'-'</tt>
  142. * (<tt>'&#92;u002d'</tt>).
  143. * <li>The first character of the string is a minus sign and the
  144. * string is of length 1.
  145. * <li>Any character of the string after the first is not a digit of
  146. * the specified <tt>radix</tt>.
  147. * <li>The integer value represented by the string cannot be
  148. * represented as a value of type <tt>long</tt>.
  149. * </ul><p>
  150. * Examples:
  151. * <blockquote><pre>
  152. * parseLong("0", 10) returns 0L
  153. * parseLong("473", 10) returns 473L
  154. * parseLong("-0", 10) returns 0L
  155. * parseLong("-FF", 16) returns -255L
  156. * parseLong("1100110", 2) returns 102L
  157. * parseLong("99", 8) throws a NumberFormatException
  158. * parseLong("Hazelnut", 10) throws a NumberFormatException
  159. * parseLong("Hazelnut", 36) returns 1356099454469L
  160. * </pre></blockquote>
  161. *
  162. * @param s the <code>String</code> containing the
  163. * <code>long</code>.
  164. * @param radix the radix to be used.
  165. * @return the <code>long</code> represented by the string argument in
  166. * the specified radix.
  167. * @exception NumberFormatException if the string does not contain a
  168. * parsable integer.
  169. */
  170. public static long parseLong(String s, int radix)
  171. throws NumberFormatException
  172. {
  173. if (s == null) {
  174. throw new NumberFormatException(
  175. /* #ifdef VERBOSE_EXCEPTIONS */
  176. /// skipped "null"
  177. /* #endif */
  178. );
  179. }
  180. if (radix < Character.MIN_RADIX) {
  181. throw new NumberFormatException(
  182. /* #ifdef VERBOSE_EXCEPTIONS */
  183. /// skipped "radix " + radix + " less than Character.MIN_RADIX"
  184. /* #endif */
  185. );
  186. }
  187. if (radix > Character.MAX_RADIX) {
  188. throw new NumberFormatException(
  189. /* #ifdef VERBOSE_EXCEPTIONS */
  190. /// skipped "radix " + radix + " greater than Character.MAX_RADIX"
  191. /* #endif */
  192. );
  193. }
  194. long result = 0;
  195. boolean negative = false;
  196. int i = 0, max = s.length();
  197. long limit;
  198. long multmin;
  199. int digit;
  200. if (max > 0) {
  201. if (s.charAt(0) == '-') {
  202. negative = true;
  203. limit = Long.MIN_VALUE;
  204. i++;
  205. } else {
  206. limit = -Long.MAX_VALUE;
  207. }
  208. multmin = limit / radix;
  209. if (i < max) {
  210. digit = Character.digit(s.charAt(i++),radix);
  211. if (digit < 0) {
  212. throw new NumberFormatException(
  213. /* #ifdef VERBOSE_EXCEPTIONS */
  214. /// skipped s
  215. /* #endif */
  216. );
  217. } else {
  218. result = -digit;
  219. }
  220. }
  221. while (i < max) {
  222. // Accumulating negatively avoids surprises near MAX_VALUE
  223. digit = Character.digit(s.charAt(i++),radix);
  224. if (digit < 0) {
  225. throw new NumberFormatException(
  226. /* #ifdef VERBOSE_EXCEPTIONS */
  227. /// skipped s
  228. /* #endif */
  229. );
  230. }
  231. if (result < multmin) {
  232. throw new NumberFormatException(
  233. /* #ifdef VERBOSE_EXCEPTIONS */
  234. /// skipped s
  235. /* #endif */
  236. );
  237. }
  238. result *= radix;
  239. if (result < limit + digit) {
  240. throw new NumberFormatException(
  241. /* #ifdef VERBOSE_EXCEPTIONS */
  242. /// skipped s
  243. /* #endif */
  244. );
  245. }
  246. result -= digit;
  247. }
  248. } else {
  249. throw new NumberFormatException(
  250. /* #ifdef VERBOSE_EXCEPTIONS */
  251. /// skipped s
  252. /* #endif */
  253. );
  254. }
  255. if (negative) {
  256. if (i > 1) {
  257. return result;
  258. } else { /* Only got "-" */
  259. throw new NumberFormatException(
  260. /* #ifdef VERBOSE_EXCEPTIONS */
  261. /// skipped s
  262. /* #endif */
  263. );
  264. }
  265. } else {
  266. return -result;
  267. }
  268. }
  269. /**
  270. * Parses the string argument as a signed decimal <code>long</code>.
  271. * The characters in the string must all be decimal digits, except
  272. * that the first character may be an ASCII minus sign
  273. * <code>'-'</code> (<code>&#92;u002d'</code>) to indicate a negative
  274. * value. The resulting long value is returned, exactly as if the
  275. * argument and the radix <tt>10</tt> were given as arguments to the
  276. * {@link #parseLong(String, int)} method that takes two arguments.
  277. * <p>
  278. * Note that neither <tt>L</tt> nor <tt>l</tt> is permitted to appear
  279. * at the end of the string as a type indicator, as would be permitted
  280. * in Java programming language source code.
  281. *
  282. * @param s a string.
  283. * @return the <code>long</code> represented by the argument in decimal.
  284. * @exception NumberFormatException if the string does not contain a
  285. * parsable <code>long</code>.
  286. */
  287. public static long parseLong(String s) throws NumberFormatException {
  288. return parseLong(s, 10);
  289. }
  290. /**
  291. * The value of the Long.
  292. *
  293. * @serial
  294. */
  295. private long value;
  296. /**
  297. * Constructs a newly allocated <code>Long</code> object that
  298. * represents the primitive <code>long</code> argument.
  299. *
  300. * @param value the value to be represented by the
  301. * <code>Long</code> object.
  302. */
  303. public Long(long value) {
  304. this.value = value;
  305. }
  306. /**
  307. * Returns the value of this Long as a long value.
  308. *
  309. * @return the <code>long</code> value represented by this object.
  310. */
  311. public long longValue() {
  312. return (long)value;
  313. }
  314. /**
  315. * Returns a String object representing this Long's value.
  316. * The long integer value represented by this Long object is converted
  317. * to signed decimal representation and returned as a string, exactly
  318. * as if the long value were given as an argument to the
  319. * {@link #toString(long)} method that takes one argument.
  320. *
  321. * @return a string representation of this object in base&nbsp;10.
  322. */
  323. public String toString() {
  324. return String.valueOf(value);
  325. }
  326. /**
  327. * Computes a hashcode for this Long. The result is the exclusive
  328. * OR of the two halves of the primitive <code>long</code> value
  329. * represented by this <code>Long</code> object. That is, the hashcode
  330. * is the value of the expression:
  331. * <blockquote><pre>
  332. * (int)(this.longValue()^(this.longValue()>>>32))
  333. * </pre></blockquote>
  334. *
  335. * @return a hash code value for this object.
  336. */
  337. public int hashCode() {
  338. return (int)(value ^ (value >> 32));
  339. }
  340. /**
  341. * Compares this object against the specified object.
  342. * The result is <code>true</code> if and only if the argument is
  343. * not <code>null</code> and is a <code>Long</code> object that
  344. * contains the same <code>long</code> value as this object.
  345. *
  346. * @param obj the object to compare with.
  347. * @return <code>true</code> if the objects are the same;
  348. * <code>false</code> otherwise.
  349. */
  350. public boolean equals(Object obj) {
  351. if (obj instanceof Long) {
  352. return value == ((Long)obj).longValue();
  353. }
  354. return false;
  355. }
  356. }