From c07d860f1d90dc3961b31c87dbe637dbcd87da76 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Thu, 11 Aug 2016 23:59:10 +0200 Subject: Refactored the Card data structure to be able to do filtering properly --- src/eu/equalparts/cardbase/Cardbase.java | 2 +- src/eu/equalparts/cardbase/card/Card.java | 68 +++++++++++ .../cardbase/card/CardSetInformation.java | 34 ++++++ src/eu/equalparts/cardbase/card/FullCardSet.java | 26 +++++ .../equalparts/cardbase/cardfield/CardField.java | 31 +++++ .../cardbase/cardfield/IntegerCardField.java | 23 ++++ .../cardbase/cardfield/StringCardField.java | 23 ++++ src/eu/equalparts/cardbase/cards/Card.java | 96 ---------------- .../cardbase/cards/CardSetInformation.java | 34 ------ src/eu/equalparts/cardbase/cards/FullCardSet.java | 26 ----- src/eu/equalparts/cardbase/cards/OldCard.java | 95 ---------------- src/eu/equalparts/cardbase/cli/CardbaseCLI.java | 32 +++--- .../cardbase/comparator/CardComparator.java | 45 ++------ .../cardbase/comparator/ComparatorDelegates.java | 13 ++- .../containers/ReferenceCardContainer.java | 2 +- .../containers/StandaloneCardContainer.java | 2 +- .../cardbase/filtering/CardFiltering.java | 30 +---- src/eu/equalparts/cardbase/gui/CardView.java | 2 +- .../equalparts/cardbase/json/CardDeserializer.java | 60 ++++++++++ .../equalparts/cardbase/json/CardSerializer.java | 126 +++++++++++++++++++++ src/eu/equalparts/cardbase/json/JSON.java | 50 ++++++++ .../equalparts/cardbase/sorting/CardSorting.java | 2 +- src/eu/equalparts/cardbase/utils/JSON.java | 41 ------- src/eu/equalparts/cardbase/utils/MTGUniverse.java | 13 ++- test/eu/equalparts/cardbase/CardbaseTest.java | 7 +- .../containers/ReferenceCardContainerTest.java | 8 +- .../containers/StandaloneCardContainerTest.java | 30 +++-- .../cardbase/filtering/CardFilteringTest.java | 52 +++------ .../cardbase/sorting/CardSortingTest.java | 43 ++++--- 29 files changed, 553 insertions(+), 463 deletions(-) create mode 100644 src/eu/equalparts/cardbase/card/Card.java create mode 100644 src/eu/equalparts/cardbase/card/CardSetInformation.java create mode 100644 src/eu/equalparts/cardbase/card/FullCardSet.java create mode 100644 src/eu/equalparts/cardbase/cardfield/CardField.java create mode 100644 src/eu/equalparts/cardbase/cardfield/IntegerCardField.java create mode 100644 src/eu/equalparts/cardbase/cardfield/StringCardField.java delete mode 100644 src/eu/equalparts/cardbase/cards/Card.java delete mode 100644 src/eu/equalparts/cardbase/cards/CardSetInformation.java delete mode 100644 src/eu/equalparts/cardbase/cards/FullCardSet.java delete mode 100644 src/eu/equalparts/cardbase/cards/OldCard.java create mode 100644 src/eu/equalparts/cardbase/json/CardDeserializer.java create mode 100644 src/eu/equalparts/cardbase/json/CardSerializer.java create mode 100644 src/eu/equalparts/cardbase/json/JSON.java delete mode 100644 src/eu/equalparts/cardbase/utils/JSON.java diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java index 90e1ffe..0fef2e7 100644 --- a/src/eu/equalparts/cardbase/Cardbase.java +++ b/src/eu/equalparts/cardbase/Cardbase.java @@ -8,7 +8,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import eu.equalparts.cardbase.containers.StandaloneCardContainer; -import eu.equalparts.cardbase.utils.JSON; +import eu.equalparts.cardbase.json.JSON; /** * Provides a variety of utility methods to interact with an optionally loaded cardbase. diff --git a/src/eu/equalparts/cardbase/card/Card.java b/src/eu/equalparts/cardbase/card/Card.java new file mode 100644 index 0000000..adac286 --- /dev/null +++ b/src/eu/equalparts/cardbase/card/Card.java @@ -0,0 +1,68 @@ +package eu.equalparts.cardbase.card; + +import eu.equalparts.cardbase.cardfield.IntegerCardField; +import eu.equalparts.cardbase.cardfield.StringCardField; +import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; +import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; + +public class Card { + public StringCardField name; + public StringCardField layout; + public StringCardField manaCost; + public IntegerCardField cmc; + public StringCardField type; + @Rarity + public StringCardField rarity; + public StringCardField text; + public StringCardField flavor; + public StringCardField artist; + @DirtyNumber + public StringCardField number; + @DirtyNumber + public StringCardField power; + @DirtyNumber + public StringCardField toughness; + public IntegerCardField loyalty; + public IntegerCardField multiverseid; + public StringCardField imageName; + public StringCardField watermark; + + // Not part of upstream JSON + public StringCardField setCode; + public StringCardField imageCode; + + 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.get(), number.get()); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Card other = (Card) 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; + } +} diff --git a/src/eu/equalparts/cardbase/card/CardSetInformation.java b/src/eu/equalparts/cardbase/card/CardSetInformation.java new file mode 100644 index 0000000..a01c18d --- /dev/null +++ b/src/eu/equalparts/cardbase/card/CardSetInformation.java @@ -0,0 +1,34 @@ +package eu.equalparts.cardbase.card; + +public class CardSetInformation { + + private String name; + private String code; + private String releaseDate; + + /** + * @return the set's name. + */ + public String getName() { + return name; + } + + /** + * @return the set code. + */ + public String getCode() { + return code; + } + + /** + * @return the set's release date. + */ + public String getReleaseDate() { + return releaseDate; + } + + @Override + public String toString() { + return String.format("%1$-12s : %2$s", code, name, releaseDate); + } +} diff --git a/src/eu/equalparts/cardbase/card/FullCardSet.java b/src/eu/equalparts/cardbase/card/FullCardSet.java new file mode 100644 index 0000000..72f1e4a --- /dev/null +++ b/src/eu/equalparts/cardbase/card/FullCardSet.java @@ -0,0 +1,26 @@ +package eu.equalparts.cardbase.card; + +import java.util.Map; + +public class FullCardSet { + + public String name; + public String code; + public String magicCardsInfoCode; + public String releaseDate; + public String border; + public String type; + public String block; + public String gathererCode; + public Map cards; + + /** + * Searches for a card by number (the one shown on the card itself). + * + * @param number the number of the card to search. + * @return the requested {@code Card}, or null if no card is found with that number. + */ + public Card getCardByNumber(String number) { + return cards.get(number); + } +} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/cardfield/CardField.java b/src/eu/equalparts/cardbase/cardfield/CardField.java new file mode 100644 index 0000000..9af775b --- /dev/null +++ b/src/eu/equalparts/cardbase/cardfield/CardField.java @@ -0,0 +1,31 @@ +package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.filtering.CardFiltering.Filter; + +public abstract class CardField> implements Comparable> { + private String name; + private T value; + + public CardField(String name, T value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public T get() { + return value; + } + + public void set(T newValue) { + this.value = newValue; + } + + @Override + public int compareTo(CardField o) { + return value.compareTo(o.get()); + } + + public abstract boolean filter(Filter filter, String s); +} diff --git a/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java new file mode 100644 index 0000000..4016252 --- /dev/null +++ b/src/eu/equalparts/cardbase/cardfield/IntegerCardField.java @@ -0,0 +1,23 @@ +package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.filtering.CardFiltering.Filter; + +public class IntegerCardField extends CardField { + + public IntegerCardField(String name, Integer value) { + super(name, value); + } + + @Override + public boolean filter(Filter filter, String s) { + switch (filter) { + case CONTAINS: + return get().toString().contains(s); + case EQUALS: + return get().toString().equalsIgnoreCase(s); + case REGEX: + return get().toString().matches(s); + default: + return false; + } + } +} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/cardfield/StringCardField.java b/src/eu/equalparts/cardbase/cardfield/StringCardField.java new file mode 100644 index 0000000..059a45d --- /dev/null +++ b/src/eu/equalparts/cardbase/cardfield/StringCardField.java @@ -0,0 +1,23 @@ +package eu.equalparts.cardbase.cardfield; +import eu.equalparts.cardbase.filtering.CardFiltering.Filter; + +public class StringCardField extends CardField { + + public StringCardField(String name, String value) { + super(name, value); + } + + @Override + public boolean filter(Filter filter, String s) { + switch (filter) { + case CONTAINS: + return get().toLowerCase().contains(s.toLowerCase()); + case EQUALS: + return get().equalsIgnoreCase(s); + case REGEX: + return get().matches(s); + default: + return false; + } + } +} diff --git a/src/eu/equalparts/cardbase/cards/Card.java b/src/eu/equalparts/cardbase/cards/Card.java deleted file mode 100644 index 37cc13e..0000000 --- a/src/eu/equalparts/cardbase/cards/Card.java +++ /dev/null @@ -1,96 +0,0 @@ -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; - 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; - - @Override - public Card clone() { - Card clone = new Card(); - - 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; - - 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; - Card other = (Card) 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/cards/CardSetInformation.java b/src/eu/equalparts/cardbase/cards/CardSetInformation.java deleted file mode 100644 index 587c99a..0000000 --- a/src/eu/equalparts/cardbase/cards/CardSetInformation.java +++ /dev/null @@ -1,34 +0,0 @@ -package eu.equalparts.cardbase.cards; - -public class CardSetInformation { - - private String name; - private String code; - private String releaseDate; - - /** - * @return the set's name. - */ - public String getName() { - return name; - } - - /** - * @return the set code. - */ - public String getCode() { - return code; - } - - /** - * @return the set's release date. - */ - public String getReleaseDate() { - return releaseDate; - } - - @Override - public String toString() { - return String.format("%1$-12s : %2$s", code, name, releaseDate); - } -} diff --git a/src/eu/equalparts/cardbase/cards/FullCardSet.java b/src/eu/equalparts/cardbase/cards/FullCardSet.java deleted file mode 100644 index d627111..0000000 --- a/src/eu/equalparts/cardbase/cards/FullCardSet.java +++ /dev/null @@ -1,26 +0,0 @@ -package eu.equalparts.cardbase.cards; - -import java.util.Map; - -public class FullCardSet { - - public String name; - public String code; - public String magicCardsInfoCode; - public String releaseDate; - public String border; - public String type; - public String block; - public String gathererCode; - public Map cards; - - /** - * Searches for a card by number (the one shown on the card itself). - * - * @param number the number of the card to search. - * @return the requested {@code Card}, or null if no card is found with that number. - */ - public Card getCardByNumber(String number) { - return cards.get(number); - } -} \ No newline at end of file diff --git a/src/eu/equalparts/cardbase/cards/OldCard.java b/src/eu/equalparts/cardbase/cards/OldCard.java deleted file mode 100644 index daa831e..0000000 --- a/src/eu/equalparts/cardbase/cards/OldCard.java +++ /dev/null @@ -1,95 +0,0 @@ -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/cli/CardbaseCLI.java b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java index 7a932d0..f86365d 100644 --- a/src/eu/equalparts/cardbase/cli/CardbaseCLI.java +++ b/src/eu/equalparts/cardbase/cli/CardbaseCLI.java @@ -13,9 +13,9 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; import eu.equalparts.cardbase.Cardbase; -import eu.equalparts.cardbase.cards.Card; -import eu.equalparts.cardbase.cards.CardSetInformation; -import eu.equalparts.cardbase.cards.FullCardSet; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.card.CardSetInformation; +import eu.equalparts.cardbase.card.FullCardSet; import eu.equalparts.cardbase.utils.MTGUniverse; /** @@ -477,7 +477,7 @@ public final class CardbaseCLI { * TODO comment */ private void addCard(Card card, int count) { - System.out.println("Added " + count + "x " + card.name + "."); + System.out.println("Added " + count + "x " + card.name.get() + "."); cardbase.addCard(card, count); savePrompt = true; lastAction = Action.ADD; @@ -496,13 +496,13 @@ public final class CardbaseCLI { private void removeCard(Card card, int count) { Integer removed = cardbase.removeCard(card, count); if (removed > 0) { - System.out.println("Removed " + removed + "x " + card.name + "."); + System.out.println("Removed " + removed + "x " + card.name.get() + "."); savePrompt = true; lastAction = Action.REMOVE; lastAction.card = card; lastAction.count = removed; } else { - System.out.println(card.name + " is not in the cardbase."); + System.out.println(card.name.get() + " is not in the cardbase."); } } @@ -529,7 +529,7 @@ public final class CardbaseCLI { * @param card the card to glance. */ private void printGlance(Card card) { - System.out.println(String.format("%1$-4d %2$s (%3$s, %4$s)", cardbase.getCount(card), card.name, card.setCode, card.number)); + System.out.println(String.format("%1$-4d %2$s (%3$s, %4$s)", cardbase.getCount(card), card.name.get(), card.setCode.get(), card.number.get())); } /** @@ -541,16 +541,16 @@ public final class CardbaseCLI { */ private void printPerusal(Card card) { printGlance(card); - if (card.type != null) System.out.println("\t" + card.type); - if (card.manaCost != null) System.out.println("\tCost: " + card.manaCost); - if (card.power != null && card.toughness != null) System.out.println("\t" + card.power + "/" + card.toughness); - if (card.loyalty != null) System.out.println("\tLoyalty: " + card.loyalty); + if (card.type.get() != null) System.out.println("\t" + card.type.get()); + if (card.manaCost.get() != null) System.out.println("\tCost: " + card.manaCost.get()); + if (card.power.get() != null && card.toughness.get() != null) System.out.println("\t" + card.power.get() + "/" + card.toughness.get()); + if (card.loyalty.get() != null) System.out.println("\tLoyalty: " + card.loyalty.get()); - if (card.text != null) System.out.println("\t" + card.text.replaceAll("\n", "\n\t")); - if (card.flavor != null) System.out.println("\t" + card.flavor.replaceAll("\n", "\n\t")); + if (card.text.get() != null) System.out.println("\t" + card.text.get().replaceAll("\n", "\n\t")); + if (card.flavor.get() != null) System.out.println("\t" + card.flavor.get().replaceAll("\n", "\n\t")); - if (card.rarity != null) System.out.println("\t" + card.rarity); - if (card.multiverseid != null) System.out.println("\tMID: " + card.multiverseid); - if (card.artist != null) System.out.println("\tIllus. " + card.artist); + if (card.rarity.get() != null) System.out.println("\t" + card.rarity.get()); + if (card.multiverseid.get() != null) System.out.println("\tMID: " + card.multiverseid.get()); + if (card.artist.get() != null) System.out.println("\tIllus. " + card.artist.get()); } } diff --git a/src/eu/equalparts/cardbase/comparator/CardComparator.java b/src/eu/equalparts/cardbase/comparator/CardComparator.java index c3ff736..d2cd0c1 100644 --- a/src/eu/equalparts/cardbase/comparator/CardComparator.java +++ b/src/eu/equalparts/cardbase/comparator/CardComparator.java @@ -2,12 +2,11 @@ package eu.equalparts.cardbase.comparator; import java.lang.annotation.Annotation; import java.lang.reflect.Field; -import java.lang.reflect.ParameterizedType; -import java.lang.reflect.Type; import java.util.Comparator; import java.util.function.BiFunction; -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.cardfield.CardField; import eu.equalparts.cardbase.comparator.SpecialFields.DirtyNumber; import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; @@ -26,7 +25,7 @@ import eu.equalparts.cardbase.comparator.SpecialFields.Rarity; * @author Eduardo Pedroni * */ -@SuppressWarnings({"rawtypes", "unchecked"}) +//@SuppressWarnings({"rawtypes", "unchecked"}) public class CardComparator implements Comparator { /** @@ -36,7 +35,7 @@ public class CardComparator implements Comparator { /** * The comparison delegate to use for the specified field. */ - private BiFunction comparisonDelegate = (field1, field2) -> field1.compareTo(field2); + private BiFunction comparisonDelegate = (field1, field2) -> field1.compareTo(field2); /** * Creates a new comparator for the specified field only. This class @@ -46,11 +45,13 @@ public class CardComparator implements Comparator { *
* For reference, {@code String} and {@code Integer} are both self comparable. * + * TODO comment + * * @param fieldToCompare the field this comparator will use to compare cards, as declared in {@code Card}. */ public CardComparator(Field fieldToCompare) { if (fieldToCompare.getDeclaringClass().equals(Card.class) && - isSelfComparable(fieldToCompare.getType())) { + CardField.class.isAssignableFrom(fieldToCompare.getType())) { this.fieldToCompare = fieldToCompare; @@ -74,17 +75,17 @@ public class CardComparator implements Comparator { * so we are now free to cast to comparable */ try { - Comparable field1 = (Comparable) fieldToCompare.get(o1); - Comparable field2 = (Comparable) fieldToCompare.get(o2); + CardField field1 = (CardField) fieldToCompare.get(o1); + CardField field2 = (CardField) fieldToCompare.get(o2); // if either or both fields' values are null, skip delegation altogether since delegates are not required to deal with null values - if (field1 == null) { - if (field2 == null) { + if (field1.get() == null) { + if (field2.get() == null) { return 0; } else { return -1; } - } else if (field2 == null) { + } else if (field2.get() == null) { return 1; } else { return comparisonDelegate.apply(field1, field2); @@ -101,26 +102,4 @@ public class CardComparator implements Comparator { // fallback, this shouldn't happen return 0; } - - /** - * Use reflection to determine if the specified class can be compared with itself. - * - * @param type the type to analyse. - * @return true if the type can be compared to itself using {@code compareTo()}, false otherwise. - */ - private boolean isSelfComparable(Class type) { - - // go through all interfaces implemented by this class - for (Type implementedInterface : type.getGenericInterfaces()) { - // check if any parameterised interface found is "Comparable" - if (implementedInterface instanceof ParameterizedType) { - ParameterizedType genericInterface = (ParameterizedType) implementedInterface; - if (genericInterface.getRawType().equals(Comparable.class)) { - // check that the type argument of comparable is the same as the field type itself - return genericInterface.getActualTypeArguments()[0].equals(type); - } - } - } - return false; - } } diff --git a/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java b/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java index cede956..292cd24 100644 --- a/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java +++ b/src/eu/equalparts/cardbase/comparator/ComparatorDelegates.java @@ -1,5 +1,6 @@ package eu.equalparts.cardbase.comparator; +import eu.equalparts.cardbase.cardfield.CardField; final class ComparatorDelegates { @@ -19,10 +20,10 @@ final class ComparatorDelegates { * @param field2 the second field to compare. * @return a value smaller than, equal to or greater than 0 according to the standard comparison convention. */ - public static Integer compareDirtyNumber(Comparable field1, Comparable field2) { + public static Integer compareDirtyNumber(CardField field1, CardField field2) { // this assumes that the format is uninterrupted numbers and letters - String number1 = ((String) field1).replaceAll("[^0-9]+", ""); - String number2 = ((String) field2).replaceAll("[^0-9]+", ""); + String number1 = field1.get().replaceAll("[^0-9]+", ""); + String number2 = field2.get().replaceAll("[^0-9]+", ""); Integer int1 = number1.matches("[0-9]+") ? Integer.parseInt(number1) : null; Integer int2 = number2.matches("[0-9]+") ? Integer.parseInt(number2) : null; @@ -48,10 +49,10 @@ final class ComparatorDelegates { return letter1.compareTo(letter2); } - public static Integer compareRarity(Comparable field1, Comparable field2) { + public static Integer compareRarity(CardField field1, CardField field2) { // assign numerical values based on the different possibilities and compare those instead - Integer value1 = getRarityValue((String) field1); - Integer value2 = getRarityValue((String) field2); + Integer value1 = getRarityValue(field1.get()); + Integer value2 = getRarityValue(field2.get()); return value1.compareTo(value2); } diff --git a/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java index 41c4224..152b6f1 100644 --- a/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java +++ b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java @@ -5,7 +5,7 @@ import java.util.Map; import com.fasterxml.jackson.annotation.JsonProperty; -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; /** * A class which contains card quantities with absolutely no other diff --git a/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java index e5a514e..1c4faf6 100644 --- a/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java +++ b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java @@ -7,7 +7,7 @@ import java.util.Map; import com.fasterxml.jackson.annotation.JsonProperty; -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; /** * TODO fix comments diff --git a/src/eu/equalparts/cardbase/filtering/CardFiltering.java b/src/eu/equalparts/cardbase/filtering/CardFiltering.java index 23f439d..de71f1c 100644 --- a/src/eu/equalparts/cardbase/filtering/CardFiltering.java +++ b/src/eu/equalparts/cardbase/filtering/CardFiltering.java @@ -1,11 +1,11 @@ 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; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.cardfield.CardField; /** * Contains and equals are not case sensitive. Regex is. @@ -15,32 +15,15 @@ import eu.equalparts.cardbase.cards.Card; */ 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 enum Filter { + EQUALS, CONTAINS, REGEX; } - public static Collection filterByField(List cards, String fieldName, FilterType filterType, String filterValue) throws NoSuchFieldException { + public static void filterByField(List cards, String fieldName, Filter 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)) { + if (!((CardField) fieldToFilter.get(iterator.next())).filter(filterType, filterValue)) { iterator.remove(); } } @@ -51,7 +34,6 @@ public class CardFiltering { System.out.println("Error: field " + fieldToFilter.getName() + " in Card is not visible."); e.printStackTrace(); } - return cards; } } diff --git a/src/eu/equalparts/cardbase/gui/CardView.java b/src/eu/equalparts/cardbase/gui/CardView.java index 2ec7576..9774537 100644 --- a/src/eu/equalparts/cardbase/gui/CardView.java +++ b/src/eu/equalparts/cardbase/gui/CardView.java @@ -1,6 +1,6 @@ package eu.equalparts.cardbase.gui; -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; import javafx.event.EventHandler; import javafx.scene.image.ImageView; import javafx.scene.input.MouseEvent; diff --git a/src/eu/equalparts/cardbase/json/CardDeserializer.java b/src/eu/equalparts/cardbase/json/CardDeserializer.java new file mode 100644 index 0000000..c7ca0a1 --- /dev/null +++ b/src/eu/equalparts/cardbase/json/CardDeserializer.java @@ -0,0 +1,60 @@ +package eu.equalparts.cardbase.json; + + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.cardfield.IntegerCardField; +import eu.equalparts.cardbase.cardfield.StringCardField; + +public class CardDeserializer extends StdDeserializer { + + /** + * + */ + private static final long serialVersionUID = 1894617997342518472L; + + protected CardDeserializer(Class vc) { + super(vc); + } + + public CardDeserializer() { + this(null); + } + + @Override + public Card deserialize(JsonParser jp, DeserializationContext ctxt) + throws IOException, JsonProcessingException { + JsonNode node = jp.getCodec().readTree(jp); + + Card newCard = new Card(); + + newCard.name = new StringCardField("name", node.hasNonNull("name") ? node.get("name").asText() : null); + newCard.layout = new StringCardField("layout", node.hasNonNull("layout") ? node.get("layout").asText() : null); + newCard.manaCost = new StringCardField("manaCost", node.hasNonNull("manaCost") ? node.get("manaCost").asText() : null); + newCard.cmc = new IntegerCardField("cmc", node.hasNonNull("cmc") ? node.get("cmc").asInt() : null); + newCard.type = new StringCardField("type", node.hasNonNull("type") ? node.get("type").asText() : null); + newCard.rarity = new StringCardField("rarity", node.hasNonNull("rarity") ? node.get("rarity").asText() : null); + newCard.text = new StringCardField("text", node.hasNonNull("text") ? node.get("text").asText() : null); + newCard.flavor = new StringCardField("flavor", node.hasNonNull("flavor") ? node.get("flavor").asText() : null); + newCard.artist = new StringCardField("artist", node.hasNonNull("artist") ? node.get("artist").asText() : null); + newCard.number = new StringCardField("number", node.hasNonNull("number") ? node.get("number").asText() : null); + newCard.power = new StringCardField("power", node.hasNonNull("power") ? node.get("power").asText() : null); + newCard.toughness = new StringCardField("toughness", node.hasNonNull("toughness") ? node.get("toughness").asText() : null); + newCard.loyalty = new IntegerCardField("loyalty", node.hasNonNull("loyalty") ? node.get("loyalty").asInt() : null); + newCard.multiverseid = new IntegerCardField("multiverseid", node.hasNonNull("multiverseid") ? node.get("multiverseid").asInt() : null); + newCard.imageName = new StringCardField("imageName", node.hasNonNull("imageName") ? node.get("imageName").asText() : null); + newCard.watermark = new StringCardField("watermark", node.hasNonNull("watermark") ? node.get("watermark").asText() : null); + newCard.setCode = new StringCardField("setCode", node.hasNonNull("setCode") ? node.get("setCode").asText() : null); + newCard.imageCode = new StringCardField("imageCode", node.hasNonNull("imageCode") ? node.get("imageCode").asText() : null); + + return newCard; + } + +} diff --git a/src/eu/equalparts/cardbase/json/CardSerializer.java b/src/eu/equalparts/cardbase/json/CardSerializer.java new file mode 100644 index 0000000..05b41c0 --- /dev/null +++ b/src/eu/equalparts/cardbase/json/CardSerializer.java @@ -0,0 +1,126 @@ +package eu.equalparts.cardbase.json; + +import java.io.IOException; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import eu.equalparts.cardbase.card.Card; + +public class CardSerializer extends JsonSerializer { + @Override + public void serialize(Card card, JsonGenerator jGen, SerializerProvider serializerProvider) throws IOException { + jGen.writeStartObject(); + + if (card.name.get() != null) { + jGen.writeStringField("name", card.name.get()); + } else { + jGen.writeNullField("name"); + } + + if (card.layout.get() != null) { + jGen.writeStringField("layout", card.layout.get()); + } else { + jGen.writeNullField("layout"); + } + + if (card.manaCost.get() != null) { + jGen.writeStringField("manaCost", card.manaCost.get()); + } else { + jGen.writeNullField("manaCost"); + } + + if (card.cmc.get() != null) { + jGen.writeNumberField("cmc", card.cmc.get()); + } else { + jGen.writeNullField("cmc"); + } + + if (card.type.get() != null) { + jGen.writeStringField("type", card.type.get()); + } else { + jGen.writeNullField("type"); + } + + if (card.rarity.get() != null) { + jGen.writeStringField("rarity", card.rarity.get()); + } else { + jGen.writeNullField("rarity"); + } + + if (card.text.get() != null) { + jGen.writeStringField("text", card.text.get()); + } else { + jGen.writeNullField("text"); + } + + if (card.flavor.get() != null) { + jGen.writeStringField("flavor", card.flavor.get()); + } else { + jGen.writeNullField("flavor"); + } + + if (card.artist.get() != null) { + jGen.writeStringField("artist", card.artist.get()); + } else { + jGen.writeNullField("artist"); + } + + if (card.number.get() != null) { + jGen.writeStringField("number", card.number.get()); + } else { + jGen.writeNullField("number"); + } + + if (card.power.get() != null) { + jGen.writeStringField("power", card.power.get()); + } else { + jGen.writeNullField("power"); + } + + if (card.toughness.get() != null) { + jGen.writeStringField("toughness", card.toughness.get()); + } else { + jGen.writeNullField("toughness"); + } + + if (card.loyalty.get() != null) { + jGen.writeNumberField("loyalty", card.loyalty.get()); + } else { + jGen.writeNullField("loyalty"); + } + + if (card.multiverseid.get() != null) { + jGen.writeNumberField("multiverseid", card.multiverseid.get()); + } else { + jGen.writeNullField("multiverseid"); + } + + if (card.imageName.get() != null) { + jGen.writeStringField("imageName", card.imageName.get()); + } else { + jGen.writeNullField("imageName"); + } + + if (card.watermark.get() != null) { + jGen.writeStringField("watermark", card.watermark.get()); + } else { + jGen.writeNullField("watermark"); + } + + if (card.setCode.get() != null) { + jGen.writeStringField("setCode", card.setCode.get()); + } else { + jGen.writeNullField("setCode"); + } + + if (card.imageCode.get() != null) { + jGen.writeStringField("imageCode", card.imageCode.get()); + } else { + jGen.writeNullField("imageCode"); + } + + jGen.writeEndObject(); + } +} diff --git a/src/eu/equalparts/cardbase/json/JSON.java b/src/eu/equalparts/cardbase/json/JSON.java new file mode 100644 index 0000000..b06f205 --- /dev/null +++ b/src/eu/equalparts/cardbase/json/JSON.java @@ -0,0 +1,50 @@ +package eu.equalparts.cardbase.json; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.MapperFeature; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.module.SimpleModule; + +import eu.equalparts.cardbase.card.Card; + +/** + * This class simply holds an {@code ObjectMapper} to be used whenever JSON must be parsed. + * In the future it may be removed in favour of individual mappers for each function. + * + * @author Eduardo Pedroni + */ +public final class JSON { + + /** + * The Jackson {@code ObjectMapper} which parses fetched JSON files. + */ + public static final ObjectMapper mapper = createMapper(); + + /** + * Private constructor, this class is not to be instantiated. + */ + private JSON() {} + + /** + * Instantiate and configure Jackson mapper statically. + * + * @return the {@code ObjectMapper}, ready to use. + */ + private static ObjectMapper createMapper() { + 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); + + SimpleModule sm = new SimpleModule(); + sm.addDeserializer(Card.class, new CardDeserializer()); + sm.addSerializer(Card.class, new CardSerializer()); + + objectMapper.registerModule(sm); + return objectMapper; + } +} diff --git a/src/eu/equalparts/cardbase/sorting/CardSorting.java b/src/eu/equalparts/cardbase/sorting/CardSorting.java index e7f7c96..b8e36f1 100644 --- a/src/eu/equalparts/cardbase/sorting/CardSorting.java +++ b/src/eu/equalparts/cardbase/sorting/CardSorting.java @@ -2,7 +2,7 @@ package eu.equalparts.cardbase.sorting; import java.util.List; -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; import eu.equalparts.cardbase.comparator.CardComparator; public abstract class CardSorting { diff --git a/src/eu/equalparts/cardbase/utils/JSON.java b/src/eu/equalparts/cardbase/utils/JSON.java deleted file mode 100644 index 4a21311..0000000 --- a/src/eu/equalparts/cardbase/utils/JSON.java +++ /dev/null @@ -1,41 +0,0 @@ -package eu.equalparts.cardbase.utils; - -import com.fasterxml.jackson.databind.DeserializationFeature; -import com.fasterxml.jackson.databind.MapperFeature; -import com.fasterxml.jackson.databind.ObjectMapper; - -/** - * This class simply holds an {@code ObjectMapper} to be used whenever JSON must be parsed. - * In the future it may be removed in favour of individual mappers for each function. - * - * @author Eduardo Pedroni - */ -public final class JSON { - - /** - * The Jackson {@code ObjectMapper} which parses fetched JSON files. - */ - public static final ObjectMapper mapper = createMapper(); - - /** - * Private constructor, this class is not to be instantiated. - */ - private JSON() {} - - /** - * Instantiate and configure Jackson mapper statically. - * - * @return the {@code ObjectMapper}, ready to use. - */ - private static ObjectMapper createMapper() { - 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; - } -} diff --git a/src/eu/equalparts/cardbase/utils/MTGUniverse.java b/src/eu/equalparts/cardbase/utils/MTGUniverse.java index 6acb092..9412e6d 100644 --- a/src/eu/equalparts/cardbase/utils/MTGUniverse.java +++ b/src/eu/equalparts/cardbase/utils/MTGUniverse.java @@ -12,9 +12,10 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.JsonNode; -import eu.equalparts.cardbase.cards.Card; -import eu.equalparts.cardbase.cards.CardSetInformation; -import eu.equalparts.cardbase.cards.FullCardSet; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.card.CardSetInformation; +import eu.equalparts.cardbase.card.FullCardSet; +import eu.equalparts.cardbase.json.JSON; /** * Access point to the complete set of cards that exist in the @@ -202,9 +203,9 @@ public final class MTGUniverse { Map cardMap = new HashMap(); for (Card card : rawList) { // add set code for convenience - card.setCode = setCode; - card.imageCode = imageCode; - cardMap.put(card.number, card); + card.setCode.set(setCode); + card.imageCode.set(imageCode); + cardMap.put(card.number.get(), card); } fcs.cards = cardMap; } else { diff --git a/test/eu/equalparts/cardbase/CardbaseTest.java b/test/eu/equalparts/cardbase/CardbaseTest.java index 832880a..12c4842 100644 --- a/test/eu/equalparts/cardbase/CardbaseTest.java +++ b/test/eu/equalparts/cardbase/CardbaseTest.java @@ -17,9 +17,9 @@ import org.junit.rules.TemporaryFolder; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.JsonMappingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.json.JSON; /** * TODO deck functionality needs to be built into these. @@ -40,8 +40,7 @@ public class CardbaseTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - testCard = mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class); + testCard = JSON.mapper.readValue(CardbaseTest.class.getResourceAsStream("/shivandragon.json"), Card.class); } @AfterClass diff --git a/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java index 849d84c..2941e45 100644 --- a/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java +++ b/test/eu/equalparts/cardbase/containers/ReferenceCardContainerTest.java @@ -9,9 +9,8 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import com.fasterxml.jackson.databind.ObjectMapper; - -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.json.JSON; public class ReferenceCardContainerTest { @@ -26,8 +25,7 @@ public class ReferenceCardContainerTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - testCard = mapper.readValue(ReferenceCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class); + testCard = JSON.mapper.readValue(ReferenceCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class); } @Before diff --git a/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java index 6e9b31a..c420ee1 100644 --- a/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java +++ b/test/eu/equalparts/cardbase/containers/StandaloneCardContainerTest.java @@ -14,9 +14,8 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; -import com.fasterxml.jackson.databind.ObjectMapper; - -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.json.JSON; public class StandaloneCardContainerTest { private StandaloneCardContainer uut; @@ -30,8 +29,7 @@ public class StandaloneCardContainerTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - testCard = mapper.readValue(StandaloneCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class); + testCard = JSON.mapper.readValue(StandaloneCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class); } @Before @@ -44,11 +42,11 @@ public class StandaloneCardContainerTest { ***********************************************************************************/ @Test public void newCardIsAdded() throws Exception { - assertNull("Container should not contain the test card to begin with.", uut.getCard(testCard.setCode, testCard.number)); + assertNull("Container should not contain the test card to begin with.", uut.getCard(testCard.setCode.get(), testCard.number.get())); uut.addCard(testCard, 1); - assertEquals("Container should contain the test card once it is added.", testCard, uut.getCard(testCard.setCode, testCard.number)); + assertEquals("Container should contain the test card once it is added.", testCard, uut.getCard(testCard.setCode.get(), testCard.number.get())); assertEquals("Container should have contained 1 test card.", 1, uut.getCount(testCard)); } @@ -57,7 +55,7 @@ public class StandaloneCardContainerTest { uut.addCard(testCard, 2); uut.addCard(testCard, 4); - Card addedCard = uut.getCard(testCard.setCode, testCard.number); + Card addedCard = uut.getCard(testCard.setCode.get(), testCard.number.get()); assertNotNull("Card was not found in cardbase.", addedCard); assertEquals("Card count was not updated correctly.", 6, uut.getCount(addedCard)); } @@ -82,7 +80,7 @@ public class StandaloneCardContainerTest { assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard)); assertEquals("Container reports wrong removed count.", 3, removed); - assertEquals("Card is missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number)); + assertEquals("Card is missing from container.", testCard, uut.getCard(testCard.setCode.get(), testCard.number.get())); } @Test @@ -93,7 +91,7 @@ public class StandaloneCardContainerTest { assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); assertEquals("Container reports wrong removed count.", 5, removed); - assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); + assertNull("Card is not missing from container.", uut.getCard(testCard.setCode.get(), testCard.number.get())); } @Test @@ -104,7 +102,7 @@ public class StandaloneCardContainerTest { assertEquals("Card was not removed from container.", 0, uut.getCount(testCard)); assertEquals("Container reports wrong removed count.", 3, removed); - assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); + assertNull("Card is not missing from container.", uut.getCard(testCard.setCode.get(), testCard.number.get())); } /* @@ -118,12 +116,12 @@ public class StandaloneCardContainerTest { @Test public void removedCardIsNotInContainer() throws Exception { - assertNull("Card is not initially missing from container.", uut.getCard(testCard.setCode, testCard.number)); + assertNull("Card is not initially missing from container.", uut.getCard(testCard.setCode.get(), testCard.number.get())); int removed = uut.removeCard(testCard, 1); assertEquals("Removed count should be 0.", 0, removed); - assertNull("Card is not missing from container.", uut.getCard(testCard.setCode, testCard.number)); + assertNull("Card is not missing from container.", uut.getCard(testCard.setCode.get(), testCard.number.get())); } @Test @@ -134,7 +132,7 @@ public class StandaloneCardContainerTest { assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard)); assertEquals("Container reports wrong removed count.", 0, removed); - assertEquals("Card should not be missing from container.", testCard, uut.getCard(testCard.setCode, testCard.number)); + assertEquals("Card should not be missing from container.", testCard, uut.getCard(testCard.setCode.get(), testCard.number.get())); } /*********************************************************************************** @@ -144,7 +142,7 @@ public class StandaloneCardContainerTest { public void correctCardIsReturnedByGetter() throws Exception { uut.addCard(testCard, 1); - Card card = uut.getCard(testCard.setCode, testCard.number); + Card card = uut.getCard(testCard.setCode.get(), testCard.number.get()); for (Field field : Card.class.getFields()) { assertEquals("Field " + field.getName(), field.get(testCard), field.get(card)); @@ -165,6 +163,6 @@ public class StandaloneCardContainerTest { @Test public void getCardIsNotInCardbase() throws Exception { - assertNull("Method should have returned null", uut.getCard(testCard.setCode, testCard.number)); + assertNull("Method should have returned null", uut.getCard(testCard.setCode.get(), testCard.number.get())); } } diff --git a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java index 4f5579d..c2ea71c 100644 --- a/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java +++ b/test/eu/equalparts/cardbase/filtering/CardFilteringTest.java @@ -13,10 +13,10 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import eu.equalparts.cardbase.cards.Card; -import eu.equalparts.cardbase.filtering.CardFiltering.FilterType; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.filtering.CardFiltering.Filter; +import eu.equalparts.cardbase.json.JSON; public class CardFilteringTest { private static List allTestCards, testCards; @@ -26,8 +26,7 @@ public class CardFilteringTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - allTestCards = mapper.readValue(CardFilteringTest.class.getResourceAsStream("/testcards.json"), new TypeReference>() {}); + allTestCards = JSON.mapper.readValue(CardFilteringTest.class.getResourceAsStream("/testcards.json"), new TypeReference>() {}); } @Before @@ -37,7 +36,7 @@ public class CardFilteringTest { @Test public void filterByNameEquals() throws Exception { - CardFiltering.filterByField(testCards, "name", FilterType.EQUALS, "Callow jushi"); + CardFiltering.filterByField(testCards, "name", Filter.EQUALS, "Callow jushi"); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name); @@ -45,7 +44,7 @@ public class CardFilteringTest { @Test public void filterByNameContains() throws Exception { - CardFiltering.filterByField(testCards, "name", FilterType.CONTAINS, "sh"); + CardFiltering.filterByField(testCards, "name", Filter.CONTAINS, "sh"); assertEquals("Wrong list size.", 3, testCards.size()); int i = 0; @@ -55,14 +54,14 @@ public class CardFilteringTest { "Disrupting Shoal", }; for (Card card : testCards) { - assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); i++; } } @Test public void filterByNameRegex() throws Exception { - CardFiltering.filterByField(testCards, "name", FilterType.REGEX, ".+?n"); + CardFiltering.filterByField(testCards, "name", Filter.REGEX, ".+?n"); assertEquals("Wrong list size.", 2, testCards.size()); int i = 0; @@ -71,14 +70,14 @@ public class CardFilteringTest { "Shivan Dragon", }; for (Card card : testCards) { - assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); i++; } } @Test public void filterByLayoutEquals() throws Exception { - CardFiltering.filterByField(testCards, "layout", FilterType.EQUALS, "flip"); + CardFiltering.filterByField(testCards, "layout", Filter.EQUALS, "flip"); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name); @@ -86,7 +85,7 @@ public class CardFilteringTest { @Test public void filterByLayoutContains() throws Exception { - CardFiltering.filterByField(testCards, "layout", FilterType.CONTAINS, "l"); + CardFiltering.filterByField(testCards, "layout", Filter.CONTAINS, "l"); assertEquals("Wrong list size.", 8, testCards.size()); int i = 0; @@ -101,14 +100,14 @@ public class CardFilteringTest { "Ugin's Construct", }; for (Card card : testCards) { - assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); i++; } } @Test public void filterByLayoutRegex() throws Exception { - CardFiltering.filterByField(testCards, "layout", FilterType.REGEX, "fl[a-z]p"); + CardFiltering.filterByField(testCards, "layout", Filter.REGEX, "fl[a-z]p"); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Callow Jushi", testCards.get(0).name); @@ -116,7 +115,7 @@ public class CardFilteringTest { @Test public void filterByManaCostEquals() throws Exception { - CardFiltering.filterByField(testCards, "manaCost", FilterType.EQUALS, "{X}{U}{U}"); + CardFiltering.filterByField(testCards, "manaCost", Filter.EQUALS, "{X}{U}{U}"); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Disrupting Shoal", testCards.get(0).name); @@ -124,7 +123,7 @@ public class CardFilteringTest { @Test public void filterByManaCostContains() throws Exception { - CardFiltering.filterByField(testCards, "manaCost", FilterType.CONTAINS, "B"); + CardFiltering.filterByField(testCards, "manaCost", Filter.CONTAINS, "B"); assertEquals("Wrong list size.", 3, testCards.size()); int i = 0; @@ -134,14 +133,14 @@ public class CardFilteringTest { "Sorin Markov", }; for (Card card : testCards) { - assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); i++; } } @Test public void filterByManaCostRegex() throws Exception { - CardFiltering.filterByField(testCards, "manaCost", FilterType.REGEX, "(\\{G\\}){8}"); + CardFiltering.filterByField(testCards, "manaCost", Filter.REGEX, "(\\{G\\}){8}"); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Khalni Hydra", testCards.get(0).name); @@ -149,7 +148,7 @@ public class CardFilteringTest { @Test public void filterByCMCEquals() throws Exception { - CardFiltering.filterByField(testCards, "cmc", FilterType.EQUALS, "5"); + CardFiltering.filterByField(testCards, "cmc", Filter.EQUALS, "5"); assertEquals("Wrong list size.", 1, testCards.size()); assertEquals("Coerced Confession", testCards.get(0).name); @@ -157,27 +156,12 @@ public class CardFilteringTest { @Test public void filterByCMCContains() throws Exception { - CardFiltering.filterByField(testCards, "cmc", FilterType.CONTAINS, "B"); - assertEquals("Wrong list size.", 3, testCards.size()); - int i = 0; - String[] names = { - "Coerced Confession", - "Nightmare", - "Sorin Markov", - }; - for (Card card : testCards) { - assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); - i++; - } } @Test public void filterByCMCRegex() throws Exception { - CardFiltering.filterByField(testCards, "cmc", FilterType.REGEX, "(\\{G\\}){8}"); - assertEquals("Wrong list size.", 1, testCards.size()); - assertEquals("Khalni Hydra", testCards.get(0).name); } @Test diff --git a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java index 4ec0f5e..04cf20d 100644 --- a/test/eu/equalparts/cardbase/sorting/CardSortingTest.java +++ b/test/eu/equalparts/cardbase/sorting/CardSortingTest.java @@ -10,9 +10,9 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import eu.equalparts.cardbase.cards.Card; +import eu.equalparts.cardbase.card.Card; +import eu.equalparts.cardbase.json.JSON; /** * Tests the sorting functionality. @@ -28,8 +28,7 @@ public class CardSortingTest { @BeforeClass public static void setUpBeforeClass() throws Exception { - ObjectMapper mapper = new ObjectMapper(); - testCards = mapper.readValue(CardSortingTest.class.getResourceAsStream("/testcards.json"), new TypeReference>() {}); + testCards = JSON.mapper.readValue(CardSortingTest.class.getResourceAsStream("/testcards.json"), new TypeReference>() {}); } @Test @@ -47,7 +46,7 @@ public class CardSortingTest { "Ugin's Construct", }; for (Card card : testCards) { - assertTrue(card.name + " should have been " + names[i] + ", i = " + i, card.name.equals(names[i])); + assertTrue(card.name.get() + " should have been " + names[i] + ", i = " + i, card.name.get().equals(names[i])); i++; } } @@ -67,7 +66,7 @@ public class CardSortingTest { "normal", }; for (Card card : testCards) { - assertTrue(card.layout + " should have been " + layouts[i] + ", i = " + i, card.layout.equals(layouts[i])); + assertTrue(card.layout.get() + " should have been " + layouts[i] + ", i = " + i, card.layout.get().equals(layouts[i])); i++; } } @@ -87,7 +86,7 @@ public class CardSortingTest { "{X}{U}{U}", }; for (Card card : testCards) { - assertTrue(card.manaCost + " should have been " + costs[i] + ", i = " + i, card.manaCost.equals(costs[i])); + assertTrue(card.manaCost.get() + " should have been " + costs[i] + ", i = " + i, card.manaCost.get().equals(costs[i])); i++; } } @@ -98,7 +97,7 @@ public class CardSortingTest { int i = 0; Integer[] cmcs = {2, 3, 4, 5, 6, 6, 6, 8}; for (Card card : testCards) { - assertTrue(card.cmc + " should have been " + cmcs[i] + ", i = " + i, card.cmc.equals(cmcs[i])); + assertTrue(card.cmc.get() + " should have been " + cmcs[i] + ", i = " + i, card.cmc.get().equals(cmcs[i])); i++; } } @@ -118,7 +117,7 @@ public class CardSortingTest { "Sorcery", }; for (Card card : testCards) { - assertTrue(card.type + " should have been " + types[i] + ", i = " + i, card.type.equals(types[i])); + assertTrue(card.type.get() + " should have been " + types[i] + ", i = " + i, card.type.get().equals(types[i])); i++; } } @@ -138,7 +137,7 @@ public class CardSortingTest { "Mythic Rare", }; for (Card card : testCards) { - assertTrue(card.rarity + " should have been " + rarities[i] + ", i = " + i, card.rarity.equals(rarities[i])); + assertTrue(card.rarity.get() + " should have been " + rarities[i] + ", i = " + i, card.rarity.get().equals(rarities[i])); i++; } } @@ -158,7 +157,7 @@ public class CardSortingTest { "You may exile a blue card with converted mana cost X from your hand rather than pay Disrupting Shoal's mana cost.\nCounter target spell if its converted mana cost is X.", }; for (Card card : testCards) { - assertTrue(card.text + " should have been " + texts[i] + ", i = " + i, card.text.equals(texts[i])); + assertTrue(card.text.get() + " should have been " + texts[i] + ", i = " + i, card.text.get().equals(texts[i])); i++; } } @@ -178,7 +177,7 @@ public class CardSortingTest { "While trapping the Eldrazi on Zendikar, Ugin learned little from Sorin, but he gleaned the rudiments of lithomancy from Nahiri.", }; for (Card card : testCards) { - String flavor = card.flavor != null ? card.flavor : ""; + String flavor = card.flavor.get() != null ? card.flavor.get() : ""; assertTrue(flavor + " should have been " + flavors[i] + ", i = " + i, flavor.equals(flavors[i])); i++; } @@ -199,7 +198,7 @@ public class CardSortingTest { "Vance Kovacs", }; for (Card card : testCards) { - assertTrue(card.artist + " should have been " + artists[i] + ", i = " + i, card.artist.equals(artists[i])); + assertTrue(card.artist.get() + " should have been " + artists[i] + ", i = " + i, card.artist.get().equals(artists[i])); i++; } } @@ -219,7 +218,7 @@ public class CardSortingTest { "281", }; for (Card card : testCards) { - assertTrue(card.number + " should have been " + numbers[i] + ", i = " + i, card.number.equals(numbers[i])); + assertTrue(card.number.get() + " should have been " + numbers[i] + ", i = " + i, card.number.get().equals(numbers[i])); i++; } } @@ -239,7 +238,7 @@ public class CardSortingTest { "8", }; for (Card card : testCards) { - String power = card.power != null ? card.power : ""; + String power = card.power.get() != null ? card.power.get() : ""; assertTrue(power + " should have been " + powers[i] + ", i = " + i, power.equals(powers[i])); i++; } @@ -260,7 +259,7 @@ public class CardSortingTest { "8", }; for (Card card : testCards) { - String toughness = card.toughness != null ? card.toughness : ""; + String toughness = card.toughness.get() != null ? card.toughness.get() : ""; assertTrue(toughness + " should have been " + toughnesses[i] + ", i = " + i, toughness.equals(toughnesses[i])); i++; } @@ -272,7 +271,7 @@ public class CardSortingTest { int i = 0; Integer[] loyalties = {0, 0, 0, 0, 0, 0, 0, 4}; for (Card card : testCards) { - Integer loyalty = card.loyalty != null ? card.loyalty : 0; + Integer loyalty = card.loyalty.get() != null ? card.loyalty.get() : 0; assertTrue(loyalty + " should have been " + loyalties[i] + ", i = " + i, loyalty.equals(loyalties[i])); i++; } @@ -284,7 +283,7 @@ public class CardSortingTest { int i = 0; Integer[] ids = {74128, 74489, 193551, 238330, 366408, 383168, 383172, 391949 }; for (Card card : testCards) { - Integer id = card.multiverseid != null ? card.multiverseid : 0; + Integer id = card.multiverseid.get() != null ? card.multiverseid.get() : 0; assertTrue(id + " should have been " + ids[i] + ", i = " + i, id.equals(ids[i])); i++; } @@ -305,7 +304,7 @@ public class CardSortingTest { "ugin's construct", }; for (Card card : testCards) { - assertTrue(card.imageName + " should have been " + names[i] + ", i = " + i, card.imageName.equals(names[i])); + assertTrue(card.imageName.get() + " should have been " + names[i] + ", i = " + i, card.imageName.get().equals(names[i])); i++; } } @@ -325,7 +324,7 @@ public class CardSortingTest { "Dimir", }; for (Card card : testCards) { - String watermark = card.watermark != null ? card.watermark : ""; + String watermark = card.watermark.get() != null ? card.watermark.get() : ""; assertTrue(watermark + " should have been " + watermarks[i] + ", i = " + i, watermark.equals(watermarks[i])); i++; } @@ -346,7 +345,7 @@ public class CardSortingTest { "ROE", }; for (Card card : testCards) { - assertTrue(card.setCode + " should have been " + sets[i] + ", i = " + i, card.setCode.equals(sets[i])); + assertTrue(card.setCode.get() + " should have been " + sets[i] + ", i = " + i, card.setCode.get().equals(sets[i])); i++; } } @@ -366,7 +365,7 @@ public class CardSortingTest { "roe", }; for (Card card : testCards) { - assertTrue(card.imageCode + " should have been " + codes[i] + ", i = " + i, card.imageCode.equals(codes[i])); + assertTrue(card.imageCode.get() + " should have been " + codes[i] + ", i = " + i, card.imageCode.get().equals(codes[i])); i++; } } -- cgit v1.2.3