diff options
Diffstat (limited to 'src/eu/equalparts/cardbase')
6 files changed, 136 insertions, 195 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index be66127..d17ab2a 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -2,20 +2,14 @@ package eu.equalparts.cardbase; import java.io.File; import java.io.IOException; -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 com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; -import eu.equalparts.cardbase.cards.Card; import eu.equalparts.cardbase.cardstorage.StandaloneCardContainer; -import eu.equalparts.cardbase.comparator.CardComparator; import eu.equalparts.cardbase.decks.ReferenceDeck; import eu.equalparts.cardbase.utils.JSON; @@ -26,31 +20,10 @@ import eu.equalparts.cardbase.utils.JSON; */ public class Cardbase extends StandaloneCardContainer { -// 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; - -// -// public DataContainer() { -// cards = new HashMap<Integer, Card>(); -// collection = new HashMap<Integer, Integer>(); -// decks = new HashMap<String, ReferenceDeck>(); -// } -// } - -// private DataContainer dataContainer; - /** * The decks which have been saved along with this collection of cards. */ - public Map<String, ReferenceDeck> decks; + @JsonProperty private Map<String, ReferenceDeck> decks; /** * Initialises the cardbase with the contents of a file. @@ -66,13 +39,6 @@ public class Cardbase extends StandaloneCardContainer { } /** - * Initialises a clean cardbase. - */ - public Cardbase() { - super(); - } - - /** * Writes the provided {@code Cardbase} to the provided file in JSON format. * * @param file the file to which to write the {@code Cardbase}. @@ -82,164 +48,7 @@ public class Cardbase extends StandaloneCardContainer { * @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 void writeCollection(File outputFile) throws JsonGenerationException, JsonMappingException, IOException { + public void write(File outputFile) throws JsonGenerationException, JsonMappingException, IOException { JSON.mapper.writeValue(outputFile, this); } - - /** - * 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 cardToAdd the card to be added. The count value - * of this object is added to the existing count if the card - * already exists. - *TODO fix comment - */ -// public void addCard(Card cardToAdd, int addCount) { -// Integer hashCode = cardToAdd.hashCode(); -// -// // ensure that card is in the card map -// dataContainer.cards.putIfAbsent(hashCode, cardToAdd); -// -// // ensure that card is in the collection, with the correct count -// Integer currentCount = dataContainer.collection.get(hashCode); -// if (currentCount != null) { -// dataContainer.collection.replace(hashCode, currentCount + addCount); -// } else { -// dataContainer.collection.put(hashCode, addCount); -// } -// } - - /** - * 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 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. - * <br><br> - * 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 the card to be removed. - * @param removeCount the amount of the card to be removed. - * @return the number of cards actually removed. - *TODO comment - */ -// public Integer removeCard(Card cardToRemove, int removeCount) { -// Integer hashCode = cardToRemove.hashCode(); -// int removed = 0; -// -// Integer currentCount = dataContainer.collection.get(hashCode); -// if (currentCount != null) { -// if (removeCount >= currentCount) { -// dataContainer.collection.remove(hashCode); -// dataContainer.cards.remove(hashCode); -// removed = currentCount; -// } else { -// dataContainer.collection.replace(hashCode, currentCount - removeCount); -// removed = removeCount; -// } -// } -// 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 dataContainer.cards.get(Card.makeHash(setCode, number)); -// } - - /** - * Returns a card from the cardbase by hash. The card's hash - * can be generated using {@code Cardbase.makeHash()}. - * If no such card is in the cardbase, returns null. - * - * @param hash the Cardbase hash of the requested card. - * @return the requested {@code Card} or null if no card is found. - */ -// protected Card getCardByHash(Integer hash) { -// return dataContainer.cards.get(hash); -// } - - /** - * @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); - } - -// public int getCount(Card card) { -// Integer count = dataContainer.collection.get(Card.makeHash(card.setCode, card.number)); -// return count != null ? count : 0; -// } - -// public List<Card> getMissingCards(StandaloneDeck deckToCheck) { -// List<Card> missingCards = new ArrayList<Card>(); -// for (Card card : deckToCheck.cards) { -// Integer hash = card.hashCode(); -// if (cards.containsKey(hash)) { -// if (cards.get(hash).count < card.count) { -// Card missingCard = card.clone(); -// missingCard.count = card.count - cards.get(hash).count; -// missingCards.add(missingCard); -// } -// } else { -// missingCards.add(card); -// } -// } -// return missingCards; -// } - -// public void addStandaloneDeck(StandaloneDeck deckToAdd) { -// List<Card> missingCards = getMissingCards(deckToAdd); -// if (missingCards.size() <= 0) { -// decks.put(deckToAdd.name, new ReferenceDeck(deckToAdd)); -// } else { -// throw new IllegalArgumentException("The cardbase is missing cards to add this deck."); -// } -// } - -// public StandaloneDeck exportDeck(String deckName) { -// ReferenceDeck referenceDeck = decks.get(deckName); -// -// if (referenceDeck != null) { -// StandaloneDeck standaloneDeck = new StandaloneDeck(); -// -// standaloneDeck.name = referenceDeck.name; -// standaloneDeck.plains = referenceDeck.plains; -// standaloneDeck.islands = referenceDeck.islands; -// standaloneDeck.swamps = referenceDeck.swamps; -// standaloneDeck.mountains = referenceDeck.mountains; -// standaloneDeck.forests = referenceDeck.forests; -// -// for (Integer cardHash : referenceDeck.cardReferences.keySet()) { -// Card card = getCardByHash(cardHash); -// if (card != null) { -// // must clone otherwise the original count is affected too -// card = card.clone(); -// card.count = referenceDeck.cardReferences.get(cardHash); -// standaloneDeck.cards.add(card); -// } else { -// throw new IllegalArgumentException("Deck refers to card not in cardbase: " + cardHash); -// } -// } -// -// return standaloneDeck; -// } else { -// throw new IllegalArgumentException("The specified deck does not exist."); -// } -// } } diff --git a/src/eu/equalparts/cardbase/cards/Card.java b/src/eu/equalparts/cardbase/cards/Card.java index 9b0975b..37cc13e 100644 --- a/src/eu/equalparts/cardbase/cards/Card.java +++ b/src/eu/equalparts/cardbase/cards/Card.java @@ -1,8 +1,11 @@ package eu.equalparts.cardbase.cards; +import com.fasterxml.jackson.annotation.JsonAutoDetect; + import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; +@JsonAutoDetect public class Card { public String name; 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); + } +} diff --git a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index cbce4f9..7a932d0 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -256,7 +256,7 @@ public final class CardbaseCLI { } else { // handle these exceptions locally - they don't necessarily mean the program should exit try { - cardbase.writeCollection(outputFile); + cardbase.write(outputFile); // we are now working off outputFile, which may or may not be the same as cardbaseFile at this point cardbaseFile = outputFile; System.out.println("Cardbase was saved to \"" + outputFile.getAbsolutePath() + "\". " diff --git a/src/eu/equalparts/cardbase/utils/JSON.java b/src/eu/equalparts/cardbase/utils/JSON.java index 17e8392..4a21311 100644 --- a/src/eu/equalparts/cardbase/utils/JSON.java +++ b/src/eu/equalparts/cardbase/utils/JSON.java @@ -1,6 +1,7 @@ package eu.equalparts.cardbase.utils; import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; import com.fasterxml.jackson.databind.ObjectMapper; /** @@ -30,6 +31,11 @@ public final class JSON { ObjectMapper objectMapper = new ObjectMapper(); // classes don't necessarily use all json fields objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + // disable auto detection + objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS, + MapperFeature.AUTO_DETECT_FIELDS, + MapperFeature.AUTO_DETECT_GETTERS, + MapperFeature.AUTO_DETECT_IS_GETTERS); return objectMapper; } } |