blob: ccd5508c35e91613ba39f199a53e3603471b65c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
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;
public abstract class ReferenceCardContainer {
@JsonProperty private Map<Integer, Integer> cardReferences;
public ReferenceCardContainer() {
cardReferences = new HashMap<>();
}
public int getCount(Card cardToCount) {
int hashCode = cardToCount.hashCode();
return cardReferences.containsKey(hashCode) ? cardReferences.get(hashCode) : 0;
}
public void addCard(Card cardToAdd, int count) {
int hashCode = cardToAdd.hashCode();
if (cardReferences.containsKey(hashCode)) {
cardReferences.replace(hashCode, cardReferences.get(hashCode) + count);
} else {
cardReferences.put(hashCode, count);
}
}
public int removeCard(Card cardToRemove, int count) {
int hashCode = cardToRemove.hashCode();
int removed = 0;
if (cardReferences.containsKey(hashCode) && count > 0) {
int oldCount = cardReferences.get(hashCode);
if (oldCount > count) {
cardReferences.replace(hashCode, oldCount - count);
removed = count;
} else {
cardReferences.remove(hashCode);
removed = oldCount;
}
}
return removed;
}
}
|