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