From 505e3dfc41d3191cf48fc201b2dac636ca9ca59a Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sat, 26 Mar 2016 22:07:50 +0100 Subject: Tests should pass now --- .../cardstorage/ReferenceCardContainer.java | 30 ++++++++++++++++++---- .../cardstorage/StandaloneCardContainer.java | 19 +++++++++----- 2 files changed, 38 insertions(+), 11 deletions(-) (limited to 'src/eu/equalparts/cardbase/cardstorage') diff --git a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java index ccd5508..f265d24 100644 --- a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java +++ b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java @@ -7,19 +7,34 @@ import com.fasterxml.jackson.annotation.JsonProperty; import eu.equalparts.cardbase.cards.Card; +/** + * A class which holds card counts by reference (hash). + * + * @author Eduardo Pedroni + * + */ public abstract class ReferenceCardContainer { - @JsonProperty private Map cardReferences; - - public ReferenceCardContainer() { - cardReferences = new HashMap<>(); - } + /** + * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) and card amounts as entry values. + */ + @JsonProperty private Map cardReferences = new HashMap<>(); + /** + * Returns the amount of the specified card. If the card is not present at all, return 0. + * + * @param cardToCount a card whose count is to be returned. + * @return the count of the returned card in the container. + */ public int getCount(Card cardToCount) { int hashCode = cardToCount.hashCode(); return cardReferences.containsKey(hashCode) ? cardReferences.get(hashCode) : 0; } + /** + * @param cardToAdd the card to add the container. + * @param count the amount to add. + */ public void addCard(Card cardToAdd, int count) { int hashCode = cardToAdd.hashCode(); if (cardReferences.containsKey(hashCode)) { @@ -29,6 +44,11 @@ public abstract class ReferenceCardContainer { } } + /** + * @param cardToRemove the card to remove from the container. + * @param count the amount to remove. + * @return the amount that was effectively removed. Could be less than {@code count} depending on how many of the card were present. + */ public int removeCard(Card cardToRemove, int count) { int hashCode = cardToRemove.hashCode(); int removed = 0; diff --git a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java index 52fc89e..6c785df 100644 --- a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java +++ b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java @@ -12,14 +12,19 @@ import com.fasterxml.jackson.annotation.JsonProperty; import eu.equalparts.cardbase.cards.Card; import eu.equalparts.cardbase.comparator.CardComparator; +/** + * Holds actual card data in addition to the card counts in {@code ReferenceCardContainer}. + * + * @author Eduardo Pedroni + * + */ public abstract class StandaloneCardContainer extends ReferenceCardContainer { - @JsonProperty private Map cardData; - - public StandaloneCardContainer() { - super(); - cardData = new HashMap<>(); - } + /** + * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) + * and card objects as entry values. + */ + @JsonProperty private Map cardData = new HashMap<>(); @Override public void addCard(Card cardToAdd, int count) { @@ -30,9 +35,11 @@ public abstract class StandaloneCardContainer extends ReferenceCardContainer { @Override public int removeCard(Card cardToRemove, int count) { int removed = super.removeCard(cardToRemove, count); + if (getCount(cardToRemove) <= 0) { cardData.remove(cardToRemove.hashCode()); } + return removed; } -- cgit v1.2.3