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