aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase
diff options
context:
space:
mode:
Diffstat (limited to 'src/eu/equalparts/cardbase')
-rw-r--r--src/eu/equalparts/cardbase/Cardbase.java67
-rw-r--r--src/eu/equalparts/cardbase/cards/Card.java34
-rw-r--r--src/eu/equalparts/cardbase/decks/ReferenceDeck.java5
-rw-r--r--src/eu/equalparts/cardbase/utils/UID.java33
4 files changed, 69 insertions, 70 deletions
diff --git a/src/eu/equalparts/cardbase/Cardbase.java b/src/eu/equalparts/cardbase/Cardbase.java
index f95e19b..bdbdeb8 100644
--- a/src/eu/equalparts/cardbase/Cardbase.java
+++ b/src/eu/equalparts/cardbase/Cardbase.java
@@ -19,7 +19,6 @@ import eu.equalparts.cardbase.comparator.CardComparator;
import eu.equalparts.cardbase.decks.ReferenceDeck;
import eu.equalparts.cardbase.decks.StandaloneDeck;
import eu.equalparts.cardbase.utils.JSON;
-import eu.equalparts.cardbase.utils.UID;
/**
* Provides a variety of utility methods to interact with an optionally loaded cardbase.
@@ -32,7 +31,7 @@ public class Cardbase {
* The cards in the cardbase, set in key-value pairs where the key is the card hash,
* generated using {makeHash()}.
*/
- private Map<String, Card> cards;
+ private Map<Integer, Card> cards;
/**
* The decks which have been saved along with this collection of cards.
*/
@@ -53,14 +52,14 @@ 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<Map<String, Card>>() {});
+ cards = JSON.mapper.readValue(cardbaseFile, new TypeReference<Map<Integer, Card>>() {});
}
/**
* Initialises a clean cardbase.
*/
public Cardbase() {
- cards = new HashMap<String, Card>();
+ cards = new HashMap<Integer, Card>();
decks = new HashMap<String, ReferenceDeck>();
}
@@ -88,11 +87,11 @@ public class Cardbase {
* already exists.
*/
public void addCard(Card cardToAdd) {
- Card card = getCard(cardToAdd.setCode, cardToAdd.number);
+ Card card = getCardByHash(cardToAdd.hashCode());
if (card != null) {
card.count += cardToAdd.count;
} else {
- cards.put(UID.makeHash(cardToAdd), cardToAdd);
+ cards.put(cardToAdd.hashCode(), cardToAdd);
}
}
@@ -113,11 +112,11 @@ public class Cardbase {
* @return the number of cards actually removed.
*/
public Integer removeCard(Card cardToRemove) {
- Card card = getCard(cardToRemove.setCode, cardToRemove.number);
+ Card card = getCardByHash(cardToRemove.hashCode());
Integer removed = 0;
if (card != null) {
if (card.count <= cardToRemove.count) {
- cards.remove(UID.makeHash(card));
+ cards.remove(card.hashCode());
removed = card.count;
} else {
card.count -= cardToRemove.count;
@@ -127,6 +126,30 @@ public class Cardbase {
return removed;
}
+// /**
+// * 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 cards.get(Card.makeHash(setCode, number));
+ }
+
+ /**
+ * Returns a card from the cardbase by hash. The card's hash
+ * can be generated using {@code Cardbase.makeHash()}.
+ * If no such card is in the cardbase, returns null.
+ *
+ * @param hash the Cardbase hash of the requested card.
+ * @return the requested {@code Card} or null if no card is found.
+ */
+ protected Card getCardByHash(Integer hash) {
+ return cards.get(hash);
+ }
+
/**
* 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.
@@ -150,30 +173,6 @@ public class Cardbase {
return Collections.unmodifiableCollection(sortedCards);
}
- /**
- * 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 cards.get(UID.makeHash(setCode, number));
- }
-
- /**
- * Returns a card from the cardbase by hash. The card's hash
- * can be generated using {@code Cardbase.makeHash()}.
- * If no such card is in the cardbase, returns null.
- *
- * @param hash the Cardbase hash of the requested card.
- * @return the requested {@code Card} or null if no card is found.
- */
- public Card getCardByHash(String hash) {
- return cards.get(hash);
- }
-
public Map<String, ReferenceDeck> getDecks() {
return Collections.unmodifiableMap(decks);
}
@@ -181,7 +180,7 @@ public class Cardbase {
public List<Card> getMissingCards(StandaloneDeck deckToCheck) {
List<Card> missingCards = new ArrayList<Card>();
for (Card card : deckToCheck.cards) {
- String hash = UID.makeHash(card);
+ Integer hash = card.hashCode();
if (cards.containsKey(hash)) {
if (cards.get(hash).count < card.count) {
Card missingCard = card.clone();
@@ -217,7 +216,7 @@ public class Cardbase {
standaloneDeck.mountains = referenceDeck.mountains;
standaloneDeck.forests = referenceDeck.forests;
- for (String cardHash : referenceDeck.cardReferences.keySet()) {
+ for (Integer cardHash : referenceDeck.cardReferences.keySet()) {
Card card = getCardByHash(cardHash);
if (card != null) {
// must clone otherwise the original count is affected too
diff --git a/src/eu/equalparts/cardbase/cards/Card.java b/src/eu/equalparts/cardbase/cards/Card.java
index 419861b..1d8fbb9 100644
--- a/src/eu/equalparts/cardbase/cards/Card.java
+++ b/src/eu/equalparts/cardbase/cards/Card.java
@@ -57,5 +57,39 @@ public class Card {
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/decks/ReferenceDeck.java b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java
index 9e8695b..5650d2f 100644
--- a/src/eu/equalparts/cardbase/decks/ReferenceDeck.java
+++ b/src/eu/equalparts/cardbase/decks/ReferenceDeck.java
@@ -4,11 +4,10 @@ import java.util.HashMap;
import java.util.Map;
import eu.equalparts.cardbase.cards.Card;
-import eu.equalparts.cardbase.utils.UID;
public class ReferenceDeck extends Deck {
- public Map<String, Integer> cardReferences = new HashMap<String, Integer>();
+ public Map<Integer, Integer> cardReferences = new HashMap<Integer, Integer>();
public ReferenceDeck() {
@@ -23,7 +22,7 @@ public class ReferenceDeck extends Deck {
this.forests = deck.forests;
for (Card card : deck.cards) {
- cardReferences.put(UID.makeHash(card), card.count);
+ cardReferences.put(card.hashCode(), card.count);
}
}
diff --git a/src/eu/equalparts/cardbase/utils/UID.java b/src/eu/equalparts/cardbase/utils/UID.java
deleted file mode 100644
index ae4a11d..0000000
--- a/src/eu/equalparts/cardbase/utils/UID.java
+++ /dev/null
@@ -1,33 +0,0 @@
-package eu.equalparts.cardbase.utils;
-
-import eu.equalparts.cardbase.cards.Card;
-
-public class UID {
-
- /**
- * Used in the hash generation.
- */
- private static final String HASH_DIVIDER = "~";
-
- /**
- * 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.
- */
- public static String makeHash(String setCode, String number) {
- return setCode + HASH_DIVIDER + number;
- }
-
- /**
- * Generate the hash used as a key in the storage map.
- *
- * @param the {@code Card} whose hash is desired.
- * @return the generated hash.
- */
- public static String makeHash(Card card) {
- return card.setCode + HASH_DIVIDER + card.number;
- }
-
-}