author | simonis |
Fri, 14 Aug 2015 10:35:45 +0200 | |
changeset 32209 | 24bb680a1609 |
parent 31671 | 362e0c0acece |
child 33663 | 2cd62a4bd471 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22283
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
15136
c17824042364
8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents:
14507
diff
changeset
|
28 |
import java.lang.annotation.Native; |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
29 |
import java.math.*; |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
30 |
import java.util.Objects; |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30645
diff
changeset
|
31 |
import jdk.internal.HotSpotIntrinsicCandidate; |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
32 |
|
17929
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
33 |
|
2 | 34 |
/** |
35 |
* The {@code Long} class wraps a value of the primitive type {@code |
|
36 |
* long} in an object. An object of type {@code Long} contains a |
|
37 |
* single field whose type is {@code long}. |
|
38 |
* |
|
39 |
* <p> In addition, this class provides several methods for converting |
|
40 |
* a {@code long} to a {@code String} and a {@code String} to a {@code |
|
41 |
* long}, as well as other constants and methods useful when dealing |
|
42 |
* with a {@code long}. |
|
43 |
* |
|
44 |
* <p>Implementation note: The implementations of the "bit twiddling" |
|
45 |
* methods (such as {@link #highestOneBit(long) highestOneBit} and |
|
46 |
* {@link #numberOfTrailingZeros(long) numberOfTrailingZeros}) are |
|
47 |
* based on material from Henry S. Warren, Jr.'s <i>Hacker's |
|
48 |
* Delight</i>, (Addison Wesley, 2002). |
|
49 |
* |
|
50 |
* @author Lee Boynton |
|
51 |
* @author Arthur van Hoff |
|
52 |
* @author Josh Bloch |
|
53 |
* @author Joseph D. Darcy |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
54 |
* @since 1.0 |
2 | 55 |
*/ |
56 |
public final class Long extends Number implements Comparable<Long> { |
|
57 |
/** |
|
58 |
* A constant holding the minimum value a {@code long} can |
|
59 |
* have, -2<sup>63</sup>. |
|
60 |
*/ |
|
15136
c17824042364
8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents:
14507
diff
changeset
|
61 |
@Native public static final long MIN_VALUE = 0x8000000000000000L; |
2 | 62 |
|
63 |
/** |
|
64 |
* A constant holding the maximum value a {@code long} can |
|
65 |
* have, 2<sup>63</sup>-1. |
|
66 |
*/ |
|
15136
c17824042364
8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents:
14507
diff
changeset
|
67 |
@Native public static final long MAX_VALUE = 0x7fffffffffffffffL; |
2 | 68 |
|
69 |
/** |
|
70 |
* The {@code Class} instance representing the primitive type |
|
71 |
* {@code long}. |
|
72 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
73 |
* @since 1.1 |
2 | 74 |
*/ |
11275 | 75 |
@SuppressWarnings("unchecked") |
2 | 76 |
public static final Class<Long> TYPE = (Class<Long>) Class.getPrimitiveClass("long"); |
77 |
||
78 |
/** |
|
79 |
* Returns a string representation of the first argument in the |
|
80 |
* radix specified by the second argument. |
|
81 |
* |
|
82 |
* <p>If the radix is smaller than {@code Character.MIN_RADIX} |
|
83 |
* or larger than {@code Character.MAX_RADIX}, then the radix |
|
84 |
* {@code 10} is used instead. |
|
85 |
* |
|
86 |
* <p>If the first argument is negative, the first element of the |
|
87 |
* result is the ASCII minus sign {@code '-'} |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
88 |
* ({@code '\u005Cu002d'}). If the first argument is not |
2 | 89 |
* negative, no sign character appears in the result. |
90 |
* |
|
91 |
* <p>The remaining characters of the result represent the magnitude |
|
92 |
* of the first argument. If the magnitude is zero, it is |
|
93 |
* represented by a single zero character {@code '0'} |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
94 |
* ({@code '\u005Cu0030'}); otherwise, the first character of |
2 | 95 |
* the representation of the magnitude will not be the zero |
96 |
* character. The following ASCII characters are used as digits: |
|
97 |
* |
|
98 |
* <blockquote> |
|
99 |
* {@code 0123456789abcdefghijklmnopqrstuvwxyz} |
|
100 |
* </blockquote> |
|
101 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
102 |
* These are {@code '\u005Cu0030'} through |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
103 |
* {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
104 |
* {@code '\u005Cu007a'}. If {@code radix} is |
2 | 105 |
* <var>N</var>, then the first <var>N</var> of these characters |
106 |
* are used as radix-<var>N</var> digits in the order shown. Thus, |
|
107 |
* the digits for hexadecimal (radix 16) are |
|
108 |
* {@code 0123456789abcdef}. If uppercase letters are |
|
109 |
* desired, the {@link java.lang.String#toUpperCase()} method may |
|
110 |
* be called on the result: |
|
111 |
* |
|
112 |
* <blockquote> |
|
113 |
* {@code Long.toString(n, 16).toUpperCase()} |
|
114 |
* </blockquote> |
|
115 |
* |
|
116 |
* @param i a {@code long} to be converted to a string. |
|
117 |
* @param radix the radix to use in the string representation. |
|
118 |
* @return a string representation of the argument in the specified radix. |
|
119 |
* @see java.lang.Character#MAX_RADIX |
|
120 |
* @see java.lang.Character#MIN_RADIX |
|
121 |
*/ |
|
122 |
public static String toString(long i, int radix) { |
|
123 |
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) |
|
124 |
radix = 10; |
|
125 |
if (radix == 10) |
|
126 |
return toString(i); |
|
127 |
char[] buf = new char[65]; |
|
128 |
int charPos = 64; |
|
129 |
boolean negative = (i < 0); |
|
130 |
||
131 |
if (!negative) { |
|
132 |
i = -i; |
|
133 |
} |
|
134 |
||
135 |
while (i <= -radix) { |
|
136 |
buf[charPos--] = Integer.digits[(int)(-(i % radix))]; |
|
137 |
i = i / radix; |
|
138 |
} |
|
139 |
buf[charPos] = Integer.digits[(int)(-i)]; |
|
140 |
||
141 |
if (negative) { |
|
142 |
buf[--charPos] = '-'; |
|
143 |
} |
|
144 |
||
145 |
return new String(buf, charPos, (65 - charPos)); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
149 |
* Returns a string representation of the first argument as an |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
150 |
* unsigned integer value in the radix specified by the second |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
151 |
* argument. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
152 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
153 |
* <p>If the radix is smaller than {@code Character.MIN_RADIX} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
154 |
* or larger than {@code Character.MAX_RADIX}, then the radix |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
155 |
* {@code 10} is used instead. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
156 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
157 |
* <p>Note that since the first argument is treated as an unsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
158 |
* value, no leading sign character is printed. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
159 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
160 |
* <p>If the magnitude is zero, it is represented by a single zero |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
161 |
* character {@code '0'} ({@code '\u005Cu0030'}); otherwise, |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
162 |
* the first character of the representation of the magnitude will |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
163 |
* not be the zero character. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
164 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
165 |
* <p>The behavior of radixes and the characters used as digits |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
166 |
* are the same as {@link #toString(long, int) toString}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
167 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
168 |
* @param i an integer to be converted to an unsigned string. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
169 |
* @param radix the radix to use in the string representation. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
170 |
* @return an unsigned string representation of the argument in the specified radix. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
171 |
* @see #toString(long, int) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
172 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
173 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
174 |
public static String toUnsignedString(long i, int radix) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
175 |
if (i >= 0) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
176 |
return toString(i, radix); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
177 |
else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
178 |
switch (radix) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
179 |
case 2: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
180 |
return toBinaryString(i); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
181 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
182 |
case 4: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
183 |
return toUnsignedString0(i, 2); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
184 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
185 |
case 8: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
186 |
return toOctalString(i); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
187 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
188 |
case 10: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
189 |
/* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
190 |
* We can get the effect of an unsigned division by 10 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
191 |
* on a long value by first shifting right, yielding a |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
192 |
* positive value, and then dividing by 5. This |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
193 |
* allows the last digit and preceding digits to be |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
194 |
* isolated more quickly than by an initial conversion |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
195 |
* to BigInteger. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
196 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
197 |
long quot = (i >>> 1) / 5; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
198 |
long rem = i - quot * 10; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
199 |
return toString(quot) + rem; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
200 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
201 |
case 16: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
202 |
return toHexString(i); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
203 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
204 |
case 32: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
205 |
return toUnsignedString0(i, 5); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
206 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
207 |
default: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
208 |
return toUnsignedBigInteger(i).toString(radix); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
209 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
210 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
211 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
212 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
213 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
214 |
* Return a BigInteger equal to the unsigned value of the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
215 |
* argument. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
216 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
217 |
private static BigInteger toUnsignedBigInteger(long i) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
218 |
if (i >= 0L) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
219 |
return BigInteger.valueOf(i); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
220 |
else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
221 |
int upper = (int) (i >>> 32); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
222 |
int lower = (int) i; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
223 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
224 |
// return (upper << 32) + lower |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
225 |
return (BigInteger.valueOf(Integer.toUnsignedLong(upper))).shiftLeft(32). |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
226 |
add(BigInteger.valueOf(Integer.toUnsignedLong(lower))); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
227 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
228 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
229 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
230 |
/** |
2 | 231 |
* Returns a string representation of the {@code long} |
232 |
* argument as an unsigned integer in base 16. |
|
233 |
* |
|
234 |
* <p>The unsigned {@code long} value is the argument plus |
|
235 |
* 2<sup>64</sup> if the argument is negative; otherwise, it is |
|
236 |
* equal to the argument. This value is converted to a string of |
|
237 |
* ASCII digits in hexadecimal (base 16) with no extra |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
238 |
* leading {@code 0}s. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
239 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
240 |
* <p>The value of the argument can be recovered from the returned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
241 |
* string {@code s} by calling {@link |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
242 |
* Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
243 |
* 16)}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
244 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
245 |
* <p>If the unsigned magnitude is zero, it is represented by a |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
246 |
* single zero character {@code '0'} ({@code '\u005Cu0030'}); |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
247 |
* otherwise, the first character of the representation of the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
248 |
* unsigned magnitude will not be the zero character. The |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
249 |
* following characters are used as hexadecimal digits: |
2 | 250 |
* |
251 |
* <blockquote> |
|
252 |
* {@code 0123456789abcdef} |
|
253 |
* </blockquote> |
|
254 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
255 |
* These are the characters {@code '\u005Cu0030'} through |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
256 |
* {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
257 |
* {@code '\u005Cu0066'}. If uppercase letters are desired, |
2 | 258 |
* the {@link java.lang.String#toUpperCase()} method may be called |
259 |
* on the result: |
|
260 |
* |
|
261 |
* <blockquote> |
|
262 |
* {@code Long.toHexString(n).toUpperCase()} |
|
263 |
* </blockquote> |
|
264 |
* |
|
265 |
* @param i a {@code long} to be converted to a string. |
|
266 |
* @return the string representation of the unsigned {@code long} |
|
267 |
* value represented by the argument in hexadecimal |
|
268 |
* (base 16). |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
269 |
* @see #parseUnsignedLong(String, int) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
270 |
* @see #toUnsignedString(long, int) |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
271 |
* @since 1.0.2 |
2 | 272 |
*/ |
273 |
public static String toHexString(long i) { |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
274 |
return toUnsignedString0(i, 4); |
2 | 275 |
} |
276 |
||
277 |
/** |
|
278 |
* Returns a string representation of the {@code long} |
|
279 |
* argument as an unsigned integer in base 8. |
|
280 |
* |
|
281 |
* <p>The unsigned {@code long} value is the argument plus |
|
282 |
* 2<sup>64</sup> if the argument is negative; otherwise, it is |
|
283 |
* equal to the argument. This value is converted to a string of |
|
284 |
* ASCII digits in octal (base 8) with no extra leading |
|
285 |
* {@code 0}s. |
|
286 |
* |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
287 |
* <p>The value of the argument can be recovered from the returned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
288 |
* string {@code s} by calling {@link |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
289 |
* Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
290 |
* 8)}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
291 |
* |
2 | 292 |
* <p>If the unsigned magnitude is zero, it is represented by a |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
293 |
* single zero character {@code '0'} ({@code '\u005Cu0030'}); |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
294 |
* otherwise, the first character of the representation of the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
295 |
* unsigned magnitude will not be the zero character. The |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
296 |
* following characters are used as octal digits: |
2 | 297 |
* |
298 |
* <blockquote> |
|
299 |
* {@code 01234567} |
|
300 |
* </blockquote> |
|
301 |
* |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
302 |
* These are the characters {@code '\u005Cu0030'} through |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
303 |
* {@code '\u005Cu0037'}. |
2 | 304 |
* |
305 |
* @param i a {@code long} to be converted to a string. |
|
306 |
* @return the string representation of the unsigned {@code long} |
|
307 |
* value represented by the argument in octal (base 8). |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
308 |
* @see #parseUnsignedLong(String, int) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
309 |
* @see #toUnsignedString(long, int) |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
310 |
* @since 1.0.2 |
2 | 311 |
*/ |
312 |
public static String toOctalString(long i) { |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
313 |
return toUnsignedString0(i, 3); |
2 | 314 |
} |
315 |
||
316 |
/** |
|
317 |
* Returns a string representation of the {@code long} |
|
318 |
* argument as an unsigned integer in base 2. |
|
319 |
* |
|
320 |
* <p>The unsigned {@code long} value is the argument plus |
|
321 |
* 2<sup>64</sup> if the argument is negative; otherwise, it is |
|
322 |
* equal to the argument. This value is converted to a string of |
|
323 |
* ASCII digits in binary (base 2) with no extra leading |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
324 |
* {@code 0}s. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
325 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
326 |
* <p>The value of the argument can be recovered from the returned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
327 |
* string {@code s} by calling {@link |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
328 |
* Long#parseUnsignedLong(String, int) Long.parseUnsignedLong(s, |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
329 |
* 2)}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
330 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
331 |
* <p>If the unsigned magnitude is zero, it is represented by a |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
332 |
* single zero character {@code '0'} ({@code '\u005Cu0030'}); |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
333 |
* otherwise, the first character of the representation of the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
334 |
* unsigned magnitude will not be the zero character. The |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
335 |
* characters {@code '0'} ({@code '\u005Cu0030'}) and {@code |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
336 |
* '1'} ({@code '\u005Cu0031'}) are used as binary digits. |
2 | 337 |
* |
338 |
* @param i a {@code long} to be converted to a string. |
|
339 |
* @return the string representation of the unsigned {@code long} |
|
340 |
* value represented by the argument in binary (base 2). |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
341 |
* @see #parseUnsignedLong(String, int) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
342 |
* @see #toUnsignedString(long, int) |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
343 |
* @since 1.0.2 |
2 | 344 |
*/ |
345 |
public static String toBinaryString(long i) { |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
346 |
return toUnsignedString0(i, 1); |
2 | 347 |
} |
348 |
||
349 |
/** |
|
17929
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
350 |
* Format a long (treated as unsigned) into a String. |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
351 |
* @param val the value to format |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
352 |
* @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) |
2 | 353 |
*/ |
17929
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
354 |
static String toUnsignedString0(long val, int shift) { |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
355 |
// assert shift > 0 && shift <=5 : "Illegal shift value"; |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
356 |
int mag = Long.SIZE - Long.numberOfLeadingZeros(val); |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
357 |
int chars = Math.max(((mag + (shift - 1)) / shift), 1); |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
358 |
char[] buf = new char[chars]; |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
359 |
|
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
360 |
formatUnsignedLong(val, shift, buf, 0, chars); |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
361 |
return new String(buf, true); |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
362 |
} |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
363 |
|
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
364 |
/** |
25660
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
365 |
* Format a long (treated as unsigned) into a character buffer. If |
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
366 |
* {@code len} exceeds the formatted ASCII representation of {@code val}, |
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
367 |
* {@code buf} will be padded with leading zeroes. |
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
368 |
* |
17929
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
369 |
* @param val the unsigned long to format |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
370 |
* @param shift the log2 of the base to format in (4 for hex, 3 for octal, 1 for binary) |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
371 |
* @param buf the character buffer to write to |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
372 |
* @param offset the offset in the destination buffer to start at |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
373 |
* @param len the number of characters to write |
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
374 |
*/ |
25660
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
375 |
static void formatUnsignedLong(long val, int shift, char[] buf, int offset, int len) { |
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
376 |
// assert shift > 0 && shift <=5 : "Illegal shift value"; |
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
377 |
// assert offset >= 0 && offset < buf.length : "illegal offset"; |
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
378 |
// assert len > 0 && (offset + len) <= buf.length : "illegal length"; |
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
379 |
int charPos = offset + len; |
2 | 380 |
int radix = 1 << shift; |
17929
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
381 |
int mask = radix - 1; |
2 | 382 |
do { |
25660
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
383 |
buf[--charPos] = Integer.digits[((int) val) & mask]; |
17929
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
16846
diff
changeset
|
384 |
val >>>= shift; |
25660
01fa3ccedf50
8050114: Expose Integer/Long formatUnsigned methods internally
redestad
parents:
25653
diff
changeset
|
385 |
} while (charPos > offset); |
2 | 386 |
} |
387 |
||
388 |
/** |
|
389 |
* Returns a {@code String} object representing the specified |
|
390 |
* {@code long}. The argument is converted to signed decimal |
|
391 |
* representation and returned as a string, exactly as if the |
|
392 |
* argument and the radix 10 were given as arguments to the {@link |
|
393 |
* #toString(long, int)} method. |
|
394 |
* |
|
395 |
* @param i a {@code long} to be converted. |
|
396 |
* @return a string representation of the argument in base 10. |
|
397 |
*/ |
|
398 |
public static String toString(long i) { |
|
399 |
if (i == Long.MIN_VALUE) |
|
400 |
return "-9223372036854775808"; |
|
401 |
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); |
|
402 |
char[] buf = new char[size]; |
|
403 |
getChars(i, size, buf); |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
11676
diff
changeset
|
404 |
return new String(buf, true); |
2 | 405 |
} |
406 |
||
407 |
/** |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
408 |
* Returns a string representation of the argument as an unsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
409 |
* decimal value. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
410 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
411 |
* The argument is converted to unsigned decimal representation |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
412 |
* and returned as a string exactly as if the argument and radix |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
413 |
* 10 were given as arguments to the {@link #toUnsignedString(long, |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
414 |
* int)} method. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
415 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
416 |
* @param i an integer to be converted to an unsigned string. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
417 |
* @return an unsigned string representation of the argument. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
418 |
* @see #toUnsignedString(long, int) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
419 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
420 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
421 |
public static String toUnsignedString(long i) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
422 |
return toUnsignedString(i, 10); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
423 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
424 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
425 |
/** |
2 | 426 |
* Places characters representing the integer i into the |
427 |
* character array buf. The characters are placed into |
|
428 |
* the buffer backwards starting with the least significant |
|
429 |
* digit at the specified index (exclusive), and working |
|
430 |
* backwards from there. |
|
431 |
* |
|
432 |
* Will fail if i == Long.MIN_VALUE |
|
433 |
*/ |
|
434 |
static void getChars(long i, int index, char[] buf) { |
|
435 |
long q; |
|
436 |
int r; |
|
437 |
int charPos = index; |
|
438 |
char sign = 0; |
|
439 |
||
440 |
if (i < 0) { |
|
441 |
sign = '-'; |
|
442 |
i = -i; |
|
443 |
} |
|
444 |
||
445 |
// Get 2 digits/iteration using longs until quotient fits into an int |
|
446 |
while (i > Integer.MAX_VALUE) { |
|
447 |
q = i / 100; |
|
448 |
// really: r = i - (q * 100); |
|
449 |
r = (int)(i - ((q << 6) + (q << 5) + (q << 2))); |
|
450 |
i = q; |
|
451 |
buf[--charPos] = Integer.DigitOnes[r]; |
|
452 |
buf[--charPos] = Integer.DigitTens[r]; |
|
453 |
} |
|
454 |
||
455 |
// Get 2 digits/iteration using ints |
|
456 |
int q2; |
|
457 |
int i2 = (int)i; |
|
458 |
while (i2 >= 65536) { |
|
459 |
q2 = i2 / 100; |
|
460 |
// really: r = i2 - (q * 100); |
|
461 |
r = i2 - ((q2 << 6) + (q2 << 5) + (q2 << 2)); |
|
462 |
i2 = q2; |
|
463 |
buf[--charPos] = Integer.DigitOnes[r]; |
|
464 |
buf[--charPos] = Integer.DigitTens[r]; |
|
465 |
} |
|
466 |
||
467 |
// Fall thru to fast mode for smaller numbers |
|
468 |
// assert(i2 <= 65536, i2); |
|
469 |
for (;;) { |
|
470 |
q2 = (i2 * 52429) >>> (16+3); |
|
471 |
r = i2 - ((q2 << 3) + (q2 << 1)); // r = i2-(q2*10) ... |
|
472 |
buf[--charPos] = Integer.digits[r]; |
|
473 |
i2 = q2; |
|
474 |
if (i2 == 0) break; |
|
475 |
} |
|
476 |
if (sign != 0) { |
|
477 |
buf[--charPos] = sign; |
|
478 |
} |
|
479 |
} |
|
480 |
||
481 |
// Requires positive x |
|
482 |
static int stringSize(long x) { |
|
483 |
long p = 10; |
|
484 |
for (int i=1; i<19; i++) { |
|
485 |
if (x < p) |
|
486 |
return i; |
|
487 |
p = 10*p; |
|
488 |
} |
|
489 |
return 19; |
|
490 |
} |
|
491 |
||
492 |
/** |
|
493 |
* Parses the string argument as a signed {@code long} in the |
|
494 |
* radix specified by the second argument. The characters in the |
|
495 |
* string must all be digits of the specified radix (as determined |
|
496 |
* by whether {@link java.lang.Character#digit(char, int)} returns |
|
497 |
* a nonnegative value), except that the first character may be an |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
498 |
* ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to |
2 | 499 |
* indicate a negative value or an ASCII plus sign {@code '+'} |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
500 |
* ({@code '\u005Cu002B'}) to indicate a positive value. The |
2 | 501 |
* resulting {@code long} value is returned. |
502 |
* |
|
503 |
* <p>Note that neither the character {@code L} |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
504 |
* ({@code '\u005Cu004C'}) nor {@code l} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
505 |
* ({@code '\u005Cu006C'}) is permitted to appear at the end |
2 | 506 |
* of the string as a type indicator, as would be permitted in |
507 |
* Java programming language source code - except that either |
|
508 |
* {@code L} or {@code l} may appear as a digit for a |
|
16846
f6a073580d8e
8011930: Long.parseLong(String, int) has inaccurate limit on radix for using 'L'
darcy
parents:
15311
diff
changeset
|
509 |
* radix greater than or equal to 22. |
2 | 510 |
* |
511 |
* <p>An exception of type {@code NumberFormatException} is |
|
512 |
* thrown if any of the following situations occurs: |
|
513 |
* <ul> |
|
514 |
* |
|
515 |
* <li>The first argument is {@code null} or is a string of |
|
516 |
* length zero. |
|
517 |
* |
|
518 |
* <li>The {@code radix} is either smaller than {@link |
|
519 |
* java.lang.Character#MIN_RADIX} or larger than {@link |
|
520 |
* java.lang.Character#MAX_RADIX}. |
|
521 |
* |
|
522 |
* <li>Any character of the string is not a digit of the specified |
|
523 |
* radix, except that the first character may be a minus sign |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
524 |
* {@code '-'} ({@code '\u005Cu002d'}) or plus sign {@code |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
525 |
* '+'} ({@code '\u005Cu002B'}) provided that the string is |
2 | 526 |
* longer than length 1. |
527 |
* |
|
528 |
* <li>The value represented by the string is not a value of type |
|
529 |
* {@code long}. |
|
530 |
* </ul> |
|
531 |
* |
|
532 |
* <p>Examples: |
|
533 |
* <blockquote><pre> |
|
534 |
* parseLong("0", 10) returns 0L |
|
535 |
* parseLong("473", 10) returns 473L |
|
536 |
* parseLong("+42", 10) returns 42L |
|
537 |
* parseLong("-0", 10) returns 0L |
|
538 |
* parseLong("-FF", 16) returns -255L |
|
539 |
* parseLong("1100110", 2) returns 102L |
|
540 |
* parseLong("99", 8) throws a NumberFormatException |
|
541 |
* parseLong("Hazelnut", 10) throws a NumberFormatException |
|
542 |
* parseLong("Hazelnut", 36) returns 1356099454469L |
|
543 |
* </pre></blockquote> |
|
544 |
* |
|
545 |
* @param s the {@code String} containing the |
|
546 |
* {@code long} representation to be parsed. |
|
547 |
* @param radix the radix to be used while parsing {@code s}. |
|
548 |
* @return the {@code long} represented by the string argument in |
|
549 |
* the specified radix. |
|
550 |
* @throws NumberFormatException if the string does not contain a |
|
551 |
* parsable {@code long}. |
|
552 |
*/ |
|
553 |
public static long parseLong(String s, int radix) |
|
554 |
throws NumberFormatException |
|
555 |
{ |
|
556 |
if (s == null) { |
|
557 |
throw new NumberFormatException("null"); |
|
558 |
} |
|
559 |
||
560 |
if (radix < Character.MIN_RADIX) { |
|
561 |
throw new NumberFormatException("radix " + radix + |
|
562 |
" less than Character.MIN_RADIX"); |
|
563 |
} |
|
564 |
if (radix > Character.MAX_RADIX) { |
|
565 |
throw new NumberFormatException("radix " + radix + |
|
566 |
" greater than Character.MAX_RADIX"); |
|
567 |
} |
|
568 |
||
569 |
boolean negative = false; |
|
570 |
int i = 0, len = s.length(); |
|
571 |
long limit = -Long.MAX_VALUE; |
|
572 |
||
573 |
if (len > 0) { |
|
574 |
char firstChar = s.charAt(0); |
|
575 |
if (firstChar < '0') { // Possible leading "+" or "-" |
|
576 |
if (firstChar == '-') { |
|
577 |
negative = true; |
|
578 |
limit = Long.MIN_VALUE; |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
579 |
} else if (firstChar != '+') { |
2 | 580 |
throw NumberFormatException.forInputString(s); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
581 |
} |
2 | 582 |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
583 |
if (len == 1) { // Cannot have lone "+" or "-" |
2 | 584 |
throw NumberFormatException.forInputString(s); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
585 |
} |
2 | 586 |
i++; |
587 |
} |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
588 |
long multmin = limit / radix; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
589 |
long result = 0; |
2 | 590 |
while (i < len) { |
591 |
// Accumulating negatively avoids surprises near MAX_VALUE |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
592 |
int digit = Character.digit(s.charAt(i++),radix); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
593 |
if (digit < 0 || result < multmin) { |
2 | 594 |
throw NumberFormatException.forInputString(s); |
595 |
} |
|
596 |
result *= radix; |
|
597 |
if (result < limit + digit) { |
|
598 |
throw NumberFormatException.forInputString(s); |
|
599 |
} |
|
600 |
result -= digit; |
|
601 |
} |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
602 |
return negative ? result : -result; |
2 | 603 |
} else { |
604 |
throw NumberFormatException.forInputString(s); |
|
605 |
} |
|
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
606 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
607 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
608 |
/** |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
609 |
* Parses the {@link CharSequence} argument as a signed {@code long} in |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
610 |
* the specified {@code radix}, beginning at the specified |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
611 |
* {@code beginIndex} and extending to {@code endIndex - 1}. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
612 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
613 |
* <p>The method does not take steps to guard against the |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
614 |
* {@code CharSequence} being mutated while parsing. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
615 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
616 |
* @param s the {@code CharSequence} containing the {@code long} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
617 |
* representation to be parsed |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
618 |
* @param beginIndex the beginning index, inclusive. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
619 |
* @param endIndex the ending index, exclusive. |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
620 |
* @param radix the radix to be used while parsing {@code s}. |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
621 |
* @return the signed {@code long} represented by the subsequence in |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
622 |
* the specified radix. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
623 |
* @throws NullPointerException if {@code s} is null. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
624 |
* @throws IndexOutOfBoundsException if {@code beginIndex} is |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
625 |
* negative, or if {@code beginIndex} is greater than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
626 |
* {@code endIndex} or if {@code endIndex} is greater than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
627 |
* {@code s.length()}. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
628 |
* @throws NumberFormatException if the {@code CharSequence} does not |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
629 |
* contain a parsable {@code int} in the specified |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
630 |
* {@code radix}, or if {@code radix} is either smaller than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
631 |
* {@link java.lang.Character#MIN_RADIX} or larger than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
632 |
* {@link java.lang.Character#MAX_RADIX}. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
633 |
* @since 1.9 |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
634 |
*/ |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
635 |
public static long parseLong(CharSequence s, int beginIndex, int endIndex, int radix) |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
636 |
throws NumberFormatException { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
637 |
s = Objects.requireNonNull(s); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
638 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
639 |
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
640 |
throw new IndexOutOfBoundsException(); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
641 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
642 |
if (radix < Character.MIN_RADIX) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
643 |
throw new NumberFormatException("radix " + radix + |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
644 |
" less than Character.MIN_RADIX"); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
645 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
646 |
if (radix > Character.MAX_RADIX) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
647 |
throw new NumberFormatException("radix " + radix + |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
648 |
" greater than Character.MAX_RADIX"); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
649 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
650 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
651 |
boolean negative = false; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
652 |
int i = beginIndex; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
653 |
long limit = -Long.MAX_VALUE; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
654 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
655 |
if (i < endIndex) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
656 |
char firstChar = s.charAt(i); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
657 |
if (firstChar < '0') { // Possible leading "+" or "-" |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
658 |
if (firstChar == '-') { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
659 |
negative = true; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
660 |
limit = Long.MIN_VALUE; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
661 |
} else if (firstChar != '+') { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
662 |
throw NumberFormatException.forCharSequence(s, beginIndex, |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
663 |
endIndex, i); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
664 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
665 |
i++; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
666 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
667 |
if (i >= endIndex) { // Cannot have lone "+", "-" or "" |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
668 |
throw NumberFormatException.forCharSequence(s, beginIndex, |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
669 |
endIndex, i); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
670 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
671 |
long multmin = limit / radix; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
672 |
long result = 0; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
673 |
while (i < endIndex) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
674 |
// Accumulating negatively avoids surprises near MAX_VALUE |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
675 |
int digit = Character.digit(s.charAt(i), radix); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
676 |
if (digit < 0 || result < multmin) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
677 |
throw NumberFormatException.forCharSequence(s, beginIndex, |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
678 |
endIndex, i); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
679 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
680 |
result *= radix; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
681 |
if (result < limit + digit) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
682 |
throw NumberFormatException.forCharSequence(s, beginIndex, |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
683 |
endIndex, i); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
684 |
} |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
685 |
i++; |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
686 |
result -= digit; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
687 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
688 |
return negative ? result : -result; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
689 |
} else { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
690 |
throw new NumberFormatException(""); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
691 |
} |
2 | 692 |
} |
693 |
||
694 |
/** |
|
695 |
* Parses the string argument as a signed decimal {@code long}. |
|
696 |
* The characters in the string must all be decimal digits, except |
|
697 |
* that the first character may be an ASCII minus sign {@code '-'} |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
698 |
* ({@code \u005Cu002D'}) to indicate a negative value or an |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
699 |
* ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to |
2 | 700 |
* indicate a positive value. The resulting {@code long} value is |
701 |
* returned, exactly as if the argument and the radix {@code 10} |
|
702 |
* were given as arguments to the {@link |
|
703 |
* #parseLong(java.lang.String, int)} method. |
|
704 |
* |
|
705 |
* <p>Note that neither the character {@code L} |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
706 |
* ({@code '\u005Cu004C'}) nor {@code l} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
707 |
* ({@code '\u005Cu006C'}) is permitted to appear at the end |
2 | 708 |
* of the string as a type indicator, as would be permitted in |
709 |
* Java programming language source code. |
|
710 |
* |
|
711 |
* @param s a {@code String} containing the {@code long} |
|
712 |
* representation to be parsed |
|
713 |
* @return the {@code long} represented by the argument in |
|
714 |
* decimal. |
|
715 |
* @throws NumberFormatException if the string does not contain a |
|
716 |
* parsable {@code long}. |
|
717 |
*/ |
|
718 |
public static long parseLong(String s) throws NumberFormatException { |
|
719 |
return parseLong(s, 10); |
|
720 |
} |
|
721 |
||
722 |
/** |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
723 |
* Parses the string argument as an unsigned {@code long} in the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
724 |
* radix specified by the second argument. An unsigned integer |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
725 |
* maps the values usually associated with negative numbers to |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
726 |
* positive numbers larger than {@code MAX_VALUE}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
727 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
728 |
* The characters in the string must all be digits of the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
729 |
* specified radix (as determined by whether {@link |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
730 |
* java.lang.Character#digit(char, int)} returns a nonnegative |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
731 |
* value), except that the first character may be an ASCII plus |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
732 |
* sign {@code '+'} ({@code '\u005Cu002B'}). The resulting |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
733 |
* integer value is returned. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
734 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
735 |
* <p>An exception of type {@code NumberFormatException} is |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
736 |
* thrown if any of the following situations occurs: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
737 |
* <ul> |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
738 |
* <li>The first argument is {@code null} or is a string of |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
739 |
* length zero. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
740 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
741 |
* <li>The radix is either smaller than |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
742 |
* {@link java.lang.Character#MIN_RADIX} or |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
743 |
* larger than {@link java.lang.Character#MAX_RADIX}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
744 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
745 |
* <li>Any character of the string is not a digit of the specified |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
746 |
* radix, except that the first character may be a plus sign |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
747 |
* {@code '+'} ({@code '\u005Cu002B'}) provided that the |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
748 |
* string is longer than length 1. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
749 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
750 |
* <li>The value represented by the string is larger than the |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
751 |
* largest unsigned {@code long}, 2<sup>64</sup>-1. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
752 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
753 |
* </ul> |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
754 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
755 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
756 |
* @param s the {@code String} containing the unsigned integer |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
757 |
* representation to be parsed |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
758 |
* @param radix the radix to be used while parsing {@code s}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
759 |
* @return the unsigned {@code long} represented by the string |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
760 |
* argument in the specified radix. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
761 |
* @throws NumberFormatException if the {@code String} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
762 |
* does not contain a parsable {@code long}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
763 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
764 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
765 |
public static long parseUnsignedLong(String s, int radix) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
766 |
throws NumberFormatException { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
767 |
if (s == null) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
768 |
throw new NumberFormatException("null"); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
769 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
770 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
771 |
int len = s.length(); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
772 |
if (len > 0) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
773 |
char firstChar = s.charAt(0); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
774 |
if (firstChar == '-') { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
775 |
throw new |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
776 |
NumberFormatException(String.format("Illegal leading minus sign " + |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
777 |
"on unsigned string %s.", s)); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
778 |
} else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
779 |
if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
780 |
(radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
781 |
return parseLong(s, radix); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
782 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
783 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
784 |
// No need for range checks on len due to testing above. |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
785 |
long first = parseLong(s, 0, len - 1, radix); |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
786 |
int second = Character.digit(s.charAt(len - 1), radix); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
787 |
if (second < 0) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
788 |
throw new NumberFormatException("Bad digit at end of " + s); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
789 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
790 |
long result = first * radix + second; |
22283
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
791 |
|
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
792 |
/* |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
793 |
* Test leftmost bits of multiprecision extension of first*radix |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
794 |
* for overflow. The number of bits needed is defined by |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
795 |
* GUARD_BIT = ceil(log2(Character.MAX_RADIX)) + 1 = 7. Then |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
796 |
* int guard = radix*(int)(first >>> (64 - GUARD_BIT)) and |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
797 |
* overflow is tested by splitting guard in the ranges |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
798 |
* guard < 92, 92 <= guard < 128, and 128 <= guard, where |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
799 |
* 92 = 128 - Character.MAX_RADIX. Note that guard cannot take |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
800 |
* on a value which does not include a prime factor in the legal |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
801 |
* radix range. |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
802 |
*/ |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
803 |
int guard = radix * (int) (first >>> 57); |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
804 |
if (guard >= 128 || |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
805 |
(result >= 0 && guard >= 128 - Character.MAX_RADIX)) { |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
806 |
/* |
22283
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
807 |
* For purposes of exposition, the programmatic statements |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
808 |
* below should be taken to be multi-precision, i.e., not |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
809 |
* subject to overflow. |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
810 |
* |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
811 |
* A) Condition guard >= 128: |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
812 |
* If guard >= 128 then first*radix >= 2^7 * 2^57 = 2^64 |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
813 |
* hence always overflow. |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
814 |
* |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
815 |
* B) Condition guard < 92: |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
816 |
* Define left7 = first >>> 57. |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
817 |
* Given first = (left7 * 2^57) + (first & (2^57 - 1)) then |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
818 |
* result <= (radix*left7)*2^57 + radix*(2^57 - 1) + second. |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
819 |
* Thus if radix*left7 < 92, radix <= 36, and second < 36, |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
820 |
* then result < 92*2^57 + 36*(2^57 - 1) + 36 = 2^64 hence |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
821 |
* never overflow. |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
822 |
* |
22283
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
823 |
* C) Condition 92 <= guard < 128: |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
824 |
* first*radix + second >= radix*left7*2^57 + second |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
825 |
* so that first*radix + second >= 92*2^57 + 0 > 2^63 |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
826 |
* |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
827 |
* D) Condition guard < 128: |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
828 |
* radix*first <= (radix*left7) * 2^57 + radix*(2^57 - 1) |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
829 |
* so |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
830 |
* radix*first + second <= (radix*left7) * 2^57 + radix*(2^57 - 1) + 36 |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
831 |
* thus |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
832 |
* radix*first + second < 128 * 2^57 + 36*2^57 - radix + 36 |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
833 |
* whence |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
834 |
* radix*first + second < 2^64 + 2^6*2^57 = 2^64 + 2^63 |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
835 |
* |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
836 |
* E) Conditions C, D, and result >= 0: |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
837 |
* C and D combined imply the mathematical result |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
838 |
* 2^63 < first*radix + second < 2^64 + 2^63. The lower |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
839 |
* bound is therefore negative as a signed long, but the |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
840 |
* upper bound is too small to overflow again after the |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
841 |
* signed long overflows to positive above 2^64 - 1. Hence |
ae235c2bbac9
8030814: Long.parseUnsignedLong should throw exception on too large input
bpb
parents:
21334
diff
changeset
|
842 |
* result >= 0 implies overflow given C and D. |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
843 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
844 |
throw new NumberFormatException(String.format("String value %s exceeds " + |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
845 |
"range of unsigned long.", s)); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
846 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
847 |
return result; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
848 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
849 |
} else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
850 |
throw NumberFormatException.forInputString(s); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
851 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
852 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
853 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
854 |
/** |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
855 |
* Parses the {@link CharSequence} argument as an unsigned {@code long} in |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
856 |
* the specified {@code radix}, beginning at the specified |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
857 |
* {@code beginIndex} and extending to {@code endIndex - 1}. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
858 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
859 |
* <p>The method does not take steps to guard against the |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
860 |
* {@code CharSequence} being mutated while parsing. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
861 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
862 |
* @param s the {@code CharSequence} containing the unsigned |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
863 |
* {@code long} representation to be parsed |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
864 |
* @param beginIndex the beginning index, inclusive. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
865 |
* @param endIndex the ending index, exclusive. |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
866 |
* @param radix the radix to be used while parsing {@code s}. |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
867 |
* @return the unsigned {@code long} represented by the subsequence in |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
868 |
* the specified radix. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
869 |
* @throws NullPointerException if {@code s} is null. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
870 |
* @throws IndexOutOfBoundsException if {@code beginIndex} is |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
871 |
* negative, or if {@code beginIndex} is greater than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
872 |
* {@code endIndex} or if {@code endIndex} is greater than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
873 |
* {@code s.length()}. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
874 |
* @throws NumberFormatException if the {@code CharSequence} does not |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
875 |
* contain a parsable unsigned {@code long} in the specified |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
876 |
* {@code radix}, or if {@code radix} is either smaller than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
877 |
* {@link java.lang.Character#MIN_RADIX} or larger than |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
878 |
* {@link java.lang.Character#MAX_RADIX}. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
879 |
* @since 1.9 |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
880 |
*/ |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
881 |
public static long parseUnsignedLong(CharSequence s, int beginIndex, int endIndex, int radix) |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
882 |
throws NumberFormatException { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
883 |
s = Objects.requireNonNull(s); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
884 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
885 |
if (beginIndex < 0 || beginIndex > endIndex || endIndex > s.length()) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
886 |
throw new IndexOutOfBoundsException(); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
887 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
888 |
int start = beginIndex, len = endIndex - beginIndex; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
889 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
890 |
if (len > 0) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
891 |
char firstChar = s.charAt(start); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
892 |
if (firstChar == '-') { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
893 |
throw new NumberFormatException(String.format("Illegal leading minus sign " + |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
894 |
"on unsigned string %s.", s.subSequence(start, start + len))); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
895 |
} else { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
896 |
if (len <= 12 || // Long.MAX_VALUE in Character.MAX_RADIX is 13 digits |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
897 |
(radix == 10 && len <= 18) ) { // Long.MAX_VALUE in base 10 is 19 digits |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
898 |
return parseLong(s, start, start + len, radix); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
899 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
900 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
901 |
// No need for range checks on end due to testing above. |
26462
d6d34934be12
8055251: Re-examine Integer.parseInt and Long.parseLong methods
alanb
parents:
25859
diff
changeset
|
902 |
long first = parseLong(s, start, start + len - 1, radix); |
25653
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
903 |
int second = Character.digit(s.charAt(start + len - 1), radix); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
904 |
if (second < 0) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
905 |
throw new NumberFormatException("Bad digit at end of " + |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
906 |
s.subSequence(start, start + len)); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
907 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
908 |
long result = first * radix + second; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
909 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
910 |
/* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
911 |
* Test leftmost bits of multiprecision extension of first*radix |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
912 |
* for overflow. The number of bits needed is defined by |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
913 |
* GUARD_BIT = ceil(log2(Character.MAX_RADIX)) + 1 = 7. Then |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
914 |
* int guard = radix*(int)(first >>> (64 - GUARD_BIT)) and |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
915 |
* overflow is tested by splitting guard in the ranges |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
916 |
* guard < 92, 92 <= guard < 128, and 128 <= guard, where |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
917 |
* 92 = 128 - Character.MAX_RADIX. Note that guard cannot take |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
918 |
* on a value which does not include a prime factor in the legal |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
919 |
* radix range. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
920 |
*/ |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
921 |
int guard = radix * (int) (first >>> 57); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
922 |
if (guard >= 128 || |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
923 |
(result >= 0 && guard >= 128 - Character.MAX_RADIX)) { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
924 |
/* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
925 |
* For purposes of exposition, the programmatic statements |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
926 |
* below should be taken to be multi-precision, i.e., not |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
927 |
* subject to overflow. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
928 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
929 |
* A) Condition guard >= 128: |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
930 |
* If guard >= 128 then first*radix >= 2^7 * 2^57 = 2^64 |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
931 |
* hence always overflow. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
932 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
933 |
* B) Condition guard < 92: |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
934 |
* Define left7 = first >>> 57. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
935 |
* Given first = (left7 * 2^57) + (first & (2^57 - 1)) then |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
936 |
* result <= (radix*left7)*2^57 + radix*(2^57 - 1) + second. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
937 |
* Thus if radix*left7 < 92, radix <= 36, and second < 36, |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
938 |
* then result < 92*2^57 + 36*(2^57 - 1) + 36 = 2^64 hence |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
939 |
* never overflow. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
940 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
941 |
* C) Condition 92 <= guard < 128: |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
942 |
* first*radix + second >= radix*left7*2^57 + second |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
943 |
* so that first*radix + second >= 92*2^57 + 0 > 2^63 |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
944 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
945 |
* D) Condition guard < 128: |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
946 |
* radix*first <= (radix*left7) * 2^57 + radix*(2^57 - 1) |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
947 |
* so |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
948 |
* radix*first + second <= (radix*left7) * 2^57 + radix*(2^57 - 1) + 36 |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
949 |
* thus |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
950 |
* radix*first + second < 128 * 2^57 + 36*2^57 - radix + 36 |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
951 |
* whence |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
952 |
* radix*first + second < 2^64 + 2^6*2^57 = 2^64 + 2^63 |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
953 |
* |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
954 |
* E) Conditions C, D, and result >= 0: |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
955 |
* C and D combined imply the mathematical result |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
956 |
* 2^63 < first*radix + second < 2^64 + 2^63. The lower |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
957 |
* bound is therefore negative as a signed long, but the |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
958 |
* upper bound is too small to overflow again after the |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
959 |
* signed long overflows to positive above 2^64 - 1. Hence |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
960 |
* result >= 0 implies overflow given C and D. |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
961 |
*/ |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
962 |
throw new NumberFormatException(String.format("String value %s exceeds " + |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
963 |
"range of unsigned long.", s.subSequence(start, start + len))); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
964 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
965 |
return result; |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
966 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
967 |
} else { |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
968 |
throw NumberFormatException.forInputString(""); |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
969 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
970 |
} |
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
971 |
|
41e5fa7ce490
8041972: Additional parse methods for Long/Integer
redestad
parents:
24865
diff
changeset
|
972 |
/** |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
973 |
* Parses the string argument as an unsigned decimal {@code long}. The |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
974 |
* characters in the string must all be decimal digits, except |
28059
e576535359cc
8067377: My hobby: caning, then then canning, the the can-can
martin
parents:
26462
diff
changeset
|
975 |
* that the first character may be an ASCII plus sign {@code |
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
976 |
* '+'} ({@code '\u005Cu002B'}). The resulting integer value |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
977 |
* is returned, exactly as if the argument and the radix 10 were |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
978 |
* given as arguments to the {@link |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
979 |
* #parseUnsignedLong(java.lang.String, int)} method. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
980 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
981 |
* @param s a {@code String} containing the unsigned {@code long} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
982 |
* representation to be parsed |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
983 |
* @return the unsigned {@code long} value represented by the decimal string argument |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
984 |
* @throws NumberFormatException if the string does not contain a |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
985 |
* parsable unsigned integer. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
986 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
987 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
988 |
public static long parseUnsignedLong(String s) throws NumberFormatException { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
989 |
return parseUnsignedLong(s, 10); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
990 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
991 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
992 |
/** |
2 | 993 |
* Returns a {@code Long} object holding the value |
994 |
* extracted from the specified {@code String} when parsed |
|
995 |
* with the radix given by the second argument. The first |
|
996 |
* argument is interpreted as representing a signed |
|
997 |
* {@code long} in the radix specified by the second |
|
998 |
* argument, exactly as if the arguments were given to the {@link |
|
999 |
* #parseLong(java.lang.String, int)} method. The result is a |
|
1000 |
* {@code Long} object that represents the {@code long} |
|
1001 |
* value specified by the string. |
|
1002 |
* |
|
1003 |
* <p>In other words, this method returns a {@code Long} object equal |
|
1004 |
* to the value of: |
|
1005 |
* |
|
1006 |
* <blockquote> |
|
1007 |
* {@code new Long(Long.parseLong(s, radix))} |
|
1008 |
* </blockquote> |
|
1009 |
* |
|
1010 |
* @param s the string to be parsed |
|
1011 |
* @param radix the radix to be used in interpreting {@code s} |
|
1012 |
* @return a {@code Long} object holding the value |
|
1013 |
* represented by the string argument in the specified |
|
1014 |
* radix. |
|
1015 |
* @throws NumberFormatException If the {@code String} does not |
|
1016 |
* contain a parsable {@code long}. |
|
1017 |
*/ |
|
1018 |
public static Long valueOf(String s, int radix) throws NumberFormatException { |
|
2425
99a19a90813e
6807702: Integer.valueOf cache should be configurable
alanb
parents:
715
diff
changeset
|
1019 |
return Long.valueOf(parseLong(s, radix)); |
2 | 1020 |
} |
1021 |
||
1022 |
/** |
|
1023 |
* Returns a {@code Long} object holding the value |
|
1024 |
* of the specified {@code String}. The argument is |
|
1025 |
* interpreted as representing a signed decimal {@code long}, |
|
1026 |
* exactly as if the argument were given to the {@link |
|
1027 |
* #parseLong(java.lang.String)} method. The result is a |
|
1028 |
* {@code Long} object that represents the integer value |
|
1029 |
* specified by the string. |
|
1030 |
* |
|
1031 |
* <p>In other words, this method returns a {@code Long} object |
|
1032 |
* equal to the value of: |
|
1033 |
* |
|
1034 |
* <blockquote> |
|
1035 |
* {@code new Long(Long.parseLong(s))} |
|
1036 |
* </blockquote> |
|
1037 |
* |
|
1038 |
* @param s the string to be parsed. |
|
1039 |
* @return a {@code Long} object holding the value |
|
1040 |
* represented by the string argument. |
|
1041 |
* @throws NumberFormatException If the string cannot be parsed |
|
1042 |
* as a {@code long}. |
|
1043 |
*/ |
|
1044 |
public static Long valueOf(String s) throws NumberFormatException |
|
1045 |
{ |
|
2425
99a19a90813e
6807702: Integer.valueOf cache should be configurable
alanb
parents:
715
diff
changeset
|
1046 |
return Long.valueOf(parseLong(s, 10)); |
2 | 1047 |
} |
1048 |
||
1049 |
private static class LongCache { |
|
1050 |
private LongCache(){} |
|
1051 |
||
1052 |
static final Long cache[] = new Long[-(-128) + 127 + 1]; |
|
1053 |
||
1054 |
static { |
|
1055 |
for(int i = 0; i < cache.length; i++) |
|
1056 |
cache[i] = new Long(i - 128); |
|
1057 |
} |
|
1058 |
} |
|
1059 |
||
1060 |
/** |
|
1061 |
* Returns a {@code Long} instance representing the specified |
|
1062 |
* {@code long} value. |
|
1063 |
* If a new {@code Long} instance is not required, this method |
|
1064 |
* should generally be used in preference to the constructor |
|
1065 |
* {@link #Long(long)}, as this method is likely to yield |
|
1066 |
* significantly better space and time performance by caching |
|
1067 |
* frequently requested values. |
|
1068 |
* |
|
3224
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2425
diff
changeset
|
1069 |
* Note that unlike the {@linkplain Integer#valueOf(int) |
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2425
diff
changeset
|
1070 |
* corresponding method} in the {@code Integer} class, this method |
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2425
diff
changeset
|
1071 |
* is <em>not</em> required to cache values within a particular |
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2425
diff
changeset
|
1072 |
* range. |
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2425
diff
changeset
|
1073 |
* |
2 | 1074 |
* @param l a long value. |
1075 |
* @return a {@code Long} instance representing {@code l}. |
|
1076 |
* @since 1.5 |
|
1077 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30645
diff
changeset
|
1078 |
@HotSpotIntrinsicCandidate |
2 | 1079 |
public static Long valueOf(long l) { |
1080 |
final int offset = 128; |
|
1081 |
if (l >= -128 && l <= 127) { // will cache |
|
1082 |
return LongCache.cache[(int)l + offset]; |
|
1083 |
} |
|
1084 |
return new Long(l); |
|
1085 |
} |
|
1086 |
||
1087 |
/** |
|
1088 |
* Decodes a {@code String} into a {@code Long}. |
|
1089 |
* Accepts decimal, hexadecimal, and octal numbers given by the |
|
1090 |
* following grammar: |
|
1091 |
* |
|
1092 |
* <blockquote> |
|
1093 |
* <dl> |
|
1094 |
* <dt><i>DecodableString:</i> |
|
1095 |
* <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> |
|
1096 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i> |
|
1097 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i> |
|
1098 |
* <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i> |
|
1099 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i> |
|
21334 | 1100 |
* |
2 | 1101 |
* <dt><i>Sign:</i> |
1102 |
* <dd>{@code -} |
|
1103 |
* <dd>{@code +} |
|
1104 |
* </dl> |
|
1105 |
* </blockquote> |
|
1106 |
* |
|
1107 |
* <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i> |
|
9266
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
1108 |
* are as defined in section 3.10.1 of |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
1109 |
* <cite>The Java™ Language Specification</cite>, |
121fb370f179
7032960: API files in java.awt need to be updated for references to JVM Spec with editions/hyperlinks
jjh
parents:
5506
diff
changeset
|
1110 |
* except that underscores are not accepted between digits. |
2 | 1111 |
* |
1112 |
* <p>The sequence of characters following an optional |
|
1113 |
* sign and/or radix specifier ("{@code 0x}", "{@code 0X}", |
|
1114 |
* "{@code #}", or leading zero) is parsed as by the {@code |
|
1115 |
* Long.parseLong} method with the indicated radix (10, 16, or 8). |
|
1116 |
* This sequence of characters must represent a positive value or |
|
1117 |
* a {@link NumberFormatException} will be thrown. The result is |
|
1118 |
* negated if first character of the specified {@code String} is |
|
1119 |
* the minus sign. No whitespace characters are permitted in the |
|
1120 |
* {@code String}. |
|
1121 |
* |
|
1122 |
* @param nm the {@code String} to decode. |
|
1123 |
* @return a {@code Long} object holding the {@code long} |
|
1124 |
* value represented by {@code nm} |
|
1125 |
* @throws NumberFormatException if the {@code String} does not |
|
1126 |
* contain a parsable {@code long}. |
|
1127 |
* @see java.lang.Long#parseLong(String, int) |
|
1128 |
* @since 1.2 |
|
1129 |
*/ |
|
1130 |
public static Long decode(String nm) throws NumberFormatException { |
|
1131 |
int radix = 10; |
|
1132 |
int index = 0; |
|
1133 |
boolean negative = false; |
|
1134 |
Long result; |
|
1135 |
||
1136 |
if (nm.length() == 0) |
|
1137 |
throw new NumberFormatException("Zero length string"); |
|
1138 |
char firstChar = nm.charAt(0); |
|
1139 |
// Handle sign, if present |
|
1140 |
if (firstChar == '-') { |
|
1141 |
negative = true; |
|
1142 |
index++; |
|
1143 |
} else if (firstChar == '+') |
|
1144 |
index++; |
|
1145 |
||
1146 |
// Handle radix specifier, if present |
|
1147 |
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { |
|
1148 |
index += 2; |
|
1149 |
radix = 16; |
|
1150 |
} |
|
1151 |
else if (nm.startsWith("#", index)) { |
|
1152 |
index ++; |
|
1153 |
radix = 16; |
|
1154 |
} |
|
1155 |
else if (nm.startsWith("0", index) && nm.length() > 1 + index) { |
|
1156 |
index ++; |
|
1157 |
radix = 8; |
|
1158 |
} |
|
1159 |
||
1160 |
if (nm.startsWith("-", index) || nm.startsWith("+", index)) |
|
1161 |
throw new NumberFormatException("Sign character in wrong position"); |
|
1162 |
||
1163 |
try { |
|
1164 |
result = Long.valueOf(nm.substring(index), radix); |
|
2425
99a19a90813e
6807702: Integer.valueOf cache should be configurable
alanb
parents:
715
diff
changeset
|
1165 |
result = negative ? Long.valueOf(-result.longValue()) : result; |
2 | 1166 |
} catch (NumberFormatException e) { |
1167 |
// If number is Long.MIN_VALUE, we'll end up here. The next line |
|
1168 |
// handles this case, and causes any genuine format error to be |
|
1169 |
// rethrown. |
|
1170 |
String constant = negative ? ("-" + nm.substring(index)) |
|
1171 |
: nm.substring(index); |
|
1172 |
result = Long.valueOf(constant, radix); |
|
1173 |
} |
|
1174 |
return result; |
|
1175 |
} |
|
1176 |
||
1177 |
/** |
|
1178 |
* The value of the {@code Long}. |
|
1179 |
* |
|
1180 |
* @serial |
|
1181 |
*/ |
|
1182 |
private final long value; |
|
1183 |
||
1184 |
/** |
|
1185 |
* Constructs a newly allocated {@code Long} object that |
|
1186 |
* represents the specified {@code long} argument. |
|
1187 |
* |
|
1188 |
* @param value the value to be represented by the |
|
1189 |
* {@code Long} object. |
|
1190 |
*/ |
|
1191 |
public Long(long value) { |
|
1192 |
this.value = value; |
|
1193 |
} |
|
1194 |
||
1195 |
/** |
|
1196 |
* Constructs a newly allocated {@code Long} object that |
|
1197 |
* represents the {@code long} value indicated by the |
|
1198 |
* {@code String} parameter. The string is converted to a |
|
1199 |
* {@code long} value in exactly the manner used by the |
|
1200 |
* {@code parseLong} method for radix 10. |
|
1201 |
* |
|
1202 |
* @param s the {@code String} to be converted to a |
|
1203 |
* {@code Long}. |
|
1204 |
* @throws NumberFormatException if the {@code String} does not |
|
1205 |
* contain a parsable {@code long}. |
|
1206 |
* @see java.lang.Long#parseLong(java.lang.String, int) |
|
1207 |
*/ |
|
1208 |
public Long(String s) throws NumberFormatException { |
|
1209 |
this.value = parseLong(s, 10); |
|
1210 |
} |
|
1211 |
||
1212 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1213 |
* Returns the value of this {@code Long} as a {@code byte} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1214 |
* a narrowing primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1215 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 1216 |
*/ |
1217 |
public byte byteValue() { |
|
1218 |
return (byte)value; |
|
1219 |
} |
|
1220 |
||
1221 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1222 |
* Returns the value of this {@code Long} as a {@code short} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1223 |
* a narrowing primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1224 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 1225 |
*/ |
1226 |
public short shortValue() { |
|
1227 |
return (short)value; |
|
1228 |
} |
|
1229 |
||
1230 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1231 |
* Returns the value of this {@code Long} as an {@code int} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1232 |
* a narrowing primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1233 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 1234 |
*/ |
1235 |
public int intValue() { |
|
1236 |
return (int)value; |
|
1237 |
} |
|
1238 |
||
1239 |
/** |
|
1240 |
* Returns the value of this {@code Long} as a |
|
1241 |
* {@code long} value. |
|
1242 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30645
diff
changeset
|
1243 |
@HotSpotIntrinsicCandidate |
2 | 1244 |
public long longValue() { |
11275 | 1245 |
return value; |
2 | 1246 |
} |
1247 |
||
1248 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1249 |
* Returns the value of this {@code Long} as a {@code float} after |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1250 |
* a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1251 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 1252 |
*/ |
1253 |
public float floatValue() { |
|
1254 |
return (float)value; |
|
1255 |
} |
|
1256 |
||
1257 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1258 |
* Returns the value of this {@code Long} as a {@code double} |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1259 |
* after a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
1260 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 1261 |
*/ |
1262 |
public double doubleValue() { |
|
1263 |
return (double)value; |
|
1264 |
} |
|
1265 |
||
1266 |
/** |
|
1267 |
* Returns a {@code String} object representing this |
|
1268 |
* {@code Long}'s value. The value is converted to signed |
|
1269 |
* decimal representation and returned as a string, exactly as if |
|
1270 |
* the {@code long} value were given as an argument to the |
|
1271 |
* {@link java.lang.Long#toString(long)} method. |
|
1272 |
* |
|
1273 |
* @return a string representation of the value of this object in |
|
1274 |
* base 10. |
|
1275 |
*/ |
|
1276 |
public String toString() { |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3943
diff
changeset
|
1277 |
return toString(value); |
2 | 1278 |
} |
1279 |
||
1280 |
/** |
|
1281 |
* Returns a hash code for this {@code Long}. The result is |
|
1282 |
* the exclusive OR of the two halves of the primitive |
|
1283 |
* {@code long} value held by this {@code Long} |
|
1284 |
* object. That is, the hashcode is the value of the expression: |
|
1285 |
* |
|
1286 |
* <blockquote> |
|
1287 |
* {@code (int)(this.longValue()^(this.longValue()>>>32))} |
|
1288 |
* </blockquote> |
|
1289 |
* |
|
1290 |
* @return a hash code value for this object. |
|
1291 |
*/ |
|
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1292 |
@Override |
2 | 1293 |
public int hashCode() { |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1294 |
return Long.hashCode(value); |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1295 |
} |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1296 |
|
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1297 |
/** |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1298 |
* Returns a hash code for a {@code long} value; compatible with |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1299 |
* {@code Long.hashCode()}. |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1300 |
* |
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1301 |
* @param value the value to hash |
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1302 |
* @return a hash code value for a {@code long} value. |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1303 |
* @since 1.8 |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1304 |
*/ |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
12858
diff
changeset
|
1305 |
public static int hashCode(long value) { |
2 | 1306 |
return (int)(value ^ (value >>> 32)); |
1307 |
} |
|
1308 |
||
1309 |
/** |
|
1310 |
* Compares this object to the specified object. The result is |
|
1311 |
* {@code true} if and only if the argument is not |
|
1312 |
* {@code null} and is a {@code Long} object that |
|
1313 |
* contains the same {@code long} value as this object. |
|
1314 |
* |
|
1315 |
* @param obj the object to compare with. |
|
1316 |
* @return {@code true} if the objects are the same; |
|
1317 |
* {@code false} otherwise. |
|
1318 |
*/ |
|
1319 |
public boolean equals(Object obj) { |
|
1320 |
if (obj instanceof Long) { |
|
1321 |
return value == ((Long)obj).longValue(); |
|
1322 |
} |
|
1323 |
return false; |
|
1324 |
} |
|
1325 |
||
1326 |
/** |
|
1327 |
* Determines the {@code long} value of the system property |
|
1328 |
* with the specified name. |
|
1329 |
* |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1330 |
* <p>The first argument is treated as the name of a system |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1331 |
* property. System properties are accessible through the {@link |
2 | 1332 |
* java.lang.System#getProperty(java.lang.String)} method. The |
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1333 |
* string value of this property is then interpreted as a {@code |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1334 |
* long} value using the grammar supported by {@link Long#decode decode} |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1335 |
* and a {@code Long} object representing this value is returned. |
2 | 1336 |
* |
1337 |
* <p>If there is no property with the specified name, if the |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1338 |
* specified name is empty or {@code null}, or if the property |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1339 |
* does not have the correct numeric format, then {@code null} is |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1340 |
* returned. |
2 | 1341 |
* |
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1342 |
* <p>In other words, this method returns a {@code Long} object |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1343 |
* equal to the value of: |
2 | 1344 |
* |
1345 |
* <blockquote> |
|
1346 |
* {@code getLong(nm, null)} |
|
1347 |
* </blockquote> |
|
1348 |
* |
|
1349 |
* @param nm property name. |
|
1350 |
* @return the {@code Long} value of the property. |
|
10602
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1351 |
* @throws SecurityException for the same reasons as |
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1352 |
* {@link System#getProperty(String) System.getProperty} |
2 | 1353 |
* @see java.lang.System#getProperty(java.lang.String) |
1354 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String) |
|
1355 |
*/ |
|
1356 |
public static Long getLong(String nm) { |
|
1357 |
return getLong(nm, null); |
|
1358 |
} |
|
1359 |
||
1360 |
/** |
|
1361 |
* Determines the {@code long} value of the system property |
|
1362 |
* with the specified name. |
|
1363 |
* |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1364 |
* <p>The first argument is treated as the name of a system |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1365 |
* property. System properties are accessible through the {@link |
2 | 1366 |
* java.lang.System#getProperty(java.lang.String)} method. The |
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1367 |
* string value of this property is then interpreted as a {@code |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1368 |
* long} value using the grammar supported by {@link Long#decode decode} |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1369 |
* and a {@code Long} object representing this value is returned. |
2 | 1370 |
* |
1371 |
* <p>The second argument is the default value. A {@code Long} object |
|
1372 |
* that represents the value of the second argument is returned if there |
|
1373 |
* is no property of the specified name, if the property does not have |
|
1374 |
* the correct numeric format, or if the specified name is empty or null. |
|
1375 |
* |
|
1376 |
* <p>In other words, this method returns a {@code Long} object equal |
|
1377 |
* to the value of: |
|
1378 |
* |
|
1379 |
* <blockquote> |
|
1380 |
* {@code getLong(nm, new Long(val))} |
|
1381 |
* </blockquote> |
|
1382 |
* |
|
1383 |
* but in practice it may be implemented in a manner such as: |
|
1384 |
* |
|
1385 |
* <blockquote><pre> |
|
1386 |
* Long result = getLong(nm, null); |
|
1387 |
* return (result == null) ? new Long(val) : result; |
|
1388 |
* </pre></blockquote> |
|
1389 |
* |
|
1390 |
* to avoid the unnecessary allocation of a {@code Long} object when |
|
1391 |
* the default value is not needed. |
|
1392 |
* |
|
1393 |
* @param nm property name. |
|
1394 |
* @param val default value. |
|
1395 |
* @return the {@code Long} value of the property. |
|
10602
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1396 |
* @throws SecurityException for the same reasons as |
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1397 |
* {@link System#getProperty(String) System.getProperty} |
2 | 1398 |
* @see java.lang.System#getProperty(java.lang.String) |
1399 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String) |
|
1400 |
*/ |
|
1401 |
public static Long getLong(String nm, long val) { |
|
1402 |
Long result = Long.getLong(nm, null); |
|
2425
99a19a90813e
6807702: Integer.valueOf cache should be configurable
alanb
parents:
715
diff
changeset
|
1403 |
return (result == null) ? Long.valueOf(val) : result; |
2 | 1404 |
} |
1405 |
||
1406 |
/** |
|
1407 |
* Returns the {@code long} value of the system property with |
|
1408 |
* the specified name. The first argument is treated as the name |
|
1409 |
* of a system property. System properties are accessible through |
|
1410 |
* the {@link java.lang.System#getProperty(java.lang.String)} |
|
1411 |
* method. The string value of this property is then interpreted |
|
1412 |
* as a {@code long} value, as per the |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1413 |
* {@link Long#decode decode} method, and a {@code Long} object |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1414 |
* representing this value is returned; in summary: |
2 | 1415 |
* |
1416 |
* <ul> |
|
1417 |
* <li>If the property value begins with the two ASCII characters |
|
1418 |
* {@code 0x} or the ASCII character {@code #}, not followed by |
|
1419 |
* a minus sign, then the rest of it is parsed as a hexadecimal integer |
|
1420 |
* exactly as for the method {@link #valueOf(java.lang.String, int)} |
|
1421 |
* with radix 16. |
|
1422 |
* <li>If the property value begins with the ASCII character |
|
1423 |
* {@code 0} followed by another character, it is parsed as |
|
1424 |
* an octal integer exactly as by the method {@link |
|
1425 |
* #valueOf(java.lang.String, int)} with radix 8. |
|
1426 |
* <li>Otherwise the property value is parsed as a decimal |
|
1427 |
* integer exactly as by the method |
|
1428 |
* {@link #valueOf(java.lang.String, int)} with radix 10. |
|
1429 |
* </ul> |
|
1430 |
* |
|
1431 |
* <p>Note that, in every case, neither {@code L} |
|
11676
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
1432 |
* ({@code '\u005Cu004C'}) nor {@code l} |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
1433 |
* ({@code '\u005Cu006C'}) is permitted to appear at the end |
2 | 1434 |
* of the property value as a type indicator, as would be |
1435 |
* permitted in Java programming language source code. |
|
1436 |
* |
|
1437 |
* <p>The second argument is the default value. The default value is |
|
1438 |
* returned if there is no property of the specified name, if the |
|
1439 |
* property does not have the correct numeric format, or if the |
|
1440 |
* specified name is empty or {@code null}. |
|
1441 |
* |
|
1442 |
* @param nm property name. |
|
1443 |
* @param val default value. |
|
1444 |
* @return the {@code Long} value of the property. |
|
10602
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1445 |
* @throws SecurityException for the same reasons as |
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1446 |
* {@link System#getProperty(String) System.getProperty} |
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1447 |
* @see System#getProperty(java.lang.String) |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1448 |
* @see System#getProperty(java.lang.String, java.lang.String) |
2 | 1449 |
*/ |
1450 |
public static Long getLong(String nm, Long val) { |
|
1451 |
String v = null; |
|
1452 |
try { |
|
1453 |
v = System.getProperty(nm); |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1454 |
} catch (IllegalArgumentException | NullPointerException e) { |
2 | 1455 |
} |
1456 |
if (v != null) { |
|
1457 |
try { |
|
1458 |
return Long.decode(v); |
|
1459 |
} catch (NumberFormatException e) { |
|
1460 |
} |
|
1461 |
} |
|
1462 |
return val; |
|
1463 |
} |
|
1464 |
||
1465 |
/** |
|
1466 |
* Compares two {@code Long} objects numerically. |
|
1467 |
* |
|
1468 |
* @param anotherLong the {@code Long} to be compared. |
|
1469 |
* @return the value {@code 0} if this {@code Long} is |
|
1470 |
* equal to the argument {@code Long}; a value less than |
|
1471 |
* {@code 0} if this {@code Long} is numerically less |
|
1472 |
* than the argument {@code Long}; and a value greater |
|
1473 |
* than {@code 0} if this {@code Long} is numerically |
|
1474 |
* greater than the argument {@code Long} (signed |
|
1475 |
* comparison). |
|
1476 |
* @since 1.2 |
|
1477 |
*/ |
|
1478 |
public int compareTo(Long anotherLong) { |
|
3943
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1479 |
return compare(this.value, anotherLong.value); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1480 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1481 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1482 |
/** |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1483 |
* Compares two {@code long} values numerically. |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1484 |
* The value returned is identical to what would be returned by: |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1485 |
* <pre> |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1486 |
* Long.valueOf(x).compareTo(Long.valueOf(y)) |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1487 |
* </pre> |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1488 |
* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1489 |
* @param x the first {@code long} to compare |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1490 |
* @param y the second {@code long} to compare |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1491 |
* @return the value {@code 0} if {@code x == y}; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1492 |
* a value less than {@code 0} if {@code x < y}; and |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1493 |
* a value greater than {@code 0} if {@code x > y} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1494 |
* @since 1.7 |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1495 |
*/ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1496 |
public static int compare(long x, long y) { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1497 |
return (x < y) ? -1 : ((x == y) ? 0 : 1); |
2 | 1498 |
} |
1499 |
||
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1500 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1501 |
* Compares two {@code long} values numerically treating the values |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1502 |
* as unsigned. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1503 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1504 |
* @param x the first {@code long} to compare |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1505 |
* @param y the second {@code long} to compare |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1506 |
* @return the value {@code 0} if {@code x == y}; a value less |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1507 |
* than {@code 0} if {@code x < y} as unsigned values; and |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1508 |
* a value greater than {@code 0} if {@code x > y} as |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1509 |
* unsigned values |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1510 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1511 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1512 |
public static int compareUnsigned(long x, long y) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1513 |
return compare(x + MIN_VALUE, y + MIN_VALUE); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1514 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1515 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1516 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1517 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1518 |
* Returns the unsigned quotient of dividing the first argument by |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1519 |
* the second where each argument and the result is interpreted as |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1520 |
* an unsigned value. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1521 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1522 |
* <p>Note that in two's complement arithmetic, the three other |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1523 |
* basic arithmetic operations of add, subtract, and multiply are |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1524 |
* bit-wise identical if the two operands are regarded as both |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1525 |
* being signed or both being unsigned. Therefore separate {@code |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1526 |
* addUnsigned}, etc. methods are not provided. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1527 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1528 |
* @param dividend the value to be divided |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1529 |
* @param divisor the value doing the dividing |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1530 |
* @return the unsigned quotient of the first argument divided by |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1531 |
* the second argument |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1532 |
* @see #remainderUnsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1533 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1534 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1535 |
public static long divideUnsigned(long dividend, long divisor) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1536 |
if (divisor < 0L) { // signed comparison |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1537 |
// Answer must be 0 or 1 depending on relative magnitude |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1538 |
// of dividend and divisor. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1539 |
return (compareUnsigned(dividend, divisor)) < 0 ? 0L :1L; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1540 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1541 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1542 |
if (dividend > 0) // Both inputs non-negative |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1543 |
return dividend/divisor; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1544 |
else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1545 |
/* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1546 |
* For simple code, leveraging BigInteger. Longer and faster |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1547 |
* code written directly in terms of operations on longs is |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1548 |
* possible; see "Hacker's Delight" for divide and remainder |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1549 |
* algorithms. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1550 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1551 |
return toUnsignedBigInteger(dividend). |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1552 |
divide(toUnsignedBigInteger(divisor)).longValue(); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1553 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1554 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1555 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1556 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1557 |
* Returns the unsigned remainder from dividing the first argument |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1558 |
* by the second where each argument and the result is interpreted |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1559 |
* as an unsigned value. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1560 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1561 |
* @param dividend the value to be divided |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1562 |
* @param divisor the value doing the dividing |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1563 |
* @return the unsigned remainder of the first argument divided by |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1564 |
* the second argument |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1565 |
* @see #divideUnsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1566 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1567 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1568 |
public static long remainderUnsigned(long dividend, long divisor) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1569 |
if (dividend > 0 && divisor > 0) { // signed comparisons |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1570 |
return dividend % divisor; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1571 |
} else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1572 |
if (compareUnsigned(dividend, divisor) < 0) // Avoid explicit check for 0 divisor |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1573 |
return dividend; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1574 |
else |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1575 |
return toUnsignedBigInteger(dividend). |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1576 |
remainder(toUnsignedBigInteger(divisor)).longValue(); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1577 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1578 |
} |
2 | 1579 |
|
1580 |
// Bit Twiddling |
|
1581 |
||
1582 |
/** |
|
1583 |
* The number of bits used to represent a {@code long} value in two's |
|
1584 |
* complement binary form. |
|
1585 |
* |
|
1586 |
* @since 1.5 |
|
1587 |
*/ |
|
15136
c17824042364
8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents:
14507
diff
changeset
|
1588 |
@Native public static final int SIZE = 64; |
2 | 1589 |
|
1590 |
/** |
|
14507
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1591 |
* The number of bytes used to represent a {@code long} value in two's |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1592 |
* complement binary form. |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1593 |
* |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1594 |
* @since 1.8 |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1595 |
*/ |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1596 |
public static final int BYTES = SIZE / Byte.SIZE; |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1597 |
|
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1598 |
/** |
2 | 1599 |
* Returns a {@code long} value with at most a single one-bit, in the |
1600 |
* position of the highest-order ("leftmost") one-bit in the specified |
|
1601 |
* {@code long} value. Returns zero if the specified value has no |
|
1602 |
* one-bits in its two's complement binary representation, that is, if it |
|
1603 |
* is equal to zero. |
|
1604 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1605 |
* @param i the value whose highest one bit is to be computed |
2 | 1606 |
* @return a {@code long} value with a single one-bit, in the position |
1607 |
* of the highest-order one-bit in the specified value, or zero if |
|
1608 |
* the specified value is itself equal to zero. |
|
1609 |
* @since 1.5 |
|
1610 |
*/ |
|
1611 |
public static long highestOneBit(long i) { |
|
1612 |
// HD, Figure 3-1 |
|
1613 |
i |= (i >> 1); |
|
1614 |
i |= (i >> 2); |
|
1615 |
i |= (i >> 4); |
|
1616 |
i |= (i >> 8); |
|
1617 |
i |= (i >> 16); |
|
1618 |
i |= (i >> 32); |
|
1619 |
return i - (i >>> 1); |
|
1620 |
} |
|
1621 |
||
1622 |
/** |
|
1623 |
* Returns a {@code long} value with at most a single one-bit, in the |
|
1624 |
* position of the lowest-order ("rightmost") one-bit in the specified |
|
1625 |
* {@code long} value. Returns zero if the specified value has no |
|
1626 |
* one-bits in its two's complement binary representation, that is, if it |
|
1627 |
* is equal to zero. |
|
1628 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1629 |
* @param i the value whose lowest one bit is to be computed |
2 | 1630 |
* @return a {@code long} value with a single one-bit, in the position |
1631 |
* of the lowest-order one-bit in the specified value, or zero if |
|
1632 |
* the specified value is itself equal to zero. |
|
1633 |
* @since 1.5 |
|
1634 |
*/ |
|
1635 |
public static long lowestOneBit(long i) { |
|
1636 |
// HD, Section 2-1 |
|
1637 |
return i & -i; |
|
1638 |
} |
|
1639 |
||
1640 |
/** |
|
1641 |
* Returns the number of zero bits preceding the highest-order |
|
1642 |
* ("leftmost") one-bit in the two's complement binary representation |
|
1643 |
* of the specified {@code long} value. Returns 64 if the |
|
1644 |
* specified value has no one-bits in its two's complement representation, |
|
1645 |
* in other words if it is equal to zero. |
|
1646 |
* |
|
1647 |
* <p>Note that this method is closely related to the logarithm base 2. |
|
1648 |
* For all positive {@code long} values x: |
|
1649 |
* <ul> |
|
1650 |
* <li>floor(log<sub>2</sub>(x)) = {@code 63 - numberOfLeadingZeros(x)} |
|
1651 |
* <li>ceil(log<sub>2</sub>(x)) = {@code 64 - numberOfLeadingZeros(x - 1)} |
|
1652 |
* </ul> |
|
1653 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1654 |
* @param i the value whose number of leading zeros is to be computed |
2 | 1655 |
* @return the number of zero bits preceding the highest-order |
1656 |
* ("leftmost") one-bit in the two's complement binary representation |
|
1657 |
* of the specified {@code long} value, or 64 if the value |
|
1658 |
* is equal to zero. |
|
1659 |
* @since 1.5 |
|
1660 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30645
diff
changeset
|
1661 |
@HotSpotIntrinsicCandidate |
2 | 1662 |
public static int numberOfLeadingZeros(long i) { |
1663 |
// HD, Figure 5-6 |
|
1664 |
if (i == 0) |
|
1665 |
return 64; |
|
1666 |
int n = 1; |
|
1667 |
int x = (int)(i >>> 32); |
|
1668 |
if (x == 0) { n += 32; x = (int)i; } |
|
1669 |
if (x >>> 16 == 0) { n += 16; x <<= 16; } |
|
1670 |
if (x >>> 24 == 0) { n += 8; x <<= 8; } |
|
1671 |
if (x >>> 28 == 0) { n += 4; x <<= 4; } |
|
1672 |
if (x >>> 30 == 0) { n += 2; x <<= 2; } |
|
1673 |
n -= x >>> 31; |
|
1674 |
return n; |
|
1675 |
} |
|
1676 |
||
1677 |
/** |
|
1678 |
* Returns the number of zero bits following the lowest-order ("rightmost") |
|
1679 |
* one-bit in the two's complement binary representation of the specified |
|
1680 |
* {@code long} value. Returns 64 if the specified value has no |
|
1681 |
* one-bits in its two's complement representation, in other words if it is |
|
1682 |
* equal to zero. |
|
1683 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1684 |
* @param i the value whose number of trailing zeros is to be computed |
2 | 1685 |
* @return the number of zero bits following the lowest-order ("rightmost") |
1686 |
* one-bit in the two's complement binary representation of the |
|
1687 |
* specified {@code long} value, or 64 if the value is equal |
|
1688 |
* to zero. |
|
1689 |
* @since 1.5 |
|
1690 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30645
diff
changeset
|
1691 |
@HotSpotIntrinsicCandidate |
2 | 1692 |
public static int numberOfTrailingZeros(long i) { |
1693 |
// HD, Figure 5-14 |
|
1694 |
int x, y; |
|
1695 |
if (i == 0) return 64; |
|
1696 |
int n = 63; |
|
1697 |
y = (int)i; if (y != 0) { n = n -32; x = y; } else x = (int)(i>>>32); |
|
1698 |
y = x <<16; if (y != 0) { n = n -16; x = y; } |
|
1699 |
y = x << 8; if (y != 0) { n = n - 8; x = y; } |
|
1700 |
y = x << 4; if (y != 0) { n = n - 4; x = y; } |
|
1701 |
y = x << 2; if (y != 0) { n = n - 2; x = y; } |
|
1702 |
return n - ((x << 1) >>> 31); |
|
1703 |
} |
|
1704 |
||
1705 |
/** |
|
1706 |
* Returns the number of one-bits in the two's complement binary |
|
1707 |
* representation of the specified {@code long} value. This function is |
|
1708 |
* sometimes referred to as the <i>population count</i>. |
|
1709 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1710 |
* @param i the value whose bits are to be counted |
2 | 1711 |
* @return the number of one-bits in the two's complement binary |
1712 |
* representation of the specified {@code long} value. |
|
1713 |
* @since 1.5 |
|
1714 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30645
diff
changeset
|
1715 |
@HotSpotIntrinsicCandidate |
2 | 1716 |
public static int bitCount(long i) { |
30645
ca213ba02185
8078136: Incorrect figure number in reference to Hacker's Delight book in Long.bitCount() method
darcy
parents:
28059
diff
changeset
|
1717 |
// HD, Figure 5-2 |
2 | 1718 |
i = i - ((i >>> 1) & 0x5555555555555555L); |
1719 |
i = (i & 0x3333333333333333L) + ((i >>> 2) & 0x3333333333333333L); |
|
1720 |
i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0fL; |
|
1721 |
i = i + (i >>> 8); |
|
1722 |
i = i + (i >>> 16); |
|
1723 |
i = i + (i >>> 32); |
|
1724 |
return (int)i & 0x7f; |
|
1725 |
} |
|
1726 |
||
1727 |
/** |
|
1728 |
* Returns the value obtained by rotating the two's complement binary |
|
1729 |
* representation of the specified {@code long} value left by the |
|
1730 |
* specified number of bits. (Bits shifted out of the left hand, or |
|
1731 |
* high-order, side reenter on the right, or low-order.) |
|
1732 |
* |
|
1733 |
* <p>Note that left rotation with a negative distance is equivalent to |
|
1734 |
* right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, |
|
1735 |
* distance)}. Note also that rotation by any multiple of 64 is a |
|
1736 |
* no-op, so all but the last six bits of the rotation distance can be |
|
1737 |
* ignored, even if the distance is negative: {@code rotateLeft(val, |
|
1738 |
* distance) == rotateLeft(val, distance & 0x3F)}. |
|
1739 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1740 |
* @param i the value whose bits are to be rotated left |
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1741 |
* @param distance the number of bit positions to rotate left |
2 | 1742 |
* @return the value obtained by rotating the two's complement binary |
1743 |
* representation of the specified {@code long} value left by the |
|
1744 |
* specified number of bits. |
|
1745 |
* @since 1.5 |
|
1746 |
*/ |
|
1747 |
public static long rotateLeft(long i, int distance) { |
|
1748 |
return (i << distance) | (i >>> -distance); |
|
1749 |
} |
|
1750 |
||
1751 |
/** |
|
1752 |
* Returns the value obtained by rotating the two's complement binary |
|
1753 |
* representation of the specified {@code long} value right by the |
|
1754 |
* specified number of bits. (Bits shifted out of the right hand, or |
|
1755 |
* low-order, side reenter on the left, or high-order.) |
|
1756 |
* |
|
1757 |
* <p>Note that right rotation with a negative distance is equivalent to |
|
1758 |
* left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, |
|
1759 |
* distance)}. Note also that rotation by any multiple of 64 is a |
|
1760 |
* no-op, so all but the last six bits of the rotation distance can be |
|
1761 |
* ignored, even if the distance is negative: {@code rotateRight(val, |
|
1762 |
* distance) == rotateRight(val, distance & 0x3F)}. |
|
1763 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1764 |
* @param i the value whose bits are to be rotated right |
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1765 |
* @param distance the number of bit positions to rotate right |
2 | 1766 |
* @return the value obtained by rotating the two's complement binary |
1767 |
* representation of the specified {@code long} value right by the |
|
1768 |
* specified number of bits. |
|
1769 |
* @since 1.5 |
|
1770 |
*/ |
|
1771 |
public static long rotateRight(long i, int distance) { |
|
1772 |
return (i >>> distance) | (i << -distance); |
|
1773 |
} |
|
1774 |
||
1775 |
/** |
|
1776 |
* Returns the value obtained by reversing the order of the bits in the |
|
1777 |
* two's complement binary representation of the specified {@code long} |
|
1778 |
* value. |
|
1779 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1780 |
* @param i the value to be reversed |
2 | 1781 |
* @return the value obtained by reversing order of the bits in the |
1782 |
* specified {@code long} value. |
|
1783 |
* @since 1.5 |
|
1784 |
*/ |
|
1785 |
public static long reverse(long i) { |
|
1786 |
// HD, Figure 7-1 |
|
1787 |
i = (i & 0x5555555555555555L) << 1 | (i >>> 1) & 0x5555555555555555L; |
|
1788 |
i = (i & 0x3333333333333333L) << 2 | (i >>> 2) & 0x3333333333333333L; |
|
1789 |
i = (i & 0x0f0f0f0f0f0f0f0fL) << 4 | (i >>> 4) & 0x0f0f0f0f0f0f0f0fL; |
|
1790 |
i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; |
|
1791 |
i = (i << 48) | ((i & 0xffff0000L) << 16) | |
|
1792 |
((i >>> 16) & 0xffff0000L) | (i >>> 48); |
|
1793 |
return i; |
|
1794 |
} |
|
1795 |
||
1796 |
/** |
|
1797 |
* Returns the signum function of the specified {@code long} value. (The |
|
1798 |
* return value is -1 if the specified value is negative; 0 if the |
|
1799 |
* specified value is zero; and 1 if the specified value is positive.) |
|
1800 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1801 |
* @param i the value whose signum is to be computed |
2 | 1802 |
* @return the signum function of the specified {@code long} value. |
1803 |
* @since 1.5 |
|
1804 |
*/ |
|
1805 |
public static int signum(long i) { |
|
1806 |
// HD, Section 2-7 |
|
1807 |
return (int) ((i >> 63) | (-i >>> 63)); |
|
1808 |
} |
|
1809 |
||
1810 |
/** |
|
1811 |
* Returns the value obtained by reversing the order of the bytes in the |
|
1812 |
* two's complement representation of the specified {@code long} value. |
|
1813 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
17929
diff
changeset
|
1814 |
* @param i the value whose bytes are to be reversed |
2 | 1815 |
* @return the value obtained by reversing the bytes in the specified |
1816 |
* {@code long} value. |
|
1817 |
* @since 1.5 |
|
1818 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
30645
diff
changeset
|
1819 |
@HotSpotIntrinsicCandidate |
2 | 1820 |
public static long reverseBytes(long i) { |
1821 |
i = (i & 0x00ff00ff00ff00ffL) << 8 | (i >>> 8) & 0x00ff00ff00ff00ffL; |
|
1822 |
return (i << 48) | ((i & 0xffff0000L) << 16) | |
|
1823 |
((i >>> 16) & 0xffff0000L) | (i >>> 48); |
|
1824 |
} |
|
1825 |
||
15311
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1826 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1827 |
* Adds two {@code long} values together as per the + operator. |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1828 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1829 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1830 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1831 |
* @return the sum of {@code a} and {@code b} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1832 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1833 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1834 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1835 |
public static long sum(long a, long b) { |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1836 |
return a + b; |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1837 |
} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1838 |
|
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1839 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1840 |
* Returns the greater of two {@code long} values |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1841 |
* as if by calling {@link Math#max(long, long) Math.max}. |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1842 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1843 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1844 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1845 |
* @return the greater of {@code a} and {@code b} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1846 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1847 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1848 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1849 |
public static long max(long a, long b) { |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1850 |
return Math.max(a, b); |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1851 |
} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1852 |
|
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1853 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1854 |
* Returns the smaller of two {@code long} values |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1855 |
* as if by calling {@link Math#min(long, long) Math.min}. |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1856 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1857 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1858 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1859 |
* @return the smaller of {@code a} and {@code b} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1860 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1861 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1862 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1863 |
public static long min(long a, long b) { |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1864 |
return Math.min(a, b); |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1865 |
} |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15136
diff
changeset
|
1866 |
|
2 | 1867 |
/** use serialVersionUID from JDK 1.0.2 for interoperability */ |
15136
c17824042364
8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents:
14507
diff
changeset
|
1868 |
@Native private static final long serialVersionUID = 4290774380558885855L; |
2 | 1869 |
} |