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