aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/cardstorage/ReferenceCardContainer.java
blob: f43b76bf8b463aa703c18989b71e6a1307173319 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
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;

/**
 * A class which contains card quantities with absolutely no other
 * information about the cards themselves.
 * 
 * @author Eduardo Pedroni
 *
 */
public class ReferenceCardContainer {

	/**
	 * Land field initialised to 0, accessed with getter and updated with setter.
	 */
	private int plains = 0, islands = 0, swamps = 0, forests = 0, mountains = 0;
	/**
	 * A map with card hashes as entry keys (calculated used {@code Card.hashCode()})
	 * and card amounts as entry values.
	 */
	@JsonProperty private Map<Integer, Integer> cardReferences = new HashMap<>();

	/**
	 * Returns the amount of the specified card. If the card is not present at all, return 0.
	 * 
	 * @param cardToCount a card whose count is to be returned.
	 * @return the count of the returned card in the container.
	 */
	public int getCount(Card cardToCount){
		int cardHash = cardToCount.hashCode();
		return cardReferences.containsKey(cardHash) ? cardReferences.get(cardHash) : 0;
	}
	
	/**
	 * @param cardToAdd the card to add the container.
	 * @param count the amount to add.
	 */
	public void addCard(Card cardToAdd, int count) {
		cardReferences.put(cardToAdd.hashCode(), getCount(cardToAdd) + count);
	}
	
	/**
	 * @param cardToRemove the card to remove from the container.
	 * @param count the amount to remove.
	 * @return the amount that was effectively removed. Could be less than {@code count}
	 * depending on how many of the card were present.
	 */
	public int removeCard(Card cardToRemove, int count) {
		int cardHash = cardToRemove.hashCode();
		if (count <= 0 || !cardReferences.containsKey(cardHash)) {
			return 0;
		}
		
		if (count >= cardReferences.get(cardHash)) {
			return cardReferences.remove(cardHash);
		} else {
			cardReferences.put(cardHash, cardReferences.get(cardHash) - count);
			return count;
		}
	}

	/**
	 * @return the plains
	 */
	public int getPlains() {
		return plains;
	}

	/**
	 * @return the islands
	 */
	public int getIslands() {
		return islands;
	}

	/**
	 * @return the swamps
	 */
	public int getSwamps() {
		return swamps;
	}

	/**
	 * @return the forests
	 */
	public int getForests() {
		return forests;
	}

	/**
	 * @return the mountains
	 */
	public int getMountains() {
		return mountains;
	}

	/**
	 * @param plains the plains to set
	 */
	public void setPlains(int plains) {
		this.plains = plains;
	}

	/**
	 * @param islands the islands to set
	 */
	public void setIslands(int islands) {
		this.islands = islands;
	}

	/**
	 * @param swamps the swamps to set
	 */
	public void setSwamps(int swamps) {
		this.swamps = swamps;
	}

	/**
	 * @param forests the forests to set
	 */
	public void setForests(int forests) {
		this.forests = forests;
	}

	/**
	 * @param mountains the mountains to set
	 */
	public void setMountains(int mountains) {
		this.mountains = mountains;
	}
}