aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2016-03-16 18:51:55 +0100
committerEduardo Pedroni <e.pedroni91@gmail.com>2016-03-16 18:51:55 +0100
commit6d561e2d162ba6b43d3f12df0643b83ec72148df (patch)
treea559bf353151102f786c7916d6675baa1faaaf0c /src
parentf70899a487cf0cdc58b8f4e0efea98e8404766c8 (diff)
Sorted out reading and writing cardbases in the new format, now need to convert existing files
Diffstat (limited to 'src')
-rw-r--r--src/eu/equalparts/cardbase/Cardbase.java84
-rw-r--r--src/eu/equalparts/cardbase/DataContainer.java5
-rw-r--r--src/eu/equalparts/cardbase/utils/MTGUniverse.java2
3 files changed, 48 insertions, 43 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java
index 075f7f3..cfb5418 100644
--- a/src/eu/equalparts/cardbase/Cardbase.java
+++ b/src/eu/equalparts/cardbase/Cardbase.java
@@ -11,6 +11,7 @@ import java.util.Map;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
@@ -27,19 +28,30 @@ import eu.equalparts.cardbase.utils.JSON;
*/
public class Cardbase {
- /**
- * The cards in the cardbase, set in key-value pairs where the key is the card hash,
- * generated using {makeHash()}.
- */
- private Map<Integer, Card> cards;
- /**
- * TODO comment
- */
- private Map<Integer, Integer> collection;
- /**
- * The decks which have been saved along with this collection of cards.
- */
- private Map<String, ReferenceDeck> decks;
+ private static class DataContainer {
+ /**
+ * The cards in the cardbase, set in key-value pairs where the key is the card hash,
+ * generated using {makeHash()}.
+ */
+ public Map<Integer, Card> cards;
+ /**
+ * TODO comment
+ */
+ public Map<Integer, Integer> collection;
+ /**
+ * The decks which have been saved along with this collection of cards.
+ */
+ public Map<String, ReferenceDeck> decks;
+
+ public DataContainer() {
+ cards = new HashMap<Integer, Card>();
+ collection = new HashMap<Integer, Integer>();
+ decks = new HashMap<String, ReferenceDeck>();
+ }
+ }
+
+ private DataContainer dataContainer;
+
/**
* Debug flag is raised when the DEBUG environment variable is set. This causes additional
* information to be printed to the console.
@@ -56,24 +68,16 @@ public class Cardbase {
* @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
*/
public Cardbase(File cardbaseFile) throws JsonParseException, JsonMappingException, IOException {
- initialise();
- parseCardbase(JSON.mapper.readValue(cardbaseFile, JsonNode.class));
-
+ dataContainer = JSON.mapper.readValue(cardbaseFile, DataContainer.class);
}
/**
* Initialises a clean cardbase.
*/
public Cardbase() {
- initialise();
+ dataContainer = new DataContainer();
}
- private void initialise() {
- cards = new HashMap<Integer, Card>();
- collection = new HashMap<Integer, Integer>();
- decks = new HashMap<String, ReferenceDeck>();
- }
-
/**
* Writes the provided {@code Cardbase} to the provided file in JSON format.
*
@@ -85,7 +89,7 @@ public class Cardbase {
* @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
*/
public void writeCollection(File outputFile) throws JsonGenerationException, JsonMappingException, IOException {
- JSON.mapper.writeValue(outputFile, cards);
+ JSON.mapper.writeValue(outputFile, dataContainer);
}
/**
@@ -102,14 +106,14 @@ public class Cardbase {
Integer hashCode = cardToAdd.hashCode();
// ensure that card is in the card map
- cards.putIfAbsent(hashCode, cardToAdd);
+ dataContainer.cards.putIfAbsent(hashCode, cardToAdd);
// ensure that card is in the collection, with the correct count
- Integer currentCount = collection.get(hashCode);
+ Integer currentCount = dataContainer.collection.get(hashCode);
if (currentCount != null) {
- collection.replace(hashCode, currentCount + addCount);
+ dataContainer.collection.replace(hashCode, currentCount + addCount);
} else {
- collection.put(hashCode, addCount);
+ dataContainer.collection.put(hashCode, addCount);
}
}
@@ -134,14 +138,14 @@ public class Cardbase {
Integer hashCode = cardToRemove.hashCode();
int removed = 0;
- Integer currentCount = collection.get(hashCode);
+ Integer currentCount = dataContainer.collection.get(hashCode);
if (currentCount != null) {
if (removeCount >= currentCount) {
- collection.remove(hashCode);
- cards.remove(hashCode);
+ dataContainer.collection.remove(hashCode);
+ dataContainer.cards.remove(hashCode);
removed = currentCount;
} else {
- collection.replace(hashCode, currentCount - removeCount);
+ dataContainer.collection.replace(hashCode, currentCount - removeCount);
removed = removeCount;
}
}
@@ -157,7 +161,7 @@ public class Cardbase {
* @return the requested {@code Card} or null if no card is found.
*/
public Card getCard(String setCode, String number) {
- return cards.get(Card.makeHash(setCode, number));
+ return dataContainer.cards.get(Card.makeHash(setCode, number));
}
/**
@@ -169,7 +173,7 @@ public class Cardbase {
* @return the requested {@code Card} or null if no card is found.
*/
protected Card getCardByHash(Integer hash) {
- return cards.get(hash);
+ return dataContainer.cards.get(hash);
}
/**
@@ -181,7 +185,7 @@ public class Cardbase {
* @return an unmodifiable list of all the cards in the cardbase.
*/
public Collection<Card> getCards() {
- return Collections.unmodifiableCollection(cards.values());
+ return Collections.unmodifiableCollection(dataContainer.cards.values());
}
/**
@@ -190,24 +194,20 @@ public class Cardbase {
* @throws NoSuchFieldException if the field provided is invalid.
*/
public Collection<Card> sort(String field) throws NoSuchFieldException {
- List<Card> sortedCards = new ArrayList<Card>(cards.values());
+ List<Card> sortedCards = new ArrayList<Card>(dataContainer.cards.values());
sortedCards.sort(new CardComparator(Card.class.getDeclaredField(field)));
return Collections.unmodifiableCollection(sortedCards);
}
public Map<String, ReferenceDeck> getDecks() {
- return Collections.unmodifiableMap(decks);
+ return Collections.unmodifiableMap(dataContainer.decks);
}
public int getCount(Card card) {
- Integer count = collection.get(Card.makeHash(card.setCode, card.number));
+ Integer count = dataContainer.collection.get(Card.makeHash(card.setCode, card.number));
return count != null ? count : 0;
}
- private void parseCardbase(JsonNode jsonTree) {
-
- }
-
// public List<Card> getMissingCards(StandaloneDeck deckToCheck) {
// List<Card> missingCards = new ArrayList<Card>();
// for (Card card : deckToCheck.cards) {
diff --git a/src/eu/equalparts/cardbase/DataContainer.java b/src/eu/equalparts/cardbase/DataContainer.java
new file mode 100644
index 0000000..892c76b
--- /dev/null
+++ b/src/eu/equalparts/cardbase/DataContainer.java
@@ -0,0 +1,5 @@
+package eu.equalparts.cardbase;
+
+public class DataContainer {
+
+}
diff --git a/src/eu/equalparts/cardbase/utils/MTGUniverse.java b/src/eu/equalparts/cardbase/utils/MTGUniverse.java
index 2e33eff..6acb092 100644
--- a/src/eu/equalparts/cardbase/utils/MTGUniverse.java
+++ b/src/eu/equalparts/cardbase/utils/MTGUniverse.java
@@ -98,7 +98,7 @@ public final class MTGUniverse {
}
// not cached; fetch and cache
else {
- requestedSet = parseFullSet(JSON.mapper.readValue(new URL(BASE_DATA_URL + validCode + ".json"), JsonNode.class));
+ requestedSet = parseFullSet(JSON.mapper.readTree(new URL(BASE_DATA_URL + validCode + ".json")));
cardSetCache.put(validCode, requestedSet);
}
}