aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2016-08-07 22:18:55 +0200
committerEduardo Pedroni <e.pedroni91@gmail.com>2016-08-07 22:18:55 +0200
commitf7fe057b745d3f0e19b5dd6bd1819b11fc89c551 (patch)
tree1d19673aa4170f91248682a21db9fbc7c3945f4e /src/eu/equalparts
parent64c8e926d167a17865a6c3d86e48b383d413b569 (diff)
Added filtering, still not working fully
Diffstat (limited to 'src/eu/equalparts')
-rw-r--r--src/eu/equalparts/cardbase/Cardbase.java2
-rw-r--r--src/eu/equalparts/cardbase/cards/OldCard.java95
-rw-r--r--src/eu/equalparts/cardbase/comparator/SpecialFields.java1
-rw-r--r--src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java (renamed from src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java)2
-rw-r--r--src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java (renamed from src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java)6
-rw-r--r--src/eu/equalparts/cardbase/decks/ReferenceDeck.java2
-rw-r--r--src/eu/equalparts/cardbase/filtering/CardFiltering.java57
-rw-r--r--src/eu/equalparts/cardbase/sorting/CardSorting.java4
8 files changed, 159 insertions, 10 deletions
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/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/cardstorage/ReferenceCardContainer.java b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java
index f43b76b..41c4224 100644
--- a/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
+++ b/src/eu/equalparts/cardbase/containers/ReferenceCardContainer.java
@@ -1,4 +1,4 @@
-package eu.equalparts.cardbase.cardstorage;
+package eu.equalparts.cardbase.containers;
import java.util.HashMap;
import java.util.Map;
diff --git a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java
index 53d248d..e5a514e 100644
--- a/src/eu/equalparts/cardbase/cardstorage/StandaloneCardContainer.java
+++ b/src/eu/equalparts/cardbase/containers/StandaloneCardContainer.java
@@ -1,4 +1,4 @@
-package eu.equalparts.cardbase.cardstorage;
+package eu.equalparts.cardbase.containers;
import java.util.HashMap;
import java.util.LinkedList;
@@ -38,8 +38,8 @@ public class StandaloneCardContainer extends ReferenceCardContainer {
/**
* 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}.
+ * 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.
*/
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<Card> filterByField(List<Card> cards, String fieldName, FilterType filterType, String filterValue) throws NoSuchFieldException {
+ Field fieldToFilter = Card.class.getDeclaredField(fieldName);
+ try {
+ for (Iterator<Card> 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<Card> sortByField(List<Card> cards, String fieldName) throws NoSuchFieldException {
+ public static void sortByField(List<Card> cards, String fieldName) throws NoSuchFieldException {
cards.sort(new CardComparator(Card.class.getDeclaredField(fieldName)));
- return cards;
}
}