author | henryjen |
Tue, 10 Jun 2014 16:18:54 -0700 | |
changeset 24865 | 09b1d992ca72 |
parent 23010 | 6dadb192ad81 |
child 25653 | 41e5fa7ce490 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
22102
diff
changeset
|
2 |
* Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
15136
c17824042364
8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents:
14507
diff
changeset
|
28 |
import java.lang.annotation.Native; |
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 |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
50 |
* @since 1.0 |
2 | 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 |
* |
|
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
69 |
* @since 1.1 |
2 | 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) |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
230 |
* @since 1.0.2 |
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) |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
268 |
* @since 1.0.2 |
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) |
24865
09b1d992ca72
8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents:
23010
diff
changeset
|
300 |
* @since 1.0.2 |
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 |
// RE: Division by Invariant Integers using Multiplication |
|
380 |
// T Gralund, P Montgomery |
|
381 |
// ACM PLDI 1994 |
|
382 |
// |
|
383 |
||
384 |
/** |
|
385 |
* Returns a {@code String} object representing the |
|
386 |
* specified integer. The argument is converted to signed decimal |
|
387 |
* representation and returned as a string, exactly as if the |
|
388 |
* argument and radix 10 were given as arguments to the {@link |
|
389 |
* #toString(int, int)} method. |
|
390 |
* |
|
391 |
* @param i an integer to be converted. |
|
392 |
* @return a string representation of the argument in base 10. |
|
393 |
*/ |
|
394 |
public static String toString(int i) { |
|
395 |
if (i == Integer.MIN_VALUE) |
|
396 |
return "-2147483648"; |
|
397 |
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); |
|
398 |
char[] buf = new char[size]; |
|
399 |
getChars(i, size, buf); |
|
12858
97e3f3f77254
6924259: Remove offset and count fields from java.lang.String
mduigou
parents:
12668
diff
changeset
|
400 |
return new String(buf, true); |
2 | 401 |
} |
402 |
||
403 |
/** |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
404 |
* 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
|
405 |
* decimal value. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
406 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
407 |
* The argument is converted to unsigned decimal representation |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
408 |
* 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
|
409 |
* 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
|
410 |
* int)} method. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
411 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
412 |
* @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
|
413 |
* @return an unsigned string representation of the argument. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
414 |
* @see #toUnsignedString(int, int) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
415 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
416 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
417 |
public static String toUnsignedString(int i) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
418 |
return Long.toString(toUnsignedLong(i)); |
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 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
421 |
/** |
2 | 422 |
* Places characters representing the integer i into the |
423 |
* character array buf. The characters are placed into |
|
424 |
* the buffer backwards starting with the least significant |
|
425 |
* digit at the specified index (exclusive), and working |
|
426 |
* backwards from there. |
|
427 |
* |
|
428 |
* Will fail if i == Integer.MIN_VALUE |
|
429 |
*/ |
|
430 |
static void getChars(int i, int index, char[] buf) { |
|
431 |
int q, r; |
|
432 |
int charPos = index; |
|
433 |
char sign = 0; |
|
434 |
||
435 |
if (i < 0) { |
|
436 |
sign = '-'; |
|
437 |
i = -i; |
|
438 |
} |
|
439 |
||
440 |
// Generate two digits per iteration |
|
441 |
while (i >= 65536) { |
|
442 |
q = i / 100; |
|
443 |
// really: r = i - (q * 100); |
|
444 |
r = i - ((q << 6) + (q << 5) + (q << 2)); |
|
445 |
i = q; |
|
446 |
buf [--charPos] = DigitOnes[r]; |
|
447 |
buf [--charPos] = DigitTens[r]; |
|
448 |
} |
|
449 |
||
450 |
// Fall thru to fast mode for smaller numbers |
|
451 |
// assert(i <= 65536, i); |
|
452 |
for (;;) { |
|
453 |
q = (i * 52429) >>> (16+3); |
|
454 |
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... |
|
455 |
buf [--charPos] = digits [r]; |
|
456 |
i = q; |
|
457 |
if (i == 0) break; |
|
458 |
} |
|
459 |
if (sign != 0) { |
|
460 |
buf [--charPos] = sign; |
|
461 |
} |
|
462 |
} |
|
463 |
||
464 |
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, |
|
465 |
99999999, 999999999, Integer.MAX_VALUE }; |
|
466 |
||
467 |
// Requires positive x |
|
468 |
static int stringSize(int x) { |
|
469 |
for (int i=0; ; i++) |
|
470 |
if (x <= sizeTable[i]) |
|
471 |
return i+1; |
|
472 |
} |
|
473 |
||
474 |
/** |
|
475 |
* Parses the string argument as a signed integer in the radix |
|
476 |
* specified by the second argument. The characters in the string |
|
477 |
* must all be digits of the specified radix (as determined by |
|
478 |
* whether {@link java.lang.Character#digit(char, int)} returns a |
|
479 |
* 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
|
480 |
* ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to |
2 | 481 |
* 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
|
482 |
* ({@code '\u005Cu002B'}) to indicate a positive value. The |
2 | 483 |
* resulting integer value is returned. |
484 |
* |
|
485 |
* <p>An exception of type {@code NumberFormatException} is |
|
486 |
* thrown if any of the following situations occurs: |
|
487 |
* <ul> |
|
488 |
* <li>The first argument is {@code null} or is a string of |
|
489 |
* length zero. |
|
490 |
* |
|
491 |
* <li>The radix is either smaller than |
|
492 |
* {@link java.lang.Character#MIN_RADIX} or |
|
493 |
* larger than {@link java.lang.Character#MAX_RADIX}. |
|
494 |
* |
|
495 |
* <li>Any character of the string is not a digit of the specified |
|
496 |
* 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
|
497 |
* {@code '-'} ({@code '\u005Cu002D'}) or plus sign |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
498 |
* {@code '+'} ({@code '\u005Cu002B'}) provided that the |
2 | 499 |
* string is longer than length 1. |
500 |
* |
|
501 |
* <li>The value represented by the string is not a value of type |
|
502 |
* {@code int}. |
|
503 |
* </ul> |
|
504 |
* |
|
505 |
* <p>Examples: |
|
506 |
* <blockquote><pre> |
|
507 |
* parseInt("0", 10) returns 0 |
|
508 |
* parseInt("473", 10) returns 473 |
|
509 |
* parseInt("+42", 10) returns 42 |
|
510 |
* parseInt("-0", 10) returns 0 |
|
511 |
* parseInt("-FF", 16) returns -255 |
|
512 |
* parseInt("1100110", 2) returns 102 |
|
513 |
* parseInt("2147483647", 10) returns 2147483647 |
|
514 |
* parseInt("-2147483648", 10) returns -2147483648 |
|
515 |
* parseInt("2147483648", 10) throws a NumberFormatException |
|
516 |
* parseInt("99", 8) throws a NumberFormatException |
|
517 |
* parseInt("Kona", 10) throws a NumberFormatException |
|
518 |
* parseInt("Kona", 27) returns 411787 |
|
519 |
* </pre></blockquote> |
|
520 |
* |
|
521 |
* @param s the {@code String} containing the integer |
|
522 |
* representation to be parsed |
|
523 |
* @param radix the radix to be used while parsing {@code s}. |
|
524 |
* @return the integer represented by the string argument in the |
|
525 |
* specified radix. |
|
526 |
* @exception NumberFormatException if the {@code String} |
|
527 |
* does not contain a parsable {@code int}. |
|
528 |
*/ |
|
529 |
public static int parseInt(String s, int radix) |
|
530 |
throws NumberFormatException |
|
531 |
{ |
|
2425 | 532 |
/* |
533 |
* WARNING: This method may be invoked early during VM initialization |
|
534 |
* before IntegerCache is initialized. Care must be taken to not use |
|
535 |
* the valueOf method. |
|
536 |
*/ |
|
537 |
||
2 | 538 |
if (s == null) { |
539 |
throw new NumberFormatException("null"); |
|
540 |
} |
|
541 |
||
542 |
if (radix < Character.MIN_RADIX) { |
|
543 |
throw new NumberFormatException("radix " + radix + |
|
544 |
" less than Character.MIN_RADIX"); |
|
545 |
} |
|
546 |
||
547 |
if (radix > Character.MAX_RADIX) { |
|
548 |
throw new NumberFormatException("radix " + radix + |
|
549 |
" greater than Character.MAX_RADIX"); |
|
550 |
} |
|
551 |
||
552 |
int result = 0; |
|
553 |
boolean negative = false; |
|
554 |
int i = 0, len = s.length(); |
|
555 |
int limit = -Integer.MAX_VALUE; |
|
556 |
int multmin; |
|
557 |
int digit; |
|
558 |
||
559 |
if (len > 0) { |
|
560 |
char firstChar = s.charAt(0); |
|
561 |
if (firstChar < '0') { // Possible leading "+" or "-" |
|
562 |
if (firstChar == '-') { |
|
563 |
negative = true; |
|
564 |
limit = Integer.MIN_VALUE; |
|
565 |
} else if (firstChar != '+') |
|
566 |
throw NumberFormatException.forInputString(s); |
|
567 |
||
568 |
if (len == 1) // Cannot have lone "+" or "-" |
|
569 |
throw NumberFormatException.forInputString(s); |
|
570 |
i++; |
|
571 |
} |
|
572 |
multmin = limit / radix; |
|
573 |
while (i < len) { |
|
574 |
// Accumulating negatively avoids surprises near MAX_VALUE |
|
575 |
digit = Character.digit(s.charAt(i++),radix); |
|
576 |
if (digit < 0) { |
|
577 |
throw NumberFormatException.forInputString(s); |
|
578 |
} |
|
579 |
if (result < multmin) { |
|
580 |
throw NumberFormatException.forInputString(s); |
|
581 |
} |
|
582 |
result *= radix; |
|
583 |
if (result < limit + digit) { |
|
584 |
throw NumberFormatException.forInputString(s); |
|
585 |
} |
|
586 |
result -= digit; |
|
587 |
} |
|
588 |
} else { |
|
589 |
throw NumberFormatException.forInputString(s); |
|
590 |
} |
|
591 |
return negative ? result : -result; |
|
592 |
} |
|
593 |
||
594 |
/** |
|
595 |
* Parses the string argument as a signed decimal integer. The |
|
596 |
* characters in the string must all be decimal digits, except |
|
597 |
* 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
|
598 |
* ({@code '\u005Cu002D'}) to indicate a negative value or an |
7e75ec031b97
7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents:
11672
diff
changeset
|
599 |
* ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to |
2 | 600 |
* indicate a positive value. The resulting integer value is |
601 |
* returned, exactly as if the argument and the radix 10 were |
|
602 |
* given as arguments to the {@link #parseInt(java.lang.String, |
|
603 |
* int)} method. |
|
604 |
* |
|
605 |
* @param s a {@code String} containing the {@code int} |
|
606 |
* representation to be parsed |
|
607 |
* @return the integer value represented by the argument in decimal. |
|
608 |
* @exception NumberFormatException if the string does not contain a |
|
609 |
* parsable integer. |
|
610 |
*/ |
|
611 |
public static int parseInt(String s) throws NumberFormatException { |
|
612 |
return parseInt(s,10); |
|
613 |
} |
|
614 |
||
615 |
/** |
|
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
616 |
* 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
|
617 |
* 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
|
618 |
* values usually associated with negative numbers to positive |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
619 |
* numbers larger than {@code MAX_VALUE}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
620 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
621 |
* 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
|
622 |
* specified radix (as determined by whether {@link |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
623 |
* 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
|
624 |
* 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
|
625 |
* sign {@code '+'} ({@code '\u005Cu002B'}). The resulting |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
626 |
* integer value is returned. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
627 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
628 |
* <p>An exception of type {@code NumberFormatException} is |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
629 |
* thrown if any of the following situations occurs: |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
630 |
* <ul> |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
631 |
* <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
|
632 |
* length zero. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
633 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
634 |
* <li>The radix is either smaller than |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
635 |
* {@link java.lang.Character#MIN_RADIX} or |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
636 |
* larger than {@link java.lang.Character#MAX_RADIX}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
637 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
638 |
* <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
|
639 |
* 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
|
640 |
* {@code '+'} ({@code '\u005Cu002B'}) provided that the |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
641 |
* string is longer than length 1. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
642 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
643 |
* <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
|
644 |
* 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
|
645 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
646 |
* </ul> |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
647 |
* |
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 |
* @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
|
650 |
* representation to be parsed |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
651 |
* @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
|
652 |
* @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
|
653 |
* specified radix. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
654 |
* @throws NumberFormatException if the {@code String} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
655 |
* does not contain a parsable {@code int}. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
656 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
657 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
658 |
public static int parseUnsignedInt(String s, int radix) |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
659 |
throws NumberFormatException { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
660 |
if (s == null) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
661 |
throw new NumberFormatException("null"); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
662 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
663 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
664 |
int len = s.length(); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
665 |
if (len > 0) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
666 |
char firstChar = s.charAt(0); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
667 |
if (firstChar == '-') { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
668 |
throw new |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
669 |
NumberFormatException(String.format("Illegal leading minus sign " + |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
670 |
"on unsigned string %s.", s)); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
671 |
} else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
672 |
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
|
673 |
(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
|
674 |
return parseInt(s, radix); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
675 |
} else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
676 |
long ell = Long.parseLong(s, radix); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
677 |
if ((ell & 0xffff_ffff_0000_0000L) == 0) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
678 |
return (int) ell; |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
679 |
} else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
680 |
throw new |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
681 |
NumberFormatException(String.format("String value %s exceeds " + |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
682 |
"range of unsigned int.", s)); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
683 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
684 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
685 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
686 |
} else { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
687 |
throw NumberFormatException.forInputString(s); |
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 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
690 |
|
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 |
* 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
|
693 |
* 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
|
694 |
* 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
|
695 |
* '+'} ({@code '\u005Cu002B'}). The resulting integer value |
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
696 |
* 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
|
697 |
* given as arguments to the {@link |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
698 |
* #parseUnsignedInt(java.lang.String, int)} method. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
699 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
700 |
* @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
|
701 |
* representation to be parsed |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
702 |
* @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
|
703 |
* @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
|
704 |
* parsable unsigned integer. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
705 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
706 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
707 |
public static int parseUnsignedInt(String s) throws NumberFormatException { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
708 |
return parseUnsignedInt(s, 10); |
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 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
711 |
/** |
2 | 712 |
* Returns an {@code Integer} object holding the value |
713 |
* extracted from the specified {@code String} when parsed |
|
714 |
* with the radix given by the second argument. The first argument |
|
715 |
* is interpreted as representing a signed integer in the radix |
|
716 |
* specified by the second argument, exactly as if the arguments |
|
717 |
* were given to the {@link #parseInt(java.lang.String, int)} |
|
718 |
* method. The result is an {@code Integer} object that |
|
719 |
* represents the integer value specified by the string. |
|
720 |
* |
|
721 |
* <p>In other words, this method returns an {@code Integer} |
|
722 |
* object equal to the value of: |
|
723 |
* |
|
724 |
* <blockquote> |
|
725 |
* {@code new Integer(Integer.parseInt(s, radix))} |
|
726 |
* </blockquote> |
|
727 |
* |
|
728 |
* @param s the string to be parsed. |
|
729 |
* @param radix the radix to be used in interpreting {@code s} |
|
730 |
* @return an {@code Integer} object holding the value |
|
731 |
* represented by the string argument in the specified |
|
732 |
* radix. |
|
733 |
* @exception NumberFormatException if the {@code String} |
|
734 |
* does not contain a parsable {@code int}. |
|
735 |
*/ |
|
736 |
public static Integer valueOf(String s, int radix) throws NumberFormatException { |
|
2425 | 737 |
return Integer.valueOf(parseInt(s,radix)); |
2 | 738 |
} |
739 |
||
740 |
/** |
|
741 |
* Returns an {@code Integer} object holding the |
|
742 |
* value of the specified {@code String}. The argument is |
|
743 |
* interpreted as representing a signed decimal integer, exactly |
|
744 |
* as if the argument were given to the {@link |
|
745 |
* #parseInt(java.lang.String)} method. The result is an |
|
746 |
* {@code Integer} object that represents the integer value |
|
747 |
* specified by the string. |
|
748 |
* |
|
749 |
* <p>In other words, this method returns an {@code Integer} |
|
750 |
* object equal to the value of: |
|
751 |
* |
|
752 |
* <blockquote> |
|
753 |
* {@code new Integer(Integer.parseInt(s))} |
|
754 |
* </blockquote> |
|
755 |
* |
|
756 |
* @param s the string to be parsed. |
|
757 |
* @return an {@code Integer} object holding the value |
|
758 |
* represented by the string argument. |
|
759 |
* @exception NumberFormatException if the string cannot be parsed |
|
760 |
* as an integer. |
|
761 |
*/ |
|
2425 | 762 |
public static Integer valueOf(String s) throws NumberFormatException { |
763 |
return Integer.valueOf(parseInt(s, 10)); |
|
764 |
} |
|
765 |
||
766 |
/** |
|
767 |
* Cache to support the object identity semantics of autoboxing for values between |
|
768 |
* -128 and 127 (inclusive) as required by JLS. |
|
769 |
* |
|
6882
637546039be3
6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents:
5506
diff
changeset
|
770 |
* The cache is initialized on first usage. The size of the cache |
14014 | 771 |
* 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
|
772 |
* 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
|
773 |
* 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
|
774 |
* sun.misc.VM class. |
2425 | 775 |
*/ |
776 |
||
2 | 777 |
private static class IntegerCache { |
2425 | 778 |
static final int low = -128; |
779 |
static final int high; |
|
780 |
static final Integer cache[]; |
|
2 | 781 |
|
782 |
static { |
|
2425 | 783 |
// high value may be configured by property |
784 |
int h = 127; |
|
6882
637546039be3
6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents:
5506
diff
changeset
|
785 |
String integerCacheHighPropValue = |
637546039be3
6977738: Deadlock between java.lang.ClassLoader and java.util.Properties
mchung
parents:
5506
diff
changeset
|
786 |
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); |
2425 | 787 |
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
|
788 |
try { |
4b4381313a3c
8015395: NumberFormatException during startup if JDK-internal property java.lang.Integer.IntegerCache.high set to bad value
bpb
parents:
17929
diff
changeset
|
789 |
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
|
790 |
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
|
791 |
// 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
|
792 |
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
|
793 |
} 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
|
794 |
// 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
|
795 |
} |
2425 | 796 |
} |
797 |
high = h; |
|
798 |
||
799 |
cache = new Integer[(high - low) + 1]; |
|
800 |
int j = low; |
|
801 |
for(int k = 0; k < cache.length; k++) |
|
802 |
cache[k] = new Integer(j++); |
|
12668
9a70f2230e3b
7165102: Only run assertion on Integer autoboxing cache size once
forax
parents:
11676
diff
changeset
|
803 |
|
9a70f2230e3b
7165102: Only run assertion on Integer autoboxing cache size once
forax
parents:
11676
diff
changeset
|
804 |
// 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
|
805 |
assert IntegerCache.high >= 127; |
2 | 806 |
} |
2425 | 807 |
|
808 |
private IntegerCache() {} |
|
2 | 809 |
} |
810 |
||
811 |
/** |
|
812 |
* Returns an {@code Integer} instance representing the specified |
|
813 |
* {@code int} value. If a new {@code Integer} instance is not |
|
814 |
* required, this method should generally be used in preference to |
|
815 |
* the constructor {@link #Integer(int)}, as this method is likely |
|
816 |
* to yield significantly better space and time performance by |
|
817 |
* caching frequently requested values. |
|
818 |
* |
|
3224
3aa65803ae07
6628737: Specification of wrapper class valueOf static factories should require caching
darcy
parents:
2425
diff
changeset
|
819 |
* 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
|
820 |
* 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
|
821 |
* |
2 | 822 |
* @param i an {@code int} value. |
823 |
* @return an {@code Integer} instance representing {@code i}. |
|
824 |
* @since 1.5 |
|
825 |
*/ |
|
826 |
public static Integer valueOf(int i) { |
|
2425 | 827 |
if (i >= IntegerCache.low && i <= IntegerCache.high) |
828 |
return IntegerCache.cache[i + (-IntegerCache.low)]; |
|
2 | 829 |
return new Integer(i); |
830 |
} |
|
831 |
||
832 |
/** |
|
833 |
* The value of the {@code Integer}. |
|
834 |
* |
|
835 |
* @serial |
|
836 |
*/ |
|
837 |
private final int value; |
|
838 |
||
839 |
/** |
|
840 |
* Constructs a newly allocated {@code Integer} object that |
|
841 |
* represents the specified {@code int} value. |
|
842 |
* |
|
843 |
* @param value the value to be represented by the |
|
844 |
* {@code Integer} object. |
|
845 |
*/ |
|
846 |
public Integer(int value) { |
|
847 |
this.value = value; |
|
848 |
} |
|
849 |
||
850 |
/** |
|
851 |
* Constructs a newly allocated {@code Integer} object that |
|
852 |
* represents the {@code int} value indicated by the |
|
853 |
* {@code String} parameter. The string is converted to an |
|
854 |
* {@code int} value in exactly the manner used by the |
|
855 |
* {@code parseInt} method for radix 10. |
|
856 |
* |
|
857 |
* @param s the {@code String} to be converted to an |
|
858 |
* {@code Integer}. |
|
859 |
* @exception NumberFormatException if the {@code String} does not |
|
860 |
* contain a parsable integer. |
|
861 |
* @see java.lang.Integer#parseInt(java.lang.String, int) |
|
862 |
*/ |
|
863 |
public Integer(String s) throws NumberFormatException { |
|
864 |
this.value = parseInt(s, 10); |
|
865 |
} |
|
866 |
||
867 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
868 |
* 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
|
869 |
* after a narrowing primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
870 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 871 |
*/ |
872 |
public byte byteValue() { |
|
873 |
return (byte)value; |
|
874 |
} |
|
875 |
||
876 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
877 |
* 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
|
878 |
* after a narrowing primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
879 |
* @jls 5.1.3 Narrowing Primitive Conversions |
2 | 880 |
*/ |
881 |
public short shortValue() { |
|
882 |
return (short)value; |
|
883 |
} |
|
884 |
||
885 |
/** |
|
886 |
* Returns the value of this {@code Integer} as an |
|
887 |
* {@code int}. |
|
888 |
*/ |
|
889 |
public int intValue() { |
|
890 |
return value; |
|
891 |
} |
|
892 |
||
893 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
894 |
* 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
|
895 |
* after a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
896 |
* @jls 5.1.2 Widening Primitive Conversions |
17929
5ef41523c723
8007398: Peformance improvements to Integer and Long string formatting.
mduigou
parents:
15311
diff
changeset
|
897 |
* @see Integer#toUnsignedLong(int) |
2 | 898 |
*/ |
899 |
public long longValue() { |
|
900 |
return (long)value; |
|
901 |
} |
|
902 |
||
903 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
904 |
* 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
|
905 |
* after a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
906 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 907 |
*/ |
908 |
public float floatValue() { |
|
909 |
return (float)value; |
|
910 |
} |
|
911 |
||
912 |
/** |
|
10067
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
913 |
* 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
|
914 |
* after a widening primitive conversion. |
1263ecd22db6
6253144: Long narrowing conversion should describe the algorithm used and implied "risks"
darcy
parents:
9266
diff
changeset
|
915 |
* @jls 5.1.2 Widening Primitive Conversions |
2 | 916 |
*/ |
917 |
public double doubleValue() { |
|
918 |
return (double)value; |
|
919 |
} |
|
920 |
||
921 |
/** |
|
922 |
* Returns a {@code String} object representing this |
|
923 |
* {@code Integer}'s value. The value is converted to signed |
|
924 |
* decimal representation and returned as a string, exactly as if |
|
925 |
* the integer value were given as an argument to the {@link |
|
926 |
* java.lang.Integer#toString(int)} method. |
|
927 |
* |
|
928 |
* @return a string representation of the value of this object in |
|
929 |
* base 10. |
|
930 |
*/ |
|
931 |
public String toString() { |
|
3964
cf913644be58
6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
darcy
parents:
3943
diff
changeset
|
932 |
return toString(value); |
2 | 933 |
} |
934 |
||
935 |
/** |
|
936 |
* Returns a hash code for this {@code Integer}. |
|
937 |
* |
|
938 |
* @return a hash code value for this object, equal to the |
|
939 |
* primitive {@code int} value represented by this |
|
940 |
* {@code Integer} object. |
|
941 |
*/ |
|
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
942 |
@Override |
2 | 943 |
public int hashCode() { |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
944 |
return Integer.hashCode(value); |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
945 |
} |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
946 |
|
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
947 |
/** |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
948 |
* 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
|
949 |
* {@code Integer.hashCode()}. |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
950 |
* |
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
951 |
* @param value the value to hash |
14503
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
952 |
* @since 1.8 |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
953 |
* |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
954 |
* @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
|
955 |
*/ |
0729d9e57ed5
7088913: Add compatible static hashCode(primitive) to primitive wrapper classes
mduigou
parents:
14014
diff
changeset
|
956 |
public static int hashCode(int value) { |
2 | 957 |
return value; |
958 |
} |
|
959 |
||
960 |
/** |
|
961 |
* Compares this object to the specified object. The result is |
|
962 |
* {@code true} if and only if the argument is not |
|
963 |
* {@code null} and is an {@code Integer} object that |
|
964 |
* contains the same {@code int} value as this object. |
|
965 |
* |
|
966 |
* @param obj the object to compare with. |
|
967 |
* @return {@code true} if the objects are the same; |
|
968 |
* {@code false} otherwise. |
|
969 |
*/ |
|
970 |
public boolean equals(Object obj) { |
|
971 |
if (obj instanceof Integer) { |
|
972 |
return value == ((Integer)obj).intValue(); |
|
973 |
} |
|
974 |
return false; |
|
975 |
} |
|
976 |
||
977 |
/** |
|
978 |
* Determines the integer value of the system property with the |
|
979 |
* specified name. |
|
980 |
* |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
981 |
* <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
|
982 |
* property. System properties are accessible through the {@link |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
983 |
* java.lang.System#getProperty(java.lang.String)} method. The |
2 | 984 |
* 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
|
985 |
* value using the grammar supported by {@link Integer#decode decode} and |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
986 |
* an {@code Integer} object representing this value is returned. |
2 | 987 |
* |
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
988 |
* <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
|
989 |
* specified name is empty or {@code null}, or if the property |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
990 |
* does not have the correct numeric format, then {@code null} is |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
991 |
* returned. |
2 | 992 |
* |
993 |
* <p>In other words, this method returns an {@code Integer} |
|
994 |
* object equal to the value of: |
|
995 |
* |
|
996 |
* <blockquote> |
|
997 |
* {@code getInteger(nm, null)} |
|
998 |
* </blockquote> |
|
999 |
* |
|
1000 |
* @param nm property name. |
|
1001 |
* @return the {@code Integer} value of the property. |
|
10602
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1002 |
* @throws SecurityException for the same reasons as |
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1003 |
* {@link System#getProperty(String) System.getProperty} |
2 | 1004 |
* @see java.lang.System#getProperty(java.lang.String) |
1005 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String) |
|
1006 |
*/ |
|
1007 |
public static Integer getInteger(String nm) { |
|
1008 |
return getInteger(nm, null); |
|
1009 |
} |
|
1010 |
||
1011 |
/** |
|
1012 |
* Determines the integer value of the system property with the |
|
1013 |
* specified name. |
|
1014 |
* |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1015 |
* <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
|
1016 |
* property. System properties are accessible through the {@link |
2 | 1017 |
* java.lang.System#getProperty(java.lang.String)} method. The |
1018 |
* 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
|
1019 |
* value using the grammar supported by {@link Integer#decode decode} and |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1020 |
* an {@code Integer} object representing this value is returned. |
2 | 1021 |
* |
1022 |
* <p>The second argument is the default value. An {@code Integer} object |
|
1023 |
* that represents the value of the second argument is returned if there |
|
1024 |
* is no property of the specified name, if the property does not have |
|
1025 |
* the correct numeric format, or if the specified name is empty or |
|
1026 |
* {@code null}. |
|
1027 |
* |
|
1028 |
* <p>In other words, this method returns an {@code Integer} object |
|
1029 |
* equal to the value of: |
|
1030 |
* |
|
1031 |
* <blockquote> |
|
1032 |
* {@code getInteger(nm, new Integer(val))} |
|
1033 |
* </blockquote> |
|
1034 |
* |
|
1035 |
* but in practice it may be implemented in a manner such as: |
|
1036 |
* |
|
1037 |
* <blockquote><pre> |
|
1038 |
* Integer result = getInteger(nm, null); |
|
1039 |
* return (result == null) ? new Integer(val) : result; |
|
1040 |
* </pre></blockquote> |
|
1041 |
* |
|
1042 |
* to avoid the unnecessary allocation of an {@code Integer} |
|
1043 |
* object when the default value is not needed. |
|
1044 |
* |
|
1045 |
* @param nm property name. |
|
1046 |
* @param val default value. |
|
1047 |
* @return the {@code Integer} value of the property. |
|
10602
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1048 |
* @throws SecurityException for the same reasons as |
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1049 |
* {@link System#getProperty(String) System.getProperty} |
2 | 1050 |
* @see java.lang.System#getProperty(java.lang.String) |
1051 |
* @see java.lang.System#getProperty(java.lang.String, java.lang.String) |
|
1052 |
*/ |
|
1053 |
public static Integer getInteger(String nm, int val) { |
|
1054 |
Integer result = getInteger(nm, null); |
|
2425 | 1055 |
return (result == null) ? Integer.valueOf(val) : result; |
2 | 1056 |
} |
1057 |
||
1058 |
/** |
|
1059 |
* Returns the integer value of the system property with the |
|
1060 |
* specified name. The first argument is treated as the name of a |
|
1061 |
* system property. System properties are accessible through the |
|
1062 |
* {@link java.lang.System#getProperty(java.lang.String)} method. |
|
1063 |
* 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
|
1064 |
* integer value, as per the {@link Integer#decode decode} method, |
2 | 1065 |
* and an {@code Integer} object representing this value is |
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1066 |
* returned; in summary: |
2 | 1067 |
* |
1068 |
* <ul><li>If the property value begins with the two ASCII characters |
|
1069 |
* {@code 0x} or the ASCII character {@code #}, not |
|
1070 |
* followed by a minus sign, then the rest of it is parsed as a |
|
1071 |
* hexadecimal integer exactly as by the method |
|
1072 |
* {@link #valueOf(java.lang.String, int)} with radix 16. |
|
1073 |
* <li>If the property value begins with the ASCII character |
|
1074 |
* {@code 0} followed by another character, it is parsed as an |
|
1075 |
* octal integer exactly as by the method |
|
1076 |
* {@link #valueOf(java.lang.String, int)} with radix 8. |
|
1077 |
* <li>Otherwise, the property value is parsed as a decimal integer |
|
1078 |
* exactly as by the method {@link #valueOf(java.lang.String, int)} |
|
1079 |
* with radix 10. |
|
1080 |
* </ul> |
|
1081 |
* |
|
1082 |
* <p>The second argument is the default value. The default value is |
|
1083 |
* returned if there is no property of the specified name, if the |
|
1084 |
* property does not have the correct numeric format, or if the |
|
1085 |
* specified name is empty or {@code null}. |
|
1086 |
* |
|
1087 |
* @param nm property name. |
|
1088 |
* @param val default value. |
|
1089 |
* @return the {@code Integer} value of the property. |
|
10602
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1090 |
* @throws SecurityException for the same reasons as |
ab8c1e3b237b
6268216: Boolean.getBoolean() throws SecurityException
darcy
parents:
10335
diff
changeset
|
1091 |
* {@link System#getProperty(String) System.getProperty} |
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1092 |
* @see System#getProperty(java.lang.String) |
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1093 |
* @see System#getProperty(java.lang.String, java.lang.String) |
2 | 1094 |
*/ |
1095 |
public static Integer getInteger(String nm, Integer val) { |
|
1096 |
String v = null; |
|
1097 |
try { |
|
1098 |
v = System.getProperty(nm); |
|
10335
3c7eda3ab2f5
4850225: Integer.getInteger() : Bad reference to getProperty?
darcy
parents:
10067
diff
changeset
|
1099 |
} catch (IllegalArgumentException | NullPointerException e) { |
2 | 1100 |
} |
1101 |
if (v != null) { |
|
1102 |
try { |
|
1103 |
return Integer.decode(v); |
|
1104 |
} catch (NumberFormatException e) { |
|
1105 |
} |
|
1106 |
} |
|
1107 |
return val; |
|
1108 |
} |
|
1109 |
||
1110 |
/** |
|
1111 |
* Decodes a {@code String} into an {@code Integer}. |
|
1112 |
* Accepts decimal, hexadecimal, and octal numbers given |
|
1113 |
* by the following grammar: |
|
1114 |
* |
|
1115 |
* <blockquote> |
|
1116 |
* <dl> |
|
1117 |
* <dt><i>DecodableString:</i> |
|
1118 |
* <dd><i>Sign<sub>opt</sub> DecimalNumeral</i> |
|
1119 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i> |
|
1120 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i> |
|
1121 |
* <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i> |
|
1122 |
* <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i> |
|
21334 | 1123 |
* |
2 | 1124 |
* <dt><i>Sign:</i> |
1125 |
* <dd>{@code -} |
|
1126 |
* <dd>{@code +} |
|
1127 |
* </dl> |
|
1128 |
* </blockquote> |
|
1129 |
* |
|
1130 |
* <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
|
1131 |
* 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
|
1132 |
* <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
|
1133 |
* except that underscores are not accepted between digits. |
2 | 1134 |
* |
1135 |
* <p>The sequence of characters following an optional |
|
1136 |
* sign and/or radix specifier ("{@code 0x}", "{@code 0X}", |
|
1137 |
* "{@code #}", or leading zero) is parsed as by the {@code |
|
1138 |
* Integer.parseInt} method with the indicated radix (10, 16, or |
|
1139 |
* 8). This sequence of characters must represent a positive |
|
1140 |
* value or a {@link NumberFormatException} will be thrown. The |
|
1141 |
* result is negated if first character of the specified {@code |
|
1142 |
* String} is the minus sign. No whitespace characters are |
|
1143 |
* permitted in the {@code String}. |
|
1144 |
* |
|
1145 |
* @param nm the {@code String} to decode. |
|
1146 |
* @return an {@code Integer} object holding the {@code int} |
|
1147 |
* value represented by {@code nm} |
|
1148 |
* @exception NumberFormatException if the {@code String} does not |
|
1149 |
* contain a parsable integer. |
|
1150 |
* @see java.lang.Integer#parseInt(java.lang.String, int) |
|
1151 |
*/ |
|
1152 |
public static Integer decode(String nm) throws NumberFormatException { |
|
1153 |
int radix = 10; |
|
1154 |
int index = 0; |
|
1155 |
boolean negative = false; |
|
1156 |
Integer result; |
|
1157 |
||
1158 |
if (nm.length() == 0) |
|
1159 |
throw new NumberFormatException("Zero length string"); |
|
1160 |
char firstChar = nm.charAt(0); |
|
1161 |
// Handle sign, if present |
|
1162 |
if (firstChar == '-') { |
|
1163 |
negative = true; |
|
1164 |
index++; |
|
1165 |
} else if (firstChar == '+') |
|
1166 |
index++; |
|
1167 |
||
1168 |
// Handle radix specifier, if present |
|
1169 |
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { |
|
1170 |
index += 2; |
|
1171 |
radix = 16; |
|
1172 |
} |
|
1173 |
else if (nm.startsWith("#", index)) { |
|
1174 |
index ++; |
|
1175 |
radix = 16; |
|
1176 |
} |
|
1177 |
else if (nm.startsWith("0", index) && nm.length() > 1 + index) { |
|
1178 |
index ++; |
|
1179 |
radix = 8; |
|
1180 |
} |
|
1181 |
||
1182 |
if (nm.startsWith("-", index) || nm.startsWith("+", index)) |
|
1183 |
throw new NumberFormatException("Sign character in wrong position"); |
|
1184 |
||
1185 |
try { |
|
1186 |
result = Integer.valueOf(nm.substring(index), radix); |
|
2425 | 1187 |
result = negative ? Integer.valueOf(-result.intValue()) : result; |
2 | 1188 |
} catch (NumberFormatException e) { |
1189 |
// If number is Integer.MIN_VALUE, we'll end up here. The next line |
|
1190 |
// handles this case, and causes any genuine format error to be |
|
1191 |
// rethrown. |
|
1192 |
String constant = negative ? ("-" + nm.substring(index)) |
|
1193 |
: nm.substring(index); |
|
1194 |
result = Integer.valueOf(constant, radix); |
|
1195 |
} |
|
1196 |
return result; |
|
1197 |
} |
|
1198 |
||
1199 |
/** |
|
1200 |
* Compares two {@code Integer} objects numerically. |
|
1201 |
* |
|
1202 |
* @param anotherInteger the {@code Integer} to be compared. |
|
1203 |
* @return the value {@code 0} if this {@code Integer} is |
|
1204 |
* equal to the argument {@code Integer}; a value less than |
|
1205 |
* {@code 0} if this {@code Integer} is numerically less |
|
1206 |
* than the argument {@code Integer}; and a value greater |
|
1207 |
* than {@code 0} if this {@code Integer} is numerically |
|
1208 |
* greater than the argument {@code Integer} (signed |
|
1209 |
* comparison). |
|
1210 |
* @since 1.2 |
|
1211 |
*/ |
|
1212 |
public int compareTo(Integer anotherInteger) { |
|
3943
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1213 |
return compare(this.value, anotherInteger.value); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1214 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1215 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1216 |
/** |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1217 |
* Compares two {@code int} values numerically. |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1218 |
* 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
|
1219 |
* <pre> |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1220 |
* 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
|
1221 |
* </pre> |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1222 |
* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1223 |
* @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
|
1224 |
* @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
|
1225 |
* @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
|
1226 |
* 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
|
1227 |
* 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
|
1228 |
* @since 1.7 |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1229 |
*/ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
3288
diff
changeset
|
1230 |
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
|
1231 |
return (x < y) ? -1 : ((x == y) ? 0 : 1); |
2 | 1232 |
} |
1233 |
||
11672
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1234 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1235 |
* 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
|
1236 |
* as unsigned. |
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 |
* @param x the first {@code int} to compare |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1239 |
* @param y the second {@code int} to compare |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1240 |
* @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
|
1241 |
* 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
|
1242 |
* 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
|
1243 |
* unsigned values |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1244 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1245 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1246 |
public static int compareUnsigned(int x, int y) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1247 |
return compare(x + MIN_VALUE, y + MIN_VALUE); |
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 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1250 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1251 |
* 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
|
1252 |
* 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
|
1253 |
* 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
|
1254 |
* 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
|
1255 |
* argument. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1256 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1257 |
* 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
|
1258 |
* 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
|
1259 |
* 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
|
1260 |
* input plus 2<sup>32</sup>. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1261 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1262 |
* @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
|
1263 |
* @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
|
1264 |
* conversion |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1265 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1266 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1267 |
public static long toUnsignedLong(int x) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1268 |
return ((long) x) & 0xffffffffL; |
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 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1271 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1272 |
* 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
|
1273 |
* 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
|
1274 |
* an unsigned value. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1275 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1276 |
* <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
|
1277 |
* 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
|
1278 |
* 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
|
1279 |
* 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
|
1280 |
* addUnsigned}, etc. methods are not provided. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1281 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1282 |
* @param dividend the value to be divided |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1283 |
* @param divisor the value doing the dividing |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1284 |
* @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
|
1285 |
* the second argument |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1286 |
* @see #remainderUnsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1287 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1288 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1289 |
public static int divideUnsigned(int dividend, int divisor) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1290 |
// 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
|
1291 |
return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor)); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1292 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1293 |
|
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1294 |
/** |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1295 |
* 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
|
1296 |
* 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
|
1297 |
* as an unsigned value. |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1298 |
* |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1299 |
* @param dividend the value to be divided |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1300 |
* @param divisor the value doing the dividing |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1301 |
* @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
|
1302 |
* the second argument |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1303 |
* @see #divideUnsigned |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1304 |
* @since 1.8 |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1305 |
*/ |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1306 |
public static int remainderUnsigned(int dividend, int divisor) { |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1307 |
// 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
|
1308 |
return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor)); |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1309 |
} |
a5fa8c844b54
4504839: Java libraries should provide support for unsigned integer arithmetic
darcy
parents:
11275
diff
changeset
|
1310 |
|
2 | 1311 |
|
1312 |
// Bit twiddling |
|
1313 |
||
1314 |
/** |
|
1315 |
* The number of bits used to represent an {@code int} value in two's |
|
1316 |
* complement binary form. |
|
1317 |
* |
|
1318 |
* @since 1.5 |
|
1319 |
*/ |
|
15136
c17824042364
8005856: build-infra: Remove special handling of base module classes header generation
erikj
parents:
14507
diff
changeset
|
1320 |
@Native public static final int SIZE = 32; |
2 | 1321 |
|
1322 |
/** |
|
14507
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1323 |
* 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
|
1324 |
* complement binary form. |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1325 |
* |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1326 |
* @since 1.8 |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1327 |
*/ |
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1328 |
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
|
1329 |
|
066419d1e732
7088952: Add size in bytes constant "BYTES" to primitive type wrapper types
mduigou
parents:
14503
diff
changeset
|
1330 |
/** |
2 | 1331 |
* Returns an {@code int} value with at most a single one-bit, in the |
1332 |
* position of the highest-order ("leftmost") one-bit in the specified |
|
1333 |
* {@code int} value. Returns zero if the specified value has no |
|
1334 |
* one-bits in its two's complement binary representation, that is, if it |
|
1335 |
* is equal to zero. |
|
1336 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1337 |
* @param i the value whose highest one bit is to be computed |
2 | 1338 |
* @return an {@code int} value with a single one-bit, in the position |
1339 |
* of the highest-order one-bit in the specified value, or zero if |
|
1340 |
* the specified value is itself equal to zero. |
|
1341 |
* @since 1.5 |
|
1342 |
*/ |
|
1343 |
public static int highestOneBit(int i) { |
|
1344 |
// HD, Figure 3-1 |
|
1345 |
i |= (i >> 1); |
|
1346 |
i |= (i >> 2); |
|
1347 |
i |= (i >> 4); |
|
1348 |
i |= (i >> 8); |
|
1349 |
i |= (i >> 16); |
|
1350 |
return i - (i >>> 1); |
|
1351 |
} |
|
1352 |
||
1353 |
/** |
|
1354 |
* Returns an {@code int} value with at most a single one-bit, in the |
|
1355 |
* position of the lowest-order ("rightmost") one-bit in the specified |
|
1356 |
* {@code int} value. Returns zero if the specified value has no |
|
1357 |
* one-bits in its two's complement binary representation, that is, if it |
|
1358 |
* is equal to zero. |
|
1359 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1360 |
* @param i the value whose lowest one bit is to be computed |
2 | 1361 |
* @return an {@code int} value with a single one-bit, in the position |
1362 |
* of the lowest-order one-bit in the specified value, or zero if |
|
1363 |
* the specified value is itself equal to zero. |
|
1364 |
* @since 1.5 |
|
1365 |
*/ |
|
1366 |
public static int lowestOneBit(int i) { |
|
1367 |
// HD, Section 2-1 |
|
1368 |
return i & -i; |
|
1369 |
} |
|
1370 |
||
1371 |
/** |
|
1372 |
* Returns the number of zero bits preceding the highest-order |
|
1373 |
* ("leftmost") one-bit in the two's complement binary representation |
|
1374 |
* of the specified {@code int} value. Returns 32 if the |
|
1375 |
* specified value has no one-bits in its two's complement representation, |
|
1376 |
* in other words if it is equal to zero. |
|
1377 |
* |
|
1378 |
* <p>Note that this method is closely related to the logarithm base 2. |
|
1379 |
* For all positive {@code int} values x: |
|
1380 |
* <ul> |
|
1381 |
* <li>floor(log<sub>2</sub>(x)) = {@code 31 - numberOfLeadingZeros(x)} |
|
1382 |
* <li>ceil(log<sub>2</sub>(x)) = {@code 32 - numberOfLeadingZeros(x - 1)} |
|
1383 |
* </ul> |
|
1384 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1385 |
* @param i the value whose number of leading zeros is to be computed |
2 | 1386 |
* @return the number of zero bits preceding the highest-order |
1387 |
* ("leftmost") one-bit in the two's complement binary representation |
|
1388 |
* of the specified {@code int} value, or 32 if the value |
|
1389 |
* is equal to zero. |
|
1390 |
* @since 1.5 |
|
1391 |
*/ |
|
1392 |
public static int numberOfLeadingZeros(int i) { |
|
1393 |
// HD, Figure 5-6 |
|
1394 |
if (i == 0) |
|
1395 |
return 32; |
|
1396 |
int n = 1; |
|
1397 |
if (i >>> 16 == 0) { n += 16; i <<= 16; } |
|
1398 |
if (i >>> 24 == 0) { n += 8; i <<= 8; } |
|
1399 |
if (i >>> 28 == 0) { n += 4; i <<= 4; } |
|
1400 |
if (i >>> 30 == 0) { n += 2; i <<= 2; } |
|
1401 |
n -= i >>> 31; |
|
1402 |
return n; |
|
1403 |
} |
|
1404 |
||
1405 |
/** |
|
1406 |
* Returns the number of zero bits following the lowest-order ("rightmost") |
|
1407 |
* one-bit in the two's complement binary representation of the specified |
|
1408 |
* {@code int} value. Returns 32 if the specified value has no |
|
1409 |
* one-bits in its two's complement representation, in other words if it is |
|
1410 |
* equal to zero. |
|
1411 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1412 |
* @param i the value whose number of trailing zeros is to be computed |
2 | 1413 |
* @return the number of zero bits following the lowest-order ("rightmost") |
1414 |
* one-bit in the two's complement binary representation of the |
|
1415 |
* specified {@code int} value, or 32 if the value is equal |
|
1416 |
* to zero. |
|
1417 |
* @since 1.5 |
|
1418 |
*/ |
|
1419 |
public static int numberOfTrailingZeros(int i) { |
|
1420 |
// HD, Figure 5-14 |
|
1421 |
int y; |
|
1422 |
if (i == 0) return 32; |
|
1423 |
int n = 31; |
|
1424 |
y = i <<16; if (y != 0) { n = n -16; i = y; } |
|
1425 |
y = i << 8; if (y != 0) { n = n - 8; i = y; } |
|
1426 |
y = i << 4; if (y != 0) { n = n - 4; i = y; } |
|
1427 |
y = i << 2; if (y != 0) { n = n - 2; i = y; } |
|
1428 |
return n - ((i << 1) >>> 31); |
|
1429 |
} |
|
1430 |
||
1431 |
/** |
|
1432 |
* Returns the number of one-bits in the two's complement binary |
|
1433 |
* representation of the specified {@code int} value. This function is |
|
1434 |
* sometimes referred to as the <i>population count</i>. |
|
1435 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1436 |
* @param i the value whose bits are to be counted |
2 | 1437 |
* @return the number of one-bits in the two's complement binary |
1438 |
* representation of the specified {@code int} value. |
|
1439 |
* @since 1.5 |
|
1440 |
*/ |
|
1441 |
public static int bitCount(int i) { |
|
1442 |
// HD, Figure 5-2 |
|
1443 |
i = i - ((i >>> 1) & 0x55555555); |
|
1444 |
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); |
|
1445 |
i = (i + (i >>> 4)) & 0x0f0f0f0f; |
|
1446 |
i = i + (i >>> 8); |
|
1447 |
i = i + (i >>> 16); |
|
1448 |
return i & 0x3f; |
|
1449 |
} |
|
1450 |
||
1451 |
/** |
|
1452 |
* Returns the value obtained by rotating the two's complement binary |
|
1453 |
* representation of the specified {@code int} value left by the |
|
1454 |
* specified number of bits. (Bits shifted out of the left hand, or |
|
1455 |
* high-order, side reenter on the right, or low-order.) |
|
1456 |
* |
|
1457 |
* <p>Note that left rotation with a negative distance is equivalent to |
|
1458 |
* right rotation: {@code rotateLeft(val, -distance) == rotateRight(val, |
|
1459 |
* distance)}. Note also that rotation by any multiple of 32 is a |
|
1460 |
* no-op, so all but the last five bits of the rotation distance can be |
|
1461 |
* ignored, even if the distance is negative: {@code rotateLeft(val, |
|
1462 |
* distance) == rotateLeft(val, distance & 0x1F)}. |
|
1463 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1464 |
* @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
|
1465 |
* @param distance the number of bit positions to rotate left |
2 | 1466 |
* @return the value obtained by rotating the two's complement binary |
1467 |
* representation of the specified {@code int} value left by the |
|
1468 |
* specified number of bits. |
|
1469 |
* @since 1.5 |
|
1470 |
*/ |
|
1471 |
public static int rotateLeft(int i, int distance) { |
|
1472 |
return (i << distance) | (i >>> -distance); |
|
1473 |
} |
|
1474 |
||
1475 |
/** |
|
1476 |
* Returns the value obtained by rotating the two's complement binary |
|
1477 |
* representation of the specified {@code int} value right by the |
|
1478 |
* specified number of bits. (Bits shifted out of the right hand, or |
|
1479 |
* low-order, side reenter on the left, or high-order.) |
|
1480 |
* |
|
1481 |
* <p>Note that right rotation with a negative distance is equivalent to |
|
1482 |
* left rotation: {@code rotateRight(val, -distance) == rotateLeft(val, |
|
1483 |
* distance)}. Note also that rotation by any multiple of 32 is a |
|
1484 |
* no-op, so all but the last five bits of the rotation distance can be |
|
1485 |
* ignored, even if the distance is negative: {@code rotateRight(val, |
|
1486 |
* distance) == rotateRight(val, distance & 0x1F)}. |
|
1487 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1488 |
* @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
|
1489 |
* @param distance the number of bit positions to rotate right |
2 | 1490 |
* @return the value obtained by rotating the two's complement binary |
1491 |
* representation of the specified {@code int} value right by the |
|
1492 |
* specified number of bits. |
|
1493 |
* @since 1.5 |
|
1494 |
*/ |
|
1495 |
public static int rotateRight(int i, int distance) { |
|
1496 |
return (i >>> distance) | (i << -distance); |
|
1497 |
} |
|
1498 |
||
1499 |
/** |
|
1500 |
* Returns the value obtained by reversing the order of the bits in the |
|
1501 |
* two's complement binary representation of the specified {@code int} |
|
1502 |
* value. |
|
1503 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1504 |
* @param i the value to be reversed |
2 | 1505 |
* @return the value obtained by reversing order of the bits in the |
1506 |
* specified {@code int} value. |
|
1507 |
* @since 1.5 |
|
1508 |
*/ |
|
1509 |
public static int reverse(int i) { |
|
1510 |
// HD, Figure 7-1 |
|
1511 |
i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; |
|
1512 |
i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; |
|
1513 |
i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; |
|
1514 |
i = (i << 24) | ((i & 0xff00) << 8) | |
|
1515 |
((i >>> 8) & 0xff00) | (i >>> 24); |
|
1516 |
return i; |
|
1517 |
} |
|
1518 |
||
1519 |
/** |
|
1520 |
* Returns the signum function of the specified {@code int} value. (The |
|
1521 |
* return value is -1 if the specified value is negative; 0 if the |
|
1522 |
* specified value is zero; and 1 if the specified value is positive.) |
|
1523 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1524 |
* @param i the value whose signum is to be computed |
2 | 1525 |
* @return the signum function of the specified {@code int} value. |
1526 |
* @since 1.5 |
|
1527 |
*/ |
|
1528 |
public static int signum(int i) { |
|
1529 |
// HD, Section 2-7 |
|
1530 |
return (i >> 31) | (-i >>> 31); |
|
1531 |
} |
|
1532 |
||
1533 |
/** |
|
1534 |
* Returns the value obtained by reversing the order of the bytes in the |
|
1535 |
* two's complement representation of the specified {@code int} value. |
|
1536 |
* |
|
18546
862067c6481c
8017550: Fix doclint issues in java.lang and subpackages
darcy
parents:
18278
diff
changeset
|
1537 |
* @param i the value whose bytes are to be reversed |
2 | 1538 |
* @return the value obtained by reversing the bytes in the specified |
1539 |
* {@code int} value. |
|
1540 |
* @since 1.5 |
|
1541 |
*/ |
|
1542 |
public static int reverseBytes(int i) { |
|
1543 |
return ((i >>> 24) ) | |
|
1544 |
((i >> 8) & 0xFF00) | |
|
1545 |
((i << 8) & 0xFF0000) | |
|
1546 |
((i << 24)); |
|
1547 |
} |
|
1548 |
||
15311
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1549 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1550 |
* 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
|
1551 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1552 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1553 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1554 |
* @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
|
1555 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1556 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1557 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1558 |
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
|
1559 |
return a + b; |
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 |
|
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1562 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1563 |
* 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
|
1564 |
* 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
|
1565 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1566 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1567 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1568 |
* @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
|
1569 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1570 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1571 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1572 |
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
|
1573 |
return Math.max(a, b); |
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 |
|
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1576 |
/** |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1577 |
* 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
|
1578 |
* 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
|
1579 |
* |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1580 |
* @param a the first operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1581 |
* @param b the second operand |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1582 |
* @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
|
1583 |
* @see java.util.function.BinaryOperator |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1584 |
* @since 1.8 |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1585 |
*/ |
be0ff4a719bf
8004201: Add static utility methods to primitives to be used for redution operations.
mduigou
parents:
15140
diff
changeset
|
1586 |
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
|
1587 |
return Math.min(a, b); |
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 |
|
2 | 1590 |
/** 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
|
1591 |
@Native private static final long serialVersionUID = 1360826667806852920L; |
2 | 1592 |
} |