aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/Cardbase.java
diff options
context:
space:
mode:
authorEduardo Pedroni <e.pedroni91@gmail.com>2015-06-11 16:57:42 +0200
committerEduardo Pedroni <e.pedroni91@gmail.com>2015-06-11 16:57:42 +0200
commit27be2dd797fc7087c5e362c22c1f30fc377ea0e9 (patch)
tree93b0b5360788f1df3b257c294dbe08ffcf291dc2 /src/eu/equalparts/cardbase/Cardbase.java
parent56dc0db523156c6ff77d9212983c51b531250733 (diff)
Streamlined Card class, cards are now stored in a hashmap, had to write a method to parse full card set json
Diffstat (limited to 'src/eu/equalparts/cardbase/Cardbase.java')
-rw-r--r--src/eu/equalparts/cardbase/Cardbase.java54
1 files changed, 32 insertions, 22 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java
index d842031..c5efc85 100644
--- a/src/eu/equalparts/cardbase/Cardbase.java
+++ b/src/eu/equalparts/cardbase/Cardbase.java
@@ -1,16 +1,16 @@
package eu.equalparts.cardbase;
import java.io.File;
import java.io.IOException;
-import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
-import java.util.List;
+import java.util.HashMap;
+import java.util.Map;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
-import eu.equalparts.cardbase.comparators.CardComparators;
import eu.equalparts.cardbase.data.Card;
import eu.equalparts.cardbase.utils.JSON;
@@ -22,9 +22,10 @@ import eu.equalparts.cardbase.utils.JSON;
public class Cardbase {
/**
- * The cards in the cardbase.
+ * The cards in the cardbase, set in key-value pairs where the key is the card hash,
+ * generated using {makeHash()}.
*/
- private List<Card> cards;
+ private Map<String, Card> cards;
/**
* Debug flag is raised when the DEBUG environment variable is set. This causes additional
* information to be printed to the console.
@@ -35,7 +36,7 @@ public class Cardbase {
* Creates an empty cardbase.
*/
public Cardbase() {
- cards = new ArrayList<Card>();
+ cards = new HashMap<String, Card>();
}
/**
@@ -48,7 +49,7 @@ public class Cardbase {
* @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
*/
public Cardbase(File cardbaseFile) throws JsonParseException, JsonMappingException, IOException {
- cards = JSON.mapper.readValue(cardbaseFile, new TypeReference<ArrayList<Card>>() {});
+ cards = JSON.mapper.readValue(cardbaseFile, new TypeReference<Map<String, Card>>() {});
}
/**
@@ -79,7 +80,7 @@ public class Cardbase {
card.count += count;
} else {
cardToAdd.count = count;
- cards.add(cardToAdd);
+ cards.put(makeHash(cardToAdd), cardToAdd);
}
}
@@ -104,13 +105,13 @@ public class Cardbase {
Integer removed = 0;
if (card != null) {
if (card.count <= count) {
- cards.remove(card);
+ cards.remove(makeHash(card));
removed = card.count;
} else {
card.count -= count;
removed = count;
}
- }
+ }
return removed;
}
@@ -122,32 +123,41 @@ public class Cardbase {
*
* @return an unmodifiable list of all the cards in the cardbase.
*/
- public List<Card> getCards() {
- return Collections.unmodifiableList(cards);
+ public Collection<Card> getCards() {
+ return Collections.unmodifiableCollection(cards.values());
}
- public void sortByName() {
- cards.sort(new CardComparators.NameComparator());
- }
-
/**
* 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) {
- for (Card card : cards) {
- if (card.setCode.equals(setCode) && card.number.equals(number))
- return card;
- }
-
- return null;
+ return cards.get(makeHash(setCode, number));
}
+ /**
+ * Generate the hash used as a key in the storage map.
+ *
+ * @param setCode the card's set code.
+ * @param number the card's set number.
+ * @return the generated hash.
+ */
private String makeHash(String setCode, String number) {
return setCode + number;
}
+
+ /**
+ * Generate the hash used as a key in the storage map.
+ *
+ * @param the {@code Card} whose hash is desired.
+ * @return the generated hash.
+ */
+ private String makeHash(Card card) {
+ return card.setCode + card.number;
+ }
}