SE450: Enumerations: Type-safe Enumeration [23/32] Previous pageContentsNext page

See http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

Enums are java.lang.Comparable and java.io.Serializable.

file:Card.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package enumeration2;

import java.util.ArrayList;
import java.util.List;

public class Card {
  public enum Rank { DEUCE, THREE, FOUR, FIVE, SIX,
    SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }

  public enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }

  private final Rank rank;
  private final Suit suit;
  private Card(Rank rank, Suit suit) {
    this.rank = rank;
    this.suit = suit;
  }

  public Rank rank() { return rank; }
  public Suit suit() { return suit; }
  public String toString() { return rank + " of " + suit; }

  private static final List<Card> protoDeck = new ArrayList<Card>();

  // Initialize prototype deck
  static {
    for (Suit suit : Suit.values())
      for (Rank rank : Rank.values())
        protoDeck.add(new Card(rank, suit));
  }

  public static List<Card> newDeck() {
    return new ArrayList<Card>(protoDeck); // Return copy of prototype deck
  }
}

file:Main.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package enumeration2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {
  private Main() {}
  public static void main(String args[]) {
    int numHands = Integer.parseInt(args[0]);
    int cardsPerHand = Integer.parseInt(args[1]);
    List<Card> deck  = Card.newDeck();
    Collections.shuffle(deck);
    for (int i=0; i < numHands; i++)
      System.out.println(deal(deck, cardsPerHand));
  }

  public static List<Card> deal(List<Card> deck, int n) {
    int deckSize = deck.size();
    List<Card> handView = deck.subList(deckSize-n, deckSize);
    List<Card> hand = new ArrayList<Card>(handView);
    handView.clear();
    return hand;
  }
}

Here are Java 1.4 versions:

file:Suit.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package enumeration;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


// Ordinal-based typesafe enum (From Bloch)
//
// No need to override equals, since only one instance of each suit.
// Note that instanceNum and name are instance fields.
// Note that cNumInstances is a class field.
// Note that the order of the constant definitions is important.
// Note that VALUES is an immutable collection.
// Java arrays are always mutable :-(
public final class Suit implements Comparable<Suit> {
  // Number of instances
  private static int cNumInstances = 0;

  // Ordinal for this instance
  private final int instanceNum;

  // Name of Suit
  private final String name;

  // Private constructor: All instances created in the class
  private Suit(String name) {
    this.name = name;
    this.instanceNum = Suit.cNumInstances++;
  }

  public String toString() {
    return name;
  }

  public int compareTo(Suit that) {
    return this.instanceNum - that.instanceNum;
  }

  public static final Suit CLUBS = new Suit("clubs");
  public static final Suit DIAMONDS = new Suit("diamonds");
  public static final Suit HEARTS = new Suit("hearts");
  public static final Suit SPADES = new Suit("spades");

  public static final List<Suit> VALUES;
  static {
    Suit[] values = { CLUBS, DIAMONDS, HEARTS, SPADES };
    VALUES = Collections.unmodifiableList(Arrays.asList(values));
  }
}

file:Rank.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package enumeration;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;


// Ordinal-based typesafe enum (From Bloch)
//
// No need to override equals, since only one instance of each rank.
// Note that instanceNum and name are instance fields.
// Note that cNumInstances is a class field.
// Note that the order of the constant definitions is important.
// Note that VALUES is an immutable collection.
// Java arrays are always mutable :-(
public final class Rank implements Comparable<Rank> {
  // Number of instances
  private static int cNumInstances = 0;

  // Ordinal for this instance
  private final int instanceNum;

  // Name of Rank
  private final String name;

  // Private constructor: All instances created in the class
  private Rank(String name) {
    this.name = name;
    instanceNum = Rank.cNumInstances++;
  }

  public String toString() {
    return name;
  }

  public int compareTo(Rank that) {
    return this.instanceNum - that.instanceNum;
  }

  public int getValue() {
    if (this == ACE_HIGH) {
      return 1;
    } else {
      return instanceNum + 1;
    }
  }

  public static final Rank ACE_LOW = new Rank("ace");
  public static final Rank TWO = new Rank("two");
  public static final Rank THREE = new Rank("three");
  public static final Rank FOUR = new Rank("four");
  public static final Rank FIVE = new Rank("five");
  public static final Rank SIX = new Rank("six");
  public static final Rank SEVEN = new Rank("seven");
  public static final Rank EIGHT = new Rank("eight");
  public static final Rank NINE = new Rank("nine");
  public static final Rank TEN = new Rank("ten");
  public static final Rank JACK = new Rank("jack");
  public static final Rank QUEEN = new Rank("queen");
  public static final Rank KING = new Rank("king");
  public static final Rank ACE_HIGH = new Rank("ace");
  public static final List<Rank> VALUES;
  static {
    Rank[] values = {
        ACE_LOW, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE_HIGH
    };
    VALUES = Collections.unmodifiableList(Arrays.asList(values));
  }
}

file:Card.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package enumeration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


// Typesafe enum (From Bloch)
//
public final class Card {
  // Rank of Card
  private final Rank rank;

  // Suit of Card
  private final Suit suit;

  // Private constructor: All instances created in the class
  private Card(Rank rank, Suit suit) {
    this.rank = rank;
    this.suit = suit;
  }

  public String toString() {
    return rank + " of " + suit;
  }

  public int compareRank(Card c) {
    return rank.compareTo(c.rank);
  }

  public int compareSuit(Card c) {
    return suit.compareTo(c.suit);
  }

  public Rank getRank() {
    return rank;
  }

  public int getRankValue() {
    return rank.getValue();
  }

  public Suit getSuit() {
    return suit;
  }

  public static final List<Card> VALUES;
  static {
    List<Card> values = new ArrayList<Card>(56);
    for (Suit s : Suit.VALUES) {
      for (Rank r : Rank.VALUES) {
        values.add(new Card(r, s));
      }
    }
    VALUES = Collections.unmodifiableList(values);
  }
}

Previous pageContentsNext page