aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/data
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2015-06-06 19:23:39 +0200
committerEduardo Pedroni <e.pedroni91@gmail.com>2015-06-06 19:23:39 +0200
commit1b5b8f11d0dd33dc41c5c5a6841307fbb392f4c7 (patch)
treed1442ad0b529a212a1eda5bbc781f15f85aacc2d /src/eu/equalparts/cardbase/data
parent8059c378fa11b835beb813e73ea6c57ddeca8553 (diff)
Squashed a few minor bugs, refactored CardbaseManager a bit
Diffstat (limited to 'src/eu/equalparts/cardbase/data')
-rw-r--r--src/eu/equalparts/cardbase/data/CardSet.java27
-rw-r--r--src/eu/equalparts/cardbase/data/CardbaseManager.java68
-rw-r--r--src/eu/equalparts/cardbase/data/Deck.java3
-rw-r--r--src/eu/equalparts/cardbase/data/FullCardSet.java46
4 files changed, 124 insertions, 20 deletions
diff --git a/src/eu/equalparts/cardbase/data/CardSet.java b/src/eu/equalparts/cardbase/data/CardSet.java
index 959e0a3..b9720bf 100644
--- a/src/eu/equalparts/cardbase/data/CardSet.java
+++ b/src/eu/equalparts/cardbase/data/CardSet.java
@@ -2,10 +2,31 @@ package eu.equalparts.cardbase.data;
public class CardSet {
- public String name = "";
- public String code = "";
- public String releaseDate = "";
+ private String name = "";
+ private String code = "";
+ private String releaseDate = "";
+ /**
+ * @return the name
+ */
+ public String getName() {
+ return name;
+ }
+
+ /**
+ * @return the code
+ */
+ public String getCode() {
+ return code;
+ }
+
+ /**
+ * @return the releaseDate
+ */
+ public String getReleaseDate() {
+ return releaseDate;
+ }
+
@Override
public String toString() {
return String.format("%1$-12s : %2$s", code, name, releaseDate);
diff --git a/src/eu/equalparts/cardbase/data/CardbaseManager.java b/src/eu/equalparts/cardbase/data/CardbaseManager.java
index e6a4f97..dfef3c8 100644
--- a/src/eu/equalparts/cardbase/data/CardbaseManager.java
+++ b/src/eu/equalparts/cardbase/data/CardbaseManager.java
@@ -2,19 +2,30 @@ package eu.equalparts.cardbase.data;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.Iterator;
+import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
-import eu.equalparts.cardbase.query.IO;
+import eu.equalparts.cardbase.io.IO;
public class CardbaseManager {
-
+
private ArrayList<CardSet> cardSets;
- public Cardbase cardbase;
/**
+ * A cache of CardSets to avoid querying the server many times for the same information.
+ */
+ private HashMap<String, FullCardSet> cardSetCache = new HashMap<String, FullCardSet>();
+ /**
+ *
+ */
+ private Cardbase cardbase;
+
+
+ /**
* Parse a cardbase file and create an associated Cardbase object.
*
* @param cardbaseFile
@@ -39,12 +50,46 @@ public class CardbaseManager {
cardSets = IO.getCardSetList();
cardbase = new Cardbase();
}
-
+
public ArrayList<CardSet> getCardSetList() {
return cardSets;
}
+
+ public void writeCardbase(File outputFile) throws JsonGenerationException, JsonMappingException, IOException {
+ IO.writeCardbase(outputFile, cardbase);
+ }
/**
+ * Returns the specified set in the form of a {@code FullCardSet} object.
+ *
+ * @param code the code of the set to be returned.
+ * @return the requested {@code FullCardSet} or null if no set matches the given code.
+ *
+ * @throws JsonParseException if the upstream JSON is not formatted correctly.
+ * @throws JsonMappingException if the upstream JSON does not map to {@code FullCardSet}.
+ * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
+ */
+ public FullCardSet getFullCardSet(String code) throws JsonParseException, JsonMappingException, IOException {
+ FullCardSet requestedSet = null;
+ for (CardSet cardSet : cardSets) {
+ if (cardSet.getCode().equalsIgnoreCase(code)) {
+ // if the set is cached, no need to fetch
+ if (cardSetCache.containsKey(cardSet.getCode())) {
+ requestedSet = cardSetCache.get(cardSet.getCode());
+ }
+ // not cached; fetch, cache and return it
+ else {
+ requestedSet = IO.getFullCardSet(cardSet.getCode());
+ cardSetCache.put(cardSet.getCode(), requestedSet);
+ }
+ return requestedSet;
+ }
+ }
+ // not found
+ return null;
+ }
+
+ /**
* Add a specific amount of a card to the cardbase.
* If the card is not already in the cardbase, it is added.
* If it is already present, the count is simply updated.
@@ -70,20 +115,25 @@ public class CardbaseManager {
* If that amount is equal to or exceeds the count already in the cardbase,
* the card entry is removed altogether.
*
- * @param remove
+ * @param cardToRemove
* @param count
+ * @return the number of cards actually removed.
*/
- public void removeCard(Card remove, Integer count) {
- Card card = cardbase.getCardByNumber(remove.setCode, remove.number);
+ public Integer removeCard(Card cardToRemove, Integer count) {
+ Card card = cardbase.getCardByNumber(cardToRemove.setCode, cardToRemove.number);
+ Integer removed = 0;
if (card != null) {
if (card.count <= count) {
cardbase.cards.remove(card);
+ removed = card.count;
} else {
card.count -= count;
+ removed = count;
}
- }
+ }
+ return removed;
}
-
+
/**
* @return an iterator to the cards in the cardbase.
*/
diff --git a/src/eu/equalparts/cardbase/data/Deck.java b/src/eu/equalparts/cardbase/data/Deck.java
index b5fe402..8a9ed12 100644
--- a/src/eu/equalparts/cardbase/data/Deck.java
+++ b/src/eu/equalparts/cardbase/data/Deck.java
@@ -9,7 +9,4 @@ public class Deck {
*/
public HashMap<Integer, Integer> cards;
- public Deck() {
-
- }
}
diff --git a/src/eu/equalparts/cardbase/data/FullCardSet.java b/src/eu/equalparts/cardbase/data/FullCardSet.java
index 0b5c099..48488b5 100644
--- a/src/eu/equalparts/cardbase/data/FullCardSet.java
+++ b/src/eu/equalparts/cardbase/data/FullCardSet.java
@@ -3,11 +3,47 @@ package eu.equalparts.cardbase.data;
import java.util.ArrayList;
public class FullCardSet extends CardSet {
- public String border;
- public String type;
- public String block;
- public String gathererCode;
- public ArrayList<Card> cards;
+
+ private String border;
+ private String type;
+ private String block;
+ private String gathererCode;
+ private ArrayList<Card> cards;
+
+ /**
+ * @return the border
+ */
+ public String getBorder() {
+ return border;
+ }
+
+ /**
+ * @return the type
+ */
+ public String getType() {
+ return type;
+ }
+
+ /**
+ * @return the block
+ */
+ public String getBlock() {
+ return block;
+ }
+
+ /**
+ * @return the gathererCode
+ */
+ public String getGathererCode() {
+ return gathererCode;
+ }
+
+ /**
+ * @return the cards
+ */
+ public ArrayList<Card> getCards() {
+ return cards;
+ }
/**
* Searches for a card by number (the one shown on the card itself).