1 /* Integer.java -- object wrapper for int
2 Copyright (C) 1998, 1999, 2001, 2002, 2004, 2005
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
43 * Instances of class <code>Integer</code> represent primitive
44 * <code>int</code> values.
46 * Additionally, this class provides various helper functions and variables
52 * @author Eric Blake (ebb9@email.byu.edu)
53 * @author Tom Tromey (tromey@redhat.com)
54 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
57 * @status updated to 1.5
59 public final class Integer extends Number implements Comparable<Integer>
62 * Compatible with JDK 1.0.2+.
64 private static final long serialVersionUID = 1360826667806852920L;
67 * The minimum value an <code>int</code> can represent is -2147483648 (or
70 public static final int MIN_VALUE = 0x80000000;
73 * The maximum value an <code>int</code> can represent is 2147483647 (or
74 * 2<sup>31</sup> - 1).
76 public static final int MAX_VALUE = 0x7fffffff;
79 * The primitive type <code>int</code> is represented by this
80 * <code>Class</code> object.
83 public static final Class<Integer> TYPE = (Class<Integer>) VMClassLoader.getPrimitiveClass('I');
86 * The number of bits needed to represent an <code>int</code>.
89 public static final int SIZE = 32;
91 // This caches some Integer values, and is used by boxing
92 // conversions via valueOf(). We must cache at least -128..127;
93 // these constants control how much we actually cache.
94 private static final int MIN_CACHE = -128;
95 private static final int MAX_CACHE = 127;
96 private static final Integer[] intCache = new Integer[MAX_CACHE - MIN_CACHE + 1];
99 for (int i=MIN_CACHE; i <= MAX_CACHE; i++)
100 intCache[i - MIN_CACHE] = new Integer(i);
104 * The immutable value of this Integer.
106 * @serial the wrapped int
108 private final int value;
111 * Create an <code>Integer</code> object representing the value of the
112 * <code>int</code> argument.
114 * @param value the value to use
116 public Integer(int value)
122 * Create an <code>Integer</code> object representing the value of the
123 * argument after conversion to an <code>int</code>.
125 * @param s the string to convert
126 * @throws NumberFormatException if the String does not contain an int
127 * @see #valueOf(String)
129 public Integer(String s)
131 value = parseInt(s, 10, false);
135 * Return the size of a string large enough to hold the given number
137 * @param num the number we want the string length for (must be positive)
138 * @param radix the radix (base) that will be used for the string
139 * @return a size sufficient for a string of num
141 private static int stringSize(int num, int radix) {
174 * Converts the <code>int</code> to a <code>String</code> using
175 * the specified radix (base). If the radix exceeds
176 * <code>Character.MIN_RADIX</code> or <code>Character.MAX_RADIX</code>, 10
177 * is used instead. If the result is negative, the leading character is
178 * '-' ('\\u002D'). The remaining characters come from
179 * <code>Character.forDigit(digit, radix)</code> ('0'-'9','a'-'z').
181 * @param num the <code>int</code> to convert to <code>String</code>
182 * @param radix the radix (base) to use in the conversion
183 * @return the <code>String</code> representation of the argument
185 public static String toString(int num, int radix)
187 if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
190 // Is the value negative?
191 boolean isNeg = num < 0;
193 // Is the string a single character?
194 if (!isNeg && num < radix)
195 return new String(digits, num, 1, true);
197 // Compute string size and allocate buffer
198 // account for a leading '-' if the value is negative
206 // When the value is MIN_VALUE, it overflows when made positive
209 i = size = stringSize(MAX_VALUE, radix) + 2;
210 buffer = new char[size];
211 buffer[--i] = digits[(int) (-(num + radix) % radix)];
212 num = -(num / radix);
216 i = size = stringSize(num, radix) + 1;
217 buffer = new char[size];
222 i = size = stringSize(num, radix);
223 buffer = new char[size];
228 buffer[--i] = digits[num % radix];
236 // Package constructor avoids an array copy.
237 return new String(buffer, i, size - i, true);
241 * Converts the <code>int</code> to a <code>String</code> assuming it is
242 * unsigned in base 16.
244 * @param i the <code>int</code> to convert to <code>String</code>
245 * @return the <code>String</code> representation of the argument
247 public static String toHexString(int i)
249 return toUnsignedString(i, 4);
253 * Converts the <code>int</code> to a <code>String</code> assuming it is
254 * unsigned in base 8.
256 * @param i the <code>int</code> to convert to <code>String</code>
257 * @return the <code>String</code> representation of the argument
259 public static String toOctalString(int i)
261 return toUnsignedString(i, 3);
265 * Converts the <code>int</code> to a <code>String</code> assuming it is
266 * unsigned in base 2.
268 * @param i the <code>int</code> to convert to <code>String</code>
269 * @return the <code>String</code> representation of the argument
271 public static String toBinaryString(int i)
273 return toUnsignedString(i, 1);
277 * Converts the <code>int</code> to a <code>String</code> and assumes
280 * @param i the <code>int</code> to convert to <code>String</code>
281 * @return the <code>String</code> representation of the argument
282 * @see #toString(int, int)
284 public static String toString(int i)
286 // This is tricky: in libgcj, String.valueOf(int) is a fast native
287 // implementation. In Classpath it just calls back to
288 // Integer.toString(int, int).
289 return String.valueOf(i);
293 * Converts the specified <code>String</code> into an <code>int</code>
294 * using the specified radix (base). The string must not be <code>null</code>
295 * or empty. It may begin with an optional '-', which will negate the answer,
296 * provided that there are also valid digits. Each digit is parsed as if by
297 * <code>Character.digit(d, radix)</code>, and must be in the range
298 * <code>0</code> to <code>radix - 1</code>. Finally, the result must be
299 * within <code>MIN_VALUE</code> to <code>MAX_VALUE</code>, inclusive.
300 * Unlike Double.parseDouble, you may not have a leading '+'.
302 * @param str the <code>String</code> to convert
303 * @param radix the radix (base) to use in the conversion
304 * @return the <code>String</code> argument converted to <code>int</code>
305 * @throws NumberFormatException if <code>s</code> cannot be parsed as an
308 public static int parseInt(String str, int radix)
310 return parseInt(str, radix, false);
314 * Converts the specified <code>String</code> into an <code>int</code>.
315 * This function assumes a radix of 10.
317 * @param s the <code>String</code> to convert
318 * @return the <code>int</code> value of <code>s</code>
319 * @throws NumberFormatException if <code>s</code> cannot be parsed as an
321 * @see #parseInt(String, int)
323 public static int parseInt(String s)
325 return parseInt(s, 10, false);
329 * Creates a new <code>Integer</code> object using the <code>String</code>
330 * and specified radix (base).
332 * @param s the <code>String</code> to convert
333 * @param radix the radix (base) to convert with
334 * @return the new <code>Integer</code>
335 * @throws NumberFormatException if <code>s</code> cannot be parsed as an
337 * @see #parseInt(String, int)
339 public static Integer valueOf(String s, int radix)
341 return valueOf(parseInt(s, radix, false));
345 * Creates a new <code>Integer</code> object using the <code>String</code>,
346 * assuming a radix of 10.
348 * @param s the <code>String</code> to convert
349 * @return the new <code>Integer</code>
350 * @throws NumberFormatException if <code>s</code> cannot be parsed as an
352 * @see #Integer(String)
353 * @see #parseInt(String)
355 public static Integer valueOf(String s)
357 return valueOf(parseInt(s, 10, false));
361 * Returns an <code>Integer</code> object wrapping the value.
362 * In contrast to the <code>Integer</code> constructor, this method
363 * will cache some values. It is used by boxing conversion.
365 * @param val the value to wrap
366 * @return the <code>Integer</code>
368 public static Integer valueOf(int val)
370 if (val < MIN_CACHE || val > MAX_CACHE)
371 return new Integer(val);
373 return intCache[val - MIN_CACHE];
377 * Return the value of this <code>Integer</code> as a <code>byte</code>.
379 * @return the byte value
381 public byte byteValue()
387 * Return the value of this <code>Integer</code> as a <code>short</code>.
389 * @return the short value
391 public short shortValue()
393 return (short) value;
397 * Return the value of this <code>Integer</code>.
398 * @return the int value
400 public int intValue()
406 * Return the value of this <code>Integer</code> as a <code>long</code>.
408 * @return the long value
410 public long longValue()
416 * Return the value of this <code>Integer</code> as a <code>float</code>.
418 * @return the float value
420 public float floatValue()
426 * Return the value of this <code>Integer</code> as a <code>double</code>.
428 * @return the double value
430 public double doubleValue()
436 * Converts the <code>Integer</code> value to a <code>String</code> and
437 * assumes a radix of 10.
439 * @return the <code>String</code> representation
441 public String toString()
443 return String.valueOf(value);
447 * Return a hashcode representing this Object. <code>Integer</code>'s hash
448 * code is simply its value.
450 * @return this Object's hash code
452 public int hashCode()
458 * Returns <code>true</code> if <code>obj</code> is an instance of
459 * <code>Integer</code> and represents the same int value.
461 * @param obj the object to compare
462 * @return whether these Objects are semantically equal
464 public boolean equals(Object obj)
466 return obj instanceof Integer && value == ((Integer) obj).value;
470 * Get the specified system property as an <code>Integer</code>. The
471 * <code>decode()</code> method will be used to interpret the value of
474 * @param nm the name of the system property
475 * @return the system property as an <code>Integer</code>, or null if the
476 * property is not found or cannot be decoded
477 * @throws SecurityException if accessing the system property is forbidden
478 * @see System#getProperty(String)
479 * @see #decode(String)
481 public static Integer getInteger(String nm)
483 return getInteger(nm, null);
487 * Get the specified system property as an <code>Integer</code>, or use a
488 * default <code>int</code> value if the property is not found or is not
489 * decodable. The <code>decode()</code> method will be used to interpret
490 * the value of the property.
492 * @param nm the name of the system property
493 * @param val the default value
494 * @return the value of the system property, or the default
495 * @throws SecurityException if accessing the system property is forbidden
496 * @see System#getProperty(String)
497 * @see #decode(String)
499 public static Integer getInteger(String nm, int val)
501 Integer result = getInteger(nm, null);
502 return result == null ? valueOf(val) : result;
506 * Get the specified system property as an <code>Integer</code>, or use a
507 * default <code>Integer</code> value if the property is not found or is
508 * not decodable. The <code>decode()</code> method will be used to
509 * interpret the value of the property.
511 * @param nm the name of the system property
512 * @param def the default value
513 * @return the value of the system property, or the default
514 * @throws SecurityException if accessing the system property is forbidden
515 * @see System#getProperty(String)
516 * @see #decode(String)
518 public static Integer getInteger(String nm, Integer def)
520 if (nm == null || "".equals(nm))
522 nm = System.getProperty(nm);
529 catch (NumberFormatException e)
536 * Convert the specified <code>String</code> into an <code>Integer</code>.
537 * The <code>String</code> may represent decimal, hexadecimal, or
540 * <p>The extended BNF grammar is as follows:<br>
542 * <em>DecodableString</em>:
543 * ( [ <code>-</code> ] <em>DecimalNumber</em> )
544 * | ( [ <code>-</code> ] ( <code>0x</code> | <code>0X</code>
545 * | <code>#</code> ) <em>HexDigit</em> { <em>HexDigit</em> } )
546 * | ( [ <code>-</code> ] <code>0</code> { <em>OctalDigit</em> } )
547 * <em>DecimalNumber</em>:
548 * <em>DecimalDigit except '0'</em> { <em>DecimalDigit</em> }
549 * <em>DecimalDigit</em>:
550 * <em>Character.digit(d, 10) has value 0 to 9</em>
551 * <em>OctalDigit</em>:
552 * <em>Character.digit(d, 8) has value 0 to 7</em>
553 * <em>DecimalDigit</em>:
554 * <em>Character.digit(d, 16) has value 0 to 15</em>
556 * Finally, the value must be in the range <code>MIN_VALUE</code> to
557 * <code>MAX_VALUE</code>, or an exception is thrown.
559 * @param str the <code>String</code> to interpret
560 * @return the value of the String as an <code>Integer</code>
561 * @throws NumberFormatException if <code>s</code> cannot be parsed as a
563 * @throws NullPointerException if <code>s</code> is null
566 public static Integer decode(String str)
568 return valueOf(parseInt(str, 10, true));
572 * Compare two Integers numerically by comparing their <code>int</code>
573 * values. The result is positive if the first is greater, negative if the
574 * second is greater, and 0 if the two are equal.
576 * @param i the Integer to compare
577 * @return the comparison
580 public int compareTo(Integer i)
582 if (value == i.value)
584 // Returns just -1 or 1 on inequality; doing math might overflow.
585 return value > i.value ? 1 : -1;
589 * Compares two unboxed int values.
590 * The result is positive if the first is greater, negative if the second
591 * is greater, and 0 if the two are equal.
593 * @param x First value to compare.
594 * @param y Second value to compare.
596 * @return positive int if the first value is greater, negative if the second
597 * is greater, and 0 if the two are equal.
600 public static int compare(int x, int y)
602 return Integer.valueOf(x).compareTo(Integer.valueOf(y));
606 * Return the number of bits set in x.
607 * @param x value to examine
610 public static int bitCount(int x)
612 // Successively collapse alternating bit groups into a sum.
613 x = ((x >> 1) & 0x55555555) + (x & 0x55555555);
614 x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
615 x = ((x >> 4) & 0x0f0f0f0f) + (x & 0x0f0f0f0f);
616 x = ((x >> 8) & 0x00ff00ff) + (x & 0x00ff00ff);
617 return ((x >> 16) & 0x0000ffff) + (x & 0x0000ffff);
621 * Rotate x to the left by distance bits.
622 * @param x the value to rotate
623 * @param distance the number of bits by which to rotate
626 public static int rotateLeft(int x, int distance)
628 // This trick works because the shift operators implicitly mask
630 return (x << distance) | (x >>> - distance);
634 * Rotate x to the right by distance bits.
635 * @param x the value to rotate
636 * @param distance the number of bits by which to rotate
639 public static int rotateRight(int x, int distance)
641 // This trick works because the shift operators implicitly mask
643 return (x << - distance) | (x >>> distance);
647 * Find the highest set bit in value, and return a new value
648 * with only that bit set.
649 * @param value the value to examine
652 public static int highestOneBit(int value)
654 value |= value >>> 1;
655 value |= value >>> 2;
656 value |= value >>> 4;
657 value |= value >>> 8;
658 value |= value >>> 16;
659 return value ^ (value >>> 1);
663 * Return the number of leading zeros in value.
664 * @param value the value to examine
667 public static int numberOfLeadingZeros(int value)
669 value |= value >>> 1;
670 value |= value >>> 2;
671 value |= value >>> 4;
672 value |= value >>> 8;
673 value |= value >>> 16;
674 return bitCount(~value);
678 * Find the lowest set bit in value, and return a new value
679 * with only that bit set.
680 * @param value the value to examine
683 public static int lowestOneBit(int value)
685 // Classic assembly trick.
686 return value & - value;
690 * Find the number of trailing zeros in value.
691 * @param value the value to examine
694 public static int numberOfTrailingZeros(int value)
696 return bitCount((value & -value) - 1);
700 * Return 1 if x is positive, -1 if it is negative, and 0 if it is
702 * @param x the value to examine
705 public static int signum(int x)
707 return (x >> 31) | (-x >>> 31);
709 // The LHS propagates the sign bit through every bit in the word;
710 // if X < 0, every bit is set to 1, else 0. if X > 0, the RHS
711 // negates x and shifts the resulting 1 in the sign bit to the
712 // LSB, leaving every other bit 0.
714 // Hacker's Delight, Section 2-7
718 * Reverse the bytes in val.
721 public static int reverseBytes(int val)
723 return ( ((val >> 24) & 0xff)
724 | ((val >> 8) & 0xff00)
725 | ((val << 8) & 0xff0000)
726 | ((val << 24) & 0xff000000));
730 * Reverse the bits in val.
733 public static int reverse(int val)
735 // Successively swap alternating bit groups.
736 val = ((val >> 1) & 0x55555555) + ((val << 1) & ~0x55555555);
737 val = ((val >> 2) & 0x33333333) + ((val << 2) & ~0x33333333);
738 val = ((val >> 4) & 0x0f0f0f0f) + ((val << 4) & ~0x0f0f0f0f);
739 val = ((val >> 8) & 0x00ff00ff) + ((val << 8) & ~0x00ff00ff);
740 return ((val >> 16) & 0x0000ffff) + ((val << 16) & ~0x0000ffff);
744 * Helper for converting unsigned numbers to String.
746 * @param num the number
747 * @param exp log2(digit) (ie. 1, 3, or 4 for binary, oct, hex)
749 // Package visible for use by Long.
750 static String toUnsignedString(int num, int exp)
752 // Compute string length
754 int copy = num >>> exp;
760 // Quick path for single character strings
762 return new String(digits, num, 1, true);
764 // Encode into buffer
765 int mask = (1 << exp) - 1;
766 char[] buffer = new char[size];
770 buffer[--i] = digits[num & mask];
775 // Package constructor avoids an array copy.
776 return new String(buffer, i, size - i, true);
780 * Helper for parsing ints, used by Integer, Short, and Byte.
782 * @param str the string to parse
783 * @param radix the radix to use, must be 10 if decode is true
784 * @param decode if called from decode
785 * @return the parsed int value
786 * @throws NumberFormatException if there is an error
787 * @throws NullPointerException if decode is true and str if null
788 * @see #parseInt(String, int)
789 * @see #decode(String)
790 * @see Byte#parseByte(String, int)
791 * @see Short#parseShort(String, int)
793 static int parseInt(String str, int radix, boolean decode)
795 if (! decode && str == null)
796 throw new NumberFormatException();
798 int len = str.length();
799 boolean isNeg = false;
801 throw new NumberFormatException("string length is null");
802 int ch = str.charAt(index);
806 throw new NumberFormatException("pure '-'");
808 ch = str.charAt(++index);
813 throw new NumberFormatException("pure '+'");
814 ch = str.charAt(++index);
822 if ((str.charAt(index) & ~('x' ^ 'X')) == 'X')
837 throw new NumberFormatException("non terminated number: " + str);
839 int max = MAX_VALUE / radix;
840 // We can't directly write `max = (MAX_VALUE + 1) / radix'.
841 // So instead we fake it.
842 if (isNeg && MAX_VALUE % radix == radix - 1)
848 if (val < 0 || val > max)
849 throw new NumberFormatException("number overflow (pos=" + index + ") : " + str);
851 ch = Character.digit(str.charAt(index++), radix);
852 val = val * radix + ch;
853 if (ch < 0 || (val < 0 && (! isNeg || val != MIN_VALUE)))
854 throw new NumberFormatException("invalid character at position " + index + " in " + str);
856 return isNeg ? -val : val;