D
Daniel Pitts
I have a "Card" class which has objects that represent cards out of a
standard poker deck. I statically initialize the array.
My basic question is: Are there circumstances in which the following
code will not produce "==" comparable Card objects? These objects may
be passed through RMI or otherwise serialized.
<sscce>
import java.io.InvalidObjectException;
import java.ibjectStreamException;
public final class Card {
static final long serialVersionUID = 1L;
public enum Suit {
DIAMOND,
CLUB,
HEART,
SPADE;
}
public static final int SUITS = Suit.values().length;
public static final int CARDS_PER_SUIT = 13;
public static final int NUM_CARDS = SUITS * CARDS_PER_SUIT;
private static final Card[] cards = new Card[NUM_CARDS];
private static final String[] numbers = new String[NUM_CARDS];
public static final int JACK_ORDINAL = 10;
private static final int QUEEN_ORDINAL = JACK_ORDINAL + 1;
private static final int KING_ORDINAL = QUEEN_ORDINAL + 1;
static {
numbers[0] = "A";
for (int i = 1; i < JACK_ORDINAL; ++i) {
numbers = String.valueOf(i + 1);
}
numbers[JACK_ORDINAL] = "J";
numbers[QUEEN_ORDINAL] = "Q";
numbers[KING_ORDINAL] = "K";
for (int i = 0; i < NUM_CARDS; ++i) {
cards = new Card(i);
}
}
public final transient Suit suit;
public final transient String number;
public final transient int value;
public final int ordinal;
private Card(int ordinal) {
this.ordinal = ordinal;
final int suitOrdinal = ordinal / CARDS_PER_SUIT;
final int cardOrdinal = ordinal % CARDS_PER_SUIT;
value = cardOrdinal == 0 ? KING_ORDINAL : cardOrdinal - 1;
suit = Suit.values()[suitOrdinal];
number = numbers[cardOrdinal];
}
protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
protected final Object readResolve() throws ObjectStreamException {
if (ordinal < 0 || NUM_CARDS <= ordinal) {
throw new InvalidObjectException("Not a valid card.");
}
return forOrdinal(ordinal);
}
public static Card forOrdinal(int ordinal) {
return cards[ordinal];
}
}
</sscce>
standard poker deck. I statically initialize the array.
My basic question is: Are there circumstances in which the following
code will not produce "==" comparable Card objects? These objects may
be passed through RMI or otherwise serialized.
<sscce>
import java.io.InvalidObjectException;
import java.ibjectStreamException;
public final class Card {
static final long serialVersionUID = 1L;
public enum Suit {
DIAMOND,
CLUB,
HEART,
SPADE;
}
public static final int SUITS = Suit.values().length;
public static final int CARDS_PER_SUIT = 13;
public static final int NUM_CARDS = SUITS * CARDS_PER_SUIT;
private static final Card[] cards = new Card[NUM_CARDS];
private static final String[] numbers = new String[NUM_CARDS];
public static final int JACK_ORDINAL = 10;
private static final int QUEEN_ORDINAL = JACK_ORDINAL + 1;
private static final int KING_ORDINAL = QUEEN_ORDINAL + 1;
static {
numbers[0] = "A";
for (int i = 1; i < JACK_ORDINAL; ++i) {
numbers = String.valueOf(i + 1);
}
numbers[JACK_ORDINAL] = "J";
numbers[QUEEN_ORDINAL] = "Q";
numbers[KING_ORDINAL] = "K";
for (int i = 0; i < NUM_CARDS; ++i) {
cards = new Card(i);
}
}
public final transient Suit suit;
public final transient String number;
public final transient int value;
public final int ordinal;
private Card(int ordinal) {
this.ordinal = ordinal;
final int suitOrdinal = ordinal / CARDS_PER_SUIT;
final int cardOrdinal = ordinal % CARDS_PER_SUIT;
value = cardOrdinal == 0 ? KING_ORDINAL : cardOrdinal - 1;
suit = Suit.values()[suitOrdinal];
number = numbers[cardOrdinal];
}
protected final Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
protected final Object readResolve() throws ObjectStreamException {
if (ordinal < 0 || NUM_CARDS <= ordinal) {
throw new InvalidObjectException("Not a valid card.");
}
return forOrdinal(ordinal);
}
public static Card forOrdinal(int ordinal) {
return cards[ordinal];
}
}
</sscce>