aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/cardstorage
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2016-03-26 14:16:42 +0100
committerEduardo Pedroni <e.pedroni91@gmail.com>2016-03-26 14:16:42 +0100
commit19800a0886d51220f28f85f3845c74e7f46500e3 (patch)
tree73ec9bc1dee88e0fac83c99c3bd98b07a9904379 /src/eu/equalparts/cardbase/cardstorage
parent5a9327b4721d50cee954369eaf25d096ac8b9c52 (diff)
Refactored storage hierarchy, now need to look into decks again
Diffstat (limited to 'src/eu/equalparts/cardbase/cardstorage')
-rw-r--r--src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java50
-rw-r--r--src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java73
2 files changed, 123 insertions, 0 deletions
diff --git a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
new file mode 100644
index 0000000..ccd5508
--- /dev/null
+++ b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
@@ -0,0 +1,50 @@
+package eu.equalparts.cardbase.cardstorage;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import eu.equalparts.cardbase.cards.Card;
+
+public abstract class ReferenceCardContainer {
+
+ @JsonProperty private Map<Integer, Integer> cardReferences;
+
+ public ReferenceCardContainer() {
+ cardReferences = new HashMap<>();
+ }
+
+ public int getCount(Card cardToCount) {
+ int hashCode = cardToCount.hashCode();
+ return cardReferences.containsKey(hashCode) ? cardReferences.get(hashCode) : 0;
+ }
+
+ public void addCard(Card cardToAdd, int count) {
+ int hashCode = cardToAdd.hashCode();
+ if (cardReferences.containsKey(hashCode)) {
+ cardReferences.replace(hashCode, cardReferences.get(hashCode) + count);
+ } else {
+ cardReferences.put(hashCode, count);
+ }
+ }
+
+ public int removeCard(Card cardToRemove, int count) {
+ int hashCode = cardToRemove.hashCode();
+ int removed = 0;
+
+ if (cardReferences.containsKey(hashCode) && count > 0) {
+ int oldCount = cardReferences.get(hashCode);
+
+ if (oldCount > count) {
+ cardReferences.replace(hashCode, oldCount - count);
+ removed = count;
+ } else {
+ cardReferences.remove(hashCode);
+ removed = oldCount;
+ }
+ }
+
+ return removed;
+ }
+}
diff --git a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java
new file mode 100644
index 0000000..52fc89e
--- /dev/null
+++ b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java
@@ -0,0 +1,73 @@
+package eu.equalparts.cardbase.cardstorage;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+import eu.equalparts.cardbase.cards.Card;
+import eu.equalparts.cardbase.comparator.CardComparator;
+
+public abstract class StandaloneCardContainer extends ReferenceCardContainer {
+
+ @JsonProperty private Map<Integer, Card> cardData;
+
+ public StandaloneCardContainer() {
+ super();
+ cardData = new HashMap<>();
+ }
+
+ @Override
+ public void addCard(Card cardToAdd, int count) {
+ super.addCard(cardToAdd, count);
+ cardData.putIfAbsent(cardToAdd.hashCode(), cardToAdd);
+ }
+
+ @Override
+ public int removeCard(Card cardToRemove, int count) {
+ int removed = super.removeCard(cardToRemove, count);
+ if (getCount(cardToRemove) <= 0) {
+ cardData.remove(cardToRemove.hashCode());
+ }
+ return removed;
+ }
+
+ /**
+ * Returns a card from the cardbase by set code and number.
+ * If no such card is in the cardbase, returns null.
+ *
+ * @param setCode the set to which the requested card belongs.
+ * @param number the requested card's set number.
+ * @return the requested {@code Card} or null if no card is found.
+ */
+ public Card getCard(String setCode, String number) {
+ return cardData.get(Card.makeHash(setCode, number));
+ }
+
+ /**
+ * This method is intended to allow iteration directly on the list of cards,
+ * while at the same time retaining control over the insert and remove procedures.
+ * The returned {@code List} is a read-only; trying to modify its structure will
+ * result in an {@code UnsupportedOperationException}.
+ *
+ * @return an unmodifiable list of all the cards in the cardbase.
+ */
+ public Collection<Card> getCards() {
+ return Collections.unmodifiableCollection(cardData.values());
+ }
+
+ /**
+ * @param fieldName the name of the field by which to sort.
+ * @return an unmodifiable collection representing the cardbase sorted in the required order.
+ * @throws NoSuchFieldException if the field provided is invalid.
+ */
+ public Collection<Card> sortByField(String fieldName) throws NoSuchFieldException {
+ List<Card> sortedCards = new ArrayList<Card>(getCards());
+ sortedCards.sort(new CardComparator(Card.class.getDeclaredField(fieldName)));
+ return Collections.unmodifiableCollection(sortedCards);
+ }
+}