public class CardCollection extends java.lang.Object implements java.lang.Iterable<Card>
A collection of cards that may represent a full deck, a player's hand or
the cards on the table, for instance. As the name suggests, it contains a
collection of Card
and their order is relevant. This class
provides several methods to operate on card collections.
This class can be instantiated but instances can also be obtained using
static methods such as getDeck()
to create a
collections with cards of all suits and values.
Each instance has a CardComparator
that is used if a particular
card order needs to be considered such as in getHighestCard()
.
If a comparator is not not provided then DefaultCardComparator
is used.
Methods with a get
prefix simply return a card without changing
the collection. Methods with a take
prefix remove a card from the
collection. If a collection is empty, methods that would return a card
will return null
instead.
Several methods return a CardCollection
to enable chaining,
including the static methods for generating a deck. For instance:
CardCollection allSpades = CardCollection.getDeck().getAllCardsWithSuit(CardSuit.SPADE); Card lowestHeartonHand = hand.getAllCardsWithSuit(CardSuit.HEARTS).getLowestCard();
In card collections the order of cards is relevant and they may hold
repeated cards. If needed, the removeRepeated()
method removes
duplicates and returns the collections itself for possible chaining;
jpleal@fc.up.pt
Modifier and Type | Field and Description |
---|---|
(package private) java.util.LinkedList<Card> |
cards |
(package private) CardComparator |
comparator |
Constructor and Description |
---|
CardCollection()
An empty instance backed by the
DefaultCardComparator . |
CardCollection(CardComparator comparator)
An empty instance backed by the given
CardComparator . |
CardCollection(CardComparator comparator,
java.util.List<Card> cards)
An instance backed by the given
CardComparator
and populated with given list of cards. |
CardCollection(java.util.List<Card> cards)
An instance backed by the by the
DefaultCardComparator
and populated with given list of cards. |
Modifier and Type | Method and Description |
---|---|
CardCollection |
addAllCards(java.util.List<Card> cards)
Add a list of cards to this collection.
|
CardCollection |
addCard(Card card)
Add a single card to this collection.
|
CardCollection |
addCard(CardSuit suit,
CardValue value)
Convenience methods to add card given suit and value.
|
CardCollection |
addCardCollection(CardCollection collection)
Add another collection to this one.
|
java.util.List<Card> |
asList()
Return cards as a new list of cards.
|
CardCollection |
clearCards()
Clear all cards from this collection
|
boolean |
containsCard(Card card)
Collection contains this card?
|
boolean |
equals(java.lang.Object obj) |
CardComparator |
getCardComparator()
Card comparator used by this card collection.
|
CardCollection |
getCardsFromSuit(CardSuit suit)
New collection with same comparator only with cards from given suit.
|
CardCollection |
getCardsLargerThan(Card limit)
New collection with same comparator
and only with cards larger than given card
|
CardCollection |
getCardsNotFromSuit(CardSuit suit)
New collection with same comparator only with cards other than given suit.
|
CardCollection |
getCardsSmallerThan(Card limit)
New collection with same comparator
and only with cards smaller than given card
|
CardCollection |
getCardsWithValue(CardValue value)
New collection with same comparator and the cards
with given value from any suit.
|
static CardCollection |
getDeck()
A
CardCollection with a deck cards of all suits and values,
backed by the default card comparator |
static CardCollection |
getDeck(CardComparator comparator)
A {code CardCollection} with a deck of cards of all suits and values
with given card comparator
|
Card |
getFirstCard()
Get first card from collection.
|
static CardCollection |
getFullDeck()
A
CardCollection with a full deck of cards, including 2 jokers,
backed by the default card comparator |
static CardCollection |
getFullDeck(CardComparator comparator)
A
CardCollection with a full deck of cards, including 2 jokers,
backed by the given card comparator |
Card |
getHighestCard()
Get the highest card using this collection comparator
If this collection is empty returns
null . |
CardCollection |
getHighestValueCards()
New collection with same comparator and the cards
with the highest value from any suit.
|
Card |
getLastCard()
Get first card from collection
If this collection is empty returns
null . |
Card |
getLowestCard()
Get the lowest card using this collection comparator.
|
CardCollection |
getLowestValueCards()
New collection with same comparator and the cards
with the lowest value from any suit.
|
Card |
getRandomCard()
A random card from this collection.
|
int |
hashCode() |
boolean |
isEmpty()
Is this card collection is empty?
|
java.util.Iterator<Card> |
iterator()
An iterator over the cards in this collection.
|
CardCollection |
removeRepeated()
Removes duplicates in this collection.
|
CardCollection |
shuffle()
Shuffle the cards in this collection.
|
int |
size()
Number of cards in this collection.
|
Card |
takeCard(Card card)
Take a specific card and return it,
if collection has at least one card; otherwise return
null . |
Card |
takeFirstCard()
Take the first card and return it,
if collection has at least one card.
|
CardCollection |
takeFirstCards(int count)
Take given count of cards from the beginning of this collection.
|
Card |
takeLastCard()
Take the last card and return it,
if collection has at least one card; otherwise return
null . |
java.lang.String |
toString() |
java.util.LinkedList<Card> cards
CardComparator comparator
public CardCollection()
DefaultCardComparator
.public CardCollection(java.util.List<Card> cards)
DefaultCardComparator
and populated with given list of cards.cards
- to populate this collection.public CardCollection(CardComparator comparator)
CardComparator
.comparator
- of cards.public CardCollection(CardComparator comparator, java.util.List<Card> cards)
CardComparator
and populated with given list of cards.comparator
- or cards.cards
- to populate this collection.public static CardCollection getFullDeck()
CardCollection
with a full deck of cards, including 2 jokers,
backed by the default card comparatorpublic static CardCollection getFullDeck(CardComparator comparator)
CardCollection
with a full deck of cards, including 2 jokers,
backed by the given card comparatorcomparator
- of cards.public static CardCollection getDeck()
CardCollection
with a deck cards of all suits and values,
backed by the default card comparatorpublic static CardCollection getDeck(CardComparator comparator)
comparator
- of cardspublic CardComparator getCardComparator()
public java.util.List<Card> asList()
public CardCollection shuffle()
public CardCollection removeRepeated()
public boolean isEmpty()
true
if empty; false
otherwise.public CardCollection clearCards()
public int size()
public CardCollection addCard(Card card)
card
- to add.public CardCollection addCard(CardSuit suit, CardValue value)
suit
- of cardvalue
- of cardpublic CardCollection addAllCards(java.util.List<Card> cards)
cards
- to add.public CardCollection addCardCollection(CardCollection collection)
collection
- to add.public boolean containsCard(Card card)
card
- to check.true
if card is contained and false
otherwise.public Card getFirstCard()
null
.null
.public Card getLastCard()
null
.null
.public CardCollection getCardsFromSuit(CardSuit suit)
suit
- on interestpublic CardCollection getCardsNotFromSuit(CardSuit suit)
suit
- of interestpublic CardCollection getCardsLargerThan(Card limit)
limit
- cardpublic CardCollection getCardsSmallerThan(Card limit)
limit
- cardpublic CardCollection getHighestValueCards()
public CardCollection getLowestValueCards()
public CardCollection getCardsWithValue(CardValue value)
value
- of card (e.g. ACE).public Card getHighestCard()
null
.null
.Collections.max(Collection,Comparator)
public Card getLowestCard()
null
.null
.Collections.min(Collection,Comparator)
.public Card getRandomCard()
null
.null
.public Card takeFirstCard()
null
.
The returned card is removed from the collection.null
.public Card takeLastCard()
null
.
The returned card is removed from the collection.null
.public CardCollection takeFirstCards(int count)
count
- of cards to take.public Card takeCard(Card card)
null
.
The returned card is removed from the collection.card
- to takenull
public java.util.Iterator<Card> iterator()
iterator
in interface java.lang.Iterable<Card>
public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(java.lang.Object obj)
equals
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object