From 4816a489e476c324155fa1f4e8adfe30867a766c Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sat, 6 Jun 2015 22:12:41 +0200 Subject: Bombproofed the CLI a bit more, tidied up the underlying API, now thinking about decks --- src/eu/equalparts/cardbase/data/CardSet.java | 13 +-- src/eu/equalparts/cardbase/data/Cardbase.java | 6 +- .../equalparts/cardbase/data/CardbaseManager.java | 129 ++++++++------------- src/eu/equalparts/cardbase/data/FullCardSet.java | 12 +- 4 files changed, 65 insertions(+), 95 deletions(-) (limited to 'src/eu/equalparts/cardbase/data') diff --git a/src/eu/equalparts/cardbase/data/CardSet.java b/src/eu/equalparts/cardbase/data/CardSet.java index b9720bf..b06be7c 100644 --- a/src/eu/equalparts/cardbase/data/CardSet.java +++ b/src/eu/equalparts/cardbase/data/CardSet.java @@ -2,26 +2,26 @@ package eu.equalparts.cardbase.data; public class CardSet { - private String name = ""; - private String code = ""; - private String releaseDate = ""; + private String name; + private String code; + private String releaseDate; /** - * @return the name + * @return the set's name. */ public String getName() { return name; } /** - * @return the code + * @return the set code. */ public String getCode() { return code; } /** - * @return the releaseDate + * @return the set's release date. */ public String getReleaseDate() { return releaseDate; @@ -31,5 +31,4 @@ public class CardSet { public String toString() { return String.format("%1$-12s : %2$s", code, name, releaseDate); } - } diff --git a/src/eu/equalparts/cardbase/data/Cardbase.java b/src/eu/equalparts/cardbase/data/Cardbase.java index 1c2aa12..021fac2 100644 --- a/src/eu/equalparts/cardbase/data/Cardbase.java +++ b/src/eu/equalparts/cardbase/data/Cardbase.java @@ -8,9 +8,9 @@ public class Cardbase { public ArrayList decks = new ArrayList<>(); /** - * @param setCode - * @param number - * @return the card if found, else 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 getCardByNumber(String setCode, String number) { for (Card card : cards) { diff --git a/src/eu/equalparts/cardbase/data/CardbaseManager.java b/src/eu/equalparts/cardbase/data/CardbaseManager.java index dfef3c8..d6ba6ee 100644 --- a/src/eu/equalparts/cardbase/data/CardbaseManager.java +++ b/src/eu/equalparts/cardbase/data/CardbaseManager.java @@ -1,122 +1,93 @@ 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.io.IO; +import eu.equalparts.cardbase.utils.IO; +/** + * Provides a variety of utility methods to interact with the loaded cardbase. + * + * @author Eduardo Pedroni + */ public class CardbaseManager { - private ArrayList cardSets; - - /** - * A cache of CardSets to avoid querying the server many times for the same information. - */ - private HashMap cardSetCache = new HashMap(); /** - * + * The cardbase being managed. */ private Cardbase cardbase; - /** - * Parse a cardbase file and create an associated Cardbase object. - * - * @param cardbaseFile + * Creates an empty cardbase. * - * @throws JsonParseException if underlying input contains invalid content of type JsonParser supports (JSON for default case). - * @throws JsonMappingException if the input JSON structure does not match structure expected for result type (or has other mismatch issues). - * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs. */ - public CardbaseManager(File cardbaseFile) throws JsonParseException, JsonMappingException, IOException { - cardSets = IO.getCardSetList(); - cardbase = IO.readCardbase(cardbaseFile); + public CardbaseManager() { + cardbase = new Cardbase(); } - + /** - * Create an empty Cardbase. + * Initialises the cardbase with the contents of a file. + * + * @param cardbaseFile the cardbase JSON to load. * - * @throws JsonParseException if underlying input contains invalid content of type JsonParser supports (JSON for default case). - * @throws JsonMappingException if the input JSON structure does not match structure expected for result type (or has other mismatch issues). + * @throws JsonParseException if the specified file does not contain valid JSON. + * @throws JsonMappingException if the specified file structure does not match that of {@code Cardbase}. * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs. */ - public CardbaseManager() throws JsonParseException, JsonMappingException, IOException { - cardSets = IO.getCardSetList(); - cardbase = new Cardbase(); - } - - public ArrayList getCardSetList() { - return cardSets; + public CardbaseManager(File cardbaseFile) throws JsonParseException, JsonMappingException, IOException { + cardbase = IO.jsonMapper.readValue(cardbaseFile, Cardbase.class); } - 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. + * Writes the provided {@code Cardbase} to the provided file in JSON format. * - * @param code the code of the set to be returned. - * @return the requested {@code FullCardSet} or null if no set matches the given code. + * @param file the file to which to write the {@code Cardbase}. + * @param cardbase the {@code Cardbase} to write out. * - * @throws JsonParseException if the upstream JSON is not formatted correctly. - * @throws JsonMappingException if the upstream JSON does not map to {@code FullCardSet}. + * @throws JsonGenerationException if the data structure given does not generate valid JSON. + * @throws JsonMappingException if the data structure given does not generate valid JSON as well? * @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; + public void writeCardbase(File outputFile) throws JsonGenerationException, JsonMappingException, IOException { + IO.jsonMapper.writeValue(outputFile, cardbase); } /** - * Add a specific amount of a card to the cardbase. + * Adds 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. * - * - * @param newCard - * @param count + * @param cardToAdd the card to be added. + * @param count the amount of the card to be added. */ - public void addCard(Card newCard, Integer count) { - Card card = cardbase.getCardByNumber(newCard.setCode, newCard.number); + public void addCard(Card cardToAdd, Integer count) { + Card card = cardbase.getCardByNumber(cardToAdd.setCode, cardToAdd.number); if (card != null) { card.count += count; } else { - newCard.count = count; - cardbase.cards.add(newCard); + cardToAdd.count = count; + cardbase.cards.add(cardToAdd); } } /** - * Remove a specific amount of a card from the cardbase. + * Removes a specific amount of a card from the cardbase. * If the card is not present in the cardbase, nothing happens. - * If the card is present in the card, the specified amount is removed. + * If the card is present in the cardbase, the specified amount is removed. * If that amount is equal to or exceeds the count already in the cardbase, - * the card entry is removed altogether. + * the card entry is removed altogether. + *

+ * In any case, the value returned is the actual number of cards removed. + * For example, if 5 Shivan Dragons are in the cardbase and the method is + * called to remove 10 Shivan Dragons, the {@code Card} representing the + * Shivan Dragon is removed from the cardbase, and the value returned is 5. * - * @param cardToRemove - * @param count + * @param cardToRemove the card to be removed. + * @param count the amount of the card to be removed. * @return the number of cards actually removed. */ public Integer removeCard(Card cardToRemove, Integer count) { @@ -135,21 +106,21 @@ public class CardbaseManager { } /** - * @return an iterator to the cards in the cardbase. + * @return an iterator to the {@code Card}s in the cardbase. */ public Iterator cardIterator() { return cardbase.cards.iterator(); } /** - * Return a card from the cardBase by setCode and number. - * If no such card is in the cardbase, return null. + * Returns a card from the cardbase by set code and number. + * If no such card is in the cardbase, returns null. * - * @param code - * @param string - * @return + * @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 code, String number) { - return cardbase.getCardByNumber(code, number); + public Card getCard(String setCode, String number) { + return cardbase.getCardByNumber(setCode, number); } } diff --git a/src/eu/equalparts/cardbase/data/FullCardSet.java b/src/eu/equalparts/cardbase/data/FullCardSet.java index 48488b5..0f58633 100644 --- a/src/eu/equalparts/cardbase/data/FullCardSet.java +++ b/src/eu/equalparts/cardbase/data/FullCardSet.java @@ -11,35 +11,35 @@ public class FullCardSet extends CardSet { private ArrayList cards; /** - * @return the border + * @return the set's border type. */ public String getBorder() { return border; } /** - * @return the type + * @return the type of the set. */ public String getType() { return type; } /** - * @return the block + * @return the set's block. */ public String getBlock() { return block; } /** - * @return the gathererCode + * @return the set's Gatherer code. */ public String getGathererCode() { return gathererCode; } /** - * @return the cards + * @return a full list of the set's cards. */ public ArrayList getCards() { return cards; @@ -49,7 +49,7 @@ public class FullCardSet extends CardSet { * Searches for a card by number (the one shown on the card itself). * * @param number the number of the card to search. - * @return the card, or null if no card is found with that number. + * @return the requested {@code Card}, or null if no card is found with that number. */ public Card getCardByNumber(String number) { for (Card card : cards) { -- cgit v1.2.3