]> gcc.gnu.org Git - gcc.git/blob - libjava/java/lang/Long.java
9e2536ec15ccea1aeb65d6b4b1912eb8fa24484a
[gcc.git] / libjava / java / lang / Long.java
1 /* Copyright (C) 1998, 1999 Red Hat, Inc.
2
3 This file is part of libgcj.
4
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
7 details. */
8
9 package java.lang;
10
11 /**
12 * @author Warren Levy <warrenl@cygnus.com>
13 * @date September 18, 1998.
14 */
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.
19 */
20
21 public final class Long extends Number implements Comparable
22 {
23 public static final long MAX_VALUE = 0x7FFFFFFFFFFFFFFFL;
24 public static final long MIN_VALUE = 0x8000000000000000L;
25
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;
29
30 /* The long value of the instance. */
31 private long value;
32
33 public Long(long val)
34 {
35 value = val;
36 }
37
38 public Long(String str) throws NumberFormatException
39 {
40 value = parseLong(str, 10);
41 }
42
43 public byte byteValue()
44 {
45 return (byte) value;
46 }
47
48 public double doubleValue()
49 {
50 return (double) value;
51 }
52
53 public float floatValue()
54 {
55 return (float) value;
56 }
57
58 public int intValue()
59 {
60 return (int) value;
61 }
62
63 public long longValue()
64 {
65 return value;
66 }
67
68 public short shortValue()
69 {
70 return (short) value;
71 }
72
73 // Added in JDK 1.2
74 public int compareTo(Long anotherLong)
75 {
76 if (this.value == anotherLong.value)
77 return 0;
78
79 // Returns just -1 or 1 on inequality; doing math might overflow the long.
80 if (this.value > anotherLong.value)
81 return 1;
82
83 return -1;
84 }
85
86 // Added in JDK 1.2
87 public int compareTo(Object o) throws ClassCastException
88 {
89 if (!(o instanceof Long))
90 throw new ClassCastException();
91
92 return this.compareTo((Long) o);
93 }
94
95 // Added in JDK 1.2
96 public static Long decode(String str) throws NumberFormatException
97 {
98 boolean isNeg = false;
99 int index = 0;
100 int radix = 10;
101 final int len;
102
103 if (str == null || (len = str.length()) == 0)
104 throw new NumberFormatException();
105
106 // Negative numbers are always radix 10.
107 if (str.charAt(0) == '-')
108 {
109 radix = 10;
110 index++;
111 isNeg = true;
112 }
113 else if (str.charAt(index) == '#')
114 {
115 radix = 16;
116 index++;
117 }
118 else if (str.charAt(index) == '0')
119 {
120 // Check if str is just "0"
121 if (len == 1)
122 return new Long(0L);
123
124 index++;
125 if (str.charAt(index) == 'x')
126 {
127 radix = 16;
128 index++;
129 }
130 else
131 radix = 8;
132 }
133
134 if (index >= len)
135 throw new NumberFormatException();
136
137 return new Long(parseLong(str, index, len, isNeg, radix));
138 }
139
140 public boolean equals(Object obj)
141 {
142 return (obj != null && (obj instanceof Long)
143 && ((Long) obj).value == value);
144 }
145
146 public static Long getLong(String prop)
147 {
148 return getLong(prop, null);
149 }
150
151 public static Long getLong(String prop, long defval)
152 {
153 Long val = getLong(prop, null);
154 return val == null ? new Long(defval) : val;
155 }
156
157 public static Long getLong(String prop, Long defobj)
158 {
159 try
160 {
161 return decode(System.getProperty(prop));
162 }
163 catch (NumberFormatException ex)
164 {
165 return defobj;
166 }
167 }
168
169 public int hashCode()
170 {
171 return (int)(this.longValue()^(this.longValue()>>>32));
172 }
173
174 public static long parseLong(String str) throws NumberFormatException
175 {
176 return parseLong(str, 10);
177 }
178
179 public static long parseLong(String str, int radix)
180 throws NumberFormatException
181 {
182 final int len;
183
184 if (str == null || (len = str.length()) == 0 ||
185 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
186 throw new NumberFormatException();
187
188 boolean isNeg = false;
189 int index = 0;
190 if (str.charAt(index) == '-')
191 if (len > 1)
192 {
193 isNeg = true;
194 index++;
195 }
196 else
197 throw new NumberFormatException();
198
199 return parseLong(str, index, len, isNeg, radix);
200 }
201
202 private static long parseLong(String str, int index, int len, boolean isNeg,
203 int radix) throws NumberFormatException
204 {
205 long val = 0;
206 int digval;
207
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)
212 ++max;
213
214 for ( ; index < len; index++)
215 {
216 if (val < 0 || val > max)
217 throw new NumberFormatException();
218
219 if ((digval = Character.digit(str.charAt(index), radix)) < 0)
220 throw new NumberFormatException();
221
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();
227 }
228
229 return isNeg ? -(val) : val;
230 }
231
232 public static String toBinaryString(long num)
233 {
234 return toUnsignedString(num, 1);
235 }
236
237 public static String toHexString(long num)
238 {
239 return toUnsignedString(num, 4);
240 }
241
242 public static String toOctalString(long num)
243 {
244 return toUnsignedString(num, 3);
245 }
246
247 private static String toUnsignedString(long num, int exp)
248 {
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];
253 int i = 64;
254 do
255 {
256 buffer[--i] = Character.forDigit((int) (num & mask), radix);
257 num = num >>> exp;
258 }
259 while (num != 0);
260
261 return String.valueOf(buffer, i, 64-i);
262 }
263
264 public String toString()
265 {
266 return toString(this.value);
267 }
268
269 public static String toString(long num)
270 {
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);
274
275 // Use an arrary large enough for "-9223372036854775808"; i.e. 11 chars.
276 char[] buffer = new char[20];
277 int i = 20;
278 boolean isNeg;
279 if (num < 0)
280 {
281 isNeg = true;
282 num = -(num);
283 if (num < 0)
284 {
285 // Must be MIN_VALUE, so handle this special case.
286 buffer[--i] = '8';
287 num = 922337203685477580L;
288 }
289 }
290 else
291 isNeg = false;
292
293 do
294 {
295 buffer[--i] = (char) ((int) '0' + (num % 10));
296 num /= 10;
297 }
298 while (num > 0);
299
300 if (isNeg)
301 buffer[--i] = '-';
302
303 return String.valueOf(buffer, i, 20-i);
304 }
305
306 public static String toString(long num, int radix)
307 {
308 // Use optimized method for the typical case.
309 if (radix == 10 ||
310 radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
311 return toString(num);
312
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);
316
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];
320 int i = 65;
321 boolean isNeg;
322 if (num < 0)
323 {
324 isNeg = true;
325 num = -(num);
326
327 // When the value is MIN_VALUE, it overflows when made positive
328 if (num < 0)
329 {
330 buffer[--i] = Character.forDigit((int) (-(num + radix) % radix),
331 radix);
332 num = -(num / radix);
333 }
334 }
335 else
336 isNeg = false;
337
338 do
339 {
340 buffer[--i] = Character.forDigit((int) (num % radix), radix);
341 num /= radix;
342 }
343 while (num > 0);
344
345 if (isNeg)
346 buffer[--i] = '-';
347
348 return String.valueOf(buffer, i, 65-i);
349 }
350
351 public static Long valueOf(String str) throws NumberFormatException
352 {
353 return new Long(parseLong(str, 10));
354 }
355
356 public static Long valueOf(String str, int radix)
357 throws NumberFormatException
358 {
359 return new Long(parseLong(str, radix));
360 }
361 }
This page took 0.052184 seconds and 4 git commands to generate.