make Boolean, Byte, Short, etc. implement Comparable

This commit is contained in:
Joel Dice 2007-10-28 18:51:08 -06:00
parent 7f1837fecd
commit 09cedfd7cb
5 changed files with 25 additions and 5 deletions

View File

@ -1,6 +1,6 @@
package java.lang;
public final class Boolean {
public final class Boolean implements Comparable<Boolean> {
public static final Class TYPE = Class.forCanonicalName("Z");
public static final Boolean FALSE = new Boolean(false);
@ -24,6 +24,10 @@ public final class Boolean {
return ("true".equals(s) ? Boolean.TRUE : Boolean.FALSE);
}
public int compareTo(Boolean o) {
return (value ? (o.value ? 0 : 1) : (o.value ? -1 : 0));
}
public boolean equals(Object o) {
return o instanceof Boolean && ((Boolean) o).value == value;
}

View File

@ -1,6 +1,6 @@
package java.lang;
public final class Byte extends Number {
public final class Byte extends Number implements Comparable<Byte> {
public static final Class TYPE = Class.forCanonicalName("B");
private final byte value;
@ -25,6 +25,10 @@ public final class Byte extends Number {
return toString(value);
}
public int compareTo(Byte o) {
return value - o.value;
}
public static String toString(byte v, int radix) {
return Long.toString(v, radix);
}

View File

@ -1,6 +1,6 @@
package java.lang;
public final class Character {
public final class Character implements Comparable<Character> {
public static final Class TYPE = Class.forCanonicalName("C");
private final char value;
@ -13,6 +13,10 @@ public final class Character {
return new Character(value);
}
public int compareTo(Character o) {
return value - o.value;
}
public boolean equals(Object o) {
return o instanceof Character && ((Character) o).value == value;
}

View File

@ -1,6 +1,6 @@
package java.lang;
public final class Long extends Number {
public final class Long extends Number implements Comparable<Long> {
public static final Class TYPE = Class.forCanonicalName("J");
public static final Long MAX_VALUE = 9223372036854775807l;
@ -22,6 +22,10 @@ public final class Long extends Number {
return new Long(value);
}
public int compareTo(Long o) {
return value > o.value ? 1 : (value < o.value ? -1 : 0);
}
public boolean equals(Object o) {
return o instanceof Long && ((Long) o).value == value;
}

View File

@ -1,6 +1,6 @@
package java.lang;
public final class Short extends Number {
public final class Short extends Number implements Comparable<Short> {
public static final Class TYPE = Class.forCanonicalName("S");
public static final short MAX_VALUE = 32767;
@ -14,6 +14,10 @@ public final class Short extends Number {
return new Short(value);
}
public int compareTo(Short o) {
return value - o.value;
}
public boolean equals(Object o) {
return o instanceof Short && ((Short) o).value == value;
}