forked from prmr/DesignBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
247 lines (214 loc) · 5.94 KB
/
Copy pathCard.java
File metadata and controls
247 lines (214 loc) · 5.94 KB
1
2
3
4
5
6
7
8
9
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java"
* by Martin P. Robillard.
*
* Copyright (C) 2019 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*******************************************************************************/
package chapter9;
import static java.util.Comparator.*;
import java.util.Comparator;
import java.util.List;
import chapter9.Suit.Color;
/**
* Implementation of a playing card. This class yields immutable objects.
* This version of the class shows an application of the Flyweight design
* pattern where the flyweight store is pre-initialized.
*/
public class Card implements Comparable<Card>
{
private static final Comparator<Card> COMPARATOR = Comparator.comparing(Card::getSuit).thenComparing(Card::getRank);
// Flyweight store
private static final Card[][] CARDS = new Card[Suit.values().length][Rank.values().length];
private final Rank aRank;
private final Suit aSuit;
// Initialization of the flyweight store
static
{
for( Suit suit : Suit.values() )
{
for( Rank rank : Rank.values() )
{
CARDS[suit.ordinal()][rank.ordinal()] = new Card(rank, suit);
}
}
}
// Private constructor
private Card( Rank pRank, Suit pSuit)
{
aRank = pRank;
aSuit = pSuit;
}
/**
* @param pRank The rank of the requested card.
* @param pSuit The suit of the requested card.
* @return The unique Card instance with pRank and pSuit
* @pre pRank != null && pSuit != null
*/
public static Card get(Rank pRank, Suit pSuit)
{
assert pRank != null && pSuit != null;
return CARDS[pSuit.ordinal()][pRank.ordinal()];
}
/**
* @return The rank of the card.
*/
public Rank getRank()
{
return aRank;
}
/**
* @return The suit of the card.
*/
public Suit getSuit()
{
return aSuit;
}
@Override
public String toString()
{
return String.format("%s of %s", aRank, aSuit);
}
public static int compareByRank(Card pCard1, Card pCard2)
{
return pCard1.getRank().compareTo(pCard2.getRank());
}
public boolean hasBlackSuit()
{
return aSuit.getColor() == Color.BLACK;
}
public boolean hasRedSuit()
{
return aSuit.getColor() == Color.RED;
}
// ********** Code samples for Section 9.3 **********
public static Comparator<Card> bySuitComparator()
{
return (card1, card2) -> card1.getSuit().compareTo(card2.getSuit());
}
public static Comparator<Card> byRankComparator()
{
return (card1, card2) -> card1.getRank().compareTo(card2.getRank());
}
/*
* This version uses basic comparison logic
*/
public static Comparator<Card> bySuitThenRankComparator1()
{
return (card1, card2) -> {
if (card1.getSuit() == card2.getSuit())
{
return card1.getRank().compareTo(card2.getRank());
}
else
{
return card1.getSuit().compareTo(card2.getSuit());
}
};
}
/* This version uses comparator factories */
public static Comparator<Card> bySuitThenRankComparator2()
{
return (card1, card2) -> {
if(bySuitComparator().compare(card1, card2) == 0)
{
return byRankComparator().compare(card1, card2);
}
else
{
return bySuitComparator().compare(card1, card2);
}
};
}
public static Comparator<Card> byRankThenSuitComparator()
{
return (card1, card2) -> {
if (byRankComparator().compare(card1, card2) == 0)
{
return bySuitComparator().compare(card1, card2);
}
else
{
return byRankComparator().compare(card1, card2);
}
};
}
/*
* This version uses the comparing method.
*/
public static Comparator<Card> byRankComparator2()
{
return Comparator.comparing(card -> card.getRank());
}
/*
* Uses comparator factories combined with thenComparing
*/
public static Comparator<Card> byRankThenSuitComparator2()
{
return byRankComparator().thenComparing(bySuitComparator());
}
/*
* Uses comparator factories combined with thenComparing
*/
public static Comparator<Card> bySuitThenRankComparator3()
{
return bySuitComparator().thenComparing(byRankComparator());
}
public static Comparator<Card> byRankComparatorReversed()
{
return (card1, card2) -> card2.getRank().compareTo(card1.getRank());
}
public static Comparator<Card> bySuitReversedThenRankComparator()
{
return bySuitComparator().reversed().thenComparing(byRankComparator());
}
public static Comparator<Card> bySuitReversedThenRankReversedComparator()
{
return bySuitComparator().reversed().thenComparing(byRankComparator().reversed());
}
public static void sampleSortingApplication1()
{
List<Card> cards = new Deck().getCards();
cards.sort(
Comparator
.comparing((Card card) -> card.getSuit())
.reversed()
.thenComparing(Comparator.comparing((Card card) -> card.getRank())
.reversed()));
}
public static void sampleSortingApplication2()
{
List<Card> cards = new Deck().getCards();
cards.sort(comparing((Card card) -> card.getSuit())
.reversed()
.thenComparing(comparing((Card card) -> card.getRank())
.reversed()));
}
public static void sampleSortingApplication3()
{
List<Card> cards = new Deck().getCards();
cards.sort(comparing(Card::getSuit)
.reversed()
.thenComparing(comparing(Card::getRank)
.reversed()));
}
public static void sampleSortingApplication4()
{
List<Card> cards = new Deck().getCards();
cards.sort(comparing(Card::getSuit)
.thenComparing(Card::getRank).reversed());
}
public boolean isFaceCard()
{
return getRank().ordinal() >= Rank.JACK.ordinal();
}
@Override
public int compareTo(Card o)
{
return COMPARATOR.compare(this, o);
}
}