]>
gcc.gnu.org Git - gcc.git/blob - libjava/java/lang/Long.java
9e2536ec15ccea1aeb65d6b4b1912eb8fa24484a
1 /* Copyright (C) 1998, 1999 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date September 18, 1998.
15 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
16 * "The Java Language Specification", ISBN 0-201-63451-1
17 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
18 * Status: Believed complete and correct.
21 public final class Long
extends Number
implements Comparable
23 public static final long MAX_VALUE
= 0x7FFFFFFFFFFFFFFFL
;
24 public static final long MIN_VALUE
= 0x8000000000000000L
;
26 // This initialization is seemingly circular, but it is accepted
27 // by javac, and is handled specially by gcc.
28 public static final Class TYPE
= long.class;
30 /* The long value of the instance. */
38 public Long(String str
) throws NumberFormatException
40 value
= parseLong(str
, 10);
43 public byte byteValue()
48 public double doubleValue()
50 return (double) value
;
53 public float floatValue()
63 public long longValue()
68 public short shortValue()
74 public int compareTo(Long anotherLong
)
76 if (this.value
== anotherLong
.value
)
79 // Returns just -1 or 1 on inequality; doing math might overflow the long.
80 if (this.value
> anotherLong
.value
)
87 public int compareTo(Object o
) throws ClassCastException
89 if (!(o
instanceof Long
))
90 throw new ClassCastException();
92 return this.compareTo((Long
) o
);
96 public static Long
decode(String str
) throws NumberFormatException
98 boolean isNeg
= false;
103 if (str
== null || (len
= str
.length()) == 0)
104 throw new NumberFormatException();
106 // Negative numbers are always radix 10.
107 if (str
.charAt(0) == '-')
113 else if (str
.charAt(index
) == '#')
118 else if (str
.charAt(index
) == '0')
120 // Check if str is just "0"
125 if (str
.charAt(index
) == 'x')
135 throw new NumberFormatException();
137 return new Long(parseLong(str
, index
, len
, isNeg
, radix
));
140 public boolean equals(Object obj
)
142 return (obj
!= null && (obj
instanceof Long
)
143 && ((Long
) obj
).value
== value
);
146 public static Long
getLong(String prop
)
148 return getLong(prop
, null);
151 public static Long
getLong(String prop
, long defval
)
153 Long val
= getLong(prop
, null);
154 return val
== null ?
new Long(defval
) : val
;
157 public static Long
getLong(String prop
, Long defobj
)
161 return decode(System
.getProperty(prop
));
163 catch (NumberFormatException ex
)
169 public int hashCode()
171 return (int)(this.longValue()^
(this.longValue()>>>32));
174 public static long parseLong(String str
) throws NumberFormatException
176 return parseLong(str
, 10);
179 public static long parseLong(String str
, int radix
)
180 throws NumberFormatException
184 if (str
== null || (len
= str
.length()) == 0 ||
185 radix
< Character
.MIN_RADIX
|| radix
> Character
.MAX_RADIX
)
186 throw new NumberFormatException();
188 boolean isNeg
= false;
190 if (str
.charAt(index
) == '-')
197 throw new NumberFormatException();
199 return parseLong(str
, index
, len
, isNeg
, radix
);
202 private static long parseLong(String str
, int index
, int len
, boolean isNeg
,
203 int radix
) throws NumberFormatException
208 long max
= MAX_VALUE
/ radix
;
209 // We can't directly write `max = (MAX_VALUE + 1) / radix'.
210 // So instead we fake it.
211 if (isNeg
&& MAX_VALUE
% radix
== radix
- 1)
214 for ( ; index
< len
; index
++)
216 if (val
< 0 || val
> max
)
217 throw new NumberFormatException();
219 if ((digval
= Character
.digit(str
.charAt(index
), radix
)) < 0)
220 throw new NumberFormatException();
222 // Throw an exception for overflow if result is negative.
223 // However, we special-case the most negative value.
224 val
= val
* radix
+ digval
;
225 if (val
< 0 && (! isNeg
|| val
!= MIN_VALUE
))
226 throw new NumberFormatException();
229 return isNeg ?
-(val
) : val
;
232 public static String
toBinaryString(long num
)
234 return toUnsignedString(num
, 1);
237 public static String
toHexString(long num
)
239 return toUnsignedString(num
, 4);
242 public static String
toOctalString(long num
)
244 return toUnsignedString(num
, 3);
247 private static String
toUnsignedString(long num
, int exp
)
249 // Use an array large enough for a binary number.
250 int radix
= 1 << exp
;
251 long mask
= radix
- 1;
252 char[] buffer
= new char[64];
256 buffer
[--i
] = Character
.forDigit((int) (num
& mask
), radix
);
261 return String
.valueOf(buffer
, i
, 64-i
);
264 public String
toString()
266 return toString(this.value
);
269 public static String
toString(long num
)
271 // Use the Integer toString for efficiency if possible.
272 if (num
<= Integer
.MAX_VALUE
&& num
>= Integer
.MIN_VALUE
)
273 return Integer
.toString((int) num
);
275 // Use an arrary large enough for "-9223372036854775808"; i.e. 11 chars.
276 char[] buffer
= new char[20];
285 // Must be MIN_VALUE, so handle this special case.
287 num
= 922337203685477580L;
295 buffer
[--i
] = (char) ((int) '0' + (num
% 10));
303 return String
.valueOf(buffer
, i
, 20-i
);
306 public static String
toString(long num
, int radix
)
308 // Use optimized method for the typical case.
310 radix
< Character
.MIN_RADIX
|| radix
> Character
.MAX_RADIX
)
311 return toString(num
);
313 // Use the Integer toString for efficiency if possible.
314 if (num
<= Integer
.MAX_VALUE
&& num
>= Integer
.MIN_VALUE
)
315 return Integer
.toString((int) num
, radix
);
317 // For negative numbers, print out the absolute value w/ a leading '-'.
318 // Use an array large enough for a binary number.
319 char[] buffer
= new char[65];
327 // When the value is MIN_VALUE, it overflows when made positive
330 buffer
[--i
] = Character
.forDigit((int) (-(num
+ radix
) % radix
),
332 num
= -(num
/ radix
);
340 buffer
[--i
] = Character
.forDigit((int) (num
% radix
), radix
);
348 return String
.valueOf(buffer
, i
, 65-i
);
351 public static Long
valueOf(String str
) throws NumberFormatException
353 return new Long(parseLong(str
, 10));
356 public static Long
valueOf(String str
, int radix
)
357 throws NumberFormatException
359 return new Long(parseLong(str
, radix
));
This page took 0.052184 seconds and 4 git commands to generate.