Source Code Cross Referenced for Integer.java in  » Apache-Harmony-Java-SE » java-package » java » lang » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *  Licensed to the Apache Software Foundation (ASF) under one or more
003:         *  contributor license agreements.  See the NOTICE file distributed with
004:         *  this work for additional information regarding copyright ownership.
005:         *  The ASF licenses this file to You under the Apache License, Version 2.0
006:         *  (the "License"); you may not use this file except in compliance with
007:         *  the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */
017:
018:        package java.lang;
019:
020:        /**
021:         * <p>
022:         * Integer is the wrapper for the primitive type <code>int</code>.
023:         * </p>
024:         * 
025:         * <p>
026:         * As with the specification, this implementation relied on code laid out in <a
027:         * href="http://www.hackersdelight.org/">Henry S. Warren, Jr.'s Hacker's
028:         * Delight, (Addison Wesley, 2002)</a> as well as <a
029:         * href="http://aggregate.org/MAGIC/">The Aggregate's Magic Algorithms</a>.
030:         * </p>
031:         * 
032:         * @see java.lang.Number
033:         * @since 1.1
034:         */
035:        public final class Integer extends Number implements 
036:                Comparable<Integer> {
037:
038:            private static final long serialVersionUID = 1360826667806852920L;
039:
040:            /**
041:             * The value which the receiver represents.
042:             */
043:            private final int value;
044:
045:            /**
046:             * <p>
047:             * Constant for the maximum <code>int</code> value, 2<sup>31</sup>-1.
048:             * </p>
049:             */
050:            public static final int MAX_VALUE = 0x7FFFFFFF;
051:
052:            /**
053:             * <p>
054:             * Constant for the minimum <code>int</code> value, -2<sup>31</sup>.
055:             * </p>
056:             */
057:            public static final int MIN_VALUE = 0x80000000;
058:
059:            /**
060:             * <p>
061:             * Constant for the number of bits to represent an <code>int</code> in
062:             * two's compliment form.
063:             * </p>
064:             * 
065:             * @since 1.5
066:             */
067:            public static final int SIZE = 32;
068:
069:            /**
070:             * The java.lang.Class that represents this class.
071:             */
072:            @SuppressWarnings("unchecked")
073:            public static final Class<Integer> TYPE = (Class<Integer>) new int[0]
074:                    .getClass().getComponentType();
075:
076:            // Note: This can't be set to "int.class", since *that* is
077:            // defined to be "java.lang.Integer.TYPE";
078:
079:            /**
080:             * Constructs a new instance of the receiver which represents the int valued
081:             * argument.
082:             * 
083:             * @param value
084:             *            the int to store in the new instance.
085:             */
086:            public Integer(int value) {
087:                this .value = value;
088:            }
089:
090:            /**
091:             * Constructs a new instance of this class given a string.
092:             * 
093:             * @param string
094:             *            a string representation of an int quantity.
095:             * @exception NumberFormatException
096:             *                if the argument could not be parsed as an int quantity.
097:             */
098:            public Integer(String string) throws NumberFormatException {
099:                this (parseInt(string));
100:            }
101:
102:            /**
103:             * Answers the byte value which the receiver represents
104:             * 
105:             * @return byte the value of the receiver.
106:             */
107:            @Override
108:            public byte byteValue() {
109:                return (byte) value;
110:            }
111:
112:            /**
113:             * <p>
114:             * Compares this <code>Integer</code> to the <code>Integer</code>
115:             * passed. If this instance's value is equal to the value of the instance
116:             * passed, then 0 is returned. If this instance's value is less than the
117:             * value of the instance passed, then a negative value is returned. If this
118:             * instance's value is greater than the value of the instance passed, then a
119:             * positive value is returned.
120:             * </p>
121:             * 
122:             * @param object
123:             *            The instance to compare to.
124:             * @throws NullPointerException
125:             *             if <code>object</code> is <code>null</code>.
126:             * @since 1.2
127:             */
128:            public int compareTo(Integer object) {
129:                return value > object.value ? 1 : (value < object.value ? -1
130:                        : 0);
131:            }
132:
133:            /**
134:             * Parses the string argument as if it was an int value and returns the
135:             * result. Throws NumberFormatException if the string does not represent an
136:             * int quantity. The string may be a hexadecimal ("0x..."), octal ("0..."),
137:             * or decimal ("...") representation of an integer
138:             * 
139:             * @param string
140:             *            a string representation of an int quantity.
141:             * @return Integer the value represented by the argument
142:             * @exception NumberFormatException
143:             *                if the argument could not be parsed as an int quantity.
144:             */
145:            public static Integer decode(String string)
146:                    throws NumberFormatException {
147:                int length = string.length(), i = 0;
148:                if (length == 0) {
149:                    throw new NumberFormatException();
150:                }
151:                char firstDigit = string.charAt(i);
152:                boolean negative = firstDigit == '-';
153:                if (negative) {
154:                    if (length == 1) {
155:                        throw new NumberFormatException(string);
156:                    }
157:                    firstDigit = string.charAt(++i);
158:                }
159:
160:                int base = 10;
161:                if (firstDigit == '0') {
162:                    if (++i == length) {
163:                        return valueOf(0);
164:                    }
165:                    if ((firstDigit = string.charAt(i)) == 'x'
166:                            || firstDigit == 'X') {
167:                        if (++i == length) {
168:                            throw new NumberFormatException(string);
169:                        }
170:                        base = 16;
171:                    } else {
172:                        base = 8;
173:                    }
174:                } else if (firstDigit == '#') {
175:                    if (++i == length) {
176:                        throw new NumberFormatException(string);
177:                    }
178:                    base = 16;
179:                }
180:
181:                int result = parse(string, i, base, negative);
182:                return valueOf(result);
183:            }
184:
185:            /**
186:             * Answers the double value which the receiver represents
187:             * 
188:             * @return double the value of the receiver.
189:             */
190:            @Override
191:            public double doubleValue() {
192:                return value;
193:            }
194:
195:            /**
196:             * Compares the argument to the receiver, and answers true if they represent
197:             * the <em>same</em> object using a class specific comparison.
198:             * <p>
199:             * In this case, the argument must also be an Integer, and the receiver and
200:             * argument must represent the same int value.
201:             * 
202:             * @param o
203:             *            the object to compare with this object
204:             * @return <code>true</code> if the object is the same as this object
205:             *         <code>false</code> if it is different from this object
206:             * @see #hashCode
207:             */
208:            @Override
209:            public boolean equals(Object o) {
210:                return (o instanceof  Integer) && (value == ((Integer) o).value);
211:            }
212:
213:            /**
214:             * Answers the float value which the receiver represents
215:             * 
216:             * @return float the value of the receiver.
217:             */
218:            @Override
219:            public float floatValue() {
220:                return value;
221:            }
222:
223:            /**
224:             * Answers an Integer representing the integer value of the property named
225:             * by the argument. If the property could not be found, or its value could
226:             * not be parsed as an integer, answer null.
227:             * 
228:             * @param string
229:             *            The name of the desired integer property.
230:             * @return Integer An Integer representing the value of the property.
231:             */
232:            public static Integer getInteger(String string) {
233:                if (string == null || string.length() == 0) {
234:                    return null;
235:                }
236:                String prop = System.getProperty(string);
237:                if (prop == null) {
238:                    return null;
239:                }
240:                try {
241:                    return decode(prop);
242:                } catch (NumberFormatException ex) {
243:                    return null;
244:                }
245:            }
246:
247:            /**
248:             * Answers an Integer representing the integer value of the property named
249:             * by the argument. If the property could not be found, or its value could
250:             * not be parsed as an integer, answer an Integer representing the second
251:             * argument.
252:             * 
253:             * @param string
254:             *            The name of the desired integer property.
255:             * @return Integer An Integer representing the value of the property.
256:             */
257:            public static Integer getInteger(String string, int defaultValue) {
258:                if (string == null || string.length() == 0) {
259:                    return valueOf(defaultValue);
260:                }
261:                String prop = System.getProperty(string);
262:                if (prop == null) {
263:                    return valueOf(defaultValue);
264:                }
265:                try {
266:                    return decode(prop);
267:                } catch (NumberFormatException ex) {
268:                    return valueOf(defaultValue);
269:                }
270:            }
271:
272:            /**
273:             * Answers an Integer representing the integer value of the property named
274:             * by the argument. If the property could not be found, or its value could
275:             * not be parsed as an integer, answer the second argument.
276:             * 
277:             * @param string
278:             *            The name of the desired integer property.
279:             * @return Integer An Integer representing the value of the property.
280:             */
281:            public static Integer getInteger(String string, Integer defaultValue) {
282:                if (string == null || string.length() == 0) {
283:                    return defaultValue;
284:                }
285:                String prop = System.getProperty(string);
286:                if (prop == null) {
287:                    return defaultValue;
288:                }
289:                try {
290:                    return decode(prop);
291:                } catch (NumberFormatException ex) {
292:                    return defaultValue;
293:                }
294:            }
295:
296:            /**
297:             * Answers an integer hash code for the receiver. Any two objects which
298:             * answer <code>true</code> when passed to <code>equals</code> must
299:             * answer the same value for this method.
300:             * 
301:             * @return the receiver's hash
302:             * 
303:             * @see #equals
304:             */
305:            @Override
306:            public int hashCode() {
307:                return value;
308:            }
309:
310:            /**
311:             * Answers the int value which the receiver represents
312:             * 
313:             * @return int the value of the receiver.
314:             */
315:            @Override
316:            public int intValue() {
317:                return value;
318:            }
319:
320:            /**
321:             * Answers the long value which the receiver represents
322:             * 
323:             * @return long the value of the receiver.
324:             */
325:            @Override
326:            public long longValue() {
327:                return value;
328:            }
329:
330:            /**
331:             * Parses the string argument as if it was an int value and returns the
332:             * result. Throws NumberFormatException if the string does not represent an
333:             * int quantity.
334:             * 
335:             * @param string
336:             *            a string representation of an int quantity.
337:             * @return int the value represented by the argument
338:             * @exception NumberFormatException
339:             *                if the argument could not be parsed as an int quantity.
340:             */
341:            public static int parseInt(String string)
342:                    throws NumberFormatException {
343:                return parseInt(string, 10);
344:            }
345:
346:            /**
347:             * Parses the string argument as if it was an int value and returns the
348:             * result. Throws NumberFormatException if the string does not represent an
349:             * int quantity. The second argument specifies the radix to use when parsing
350:             * the value.
351:             * 
352:             * @param string
353:             *            a string representation of an int quantity.
354:             * @param radix
355:             *            the base to use for conversion.
356:             * @return int the value represented by the argument
357:             * @exception NumberFormatException
358:             *                if the argument could not be parsed as an int quantity.
359:             */
360:            public static int parseInt(String string, int radix)
361:                    throws NumberFormatException {
362:                if (string == null || radix < Character.MIN_RADIX
363:                        || radix > Character.MAX_RADIX) {
364:                    throw new NumberFormatException();
365:                }
366:                int length = string.length(), i = 0;
367:                if (length == 0) {
368:                    throw new NumberFormatException(string);
369:                }
370:                boolean negative = string.charAt(i) == '-';
371:                if (negative && ++i == length) {
372:                    throw new NumberFormatException(string);
373:                }
374:
375:                return parse(string, i, radix, negative);
376:            }
377:
378:            private static int parse(String string, int offset, int radix,
379:                    boolean negative) throws NumberFormatException {
380:                int max = Integer.MIN_VALUE / radix;
381:                int result = 0, length = string.length();
382:                while (offset < length) {
383:                    int digit = Character.digit(string.charAt(offset++), radix);
384:                    if (digit == -1) {
385:                        throw new NumberFormatException(string);
386:                    }
387:                    if (max > result) {
388:                        throw new NumberFormatException(string);
389:                    }
390:                    int next = result * radix - digit;
391:                    if (next > result) {
392:                        throw new NumberFormatException(string);
393:                    }
394:                    result = next;
395:                }
396:                if (!negative) {
397:                    result = -result;
398:                    if (result < 0) {
399:                        throw new NumberFormatException(string);
400:                    }
401:                }
402:                return result;
403:            }
404:
405:            /**
406:             * Answers the short value which the receiver represents
407:             * 
408:             * @return short the value of the receiver.
409:             */
410:            @Override
411:            public short shortValue() {
412:                return (short) value;
413:            }
414:
415:            /**
416:             * Answers a string containing '0' and '1' characters which describe the
417:             * binary representation of the argument.
418:             * 
419:             * @param i
420:             *            an int to get the binary representation of
421:             * @return String the binary representation of the argument
422:             */
423:            public static String toBinaryString(int i) {
424:                int count = 1, j = i;
425:
426:                if (i < 0) {
427:                    count = 32;
428:                } else {
429:                    while ((j >>>= 1) != 0) {
430:                        count++;
431:                    }
432:                }
433:
434:                char[] buffer = new char[count];
435:                do {
436:                    buffer[--count] = (char) ((i & 1) + '0');
437:                    i >>>= 1;
438:                } while (count > 0);
439:                return new String(0, buffer.length, buffer);
440:            }
441:
442:            /**
443:             * Answers a string containing characters in the range 0..9, a..f which
444:             * describe the hexadecimal representation of the argument.
445:             * 
446:             * @param i
447:             *            an int to get the hex representation of
448:             * @return String the hex representation of the argument
449:             */
450:            public static String toHexString(int i) {
451:                int count = 1, j = i;
452:
453:                if (i < 0) {
454:                    count = 8;
455:                } else {
456:                    while ((j >>>= 4) != 0) {
457:                        count++;
458:                    }
459:                }
460:
461:                char[] buffer = new char[count];
462:                do {
463:                    int t = i & 15;
464:                    if (t > 9) {
465:                        t = t - 10 + 'a';
466:                    } else {
467:                        t += '0';
468:                    }
469:                    buffer[--count] = (char) t;
470:                    i >>>= 4;
471:                } while (count > 0);
472:                return new String(0, buffer.length, buffer);
473:            }
474:
475:            /**
476:             * Answers a string containing characters in the range 0..7 which describe
477:             * the octal representation of the argument.
478:             * 
479:             * @param i
480:             *            an int to get the octal representation of
481:             * @return String the hex representation of the argument
482:             */
483:            public static String toOctalString(int i) {
484:                int count = 1, j = i;
485:
486:                if (i < 0) {
487:                    count = 11;
488:                } else {
489:                    while ((j >>>= 3) != 0) {
490:                        count++;
491:                    }
492:                }
493:
494:                char[] buffer = new char[count];
495:                do {
496:                    buffer[--count] = (char) ((i & 7) + '0');
497:                    i >>>= 3;
498:                } while (count > 0);
499:                return new String(0, buffer.length, buffer);
500:            }
501:
502:            /**
503:             * Answers a string containing a concise, human-readable description of the
504:             * receiver.
505:             * 
506:             * @return a printable representation for the receiver.
507:             */
508:            @Override
509:            public String toString() {
510:                return Integer.toString(value);
511:            }
512:
513:            /**
514:             * Answers a string containing characters in the range 0..9 which describe
515:             * the decimal representation of the argument.
516:             * 
517:             * @param i
518:             *            an int to get the representation of
519:             * @return String the representation of the argument
520:             */
521:            public static String toString(int i) {
522:                return toString(i, 10);
523:            }
524:
525:            /**
526:             * Answers a string containing characters in the range 0..9, a..z (depending
527:             * on the radix) which describe the representation of the argument in that
528:             * radix.
529:             * 
530:             * @param i
531:             *            an int to get the representation of
532:             * @param radix
533:             *            the base to use for conversion.
534:             * @return String the representation of the argument
535:             */
536:            public static String toString(int i, int radix) {
537:                if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
538:                    radix = 10;
539:                }
540:                if (i == 0) {
541:                    return "0"; //$NON-NLS-1$
542:                }
543:
544:                int count = 2, j = i;
545:                boolean negative = i < 0;
546:                if (!negative) {
547:                    count = 1;
548:                    j = -i;
549:                }
550:                while ((i /= radix) != 0) {
551:                    count++;
552:                }
553:
554:                char[] buffer = new char[count];
555:                do {
556:                    int ch = 0 - (j % radix);
557:                    if (ch > 9) {
558:                        ch = ch - 10 + 'a';
559:                    } else {
560:                        ch += '0';
561:                    }
562:                    buffer[--count] = (char) ch;
563:                } while ((j /= radix) != 0);
564:                if (negative) {
565:                    buffer[0] = '-';
566:                }
567:                return new String(0, buffer.length, buffer);
568:            }
569:
570:            /**
571:             * Parses the string argument as if it was an int value and returns the
572:             * result. Throws NumberFormatException if the string does not represent an
573:             * int quantity.
574:             * 
575:             * @param string
576:             *            a string representation of an int quantity.
577:             * @return Integer the value represented by the argument
578:             * @exception NumberFormatException
579:             *                if the argument could not be parsed as an int quantity.
580:             */
581:            public static Integer valueOf(String string)
582:                    throws NumberFormatException {
583:                return valueOf(parseInt(string));
584:            }
585:
586:            /**
587:             * Parses the string argument as if it was an int value and returns the
588:             * result. Throws NumberFormatException if the string does not represent an
589:             * int quantity. The second argument specifies the radix to use when parsing
590:             * the value.
591:             * 
592:             * @param string
593:             *            a string representation of an int quantity.
594:             * @param radix
595:             *            the base to use for conversion.
596:             * @return Integer the value represented by the argument
597:             * @exception NumberFormatException
598:             *                if the argument could not be parsed as an int quantity.
599:             */
600:            public static Integer valueOf(String string, int radix)
601:                    throws NumberFormatException {
602:                return valueOf(parseInt(string, radix));
603:            }
604:
605:            /**
606:             * <p>
607:             * Determines the highest (leftmost) bit that is 1 and returns the value
608:             * that is the bit mask for that bit. This is sometimes referred to as the
609:             * Most Significant 1 Bit.
610:             * </p>
611:             * 
612:             * @param i
613:             *            The <code>int</code> to interrogate.
614:             * @return The bit mask indicating the highest 1 bit.
615:             * @since 1.5
616:             */
617:            public static int highestOneBit(int i) {
618:                i |= (i >> 1);
619:                i |= (i >> 2);
620:                i |= (i >> 4);
621:                i |= (i >> 8);
622:                i |= (i >> 16);
623:                return (i & ~(i >>> 1));
624:            }
625:
626:            /**
627:             * <p>
628:             * Determines the lowest (rightmost) bit that is 1 and returns the value
629:             * that is the bit mask for that bit. This is sometimes referred to as the
630:             * Least Significant 1 Bit.
631:             * </p>
632:             * 
633:             * @param i
634:             *            The <code>int</code> to interrogate.
635:             * @return The bit mask indicating the lowest 1 bit.
636:             * @since 1.5
637:             */
638:            public static int lowestOneBit(int i) {
639:                return (i & (-i));
640:            }
641:
642:            /**
643:             * <p>
644:             * Determines the number of leading zeros in the <code>int</code> passed
645:             * prior to the {@link #highestOneBit(int) highest one bit}.
646:             * </p>
647:             * 
648:             * @param i
649:             *            The <code>int</code> to process.
650:             * @return The number of leading zeros.
651:             * @since 1.5
652:             */
653:            public static int numberOfLeadingZeros(int i) {
654:                i |= i >> 1;
655:                i |= i >> 2;
656:                i |= i >> 4;
657:                i |= i >> 8;
658:                i |= i >> 16;
659:                return bitCount(~i);
660:            }
661:
662:            /**
663:             * <p>
664:             * Determines the number of trailing zeros in the <code>int</code> passed
665:             * after the {@link #lowestOneBit(int) lowest one bit}.
666:             * </p>
667:             * 
668:             * @param i
669:             *            The <code>int</code> to process.
670:             * @return The number of trailing zeros.
671:             * @since 1.5
672:             */
673:            public static int numberOfTrailingZeros(int i) {
674:                return bitCount((i & -i) - 1);
675:            }
676:
677:            /**
678:             * <p>
679:             * Counts the number of 1 bits in the <code>int</code> value passed; this
680:             * is sometimes referred to as a population count.
681:             * </p>
682:             * 
683:             * @param i
684:             *            The <code>int</code> value to process.
685:             * @return The number of 1 bits.
686:             * @since 1.5
687:             */
688:            public static int bitCount(int i) {
689:                i -= ((i >> 1) & 0x55555555);
690:                i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
691:                i = (((i >> 4) + i) & 0x0F0F0F0F);
692:                i += (i >> 8);
693:                i += (i >> 16);
694:                return (i & 0x0000003F);
695:            }
696:
697:            /**
698:             * <p>
699:             * Rotates the bits of <code>i</code> to the left by the
700:             * <code>distance</code> bits.
701:             * </p>
702:             * 
703:             * @param i
704:             *            The <code>int</code> value to rotate left.
705:             * @param distance
706:             *            The number of bits to rotate.
707:             * @return The rotated value.
708:             * @since 1.5
709:             */
710:            public static int rotateLeft(int i, int distance) {
711:                if (distance == 0) {
712:                    return i;
713:                }
714:                /*
715:                 * According to JLS3, 15.19, the right operand of a shift is always
716:                 * implicitly masked with 0x1F, which the negation of 'distance' is
717:                 * taking advantage of.
718:                 */
719:                return ((i << distance) | (i >>> (-distance)));
720:            }
721:
722:            /**
723:             * <p>
724:             * Rotates the bits of <code>i</code> to the right by the
725:             * <code>distance</code> bits.
726:             * </p>
727:             * 
728:             * @param i
729:             *            The <code>int</code> value to rotate right.
730:             * @param distance
731:             *            The number of bits to rotate.
732:             * @return The rotated value.
733:             * @since 1.5
734:             */
735:            public static int rotateRight(int i, int distance) {
736:                if (distance == 0) {
737:                    return i;
738:                }
739:                /*
740:                 * According to JLS3, 15.19, the right operand of a shift is always
741:                 * implicitly masked with 0x1F, which the negation of 'distance' is
742:                 * taking advantage of.
743:                 */
744:                return ((i >>> distance) | (i << (-distance)));
745:            }
746:
747:            /**
748:             * <p>
749:             * Reverses the bytes of a <code>int</code>.
750:             * </p>
751:             * 
752:             * @param i
753:             *            The <code>int</code> to reverse.
754:             * @return The reversed value.
755:             * @since 1.5
756:             */
757:            public static int reverseBytes(int i) {
758:                int b3 = i >>> 24;
759:                int b2 = (i >>> 8) & 0xFF00;
760:                int b1 = (i & 0xFF00) << 8;
761:                int b0 = i << 24;
762:                return (b0 | b1 | b2 | b3);
763:            }
764:
765:            /**
766:             * <p>
767:             * Reverses the bytes of a <code>int</code>.
768:             * </p>
769:             * 
770:             * @param i
771:             *            The <code>int</code> to reverse.
772:             * @return The reversed value.
773:             * @since 1.5
774:             */
775:            public static int reverse(int i) {
776:                // From Hacker's Delight, 7-1, Figure 7-1
777:                i = (i & 0x55555555) << 1 | (i >> 1) & 0x55555555;
778:                i = (i & 0x33333333) << 2 | (i >> 2) & 0x33333333;
779:                i = (i & 0x0F0F0F0F) << 4 | (i >> 4) & 0x0F0F0F0F;
780:                return reverseBytes(i);
781:            }
782:
783:            /**
784:             * <p>
785:             * The <code>signum</code> function for <code>int</code> values. This
786:             * method returns -1 for negative values, 1 for positive values and 0 for
787:             * the value 0.
788:             * </p>
789:             * 
790:             * @param i
791:             *            The <code>int</code> value.
792:             * @return -1 if negative, 1 if positive otherwise 0.
793:             * @since 1.5
794:             */
795:            public static int signum(int i) {
796:                return (i == 0 ? 0 : (i < 0 ? -1 : 1));
797:            }
798:
799:            /**
800:             * <p>
801:             * Returns a <code>Integer</code> instance for the <code>int</code>
802:             * value passed. This method is preferred over the constructor, as this
803:             * method may maintain a cache of instances.
804:             * </p>
805:             * 
806:             * @param i
807:             *            The int value.
808:             * @return A <code>Integer</code> instance.
809:             * @since 1.5
810:             */
811:            public static Integer valueOf(int i) {
812:                if (i < -128 || i > 127) {
813:                    return new Integer(i);
814:                }
815:                return valueOfCache.CACHE[i + 128];
816:
817:            }
818:
819:            static class valueOfCache {
820:                /**
821:                 * <p>
822:                 * A cache of instances used by {@link Integer#valueOf(int)} and auto-boxing.
823:                 * </p>
824:                 */
825:                static final Integer[] CACHE = new Integer[256];
826:
827:                static {
828:                    for (int i = -128; i <= 127; i++) {
829:                        CACHE[i + 128] = new Integer(i);
830:                    }
831:                }
832:            }
833:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.