aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/cardstorage
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2016-03-26 22:07:50 +0100
committerEduardo Pedroni <e.pedroni91@gmail.com>2016-03-26 22:07:50 +0100
commit505e3dfc41d3191cf48fc201b2dac636ca9ca59a (patch)
tree907d9f59cfee165c9ec5878e75a38ac0135a9c28 /src/eu/equalparts/cardbase/cardstorage
parent0e2391574b26b66f9d1fa0f04c30dc50127aa457 (diff)
Tests should pass now
Diffstat (limited to 'src/eu/equalparts/cardbase/cardstorage')
-rw-r--r--src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java30
-rw-r--r--src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java19
2 files changed, 38 insertions, 11 deletions
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<Integer, Integer> 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<Integer, Integer> 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<Integer, Card> 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<Integer, Card> 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;
}