001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package java.lang;
028:
029: /**
030: * The Integer class wraps a value of the primitive type <code>int</code>
031: * in an object. An object of type <code>Integer</code> contains a
032: * single field whose type is <code>int</code>.
033: * <p>
034: * In addition, this class provides several methods for converting
035: * an <code>int</code> to a <code>String</code> and a
036: * <code>String</code> to an <code>int</code>, as well as other
037: * constants and methods useful when dealing with an
038: * <code>int</code>.
039: *
040: * @version 12/17/01 (CLDC 1.1)
041: * @since JDK1.0, CLDC 1.0
042: */
043: public final class Integer {
044:
045: /**
046: * The smallest value of type <code>int</code>. The constant
047: * value of this field is <tt>-2147483648</tt>.
048: */
049: public static final int MIN_VALUE = 0x80000000;
050:
051: /**
052: * The largest value of type <code>int</code>. The constant
053: * value of this field is <tt>2147483647</tt>.
054: */
055: public static final int MAX_VALUE = 0x7fffffff;
056:
057: /**
058: * All possible chars for representing a number as a String
059: */
060: final static char[] digits = { '0', '1', '2', '3', '4', '5', '6',
061: '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
062: 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u',
063: 'v', 'w', 'x', 'y', 'z' };
064:
065: /**
066: * Creates a string representation of the first argument in the
067: * radix specified by the second argument.
068: * <p>
069: * If the radix is smaller than <code>Character.MIN_RADIX</code> or
070: * larger than <code>Character.MAX_RADIX</code>, then the radix
071: * <code>10</code> is used instead.
072: * <p>
073: * If the first argument is negative, the first element of the
074: * result is the ASCII minus character <code>'-'</code>
075: * (<tt>'\u002d'</tt>). If the first
076: * argument is not negative, no sign character appears in the result.
077: * <p>
078: * The remaining characters of the result represent the magnitude of
079: * the first argument. If the magnitude is zero, it is represented by
080: * a single zero character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise,
081: * the first character of the representation of the magnitude will
082: * not be the zero character.
083: * The following ASCII characters are used as digits:
084: * <blockquote><pre>
085: * 0123456789abcdefghijklmnopqrstuvwxyz
086: * </pre></blockquote>
087: * These are <tt>'\u0030'</tt> through <tt>'\u0039'</tt> and
088: * <tt>'\u0061'</tt> through <tt>'\u007a'</tt>. If the
089: * <tt>radix</tt> is <var>N</var>, then the first <var>N</var> of these
090: * characters are used as radix-<var>N</var> digits in the order shown.
091: * Thus, the digits for hexadecimal (radix 16) are
092: * <blockquote><pre>
093: * <tt>0123456789abcdef</tt>.
094: * </pre></blockquote>
095: *
096: * @param i an integer.
097: * @param radix the radix.
098: * @return a string representation of the argument in the specified radix.
099: * @see java.lang.Character#MAX_RADIX
100: * @see java.lang.Character#MIN_RADIX
101: */
102: public static String toString(int i, int radix) {
103:
104: if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
105: radix = 10;
106:
107: char buf[] = new char[33];
108: boolean negative = (i < 0);
109: int charPos = 32;
110:
111: if (!negative) {
112: i = -i;
113: }
114:
115: while (i <= -radix) {
116: buf[charPos--] = digits[-(i % radix)];
117: i = i / radix;
118: }
119: buf[charPos] = digits[-i];
120:
121: if (negative) {
122: buf[--charPos] = '-';
123: }
124:
125: return new String(buf, charPos, (33 - charPos));
126: }
127:
128: /**
129: * Creates a string representation of the integer argument as an
130: * unsigned integer in base 16.
131: * <p>
132: * The unsigned integer value is the argument plus 2<sup>32</sup> if
133: * the argument is negative; otherwise, it is equal to the argument.
134: * This value is converted to a string of ASCII digits in hexadecimal
135: * (base 16) with no extra leading <code>0</code>s. If the
136: * unsigned magnitude is zero, it is represented by a single zero
137: * character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, the first
138: * character of the representation of the unsigned magnitude will
139: * not be the zero character. The following characters are used as
140: * hexadecimal digits:
141: * <blockquote><pre>
142: * 0123456789abcdef
143: * </pre></blockquote>
144: * These are the characters <tt>'\u0030'</tt> through <tt>'\u0039'</tt>
145: * and <tt>'u\0039'</tt> through <tt>'\u0066'</tt>.
146: *
147: * @param i an integer.
148: * @return the string representation of the unsigned integer value
149: * represented by the argument in hexadecimal (base 16).
150: * @since JDK1.0.2
151: */
152: public static String toHexString(int i) {
153: return toUnsignedString(i, 4);
154: }
155:
156: /**
157: * Creates a string representation of the integer argument as an
158: * unsigned integer in base 8.
159: * <p>
160: * The unsigned integer value is the argument plus 2<sup>32</sup> if
161: * the argument is negative; otherwise, it is equal to the argument.
162: * This value is converted to a string of ASCII digits in octal
163: * (base 8) with no extra leading <code>0</code>s.
164: * <p>
165: * If the unsigned magnitude is zero, it is represented by a single
166: * zero character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, the
167: * first character of the representation of the unsigned magnitude will
168: * not be the zero character. The octal digits are:
169: * <blockquote><pre>
170: * 01234567
171: * </pre></blockquote>
172: * These are the characters <tt>'\u0030'</tt> through <tt>'\u0037'</tt>.
173: *
174: * @param i an integer
175: * @return the string representation of the unsigned integer value
176: * represented by the argument in octal (base 8).
177: * @since JDK1.0.2
178: */
179: public static String toOctalString(int i) {
180: return toUnsignedString(i, 3);
181: }
182:
183: /**
184: * Creates a string representation of the integer argument as an
185: * unsigned integer in base 2.
186: * <p>
187: * The unsigned integer value is the argument plus 2<sup>32</sup>if
188: * the argument is negative; otherwise it is equal to the argument.
189: * This value is converted to a string of ASCII digits in binary
190: * (base 2) with no extra leading <code>0</code>s.
191: *
192: * If the unsigned magnitude is zero, it is represented by a single
193: * zero character <tt>'0'</tt> (<tt>'\u0030'</tt>); otherwise, the
194: * first character of the representation of the unsigned magnitude
195: * will not be the zero character. The characters <tt>'0'</tt>
196: * (<tt>'\u0030'</tt>) and <tt>'1'</tt> (<tt>'\u0031'</tt>) are used
197: * as binary digits.
198: *
199: * @param i an integer.
200: * @return the string representation of the unsigned integer value
201: * represented by the argument in binary (base 2).
202: * @since JDK1.0.2
203: */
204: public static String toBinaryString(int i) {
205: return toUnsignedString(i, 1);
206: }
207:
208: /**
209: * Convert the integer to an unsigned number.
210: */
211: private static String toUnsignedString(int i, int shift) {
212: char[] buf = new char[32];
213: int charPos = 32;
214: int radix = 1 << shift;
215: int mask = radix - 1;
216: do {
217: buf[--charPos] = digits[i & mask];
218: i >>>= shift;
219: } while (i != 0);
220:
221: return new String(buf, charPos, (32 - charPos));
222: }
223:
224: /**
225: * Returns a new String object representing the specified integer. The
226: * argument is converted to signed decimal representation and returned
227: * as a string, exactly as if the argument and radix <tt>10</tt> were
228: * given as arguments to the {@link #toString(int, int)} method.
229: *
230: * @param i an integer to be converted.
231: * @return a string representation of the argument in base 10.
232: */
233: public static String toString(int i) {
234: return toString(i, 10);
235: }
236:
237: /**
238: * Parses the string argument as a signed integer in the radix
239: * specified by the second argument. The characters in the string
240: * must all be digits of the specified radix (as determined by
241: * whether {@link java.lang.Character#digit(char, int)} returns a
242: * nonnegative value), except that the first character may be an
243: * ASCII minus sign <code>'-'</code> (<code>'\u002d'</code>) to
244: * indicate a negative value. The resulting integer value is returned.
245: * <p>
246: * An exception of type <tt>NumberFormatException</tt> is thrown if any
247: * of the following situations occurs:
248: * <ul>
249: * <li>The first argument is <tt>null</tt> or is a string of length zero.
250: * <li>The radix is either smaller than
251: * {@link java.lang.Character#MIN_RADIX} or
252: * larger than {@link java.lang.Character#MAX_RADIX}.
253: * <li>Any character of the string is not a digit of the specified radix,
254: * except that the first character may be a minus sign <tt>'-'</tt>
255: * (<tt>'\u002d'</tt>) provided that the string is longer than length 1.
256: * <li>The integer value represented by the string is not a value of type
257: * <tt>int</tt>.
258: * </ul><p>
259: * Examples:
260: * <blockquote><pre>
261: * parseInt("0", 10) returns 0
262: * parseInt("473", 10) returns 473
263: * parseInt("-0", 10) returns 0
264: * parseInt("-FF", 16) returns -255
265: * parseInt("1100110", 2) returns 102
266: * parseInt("2147483647", 10) returns 2147483647
267: * parseInt("-2147483648", 10) returns -2147483648
268: * parseInt("2147483648", 10) throws a NumberFormatException
269: * parseInt("99", 8) throws a NumberFormatException
270: * parseInt("Kona", 10) throws a NumberFormatException
271: * parseInt("Kona", 27) returns 411787
272: * </pre></blockquote>
273: *
274: * @param s the <code>String</code> containing the integer.
275: * @param radix the radix to be used.
276: * @return the integer represented by the string argument in the
277: * specified radix.
278: * @exception NumberFormatException if the string does not contain a
279: * parsable integer.
280:
281: */
282: public static int parseInt(String s, int radix)
283: throws NumberFormatException {
284: if (s == null) {
285: throw new NumberFormatException(
286: /* #ifdef VERBOSE_EXCEPTIONS */
287: /// skipped "null"
288: /* #endif */
289: );
290: }
291:
292: if (radix < Character.MIN_RADIX) {
293: throw new NumberFormatException(
294: /* #ifdef VERBOSE_EXCEPTIONS */
295: /// skipped "radix " + radix +
296: /// skipped " less than Character.MIN_RADIX"
297: /* #endif */
298: );
299: }
300:
301: if (radix > Character.MAX_RADIX) {
302: throw new NumberFormatException(
303: /* #ifdef VERBOSE_EXCEPTIONS */
304: /// skipped "radix " + radix +
305: /// skipped " greater than Character.MAX_RADIX"
306: /* #endif */
307: );
308: }
309:
310: int result = 0;
311: boolean negative = false;
312: int i = 0, max = s.length();
313: int limit;
314: int multmin;
315: int digit;
316:
317: if (max > 0) {
318: if (s.charAt(0) == '-') {
319: negative = true;
320: limit = Integer.MIN_VALUE;
321: i++;
322: } else {
323: limit = -Integer.MAX_VALUE;
324: }
325: multmin = limit / radix;
326: if (i < max) {
327: digit = Character.digit(s.charAt(i++), radix);
328: if (digit < 0) {
329: throw new NumberFormatException(
330: /* #ifdef VERBOSE_EXCEPTIONS */
331: /// skipped s
332: /* #endif */
333: );
334: } else {
335: result = -digit;
336: }
337: }
338: while (i < max) {
339: // Accumulating negatively avoids surprises near MAX_VALUE
340: digit = Character.digit(s.charAt(i++), radix);
341: if (digit < 0) {
342: throw new NumberFormatException(
343: /* #ifdef VERBOSE_EXCEPTIONS */
344: /// skipped s
345: /* #endif */
346: );
347: }
348: if (result < multmin) {
349: throw new NumberFormatException(
350: /* #ifdef VERBOSE_EXCEPTIONS */
351: /// skipped s
352: /* #endif */
353: );
354: }
355: result *= radix;
356: if (result < limit + digit) {
357: throw new NumberFormatException(
358: /* #ifdef VERBOSE_EXCEPTIONS */
359: /// skipped s
360: /* #endif */
361: );
362: }
363: result -= digit;
364: }
365: } else {
366: throw new NumberFormatException(
367: /* #ifdef VERBOSE_EXCEPTIONS */
368: /// skipped s
369: /* #endif */
370: );
371: }
372: if (negative) {
373: if (i > 1) {
374: return result;
375: } else { /* Only got "-" */
376: throw new NumberFormatException(
377: /* #ifdef VERBOSE_EXCEPTIONS */
378: /// skipped s
379: /* #endif */
380: );
381: }
382: } else {
383: return -result;
384: }
385: }
386:
387: /**
388: * Parses the string argument as a signed decimal integer. The
389: * characters in the string must all be decimal digits, except that
390: * the first character may be an ASCII minus sign <code>'-'</code>
391: * (<tt>'\u002d'</tt>) to indicate a negative value. The resulting
392: * integer value is returned, exactly as if the argument and the radix
393: * 10 were given as arguments to the
394: * {@link #parseInt(java.lang.String, int)} method.
395: *
396: * @param s a string.
397: * @return the integer represented by the argument in decimal.
398: * @exception NumberFormatException if the string does not contain a
399: * parsable integer.
400: */
401: public static int parseInt(String s) throws NumberFormatException {
402: return parseInt(s, 10);
403: }
404:
405: /**
406: * Returns a new Integer object initialized to the value of the
407: * specified String. The first argument is interpreted as representing
408: * a signed integer in the radix specified by the second argument,
409: * exactly as if the arguments were given to the
410: * {@link #parseInt(java.lang.String, int)} method. The result is an
411: * <code>Integer</code> object that represents the integer value
412: * specified by the string.
413: * <p>
414: * In other words, this method returns an <code>Integer</code> object
415: * equal to the value of:
416: * <blockquote><pre>
417: * new Integer(Integer.parseInt(s, radix))
418: * </pre></blockquote>
419: *
420: * @param s the string to be parsed.
421: * @param radix the radix of the integer represented by string
422: * <tt>s</tt>
423: * @return a newly constructed <code>Integer</code> initialized to the
424: * value represented by the string argument in the specified
425: * radix.
426: * @exception NumberFormatException if the String cannot be
427: * parsed as an <code>int</code>.
428: */
429: public static Integer valueOf(String s, int radix)
430: throws NumberFormatException {
431: return new Integer(parseInt(s, radix));
432: }
433:
434: /**
435: * Returns a new Integer object initialized to the value of the
436: * specified String. The argument is interpreted as representing a
437: * signed decimal integer, exactly as if the argument were given to
438: * the {@link #parseInt(java.lang.String)} method. The result is an
439: * <tt>Integer</tt> object that represents the integer value specified
440: * by the string.
441: * <p>
442: * In other words, this method returns an <tt>Integer</tt> object equal
443: * to the value of:
444: * <blockquote><pre>
445: * new Integer(Integer.parseInt(s))
446: * </pre></blockquote>
447: *
448: * @param s the string to be parsed.
449: * @return a newly constructed <code>Integer</code> initialized to the
450: * value represented by the string argument.
451: * @exception NumberFormatException if the string cannot be parsed
452: * as an integer.
453: */
454: public static Integer valueOf(String s)
455: throws NumberFormatException {
456: return new Integer(parseInt(s, 10));
457: }
458:
459: /**
460: * The value of the Integer.
461: *
462: * @serial
463: */
464: private int value;
465:
466: /**
467: * Constructs a newly allocated <code>Integer</code> object that
468: * represents the primitive <code>int</code> argument.
469: *
470: * @param value the value to be represented by the <code>Integer</code>.
471: */
472: public Integer(int value) {
473: this .value = value;
474: }
475:
476: /**
477: * Returns the value of this Integer as a byte.
478: *
479: * @return the value of this Integer as a byte.
480: *
481: * @since JDK1.1
482: */
483: public byte byteValue() {
484: return (byte) value;
485: }
486:
487: /**
488: * Returns the value of this Integer as a short.
489: *
490: * @return the value of this Integer as a short.
491: *
492: * @since JDK1.1
493: */
494: public short shortValue() {
495: return (short) value;
496: }
497:
498: /**
499: * Returns the value of this Integer as an int.
500: *
501: * @return the <code>int</code> value represented by this object.
502: */
503: public int intValue() {
504: return value;
505: }
506:
507: /**
508: * Returns the value of this Integer as a <tt>long</tt>.
509: *
510: * @return the <code>int</code> value represented by this object that is
511: * converted to type <code>long</code> and the result of the
512: * conversion is returned.
513: */
514: public long longValue() {
515: return (long) value;
516: }
517:
518: /**
519: * Returns the value of this Integer as a <tt>float</tt>.
520: *
521: * @return the <code>int</code> value represented by this object is
522: * converted to type <code>float</code> and the result of the
523: * conversion is returned.
524: * @since CLDC 1.1
525: */
526: public float floatValue() {
527: return (float) value;
528: }
529:
530: /**
531: * Returns the value of this Integer as a <tt>double</tt>.
532: *
533: * @return the <code>int</code> value represented by this object is
534: * converted to type <code>double</code> and the result of the
535: * conversion is returned.
536: * @since CLDC 1.1
537: */
538: public double doubleValue() {
539: return (double) value;
540: }
541:
542: /**
543: * Returns a String object representing this Integer's value. The
544: * value is converted to signed decimal representation and returned
545: * as a string, exactly as if the integer value were given as an
546: * argument to the {@link java.lang.Integer#toString(int)} method.
547: *
548: * @return a string representation of the value of this object in
549: * base 10.
550: */
551: public String toString() {
552: return String.valueOf(value);
553: }
554:
555: /**
556: * Returns a hashcode for this Integer.
557: *
558: * @return a hash code value for this object, equal to the
559: * primitive <tt>int</tt> value represented by this
560: * <tt>Integer</tt> object.
561: */
562: public int hashCode() {
563: return value;
564: }
565:
566: /**
567: * Compares this object to the specified object.
568: * The result is <code>true</code> if and only if the argument is not
569: * <code>null</code> and is an <code>Integer</code> object that contains
570: * the same <code>int</code> value as this object.
571: *
572: * @param obj the object to compare with.
573: * @return <code>true</code> if the objects are the same;
574: * <code>false</code> otherwise.
575: */
576: public boolean equals(Object obj) {
577: if (obj instanceof Integer) {
578: return value == ((Integer) obj).intValue();
579: }
580: return false;
581: }
582:
583: }
|