Permalink
Browse files

Implemented proper enum toString() behavior and an enum test. it should

work, but it fails with the current build.
  • Loading branch information...
1 parent c7567b4 commit a88f7c84736e43f3b45d589519e7c43d675ff1a7 Eric Scharff committed Sep 26, 2007
Showing with 44 additions and 1 deletion.
  1. +4 −0 classpath/java/lang/Enum.java
  2. +1 −1 classpath/java/lang/Object.java
  3. +39 −0 test/Enums.java
@@ -35,4 +35,8 @@ public int compareTo(E other) {
public int ordinal() {
return ordinal;
}
+
+ public String toString() {
+ return name;
+ }
}
@@ -2,7 +2,7 @@
public class Object {
protected Object clone() throws CloneNotSupportedException {
- if (this instanceof Cloneable) {
+ if ((this instanceof Cloneable) || getClass().isArray()) {
return clone(this);
} else {
throw new CloneNotSupportedException(getClass().getName());
View
@@ -0,0 +1,39 @@
+public class Enums {
+ private enum Suit { CLUBS, HEARTS, SPADES, DIAMONDS };
+ private enum Rank { ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT,
+ NINE, TEN, JACK, QUEEN, KING };
+ private enum Person { Joe(4), Mike(5) ;
+ private final int age;
+ private Person(int age) {
+ this.age = age;
+ }
+ public int getAge() {
+ return age;
+ }
+ };
+
+ private static void expect(boolean v) {
+ if (! v) throw new RuntimeException();
+ }
+
+ private static boolean checkFaceCard(Rank r) {
+ switch (r) {
+ case ACE:
+ case JACK:
+ case QUEEN:
+ case KING:
+ return true;
+ }
+ return false;
+ }
+
+ public static void main(String[] args) {
+ expect(Suit.CLUBS.ordinal() == 0);
+ expect(Suit.valueOf("DIAMONDS") == Suit.DIAMONDS);
+ System.out.println(Suit.SPADES);
+ expect(Suit.values()[1] == Suit.HEARTS);
+ expect(!checkFaceCard(Rank.FIVE));
+ expect(checkFaceCard(Rank.KING));
+ expect(Person.Mike.getAge() == 5);
+ }
+}

0 comments on commit a88f7c8

Please sign in to comment.