diff options
author | Eduardo Pedroni <e.pedroni91@gmail.com> | 2016-03-30 22:00:49 +0200 |
---|---|---|
committer | Eduardo Pedroni <e.pedroni91@gmail.com> | 2016-03-30 22:00:49 +0200 |
commit | 499e77db6c4d535cd3cb0023230618565624b744 (patch) | |
tree | 83cf6ca8d62f4affb24993c71e59d5fa240b003c /src/eu/equalparts/cardbase | |
parent | fa90e150b74ce2e46128738fea40dbb8e6d182c3 (diff) |
It appears to be working, will continue tomorrow
Diffstat (limited to 'src/eu/equalparts/cardbase')
3 files changed, 82 insertions, 90 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 41b7781..4404613 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -2,6 +2,11 @@ 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; @@ -9,7 +14,9 @@ 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.utils.JSON; /** @@ -17,9 +24,18 @@ import eu.equalparts.cardbase.utils.JSON; * * @author Eduardo Pedroni */ -public class Cardbase extends StandaloneCardContainer { +public class Cardbase implements StandaloneCardContainer { /** + * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) and card amounts as entry values. + */ + @JsonProperty private Map<Integer, Integer> cardReferences = new HashMap<>(); + /** + * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) + * and card objects as entry values. + */ + @JsonProperty private Map<Integer, Card> cardData = new HashMap<>(); + /** * The decks which have been saved along with this collection of cards. */ @JsonProperty private Map<Integer, Object> decks; @@ -52,4 +68,59 @@ public class Cardbase extends StandaloneCardContainer { public void write(File outputFile) throws JsonGenerationException, JsonMappingException, IOException { JSON.mapper.writeValue(outputFile, this); } + + @Override + public int getCount(Card cardToCount) { + int hashCode = cardToCount.hashCode(); + return cardReferences.containsKey(hashCode) ? cardReferences.get(hashCode) : 0; + } + + @Override + 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); + } + cardData.putIfAbsent(hashCode, cardToAdd); + } + + @Override + 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); + cardData.remove(cardToRemove.hashCode()); + removed = oldCount; + } + } + + return removed; + } + + @Override + public Card getCard(String setCode, String number) { + return cardData.get(Card.makeHash(setCode, number)); + } + + @Override + public Collection<Card> getCards() { + return Collections.unmodifiableCollection(cardData.values()); + } + + @Override + 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/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java index f265d24..284fc89 100644 --- a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java +++ b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java @@ -1,10 +1,5 @@ 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; /** @@ -13,58 +8,25 @@ import eu.equalparts.cardbase.cards.Card; * @author Eduardo Pedroni * */ -public abstract class ReferenceCardContainer { - - /** - * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) and card amounts as entry values. - */ - @JsonProperty private Map<Integer, Integer> cardReferences = new HashMap<>(); - +public interface ReferenceCardContainer { /** * Returns the amount of the specified card. If the card is not present at all, return 0. * * @param cardToCount a card whose count is to be returned. * @return the count of the returned card in the container. */ - public int getCount(Card cardToCount) { - int hashCode = cardToCount.hashCode(); - return cardReferences.containsKey(hashCode) ? cardReferences.get(hashCode) : 0; - } + public int getCount(Card cardToCount); /** * @param cardToAdd the card to add the container. * @param count the amount to add. */ - 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 void addCard(Card cardToAdd, int count); + /** * @param cardToRemove the card to remove from the container. * @param count the amount to remove. * @return the amount that was effectively removed. Could be less than {@code count} depending on how many of the card were present. */ - 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; - } + public int removeCard(Card cardToRemove, int count); } diff --git a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java index 6c785df..517bf47 100644 --- a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java +++ b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java @@ -1,16 +1,8 @@ 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; /** * Holds actual card data in addition to the card counts in {@code ReferenceCardContainer}. @@ -18,31 +10,7 @@ import eu.equalparts.cardbase.comparator.CardComparator; * @author Eduardo Pedroni * */ -public abstract class StandaloneCardContainer extends ReferenceCardContainer { - - /** - * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) - * and card objects as entry values. - */ - @JsonProperty private Map<Integer, Card> 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; - } - +public interface StandaloneCardContainer extends ReferenceCardContainer { /** * Returns a card from the cardbase by set code and number. * If no such card is in the cardbase, returns null. @@ -51,10 +19,7 @@ public abstract class StandaloneCardContainer extends ReferenceCardContainer { * @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)); - } - + public Card getCard(String setCode, String 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. @@ -63,18 +28,12 @@ public abstract class StandaloneCardContainer extends ReferenceCardContainer { * * @return an unmodifiable list of all the cards in the cardbase. */ - public Collection<Card> getCards() { - return Collections.unmodifiableCollection(cardData.values()); - } - + public Collection<Card> getCards(); /** * @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 Collection<Card> sortByField(String fieldName) throws NoSuchFieldException; + } |