From f7fe057b745d3f0e19b5dd6bd1819b11fc89c551 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Sun, 7 Aug 2016 22:18:55 +0200 Subject: Added filtering, still not working fully --- src/eu/equalparts/cardbase/Cardbase.java | 2 +- src/eu/equalparts/cardbase/cards/OldCard.java | 95 ++++++++++++++ .../cardstorage/ReferenceCardContainer.java | 137 --------------------- .../cardstorage/StandaloneCardContainer.java | 66 ---------- .../cardbase/comparator/SpecialFields.java | 1 - .../containers/ReferenceCardContainer.java | 137 +++++++++++++++++++++ .../containers/StandaloneCardContainer.java | 66 ++++++++++ .../equalparts/cardbase/decks/ReferenceDeck.java | 2 +- .../cardbase/filtering/CardFiltering.java | 57 +++++++++ .../equalparts/cardbase/sorting/CardSorting.java | 4 +- 10 files changed, 358 insertions(+), 209 deletions(-) create mode 100644 src/eu/equalparts/cardbase/cards/OldCard.java delete mode 100644 src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java delete mode 100644 src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java create mode 100644 src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java create mode 100644 src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java create mode 100644 src/eu/equalparts/cardbase/filtering/CardFiltering.java (limited to 'src/eu/equalparts') diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 9c630bd..90e1ffe 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -7,7 +7,7 @@ import com.fasterxml.jackson.core.JsonGenerationException; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; -import eu.equalparts.cardbase.cardstorage.StandaloneCardContainer; +import eu.equalparts.cardbase.containers.StandaloneCardContainer; import eu.equalparts.cardbase.utils.JSON; /** diff --git a/src/eu/equalparts/cardbase/cards/OldCard.java b/src/eu/equalparts/cardbase/cards/OldCard.java new file mode 100644 index 0000000..daa831e --- /dev/null +++ b/src/eu/equalparts/cardbase/cards/OldCard.java @@ -0,0 +1,95 @@ +package eu.equalparts.cardbase.cards; + +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; +import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; + +public class OldCard { + + public String name; + public String layout; + public String manaCost; + public Integer cmc; + public String type; + @Rarity + public String rarity; + public String text; + public String flavor; + public String artist; + @DirtyNumber + public String number; + @DirtyNumber + public String power; + @DirtyNumber + public String toughness; + public Integer loyalty; + public Integer multiverseid; + public String imageName; + public String watermark; + + // Not part of upstream JSON + public String setCode; + public String imageCode; + public Integer count; + + @Override + public OldCard clone() { + OldCard clone = new OldCard(); + + clone.name = this.name; + clone.layout = this.layout; + clone.manaCost = this.manaCost; + clone.cmc = this.cmc; + clone.type = this.type; + clone.rarity = this.rarity; + clone.text = this.text; + clone.flavor = this.flavor; + clone.artist = this.artist; + clone.number = this.number; + clone.power = this.power; + clone.toughness = this.toughness; + clone.loyalty = this.loyalty; + clone.multiverseid = this.multiverseid; + clone.imageName = this.imageName; + clone.watermark = this.watermark; + clone.setCode = this.setCode; + clone.imageCode = this.imageCode; + clone.count = this.count; + + return clone; + } + + public static int makeHash(String setCode, String number) { + final int prime = 31; + int result = 1; + result = prime * result + ((number == null) ? 0 : number.hashCode()); + result = prime * result + ((setCode == null) ? 0 : setCode.hashCode()); + return result; + } + + @Override + public int hashCode() { + return makeHash(setCode, number); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OldCard other = (OldCard) obj; + if (number == null) { + if (other.number != null) + return false; + } else if (!number.equals(other.number)) + return false; + if (setCode == null) { + if (other.setCode != null) + return false; + } else if (!setCode.equals(other.setCode)) + return false; + return true; + } +} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java deleted file mode 100644 index f43b76b..0000000 --- a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java +++ /dev/null @@ -1,137 +0,0 @@ -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; - -/** - * A class which contains card quantities with absolutely no other - * information about the cards themselves. - * - * @author Eduardo Pedroni - * - */ -public class ReferenceCardContainer { - - /** - * Land field initialised to 0, accessed with getter and updated with setter. - */ - private int plains = 0, islands = 0, swamps = 0, forests = 0, mountains = 0; - /** - * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) - * and card amounts as entry values. - */ - @JsonProperty private Map cardReferences = new HashMap<>(); - - /** - * 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 cardHash = cardToCount.hashCode(); - return cardReferences.containsKey(cardHash) ? cardReferences.get(cardHash) : 0; - } - - /** - * @param cardToAdd the card to add the container. - * @param count the amount to add. - */ - public void addCard(Card cardToAdd, int count) { - cardReferences.put(cardToAdd.hashCode(), getCount(cardToAdd) + 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 cardHash = cardToRemove.hashCode(); - if (count <= 0 || !cardReferences.containsKey(cardHash)) { - return 0; - } - - if (count >= cardReferences.get(cardHash)) { - return cardReferences.remove(cardHash); - } else { - cardReferences.put(cardHash, cardReferences.get(cardHash) - count); - return count; - } - } - - /** - * @return the plains - */ - public int getPlains() { - return plains; - } - - /** - * @return the islands - */ - public int getIslands() { - return islands; - } - - /** - * @return the swamps - */ - public int getSwamps() { - return swamps; - } - - /** - * @return the forests - */ - public int getForests() { - return forests; - } - - /** - * @return the mountains - */ - public int getMountains() { - return mountains; - } - - /** - * @param plains the plains to set - */ - public void setPlains(int plains) { - this.plains = plains; - } - - /** - * @param islands the islands to set - */ - public void setIslands(int islands) { - this.islands = islands; - } - - /** - * @param swamps the swamps to set - */ - public void setSwamps(int swamps) { - this.swamps = swamps; - } - - /** - * @param forests the forests to set - */ - public void setForests(int forests) { - this.forests = forests; - } - - /** - * @param mountains the mountains to set - */ - public void setMountains(int mountains) { - this.mountains = mountains; - } -} diff --git a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java deleted file mode 100644 index 53d248d..0000000 --- a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java +++ /dev/null @@ -1,66 +0,0 @@ -package eu.equalparts.cardbase.cardstorage; - -import java.util.HashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import com.fasterxml.jackson.annotation.JsonProperty; - -import eu.equalparts.cardbase.cards.Card; - -/** - * TODO fix comments - * Holds actual card data in addition to the card counts in {@code ReferenceCardContainer}. - * - * @author Eduardo Pedroni - * - */ -public 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 cardData = new HashMap<>(); - - /** - * 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 List getCards() { - return new LinkedList(cardData.values()); - } - - @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; - } -} diff --git a/src/eu/equalparts/cardbase/comparator/SpecialFields.java b/src/eu/equalparts/cardbase/comparator/SpecialFields.java index 692f774..433c8ab 100644 --- a/src/eu/equalparts/cardbase/comparator/SpecialFields.java +++ b/src/eu/equalparts/cardbase/comparator/SpecialFields.java @@ -13,5 +13,4 @@ public final class SpecialFields { @Retention(RetentionPolicy.RUNTIME) public @interface Rarity {} - } diff --git a/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java new file mode 100644 index 0000000..41c4224 --- /dev/null +++ b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java @@ -0,0 +1,137 @@ +package eu.equalparts.cardbase.containers; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.equalparts.cardbase.cards.Card; + +/** + * A class which contains card quantities with absolutely no other + * information about the cards themselves. + * + * @author Eduardo Pedroni + * + */ +public class ReferenceCardContainer { + + /** + * Land field initialised to 0, accessed with getter and updated with setter. + */ + private int plains = 0, islands = 0, swamps = 0, forests = 0, mountains = 0; + /** + * A map with card hashes as entry keys (calculated used {@code Card.hashCode()}) + * and card amounts as entry values. + */ + @JsonProperty private Map cardReferences = new HashMap<>(); + + /** + * 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 cardHash = cardToCount.hashCode(); + return cardReferences.containsKey(cardHash) ? cardReferences.get(cardHash) : 0; + } + + /** + * @param cardToAdd the card to add the container. + * @param count the amount to add. + */ + public void addCard(Card cardToAdd, int count) { + cardReferences.put(cardToAdd.hashCode(), getCount(cardToAdd) + 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 cardHash = cardToRemove.hashCode(); + if (count <= 0 || !cardReferences.containsKey(cardHash)) { + return 0; + } + + if (count >= cardReferences.get(cardHash)) { + return cardReferences.remove(cardHash); + } else { + cardReferences.put(cardHash, cardReferences.get(cardHash) - count); + return count; + } + } + + /** + * @return the plains + */ + public int getPlains() { + return plains; + } + + /** + * @return the islands + */ + public int getIslands() { + return islands; + } + + /** + * @return the swamps + */ + public int getSwamps() { + return swamps; + } + + /** + * @return the forests + */ + public int getForests() { + return forests; + } + + /** + * @return the mountains + */ + public int getMountains() { + return mountains; + } + + /** + * @param plains the plains to set + */ + public void setPlains(int plains) { + this.plains = plains; + } + + /** + * @param islands the islands to set + */ + public void setIslands(int islands) { + this.islands = islands; + } + + /** + * @param swamps the swamps to set + */ + public void setSwamps(int swamps) { + this.swamps = swamps; + } + + /** + * @param forests the forests to set + */ + public void setForests(int forests) { + this.forests = forests; + } + + /** + * @param mountains the mountains to set + */ + public void setMountains(int mountains) { + this.mountains = mountains; + } +} diff --git a/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java new file mode 100644 index 0000000..e5a514e --- /dev/null +++ b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java @@ -0,0 +1,66 @@ +package eu.equalparts.cardbase.containers; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; + +import eu.equalparts.cardbase.cards.Card; + +/** + * TODO fix comments + * Holds actual card data in addition to the card counts in {@code ReferenceCardContainer}. + * + * @author Eduardo Pedroni + * + */ +public 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 cardData = new HashMap<>(); + + /** + * 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 copy of the list kept internally by {@code StandaloneCardContainer}; + * modifying its structure is possible but will not affect the container instance from which it came. + * + * @return an unmodifiable list of all the cards in the cardbase. + */ + public List getCards() { + return new LinkedList(cardData.values()); + } + + @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; + } +} diff --git a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java index 8c8e227..9875796 100644 --- a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java +++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java @@ -1,6 +1,6 @@ package eu.equalparts.cardbase.decks; -import eu.equalparts.cardbase.cardstorage.ReferenceCardContainer; +import eu.equalparts.cardbase.containers.ReferenceCardContainer; public class ReferenceDeck extends ReferenceCardContainer { private String name = ""; diff --git a/src/eu/equalparts/cardbase/filtering/CardFiltering.java b/src/eu/equalparts/cardbase/filtering/CardFiltering.java new file mode 100644 index 0000000..23f439d --- /dev/null +++ b/src/eu/equalparts/cardbase/filtering/CardFiltering.java @@ -0,0 +1,57 @@ +package eu.equalparts.cardbase.filtering; + +import java.lang.reflect.Field; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +import eu.equalparts.cardbase.cards.Card; + +/** + * Contains and equals are not case sensitive. Regex is. + * + * @author Eduardo Pedroni + * + */ +public class CardFiltering { + + enum FilterType { + EQUALS { + @Override + public boolean doFilter(String value, String filter) { + return value.equalsIgnoreCase(filter); + } + }, CONTAINS { + @Override + public boolean doFilter(String value, String filter) { + return value.toLowerCase().contains(filter.toLowerCase()); + } + }, REGEX { + @Override + public boolean doFilter(String value, String filter) { + return value.matches(filter); + } + }; + + public abstract boolean doFilter(String value, String filter); + } + + public static Collection filterByField(List cards, String fieldName, FilterType filterType, String filterValue) throws NoSuchFieldException { + Field fieldToFilter = Card.class.getDeclaredField(fieldName); + try { + for (Iterator iterator = cards.iterator(); iterator.hasNext();) { + if (!filterType.doFilter((String) fieldToFilter.get(iterator.next()), filterValue)) { + iterator.remove(); + } + } + } catch (IllegalArgumentException e) { + System.out.println("Error: class Card does not define field \"" + fieldToFilter.getName() + "\"."); + e.printStackTrace(); + } catch (IllegalAccessException e) { + System.out.println("Error: field " + fieldToFilter.getName() + " in Card is not visible."); + e.printStackTrace(); + } + return cards; + } + +} diff --git a/src/eu/equalparts/cardbase/sorting/CardSorting.java b/src/eu/equalparts/cardbase/sorting/CardSorting.java index 04413b9..e7f7c96 100644 --- a/src/eu/equalparts/cardbase/sorting/CardSorting.java +++ b/src/eu/equalparts/cardbase/sorting/CardSorting.java @@ -10,11 +10,9 @@ public abstract class CardSorting { /** * @param cards * @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 static List sortByField(List cards, String fieldName) throws NoSuchFieldException { + public static void sortByField(List cards, String fieldName) throws NoSuchFieldException { cards.sort(new CardComparator(Card.class.getDeclaredField(fieldName))); - return cards; } } -- cgit v1.2.3