| 1 | /* java.lang.Integer
|
|---|
| 2 | Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 6 | GNU Classpath is free software; you can redistribute it and/or modify
|
|---|
| 7 | it under the terms of the GNU General Public License as published by
|
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | any later version.
|
|---|
| 10 |
|
|---|
| 11 | GNU Classpath is distributed in the hope that it will be useful, but
|
|---|
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 14 | General Public License for more details.
|
|---|
| 15 |
|
|---|
| 16 | You should have received a copy of the GNU General Public License
|
|---|
| 17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
|---|
| 18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA.
|
|---|
| 20 |
|
|---|
| 21 | Linking this library statically or dynamically with other modules is
|
|---|
| 22 | making a combined work based on this library. Thus, the terms and
|
|---|
| 23 | conditions of the GNU General Public License cover the whole
|
|---|
| 24 | combination.
|
|---|
| 25 |
|
|---|
| 26 | As a special exception, the copyright holders of this library give you
|
|---|
| 27 | permission to link this library with independent modules to produce an
|
|---|
| 28 | executable, regardless of the license terms of these independent
|
|---|
| 29 | modules, and to copy and distribute the resulting executable under
|
|---|
| 30 | terms of your choice, provided that you also meet, for each linked
|
|---|
| 31 | independent module, the terms and conditions of the license of that
|
|---|
| 32 | module. An independent module is a module which is not derived from
|
|---|
| 33 | or based on this library. If you modify this library, you may extend
|
|---|
| 34 | this exception to your version of the library, but you are not
|
|---|
| 35 | obligated to do so. If you do not wish to do so, delete this
|
|---|
| 36 | exception statement from your version. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.lang;
|
|---|
| 40 |
|
|---|
| 41 | /**
|
|---|
| 42 | * Instances of class <code>Integer</code> represent primitive
|
|---|
| 43 | * <code>int</code> values.
|
|---|
| 44 | *
|
|---|
| 45 | * Additionally, this class provides various helper functions and variables
|
|---|
| 46 | * related to ints.
|
|---|
| 47 | *
|
|---|
| 48 | * @author Paul Fisher
|
|---|
| 49 | * @author John Keiser
|
|---|
| 50 | * @author Warren Levy
|
|---|
| 51 | * @since JDK 1.0
|
|---|
| 52 | */
|
|---|
| 53 | public final class Integer extends Number implements Comparable
|
|---|
| 54 | {
|
|---|
| 55 | // compatible with JDK 1.0.2+
|
|---|
| 56 | private static final long serialVersionUID = 1360826667806852920L;
|
|---|
| 57 |
|
|---|
| 58 | /**
|
|---|
| 59 | * The minimum value an <code>int</code> can represent is -2147483648.
|
|---|
| 60 | */
|
|---|
| 61 | public static final int MIN_VALUE = 0x80000000;
|
|---|
| 62 |
|
|---|
| 63 | /**
|
|---|
| 64 | * The maximum value an <code>int</code> can represent is 2147483647.
|
|---|
| 65 | */
|
|---|
| 66 | public static final int MAX_VALUE = 0x7fffffff;
|
|---|
| 67 |
|
|---|
| 68 | /**
|
|---|
| 69 | * The primitive type <code>int</code> is represented by this
|
|---|
| 70 | * <code>Class</code> object.
|
|---|
| 71 | */
|
|---|
| 72 | public static final Class TYPE = VMClassLoader.getPrimitiveClass ('I');
|
|---|
| 73 |
|
|---|
| 74 | /**
|
|---|
| 75 | * The immutable value of this Integer.
|
|---|
| 76 | */
|
|---|
| 77 | private final int value;
|
|---|
| 78 |
|
|---|
| 79 | /**
|
|---|
| 80 | * Create an <code>Integer</code> object representing the value of the
|
|---|
| 81 | * <code>int</code> argument.
|
|---|
| 82 | *
|
|---|
| 83 | * @param value the value to use
|
|---|
| 84 | */
|
|---|
| 85 | public Integer(int value)
|
|---|
| 86 | {
|
|---|
| 87 | this.value = value;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * Create an <code>Integer</code> object representing the value of the
|
|---|
| 92 | * argument after conversion to an <code>int</code>.
|
|---|
| 93 | *
|
|---|
| 94 | * @param s the string to convert.
|
|---|
| 95 | */
|
|---|
| 96 | public Integer(String s) throws NumberFormatException
|
|---|
| 97 | {
|
|---|
| 98 | value = parseInt(s, 10);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | /**
|
|---|
| 102 | * Return a hashcode representing this Object.
|
|---|
| 103 | *
|
|---|
| 104 | * <code>Integer</code>'s hash code is calculated by simply returning its
|
|---|
| 105 | * value.
|
|---|
| 106 | *
|
|---|
| 107 | * @return this Object's hash code.
|
|---|
| 108 | */
|
|---|
| 109 | public int hashCode()
|
|---|
| 110 | {
|
|---|
| 111 | return value;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | /**
|
|---|
| 115 | * If the <code>Object</code> is not <code>null</code>, is an
|
|---|
| 116 | * <code>instanceof</code> <code>Integer</code>, and represents
|
|---|
| 117 | * the same primitive <code>int</code> value return
|
|---|
| 118 | * <code>true</code>. Otherwise <code>false</code> is returned.
|
|---|
| 119 | */
|
|---|
| 120 | public boolean equals(Object obj)
|
|---|
| 121 | {
|
|---|
| 122 | return obj instanceof Integer && value == ((Integer)obj).value;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /**
|
|---|
| 126 | * Get the specified system property as an <code>Integer</code>.
|
|---|
| 127 | *
|
|---|
| 128 | * The <code>decode()</code> method will be used to interpret the value of
|
|---|
| 129 | * the property.
|
|---|
| 130 | * @param nm the name of the system property
|
|---|
| 131 | * @return the system property as an <code>Integer</code>, or
|
|---|
| 132 | * <code>null</code> if the property is not found or cannot be
|
|---|
| 133 | * decoded as an <code>Integer</code>.
|
|---|
| 134 | * @see java.lang.System#getProperty(java.lang.String)
|
|---|
| 135 | * @see #decode(int)
|
|---|
| 136 | */
|
|---|
| 137 | public static Integer getInteger(String nm)
|
|---|
| 138 | {
|
|---|
| 139 | return getInteger(nm, null);
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /**
|
|---|
| 143 | * Get the specified system property as an <code>Integer</code>, or use a
|
|---|
| 144 | * default <code>int</code> value if the property is not found or is not
|
|---|
| 145 | * decodable.
|
|---|
| 146 | *
|
|---|
| 147 | * The <code>decode()</code> method will be used to interpret the value of
|
|---|
| 148 | * the property.
|
|---|
| 149 | *
|
|---|
| 150 | * @param nm the name of the system property
|
|---|
| 151 | * @param val the default value to use if the property is not found or not
|
|---|
| 152 | * a number.
|
|---|
| 153 | * @return the system property as an <code>Integer</code>, or the default
|
|---|
| 154 | * value if the property is not found or cannot be decoded as an
|
|---|
| 155 | * <code>Integer</code>.
|
|---|
| 156 | * @see java.lang.System#getProperty(java.lang.String)
|
|---|
| 157 | * @see #decode(int)
|
|---|
| 158 | * @see #getInteger(java.lang.String,java.lang.Integer)
|
|---|
| 159 | */
|
|---|
| 160 | public static Integer getInteger(String nm, int val)
|
|---|
| 161 | {
|
|---|
| 162 | Integer result = getInteger(nm, null);
|
|---|
| 163 | return (result == null) ? new Integer(val) : result;
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Get the specified system property as an <code>Integer</code>, or use a
|
|---|
| 168 | * default <code>Integer</code> value if the property is not found or is
|
|---|
| 169 | * not decodable.
|
|---|
| 170 | *
|
|---|
| 171 | * The <code>decode()</code> method will be used to interpret the value of
|
|---|
| 172 | * the property.
|
|---|
| 173 | *
|
|---|
| 174 | * @param nm the name of the system property
|
|---|
| 175 | * @param val the default value to use if the property is not found or not
|
|---|
| 176 | * a number.
|
|---|
| 177 | * @return the system property as an <code>Integer</code>, or the default
|
|---|
| 178 | * value if the property is not found or cannot be decoded as an
|
|---|
| 179 | * <code>Integer</code>.
|
|---|
| 180 | * @see java.lang.System#getProperty(java.lang.String)
|
|---|
| 181 | * @see #decode(int)
|
|---|
| 182 | * @see #getInteger(java.lang.String,int)
|
|---|
| 183 | */
|
|---|
| 184 | public static Integer getInteger(String nm, Integer def)
|
|---|
| 185 | {
|
|---|
| 186 | if (nm == null || "".equals(nm))
|
|---|
| 187 | return def;
|
|---|
| 188 | nm = System.getProperty(nm);
|
|---|
| 189 | if (nm == null) return def;
|
|---|
| 190 | try
|
|---|
| 191 | {
|
|---|
| 192 | return decode(nm);
|
|---|
| 193 | }
|
|---|
| 194 | catch (NumberFormatException e)
|
|---|
| 195 | {
|
|---|
| 196 | return def;
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | private static String toUnsignedString(int num, int exp)
|
|---|
| 201 | {
|
|---|
| 202 | // Use an array large enough for a binary number.
|
|---|
| 203 | int radix = 1 << exp;
|
|---|
| 204 | int mask = radix - 1;
|
|---|
| 205 | char[] buffer = new char[32];
|
|---|
| 206 | int i = 32;
|
|---|
| 207 | do
|
|---|
| 208 | {
|
|---|
| 209 | buffer[--i] = Character.forDigit(num & mask, radix);
|
|---|
| 210 | num = num >>> exp;
|
|---|
| 211 | }
|
|---|
| 212 | while (num != 0);
|
|---|
| 213 |
|
|---|
| 214 | return String.valueOf(buffer, i, 32-i);
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | /**
|
|---|
| 218 | * Converts the <code>int</code> to a <code>String</code> assuming it is
|
|---|
| 219 | * unsigned in base 16.
|
|---|
| 220 | * @param i the <code>int</code> to convert to <code>String</code>
|
|---|
| 221 | * @return the <code>String</code> representation of the argument.
|
|---|
| 222 | */
|
|---|
| 223 | public static String toHexString(int i)
|
|---|
| 224 | {
|
|---|
| 225 | return toUnsignedString(i, 4);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /**
|
|---|
| 229 | * Converts the <code>int</code> to a <code>String</code> assuming it is
|
|---|
| 230 | * unsigned in base 8.
|
|---|
| 231 | * @param i the <code>int</code> to convert to <code>String</code>
|
|---|
| 232 | * @return the <code>String</code> representation of the argument.
|
|---|
| 233 | */
|
|---|
| 234 | public static String toOctalString(int i)
|
|---|
| 235 | {
|
|---|
| 236 | return toUnsignedString(i, 3);
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | /**
|
|---|
| 240 | * Converts the <code>int</code> to a <code>String</code> assuming it is
|
|---|
| 241 | * unsigned in base 2.
|
|---|
| 242 | * @param i the <code>int</code> to convert to <code>String</code>
|
|---|
| 243 | * @return the <code>String</code> representation of the argument.
|
|---|
| 244 | */
|
|---|
| 245 | public static String toBinaryString(int i)
|
|---|
| 246 | {
|
|---|
| 247 | return toUnsignedString(i, 1);
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | /**
|
|---|
| 251 | * Converts the <code>int</code> to a <code>String</code> and assumes
|
|---|
| 252 | * a radix of 10.
|
|---|
| 253 | * @param i the <code>int</code> to convert to <code>String</code>
|
|---|
| 254 | * @return the <code>String</code> representation of the argument.
|
|---|
| 255 | */
|
|---|
| 256 | public static String toString(int i)
|
|---|
| 257 | {
|
|---|
| 258 | // This is tricky: in libgcj, String.valueOf(int) is a fast native
|
|---|
| 259 | // implementation. In Classpath it just calls back to
|
|---|
| 260 | // Integer.toString(int,int).
|
|---|
| 261 | return String.valueOf (i);
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | /**
|
|---|
| 265 | * Converts the <code>Integer</code> value to a <code>String</code> and
|
|---|
| 266 | * assumes a radix of 10.
|
|---|
| 267 | * @return the <code>String</code> representation of this <code>Integer</code>.
|
|---|
| 268 | */
|
|---|
| 269 | public String toString()
|
|---|
| 270 | {
|
|---|
| 271 | return toString (value);
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | /**
|
|---|
| 275 | * Converts the <code>int</code> to a <code>String</code> using
|
|---|
| 276 | * the specified radix (base).
|
|---|
| 277 | * @param i the <code>int</code> to convert to <code>String</code>.
|
|---|
| 278 | * @param radix the radix (base) to use in the conversion.
|
|---|
| 279 | * @return the <code>String</code> representation of the argument.
|
|---|
| 280 | */
|
|---|
| 281 | public static String toString(int num, int radix)
|
|---|
| 282 | {
|
|---|
| 283 | if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
|
|---|
| 284 | radix = 10;
|
|---|
| 285 |
|
|---|
| 286 | // For negative numbers, print out the absolute value w/ a leading '-'.
|
|---|
| 287 | // Use an array large enough for a binary number.
|
|---|
| 288 | char[] buffer = new char[33];
|
|---|
| 289 | int i = 33;
|
|---|
| 290 | boolean isNeg;
|
|---|
| 291 | if (num < 0)
|
|---|
| 292 | {
|
|---|
| 293 | isNeg = true;
|
|---|
| 294 | num = -(num);
|
|---|
| 295 |
|
|---|
| 296 | // When the value is MIN_VALUE, it overflows when made positive
|
|---|
| 297 | if (num < 0)
|
|---|
| 298 | {
|
|---|
| 299 | buffer[--i] = Character.forDigit(-(num + radix) % radix, radix);
|
|---|
| 300 | num = -(num / radix);
|
|---|
| 301 | }
|
|---|
| 302 | }
|
|---|
| 303 | else
|
|---|
| 304 | isNeg = false;
|
|---|
| 305 |
|
|---|
| 306 | do
|
|---|
| 307 | {
|
|---|
| 308 | buffer[--i] = Character.forDigit(num % radix, radix);
|
|---|
| 309 | num /= radix;
|
|---|
| 310 | }
|
|---|
| 311 | while (num > 0);
|
|---|
| 312 |
|
|---|
| 313 | if (isNeg)
|
|---|
| 314 | buffer[--i] = '-';
|
|---|
| 315 |
|
|---|
| 316 | return String.valueOf(buffer, i, 33-i);
|
|---|
| 317 | }
|
|---|
| 318 |
|
|---|
| 319 | /**
|
|---|
| 320 | * Creates a new <code>Integer</code> object using the <code>String</code>,
|
|---|
| 321 | * assuming a radix of 10.
|
|---|
| 322 | * @param s the <code>String</code> to convert.
|
|---|
| 323 | * @return the new <code>Integer</code>.
|
|---|
| 324 | * @see #Integer(java.lang.String)
|
|---|
| 325 | * @see #parseInt(java.lang.String)
|
|---|
| 326 | * @exception NumberFormatException thrown if the <code>String</code>
|
|---|
| 327 | * cannot be parsed as an <code>int</code>.
|
|---|
| 328 | */
|
|---|
| 329 | public static Integer valueOf(String s) throws NumberFormatException
|
|---|
| 330 | {
|
|---|
| 331 | return new Integer(parseInt(s));
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /**
|
|---|
| 335 | * Creates a new <code>Integer</code> object using the <code>String</code>
|
|---|
| 336 | * and specified radix (base).
|
|---|
| 337 | * @param s the <code>String</code> to convert.
|
|---|
| 338 | * @param radix the radix (base) to convert with.
|
|---|
| 339 | * @return the new <code>Integer</code>.
|
|---|
| 340 | * @see #parseInt(java.lang.String,int)
|
|---|
| 341 | * @exception NumberFormatException thrown if the <code>String</code>
|
|---|
| 342 | * cannot be parsed as an <code>int</code>.
|
|---|
| 343 | */
|
|---|
| 344 | public static Integer valueOf(String s, int radix)
|
|---|
| 345 | throws NumberFormatException
|
|---|
| 346 | {
|
|---|
| 347 | return new Integer(parseInt(s, radix));
|
|---|
| 348 | }
|
|---|
| 349 |
|
|---|
| 350 | /**
|
|---|
| 351 | * Converts the specified <code>String</code> into an <code>int</code>.
|
|---|
| 352 | * This function assumes a radix of 10.
|
|---|
| 353 | *
|
|---|
| 354 | * @param s the <code>String</code> to convert
|
|---|
| 355 | * @return the <code>int</code> value of the <code>String</code>
|
|---|
| 356 | * argument.
|
|---|
| 357 | * @exception NumberFormatException thrown if the <code>String</code>
|
|---|
| 358 | * cannot be parsed as an <code>int</code>.
|
|---|
| 359 | */
|
|---|
| 360 | public static int parseInt(String s) throws NumberFormatException
|
|---|
| 361 | {
|
|---|
| 362 | return parseInt(s, 10);
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|
| 365 | /**
|
|---|
| 366 | * Converts the specified <code>String</code> into an <code>int</code>
|
|---|
| 367 | * using the specified radix (base).
|
|---|
| 368 | *
|
|---|
| 369 | * @param s the <code>String</code> to convert
|
|---|
| 370 | * @param radix the radix (base) to use in the conversion
|
|---|
| 371 | * @return the <code>String</code> argument converted to </code>int</code>.
|
|---|
| 372 | * @exception NumberFormatException thrown if the <code>String</code>
|
|---|
| 373 | * cannot be parsed as a <code>int</code>.
|
|---|
| 374 | */
|
|---|
| 375 | public static int parseInt(String str, int radix)
|
|---|
| 376 | throws NumberFormatException
|
|---|
| 377 | {
|
|---|
| 378 | final int len;
|
|---|
| 379 |
|
|---|
| 380 | if (str == null)
|
|---|
| 381 | throw new NumberFormatException ();
|
|---|
| 382 |
|
|---|
| 383 | if ((len = str.length()) == 0 ||
|
|---|
| 384 | radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
|
|---|
| 385 | throw new NumberFormatException();
|
|---|
| 386 |
|
|---|
| 387 | boolean isNeg = false;
|
|---|
| 388 | int index = 0;
|
|---|
| 389 | if (str.charAt(index) == '-')
|
|---|
| 390 | if (len > 1)
|
|---|
| 391 | {
|
|---|
| 392 | isNeg = true;
|
|---|
| 393 | index++;
|
|---|
| 394 | }
|
|---|
| 395 | else
|
|---|
| 396 | throw new NumberFormatException();
|
|---|
| 397 |
|
|---|
| 398 | return parseInt(str, index, len, isNeg, radix);
|
|---|
| 399 | }
|
|---|
| 400 |
|
|---|
| 401 | private static int parseInt(String str, int index, int len, boolean isNeg,
|
|---|
| 402 | int radix)
|
|---|
| 403 | throws NumberFormatException
|
|---|
| 404 | {
|
|---|
| 405 | int val = 0;
|
|---|
| 406 | int digval;
|
|---|
| 407 |
|
|---|
| 408 | int max = MAX_VALUE / radix;
|
|---|
| 409 | // We can't directly write `max = (MAX_VALUE + 1) / radix'.
|
|---|
| 410 | // So instead we fake it.
|
|---|
| 411 | if (isNeg && MAX_VALUE % radix == radix - 1)
|
|---|
| 412 | ++max;
|
|---|
| 413 |
|
|---|
| 414 | for ( ; index < len; index++)
|
|---|
| 415 | {
|
|---|
| 416 | if (val < 0 || val > max)
|
|---|
| 417 | throw new NumberFormatException();
|
|---|
| 418 |
|
|---|
| 419 | if ((digval = Character.digit(str.charAt(index), radix)) < 0)
|
|---|
| 420 | throw new NumberFormatException();
|
|---|
| 421 |
|
|---|
| 422 | // Throw an exception for overflow if result is negative.
|
|---|
| 423 | // However, we special-case the most negative value.
|
|---|
| 424 | val = val * radix + digval;
|
|---|
| 425 | if (val < 0 && (! isNeg || val != MIN_VALUE))
|
|---|
| 426 | throw new NumberFormatException();
|
|---|
| 427 | }
|
|---|
| 428 |
|
|---|
| 429 | return isNeg ? -(val) : val;
|
|---|
| 430 | }
|
|---|
| 431 |
|
|---|
| 432 | /**
|
|---|
| 433 | * Convert the specified <code>String</code> into an <code>Integer</code>.
|
|---|
| 434 | * The <code>String</code> may represent decimal, hexadecimal, or
|
|---|
| 435 | * octal numbers.
|
|---|
| 436 | *
|
|---|
| 437 | * The <code>String</code> argument is interpreted based on the leading
|
|---|
| 438 | * characters. Depending on what the String begins with (after an optional
|
|---|
| 439 | * minus sign), the base will be interpreted differently:
|
|---|
| 440 | *
|
|---|
| 441 | * <table border=1>
|
|---|
| 442 | * <tr><th>Leading<br>Characters</th><th>Base</th></tr>
|
|---|
| 443 | * <tr><td>#</td><td>16</td></tr>
|
|---|
| 444 | * <tr><td>0x</td><td>16</td></tr>
|
|---|
| 445 | * <tr><td>0X</td><td>16</td></tr>
|
|---|
| 446 | * <tr><td>0</td><td>8</td></tr>
|
|---|
| 447 | * <tr><td>Anything<br>Else</td><td>10</td></tr>
|
|---|
| 448 | * </table>
|
|---|
| 449 | *
|
|---|
| 450 | * If the String starts with a minus sign the result is negated.
|
|---|
| 451 | *
|
|---|
| 452 | * @param str the <code>String</code> to interpret.
|
|---|
| 453 | * @return the value of the String as an <code>Integer</code>.
|
|---|
| 454 | * @exception NumberFormatException thrown if the <code>String</code>
|
|---|
| 455 | * cannot be parsed as an <code>int</code>.
|
|---|
| 456 | */
|
|---|
| 457 | public static Integer decode(String str) throws NumberFormatException
|
|---|
| 458 | {
|
|---|
| 459 | boolean isNeg = false;
|
|---|
| 460 | int index = 0;
|
|---|
| 461 | int radix = 10;
|
|---|
| 462 | final int len;
|
|---|
| 463 |
|
|---|
| 464 | if ((len = str.length()) == 0)
|
|---|
| 465 | throw new NumberFormatException("empty string");
|
|---|
| 466 |
|
|---|
| 467 | if (str.charAt(index) == '-')
|
|---|
| 468 | {
|
|---|
| 469 | // The minus sign should be followed by at least one more char
|
|---|
| 470 | if (len > 1)
|
|---|
| 471 | {
|
|---|
| 472 | isNeg = true;
|
|---|
| 473 | index++;
|
|---|
| 474 | }
|
|---|
| 475 | else
|
|---|
| 476 | throw new NumberFormatException();
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | if (str.charAt(index) == '#')
|
|---|
| 480 | {
|
|---|
| 481 | radix = 16;
|
|---|
| 482 | index++;
|
|---|
| 483 | }
|
|---|
| 484 | else if (str.charAt(index) == '0')
|
|---|
| 485 | {
|
|---|
| 486 | index++;
|
|---|
| 487 |
|
|---|
| 488 | // Check if str is just "0" or "-0"
|
|---|
| 489 | if (len == index)
|
|---|
| 490 | return new Integer(0);
|
|---|
| 491 |
|
|---|
| 492 | if (str.charAt(index) == 'x' || str.charAt(index) == 'X')
|
|---|
| 493 | {
|
|---|
| 494 | radix = 16;
|
|---|
| 495 | index++;
|
|---|
| 496 | }
|
|---|
| 497 | else
|
|---|
| 498 | radix = 8;
|
|---|
| 499 | }
|
|---|
| 500 |
|
|---|
| 501 | if (index >= len)
|
|---|
| 502 | throw new NumberFormatException("empty value");
|
|---|
| 503 |
|
|---|
| 504 | return new Integer(parseInt(str, index, len, isNeg, radix));
|
|---|
| 505 | }
|
|---|
| 506 |
|
|---|
| 507 | /** Return the value of this <code>Integer</code> as a <code>byte</code>.
|
|---|
| 508 | ** @return the value of this <code>Integer</code> as a <code>byte</code>.
|
|---|
| 509 | **/
|
|---|
| 510 | public byte byteValue()
|
|---|
| 511 | {
|
|---|
| 512 | return (byte) value;
|
|---|
| 513 | }
|
|---|
| 514 |
|
|---|
| 515 | /** Return the value of this <code>Integer</code> as a <code>short</code>.
|
|---|
| 516 | ** @return the value of this <code>Integer</code> as a <code>short</code>.
|
|---|
| 517 | **/
|
|---|
| 518 | public short shortValue()
|
|---|
| 519 | {
|
|---|
| 520 | return (short) value;
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | /** Return the value of this <code>Integer</code> as an <code>int</code>.
|
|---|
| 524 | ** @return the value of this <code>Integer</code> as an <code>int</code>.
|
|---|
| 525 | **/
|
|---|
| 526 | public int intValue()
|
|---|
| 527 | {
|
|---|
| 528 | return value;
|
|---|
| 529 | }
|
|---|
| 530 |
|
|---|
| 531 | /** Return the value of this <code>Integer</code> as a <code>long</code>.
|
|---|
| 532 | ** @return the value of this <code>Integer</code> as a <code>long</code>.
|
|---|
| 533 | **/
|
|---|
| 534 | public long longValue()
|
|---|
| 535 | {
|
|---|
| 536 | return value;
|
|---|
| 537 | }
|
|---|
| 538 |
|
|---|
| 539 | /** Return the value of this <code>Integer</code> as a <code>float</code>.
|
|---|
| 540 | ** @return the value of this <code>Integer</code> as a <code>float</code>.
|
|---|
| 541 | **/
|
|---|
| 542 | public float floatValue()
|
|---|
| 543 | {
|
|---|
| 544 | return value;
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | /** Return the value of this <code>Integer</code> as a <code>double</code>.
|
|---|
| 548 | ** @return the value of this <code>Integer</code> as a <code>double</code>.
|
|---|
| 549 | **/
|
|---|
| 550 | public double doubleValue()
|
|---|
| 551 | {
|
|---|
| 552 | return value;
|
|---|
| 553 | }
|
|---|
| 554 |
|
|---|
| 555 | /**
|
|---|
| 556 | * Compare two Integers numerically by comparing their
|
|---|
| 557 | * <code>int</code> values.
|
|---|
| 558 | * @return a positive value if this <code>Integer</code> is greater
|
|---|
| 559 | * in value than the argument <code>Integer</code>; a negative value
|
|---|
| 560 | * if this <code>Integer</code> is smaller in value than the argument
|
|---|
| 561 | * <code>Integer</code>; and <code>0</code>, zero, if this
|
|---|
| 562 | * <code>Integer</code> is equal in value to the argument
|
|---|
| 563 | * <code>Integer</code>.
|
|---|
| 564 | *
|
|---|
| 565 | * @since 1.2
|
|---|
| 566 | */
|
|---|
| 567 | public int compareTo(Integer i)
|
|---|
| 568 | {
|
|---|
| 569 | if (this.value == i.value)
|
|---|
| 570 | return 0;
|
|---|
| 571 |
|
|---|
| 572 | // Returns just -1 or 1 on inequality; doing math might overflow.
|
|---|
| 573 | if (this.value > i.value)
|
|---|
| 574 | return 1;
|
|---|
| 575 |
|
|---|
| 576 | return -1;
|
|---|
| 577 | }
|
|---|
| 578 |
|
|---|
| 579 | /**
|
|---|
| 580 | * Behaves like <code>compareTo(java.lang.Integer)</code> unless the Object
|
|---|
| 581 | * is not a <code>Integer</code>. Then it throws a
|
|---|
| 582 | * <code>ClassCastException</code>.
|
|---|
| 583 | * @exception ClassCastException if the argument is not a
|
|---|
| 584 | * <code>Integer</code>.
|
|---|
| 585 | *
|
|---|
| 586 | * @since 1.2
|
|---|
| 587 | */
|
|---|
| 588 | public int compareTo(Object o)
|
|---|
| 589 | {
|
|---|
| 590 | return compareTo((Integer)o);
|
|---|
| 591 | }
|
|---|
| 592 | }
|
|---|