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 --- src/eu/equalparts/cardbase/Cardbase.java | 8 +++--- .../cardstorage/ReferenceCardContainer.java | 30 ++++++++++++++++++---- .../cardstorage/StandaloneCardContainer.java | 19 +++++++++----- 3 files changed, 43 insertions(+), 14 deletions(-) (limited to 'src/eu/equalparts/cardbase') diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index e42a105..41b7781 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -22,23 +22,25 @@ public class Cardbase extends StandaloneCardContainer { /** * The decks which have been saved along with this collection of cards. */ - @JsonProperty private Map decks; + @JsonProperty private Map decks; /** - * Initialises the cardbase with the contents of a file. + * Creates and returns a cardbase with the contents of a file. * * @param cardbaseFile the cardbase JSON to load. * * @throws JsonParseException if the specified file does not contain valid JSON. * @throws JsonMappingException if the specified file structure does not match that of {@code Cardbase}. * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs. + * + * @return the initialised {@code Cardbase} object. */ public static Cardbase load(File cardbaseFile) throws JsonParseException, JsonMappingException, IOException { return JSON.mapper.readValue(cardbaseFile, Cardbase.class); } /** - * Writes the provided {@code Cardbase} to the provided file in JSON format. + * Writes the {@code Cardbase} instance to the provided file in JSON format. * * @param file the file to which to write the {@code Cardbase}. * @param cardbase the {@code Cardbase} to write out. 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