001package enumeration;
002
003import java.util.Arrays;
004import java.util.Collections;
005import java.util.List;
006
007
008// Ordinal-based typesafe enum (From Bloch)
009//
010// No need to override equals, since only one instance of each rank.
011// Note that instanceNum and name are instance fields.
012// Note that cNumInstances is a class field.
013// Note that the order of the constant definitions is important.
014// Note that VALUES is an immutable collection.
015// Java arrays are always mutable :-(
016public final class Rank implements Comparable<Rank> {
017        // Number of instances
018        private static int cNumInstances = 0;
019
020        // Ordinal for this instance
021        private final int instanceNum;
022
023        // Name of Rank
024        private final String name;
025
026        // Private constructor: All instances created in the class
027        private Rank(String name) {
028                this.name = name;
029                instanceNum = Rank.cNumInstances++;
030        }
031
032        public String toString() {
033                return name;
034        }
035
036        public int compareTo(Rank that) {
037                return this.instanceNum - that.instanceNum;
038        }
039
040        public int getValue() {
041                if (this == ACE_HIGH) {
042                        return 1;
043                } else {
044                        return instanceNum + 1;
045                }
046        }
047
048        public static final Rank ACE_LOW = new Rank("ace");
049        public static final Rank TWO = new Rank("two");
050        public static final Rank THREE = new Rank("three");
051        public static final Rank FOUR = new Rank("four");
052        public static final Rank FIVE = new Rank("five");
053        public static final Rank SIX = new Rank("six");
054        public static final Rank SEVEN = new Rank("seven");
055        public static final Rank EIGHT = new Rank("eight");
056        public static final Rank NINE = new Rank("nine");
057        public static final Rank TEN = new Rank("ten");
058        public static final Rank JACK = new Rank("jack");
059        public static final Rank QUEEN = new Rank("queen");
060        public static final Rank KING = new Rank("king");
061        public static final Rank ACE_HIGH = new Rank("ace");
062        public static final List<Rank> VALUES;
063        static {
064                Rank[] values = {
065                                ACE_LOW, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE_HIGH
066                };
067                VALUES = Collections.unmodifiableList(Arrays.asList(values));
068        }
069}