2009-03-15 18:02:36 +00:00
|
|
|
/* Copyright (c) 2008-2009, Avian Contributors
|
2008-02-19 18:06:52 +00:00
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software
|
|
|
|
for any purpose with or without fee is hereby granted, provided
|
|
|
|
that the above copyright notice and this permission notice appear
|
|
|
|
in all copies.
|
|
|
|
|
|
|
|
There is NO WARRANTY for this software. See license.txt for
|
|
|
|
details. */
|
|
|
|
|
2007-07-04 22:27:08 +00:00
|
|
|
package java.lang;
|
|
|
|
|
2007-10-29 00:51:08 +00:00
|
|
|
public final class Long extends Number implements Comparable<Long> {
|
2008-03-21 00:40:18 +00:00
|
|
|
public static final Long MIN_VALUE = -9223372036854775808l;
|
|
|
|
public static final Long MAX_VALUE = 9223372036854775807l;
|
|
|
|
|
2007-07-28 16:55:24 +00:00
|
|
|
public static final Class TYPE = Class.forCanonicalName("J");
|
2007-07-27 02:39:53 +00:00
|
|
|
|
2007-07-04 22:27:08 +00:00
|
|
|
private final long value;
|
|
|
|
|
|
|
|
public Long(long value) {
|
|
|
|
this.value = value;
|
|
|
|
}
|
2007-08-13 00:50:25 +00:00
|
|
|
|
2007-09-14 02:19:44 +00:00
|
|
|
public Long(String s) {
|
|
|
|
this.value = parseLong(s);
|
|
|
|
}
|
|
|
|
|
2007-09-26 16:32:02 +00:00
|
|
|
public static Long valueOf(String value) {
|
|
|
|
return new Long(value);
|
|
|
|
}
|
|
|
|
|
2007-08-13 00:50:25 +00:00
|
|
|
public static Long valueOf(long value) {
|
|
|
|
return new Long(value);
|
|
|
|
}
|
2007-07-04 22:27:08 +00:00
|
|
|
|
2007-10-29 00:51:08 +00:00
|
|
|
public int compareTo(Long o) {
|
|
|
|
return value > o.value ? 1 : (value < o.value ? -1 : 0);
|
|
|
|
}
|
|
|
|
|
2007-07-29 23:32:23 +00:00
|
|
|
public boolean equals(Object o) {
|
|
|
|
return o instanceof Long && ((Long) o).value == value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int hashCode() {
|
|
|
|
return (int) ((value >> 32) ^ (value & 0xFF));
|
|
|
|
}
|
|
|
|
|
|
|
|
public String toString() {
|
|
|
|
return String.valueOf(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String toString(long v, int radix) {
|
|
|
|
if (radix < 1 || radix > 36) {
|
|
|
|
throw new IllegalArgumentException("radix " + radix + " not in [1,36]");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (v == 0) {
|
|
|
|
return "0";
|
|
|
|
}
|
|
|
|
|
2007-08-20 02:57:32 +00:00
|
|
|
boolean negative = v < 0;
|
|
|
|
|
|
|
|
int size = (negative ? 1 : 0);
|
2008-07-13 23:54:44 +00:00
|
|
|
for (long n = v; n != 0; n /= radix) ++size;
|
2007-07-29 23:32:23 +00:00
|
|
|
|
|
|
|
char[] array = new char[size];
|
|
|
|
|
2009-04-26 18:46:55 +00:00
|
|
|
int i = size - 1;
|
2008-07-13 23:54:44 +00:00
|
|
|
for (long n = v; n != 0; n /= radix) {
|
2007-07-29 23:32:23 +00:00
|
|
|
long digit = n % radix;
|
2008-07-13 23:54:44 +00:00
|
|
|
if (negative) digit = -digit;
|
|
|
|
|
2007-07-29 23:32:23 +00:00
|
|
|
if (digit >= 0 && digit <= 9) {
|
|
|
|
array[i] = (char) ('0' + digit);
|
|
|
|
} else {
|
|
|
|
array[i] = (char) ('a' + (digit - 10));
|
|
|
|
}
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
|
2007-08-20 02:57:32 +00:00
|
|
|
if (negative) {
|
2007-07-29 23:32:23 +00:00
|
|
|
array[i] = '-';
|
|
|
|
}
|
|
|
|
|
2009-04-26 18:46:55 +00:00
|
|
|
return new String(array, 0, size, false);
|
2007-07-29 23:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static String toString(long v) {
|
|
|
|
return toString(v, 10);
|
|
|
|
}
|
|
|
|
|
2009-03-09 20:54:31 +00:00
|
|
|
public static String toHexString(long v) {
|
|
|
|
return toString(v, 16);
|
|
|
|
}
|
|
|
|
|
2007-07-21 17:50:26 +00:00
|
|
|
public byte byteValue() {
|
|
|
|
return (byte) value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public short shortValue() {
|
|
|
|
return (short) value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public int intValue() {
|
|
|
|
return (int) value;
|
|
|
|
}
|
|
|
|
|
2007-07-04 22:27:08 +00:00
|
|
|
public long longValue() {
|
|
|
|
return value;
|
|
|
|
}
|
2007-07-21 17:50:26 +00:00
|
|
|
|
|
|
|
public float floatValue() {
|
|
|
|
return (float) value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public double doubleValue() {
|
|
|
|
return (double) value;
|
|
|
|
}
|
2007-07-27 23:56:19 +00:00
|
|
|
|
|
|
|
private static long pow(long a, long b) {
|
|
|
|
long c = 1;
|
|
|
|
for (int i = 0; i < b; ++i) c *= a;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2007-08-03 01:49:32 +00:00
|
|
|
public static long parseLong(String s) {
|
|
|
|
return parseLong(s, 10);
|
|
|
|
}
|
|
|
|
|
2007-08-24 02:35:27 +00:00
|
|
|
public static long parseLong(String s, int radix) {
|
|
|
|
int i = 0;
|
2007-07-27 23:56:19 +00:00
|
|
|
long number = 0;
|
2007-08-24 02:35:27 +00:00
|
|
|
boolean negative = s.startsWith("-");
|
2008-08-29 17:37:53 +00:00
|
|
|
int length = s.length();
|
2007-08-24 02:35:27 +00:00
|
|
|
if (negative) {
|
|
|
|
i = 1;
|
2008-08-29 17:37:53 +00:00
|
|
|
-- length;
|
2007-08-24 02:35:27 +00:00
|
|
|
}
|
2007-07-27 23:56:19 +00:00
|
|
|
|
2008-08-29 17:37:53 +00:00
|
|
|
long factor = pow(radix, length - 1);
|
2007-08-24 02:35:27 +00:00
|
|
|
for (; i < s.length(); ++i) {
|
2007-07-27 23:56:19 +00:00
|
|
|
char c = s.charAt(i);
|
2008-03-21 00:40:18 +00:00
|
|
|
int digit = Character.digit(c, radix);
|
|
|
|
if (digit >= 0) {
|
2008-08-29 17:37:53 +00:00
|
|
|
number += digit * factor;
|
|
|
|
factor /= radix;
|
2008-03-21 00:40:18 +00:00
|
|
|
} else {
|
|
|
|
throw new NumberFormatException("invalid character " + c + " code " +
|
|
|
|
(int) c);
|
2007-07-27 23:56:19 +00:00
|
|
|
}
|
2007-08-24 02:35:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (negative) {
|
|
|
|
number = -number;
|
2007-07-27 23:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return number;
|
|
|
|
}
|
2007-07-04 22:27:08 +00:00
|
|
|
}
|