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