aboutsummaryrefslogtreecommitdiffstats
path: root/src/eu/equalparts/cardbase/data/CardbaseManager.java
blob: dfef3c8b7197f0c20cb90fd001956fb4b59f66d7 (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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package eu.equalparts.cardbase.data;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import eu.equalparts.cardbase.io.IO;

public class CardbaseManager {

	private ArrayList<CardSet> cardSets;

	/**
	 * A cache of CardSets to avoid querying the server many times for the same information.
	 */
	private HashMap<String, FullCardSet> cardSetCache = new HashMap<String, FullCardSet>();
	/**
	 * 
	 */
	private Cardbase cardbase;
	
	
	/**
	 * Parse a cardbase file and create an associated Cardbase object.
	 *
	 * @param cardbaseFile
	 * 
	 * @throws JsonParseException if underlying input contains invalid content of type JsonParser supports (JSON for default case).
	 * @throws JsonMappingException if the input JSON structure does not match structure expected for result type (or has other mismatch issues).
	 * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
	 */
	public CardbaseManager(File cardbaseFile) throws JsonParseException, JsonMappingException, IOException {
		cardSets = IO.getCardSetList();
		cardbase = IO.readCardbase(cardbaseFile);
	}

	/**
	 * Create an empty Cardbase.
	 * 
	 * @throws JsonParseException if underlying input contains invalid content of type JsonParser supports (JSON for default case).
	 * @throws JsonMappingException if the input JSON structure does not match structure expected for result type (or has other mismatch issues).
	 * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
	 */
	public CardbaseManager() throws JsonParseException, JsonMappingException, IOException {
		cardSets = IO.getCardSetList();
		cardbase = new Cardbase();
	}

	public ArrayList<CardSet> getCardSetList() {
		return cardSets;
	}

	public void writeCardbase(File outputFile) throws JsonGenerationException, JsonMappingException, IOException {
		IO.writeCardbase(outputFile, cardbase);
	}
	
	/**
	 * Returns the specified set in the form of a {@code FullCardSet} object. 
	 * 
	 * @param code the code of the set to be returned.
	 * @return the requested {@code FullCardSet} or null if no set matches the given code.
	 * 
	 * @throws JsonParseException if the upstream JSON is not formatted correctly.
	 * @throws JsonMappingException if the upstream JSON does not map to {@code FullCardSet}.
	 * @throws IOException if a low-level I/O problem (unexpected end-of-input, network error) occurs.
	 */
	public FullCardSet getFullCardSet(String code) throws JsonParseException, JsonMappingException, IOException {
		FullCardSet requestedSet = null;
		for (CardSet cardSet : cardSets) {
			if (cardSet.getCode().equalsIgnoreCase(code)) {
				// if the set is cached, no need to fetch
				if (cardSetCache.containsKey(cardSet.getCode())) {
					requestedSet = cardSetCache.get(cardSet.getCode());
				} 
				// not cached; fetch, cache and return it
				else {
					requestedSet = IO.getFullCardSet(cardSet.getCode());
					cardSetCache.put(cardSet.getCode(), requestedSet);
				}
				return requestedSet;
			}
		}
		// not found
		return null;
	}

	/**
	 * Add a specific amount of a card to the cardbase.
	 * If the card is not already in the cardbase, it is added.
	 * If it is already present, the count is simply updated.
	 * 
	 * 
	 * @param newCard
	 * @param count
	 */
	public void addCard(Card newCard, Integer count) {
		Card card = cardbase.getCardByNumber(newCard.setCode, newCard.number);
		if (card != null) {
			card.count += count;
		} else {
			newCard.count = count;
			cardbase.cards.add(newCard);
		}
	}

	/**
	 * Remove a specific amount of a card from the cardbase.
	 * If the card is not present in the cardbase, nothing happens.
	 * If the card is present in the card, the specified amount is removed.
	 * If that amount is equal to or exceeds the count already in the cardbase,
	 * the card entry is removed altogether. 
	 * 
	 * @param cardToRemove
	 * @param count
	 * @return the number of cards actually removed.
	 */
	public Integer removeCard(Card cardToRemove, Integer count) {
		Card card = cardbase.getCardByNumber(cardToRemove.setCode, cardToRemove.number);
		Integer removed = 0;
		if (card != null) {
			if (card.count <= count) {
				cardbase.cards.remove(card);
				removed = card.count;
			} else {
				card.count -= count;
				removed = count;
			}
		} 
		return removed;
	}

	/**
	 * @return an iterator to the cards in the cardbase.
	 */
	public Iterator<Card> cardIterator() {
		return cardbase.cards.iterator();
	}

	/**
	 * Return a card from the cardBase by setCode and number.
	 * If no such card is in the cardbase, return null.
	 * 
	 * @param code
	 * @param string
	 * @return
	 */
	public Card getCard(String code, String number) {
		return cardbase.getCardByNumber(code, number);
	}
}