From 6d561e2d162ba6b43d3f12df0643b83ec72148df Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Wed, 16 Mar 2016 18:51:55 +0100 Subject: Sorted out reading and writing cardbases in the new format, now need to convert existing files --- src/eu/equalparts/cardbase/Cardbase.java | 84 +++++++++++------------ src/eu/equalparts/cardbase/DataContainer.java | 5 ++ src/eu/equalparts/cardbase/utils/MTGUniverse.java | 2 +- 3 files changed, 48 insertions(+), 43 deletions(-) create mode 100644 src/eu/equalparts/cardbase/DataContainer.java 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 cards; - /** - * TODO comment - */ - private Map collection; - /** - * The decks which have been saved along with this collection of cards. - */ - private Map 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 cards; + /** + * TODO comment + */ + public Map collection; + /** + * The decks which have been saved along with this collection of cards. + */ + public Map decks; + + public DataContainer() { + cards = new HashMap(); + collection = new HashMap(); + decks = new HashMap(); + } + } + + 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(); - collection = new HashMap(); - decks = new HashMap(); - } - /** * 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 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 sort(String field) throws NoSuchFieldException { - List sortedCards = new ArrayList(cards.values()); + List sortedCards = new ArrayList(dataContainer.cards.values()); sortedCards.sort(new CardComparator(Card.class.getDeclaredField(field))); return Collections.unmodifiableCollection(sortedCards); } public Map 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 getMissingCards(StandaloneDeck deckToCheck) { // List missingCards = new ArrayList(); // 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); } } -- cgit v1.2.3