Source Code Cross Referenced for Long.java in  » 6.0-JDK-Modules » j2me » 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 » 6.0 JDK Modules » j2me » java.lang 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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 Long class wraps a value of the primitive type <code>long</code>
031:         * in an object. An object of type <code>Long</code> contains a single
032:         * field whose type is <code>long</code>.
033:         * <p>
034:         * In addition, this class provides several methods for converting a
035:         * <code>long</code> to a <code>String</code> and a
036:         * <code>String</code> to a <code>long</code>, as well as other
037:         * constants and methods useful when dealing with a
038:         * <code>long</code>.
039:         *
040:         * @version 12/17/01 (CLDC 1.1)
041:         * @since   JDK1.0, CLDC 1.0
042:         */
043:        public final class Long {
044:            /**
045:             * The smallest value of type <code>long</code>.
046:             */
047:            public static final long MIN_VALUE = 0x8000000000000000L;
048:
049:            /**
050:             * The largest value of type <code>long</code>.
051:             */
052:            public static final long MAX_VALUE = 0x7fffffffffffffffL;
053:
054:            /**
055:             * Creates a string representation of the first argument in the
056:             * radix specified by the second argument.
057:             * <p>
058:             * If the radix is smaller than <code>Character.MIN_RADIX</code> or
059:             * larger than <code>Character.MAX_RADIX</code>, then the radix
060:             * <code>10</code> is used instead.
061:             * <p>
062:             * If the first argument is negative, the first element of the
063:             * result is the ASCII minus sign <code>'-'</code>
064:             * (<code>'&#92;u002d'</code>. If the first argument is not negative,
065:             * no sign character appears in the result.
066:             * <p>
067:             * The remaining characters of the result represent the magnitude of
068:             * the first argument. If the magnitude is zero, it is represented by
069:             * a single zero character <code>'0'</code>
070:             * (<code>'&#92;u0030'</code>); otherwise, the first character of the
071:             * representation of the magnitude will not be the zero character.
072:             * The following ASCII characters are used as digits:
073:             * <blockquote><pre>
074:             *   0123456789abcdefghijklmnopqrstuvwxyz
075:             * </pre></blockquote>
076:             * These are <tt>'&#92;u0030'</tt> through <tt>'&#92;u0039'</tt>
077:             * and <tt>'&#92;u0061'</tt> through <tt>'&#92;u007a'</tt>. If the
078:             * radix is <var>N</var>, then the first <var>N</var> of these
079:             * characters are used as radix-<var>N</var> digits in the order
080:             * shown. Thus, the digits for hexadecimal (radix 16) are
081:             * <blockquote><pre>
082:             * <tt>0123456789abcdef</tt>.
083:             * </pre></blockquote>
084:             *
085:             * @param   i       a long.
086:             * @param   radix   the radix.
087:             * @return  a string representation of the argument in the specified radix.
088:             * @see     java.lang.Character#MAX_RADIX
089:             * @see     java.lang.Character#MIN_RADIX
090:             */
091:            public static String toString(long i, int radix) {
092:                if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
093:                    radix = 10;
094:
095:                char[] buf = new char[65];
096:                int charPos = 64;
097:                boolean negative = (i < 0);
098:
099:                if (!negative) {
100:                    i = -i;
101:                }
102:
103:                while (i <= -radix) {
104:                    buf[charPos--] = Integer.digits[(int) (-(i % radix))];
105:                    i = i / radix;
106:                }
107:                buf[charPos] = Integer.digits[(int) (-i)];
108:
109:                if (negative) {
110:                    buf[--charPos] = '-';
111:                }
112:
113:                return new String(buf, charPos, (65 - charPos));
114:            }
115:
116:            /**
117:             * Returns a new String object representing the specified integer.
118:             * The argument is converted to signed decimal representation and
119:             * returned as a string, exactly as if the argument and the radix
120:             * 10 were given as arguments to the
121:             * {@link #toString(long, int)} method that takes two arguments.
122:             *
123:             * @param   i   a <code>long</code> to be converted.
124:             * @return  a string representation of the argument in base&nbsp;10.
125:             */
126:            public static String toString(long i) {
127:                return toString(i, 10);
128:            }
129:
130:            /**
131:             * Parses the string argument as a signed <code>long</code> in the
132:             * radix specified by the second argument. The characters in the
133:             * string must all be digits of the specified radix (as determined by
134:             * whether <code>Character.digit</code> returns a
135:             * nonnegative value), except that the first character may be an
136:             * ASCII minus sign <code>'-'</code> (<tt>'&#92;u002d'</tt> to indicate
137:             * a negative value. The resulting <code>long</code> value is returned.
138:             * <p>
139:             * Note that neither <tt>L</tt> nor <tt>l</tt> is permitted to appear at
140:             * the end of the string as a type indicator, as would be permitted in
141:             * Java programming language source code - except that either <tt>L</tt>
142:             * or <tt>l</tt> may appear as a digit for a radix greater than 22.
143:             * <p>
144:             * An exception of type <tt>NumberFormatException</tt> is thrown if any of
145:             * the following situations occurs:
146:             * <ul>
147:             * <li>The first argument is <tt>null</tt> or is a string of length zero.
148:             * <li>The <tt>radix</tt> is either smaller than
149:             *     {@link java.lang.Character#MIN_RADIX} or larger than
150:             *     {@link java.lang.Character#MAX_RADIX}.
151:             * <li>The first character of the string is not a digit of the
152:             *     specified <tt>radix</tt> and is not a minus sign <tt>'-'</tt>
153:             *     (<tt>'&#92;u002d'</tt>).
154:             * <li>The first character of the string is a minus sign and the
155:             *     string is of length 1.
156:             * <li>Any character of the string after the first is not a digit of
157:             *     the specified <tt>radix</tt>.
158:             * <li>The integer value represented by the string cannot be
159:             *     represented as a value of type <tt>long</tt>.
160:             * </ul><p>
161:             * Examples:
162:             * <blockquote><pre>
163:             * parseLong("0", 10) returns 0L
164:             * parseLong("473", 10) returns 473L
165:             * parseLong("-0", 10) returns 0L
166:             * parseLong("-FF", 16) returns -255L
167:             * parseLong("1100110", 2) returns 102L
168:             * parseLong("99", 8) throws a NumberFormatException
169:             * parseLong("Hazelnut", 10) throws a NumberFormatException
170:             * parseLong("Hazelnut", 36) returns 1356099454469L
171:             * </pre></blockquote>
172:             *
173:             * @param      s       the <code>String</code> containing the
174:             *                     <code>long</code>.
175:             * @param      radix   the radix to be used.
176:             * @return     the <code>long</code> represented by the string argument in
177:             *             the specified radix.
178:             * @exception  NumberFormatException  if the string does not contain a
179:             *                                    parsable integer.
180:             */
181:            public static long parseLong(String s, int radix)
182:                    throws NumberFormatException {
183:                if (s == null) {
184:                    throw new NumberFormatException(
185:                    /* #ifdef VERBOSE_EXCEPTIONS */
186:                    /// skipped                     "null"
187:                    /* #endif */
188:                    );
189:                }
190:
191:                if (radix < Character.MIN_RADIX) {
192:                    throw new NumberFormatException(
193:                    /* #ifdef VERBOSE_EXCEPTIONS */
194:                    /// skipped                     "radix " + radix +
195:                    /// skipped                     " less than Character.MIN_RADIX"
196:                    /* #endif */
197:                    );
198:                }
199:                if (radix > Character.MAX_RADIX) {
200:                    throw new NumberFormatException(
201:                    /* #ifdef VERBOSE_EXCEPTIONS */
202:                    /// skipped                     "radix " + radix +
203:                    /// skipped                     " greater than Character.MAX_RADIX"
204:                    /* #endif */
205:                    );
206:                }
207:
208:                long result = 0;
209:                boolean negative = false;
210:                int i = 0, max = s.length();
211:                long limit;
212:                long multmin;
213:                int digit;
214:
215:                if (max > 0) {
216:                    if (s.charAt(0) == '-') {
217:                        negative = true;
218:                        limit = Long.MIN_VALUE;
219:                        i++;
220:                    } else {
221:                        limit = -Long.MAX_VALUE;
222:                    }
223:                    multmin = limit / radix;
224:                    if (i < max) {
225:                        digit = Character.digit(s.charAt(i++), radix);
226:                        if (digit < 0) {
227:                            throw new NumberFormatException(
228:                            /* #ifdef VERBOSE_EXCEPTIONS */
229:                            /// skipped                             s
230:                            /* #endif */
231:                            );
232:                        } else {
233:                            result = -digit;
234:                        }
235:                    }
236:                    while (i < max) {
237:                        // Accumulating negatively avoids surprises near MAX_VALUE
238:                        digit = Character.digit(s.charAt(i++), radix);
239:                        if (digit < 0) {
240:                            throw new NumberFormatException(
241:                            /* #ifdef VERBOSE_EXCEPTIONS */
242:                            /// skipped                             s
243:                            /* #endif */
244:                            );
245:                        }
246:                        if (result < multmin) {
247:                            throw new NumberFormatException(
248:                            /* #ifdef VERBOSE_EXCEPTIONS */
249:                            /// skipped                             s
250:                            /* #endif */
251:                            );
252:                        }
253:                        result *= radix;
254:                        if (result < limit + digit) {
255:                            throw new NumberFormatException(
256:                            /* #ifdef VERBOSE_EXCEPTIONS */
257:                            /// skipped                             s
258:                            /* #endif */
259:                            );
260:                        }
261:                        result -= digit;
262:                    }
263:                } else {
264:                    throw new NumberFormatException(
265:                    /* #ifdef VERBOSE_EXCEPTIONS */
266:                    /// skipped                     s
267:                    /* #endif */
268:                    );
269:                }
270:                if (negative) {
271:                    if (i > 1) {
272:                        return result;
273:                    } else { /* Only got "-" */
274:                        throw new NumberFormatException(
275:                        /* #ifdef VERBOSE_EXCEPTIONS */
276:                        /// skipped                         s
277:                        /* #endif */
278:                        );
279:                    }
280:                } else {
281:                    return -result;
282:                }
283:            }
284:
285:            /**
286:             * Parses the string argument as a signed decimal <code>long</code>.
287:             * The characters in the string must all be decimal digits, except
288:             * that the first character may be an ASCII minus sign
289:             * <code>'-'</code> (<code>&#92;u002d'</code>) to indicate a negative
290:             * value. The resulting long value is returned, exactly as if the
291:             * argument and the radix <tt>10</tt> were given as arguments to the
292:             * {@link #parseLong(String, int)} method that takes two arguments.
293:             * <p>
294:             * Note that neither <tt>L</tt> nor <tt>l</tt> is permitted to appear
295:             * at the end of the string as a type indicator, as would be permitted
296:             * in Java programming language source code.
297:             *
298:             * @param      s   a string.
299:             * @return     the <code>long</code> represented by the argument in decimal.
300:             * @exception  NumberFormatException  if the string does not contain a
301:             *               parsable <code>long</code>.
302:             */
303:            public static long parseLong(String s) throws NumberFormatException {
304:                return parseLong(s, 10);
305:            }
306:
307:            /**
308:             * The value of the Long.
309:             */
310:            private long value;
311:
312:            /**
313:             * Constructs a newly allocated <code>Long</code> object that
314:             * represents the primitive <code>long</code> argument.
315:             *
316:             * @param   value   the value to be represented by the
317:             *          <code>Long</code> object.
318:             */
319:            public Long(long value) {
320:                this .value = value;
321:            }
322:
323:            /**
324:             * Returns the value of this Long as a long value.
325:             *
326:             * @return  the <code>long</code> value represented by this object.
327:             */
328:            public long longValue() {
329:                return (long) value;
330:            }
331:
332:            /**
333:             * Returns the value of this Long as a float.
334:             *
335:             * @return  the <code>long</code> value represented by this object is
336:             *          converted to type <code>float</code> and the result of
337:             *          the conversion is returned.
338:             * @since   CLDC 1.1
339:             */
340:            public float floatValue() {
341:                return (float) value;
342:            }
343:
344:            /**
345:             * Returns the value of this Long as a double.
346:             *
347:             * @return  the <code>long</code> value represented by this object that
348:             *          is converted to type <code>double</code> and the result of
349:             *          the conversion is returned.
350:             * @since   CLDC 1.1
351:             */
352:            public double doubleValue() {
353:                return (double) value;
354:            }
355:
356:            /**
357:             * Returns a String object representing this Long's value.
358:             * The long integer value represented by this Long object is converted
359:             * to signed decimal representation and returned as a string, exactly
360:             * as if the long value were given as an argument to the
361:             * {@link #toString(long)} method that takes one argument.
362:             *
363:             * @return  a string representation of this object in base&nbsp;10.
364:             */
365:            public String toString() {
366:                return String.valueOf(value);
367:            }
368:
369:            /**
370:             * Computes a hashcode for this Long. The result is the exclusive
371:             * OR of the two halves of the primitive <code>long</code> value
372:             * represented by this <code>Long</code> object. That is, the hashcode
373:             * is the value of the expression:
374:             * <blockquote><pre>
375:             * (int)(this.longValue()^(this.longValue()>>>32))
376:             * </pre></blockquote>
377:             *
378:             * @return  a hash code value for this object.
379:             */
380:            public int hashCode() {
381:                return (int) (value ^ (value >> 32));
382:            }
383:
384:            /**
385:             * Compares this object against the specified object.
386:             * The result is <code>true</code> if and only if the argument is
387:             * not <code>null</code> and is a <code>Long</code> object that
388:             * contains the same <code>long</code> value as this object.
389:             *
390:             * @param   obj   the object to compare with.
391:             * @return  <code>true</code> if the objects are the same;
392:             *          <code>false</code> otherwise.
393:             */
394:            public boolean equals(Object obj) {
395:                if (obj instanceof  Long) {
396:                    return value == ((Long) obj).longValue();
397:                }
398:                return false;
399:            }
400:
401:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.