001package enumeration; 002 003import java.util.ArrayList; 004import java.util.Collections; 005import java.util.List; 006 007 008// Typesafe enum (From Bloch) 009// 010public final class Card { 011 // Rank of Card 012 private final Rank rank; 013 014 // Suit of Card 015 private final Suit suit; 016 017 // Private constructor: All instances created in the class 018 private Card(Rank rank, Suit suit) { 019 this.rank = rank; 020 this.suit = suit; 021 } 022 023 public String toString() { 024 return rank + " of " + suit; 025 } 026 027 public int compareRank(Card c) { 028 return rank.compareTo(c.rank); 029 } 030 031 public int compareSuit(Card c) { 032 return suit.compareTo(c.suit); 033 } 034 035 public Rank getRank() { 036 return rank; 037 } 038 039 public int getRankValue() { 040 return rank.getValue(); 041 } 042 043 public Suit getSuit() { 044 return suit; 045 } 046 047 public static final List<Card> VALUES; 048 static { 049 List<Card> values = new ArrayList<Card>(56); 050 for (Suit s : Suit.VALUES) { 051 for (Rank r : Rank.VALUES) { 052 values.add(new Card(r, s)); 053 } 054 } 055 VALUES = Collections.unmodifiableList(values); 056 } 057}