1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.tools;
20
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.util.Comparator;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27
28 import net.sf.magicproject.clickable.targetable.Targetable;
29 import net.sf.magicproject.clickable.targetable.card.CardFactory;
30 import net.sf.magicproject.clickable.targetable.card.CardModel;
31 import net.sf.magicproject.clickable.targetable.card.MCard;
32
33 /***
34 * This class represents card's name and offset of card's code in mdb file.
35 *
36 * @author Jan Blaha
37 * @author Fabrice Daugan
38 * @since 0.53
39 */
40 public class MCardCompare implements Comparator<MCardCompare>,
41 Comparable<MCardCompare> {
42
43 /***
44 * Create a new instance of MCardCompare
45 */
46 public MCardCompare() {
47 super();
48 name = null;
49 mdbOffset = 0L;
50 }
51
52 /***
53 * Create a new instance of MCardCompare
54 *
55 * @param name
56 * card's name.
57 * @param mdbOffset
58 * The MDB offset of first byte of card.
59 */
60 public MCardCompare(String name, long mdbOffset) {
61 this.name = name;
62 this.mdbOffset = mdbOffset;
63 }
64
65 /***
66 * Create a new instance of MCardCompare
67 *
68 * @param name
69 * card's name.
70 * @param amount
71 * amount of card
72 * @param properties
73 * the optional properties attached to these cards.
74 * @param mdbOffset
75 * The MDB offset of first byte of card.
76 */
77 public MCardCompare(String name, int amount, Map<String, String> properties,
78 long mdbOffset) {
79 this(name, mdbOffset);
80 this.amount = amount;
81 if (properties != null) {
82 this.constraints.putAll(properties);
83 }
84 }
85
86 /***
87 * return card's name
88 *
89 * @return card's name
90 */
91 public String getName() {
92 return name;
93 }
94
95 /***
96 * return the associated integer
97 *
98 * @return the associated integer
99 */
100 public int getAmount() {
101 return amount;
102 }
103
104 /***
105 * Add some cards.
106 *
107 * @param amount
108 * the amount of card to add.
109 */
110 public void add(int amount) {
111 this.amount += amount;
112 }
113
114 /***
115 * Remove some cards.
116 *
117 * @param amount
118 * the amount of card to remove.
119 */
120 public void remove(int amount) {
121 this.amount -= amount;
122 }
123
124 /***
125 * @return the list of properties of the current card
126 */
127 public Map<String, String> getConstraints() {
128 return constraints;
129 }
130
131 /***
132 * return the string representation of this item.
133 *
134 * @return <b>name </b>; <b>amount </b>; <b>constraints </b>
135 */
136 @Override
137 public String toString() {
138 if (amount == 0 && constraints.isEmpty()) {
139 return name;
140 }
141 StringBuffer buffer = new StringBuffer(name);
142 if (amount > 0) {
143 buffer.append(";");
144 buffer.append(amount);
145 }
146 if (!constraints.isEmpty()) {
147 Iterator<String> keys = constraints.keySet().iterator();
148 while (keys.hasNext()) {
149 String key = keys.next();
150 buffer.append(";");
151 buffer.append(key);
152 buffer.append("=");
153 buffer.append(constraints.get(key));
154 }
155 }
156 return buffer.toString();
157 }
158
159 public int compare(MCardCompare o1, MCardCompare o2) {
160 return o1.name.toLowerCase().compareTo(o2.name.toLowerCase());
161 }
162
163 @Override
164 public boolean equals(Object other) {
165 if (other instanceof String) {
166 return ((String) other).equalsIgnoreCase(name);
167 }
168 return ((MCardCompare) other).name.equalsIgnoreCase(name);
169 }
170
171 @Override
172 public int hashCode() {
173 return name.toLowerCase().hashCode();
174 }
175
176 public int compareTo(MCardCompare o) {
177 return name.toLowerCase().compareTo(o.name.toLowerCase());
178 }
179
180 /***
181 * Return the MDB offset of first byte of card.
182 *
183 * @return the MDB offset of first byte of card.
184 */
185 public long getMdbOffset() {
186 return this.mdbOffset;
187 }
188
189 /***
190 * The list of properties associated to the card.
191 */
192 private Map<String, String> constraints = new HashMap<String, String>(5);
193
194 /***
195 * represents the card's name
196 */
197 private String name;
198
199 /***
200 * The MDB offset of first byte of card.
201 */
202 private long mdbOffset;
203
204 /***
205 * represents the associated amount
206 */
207 private int amount;
208
209 /***
210 * @param realCardName
211 */
212 public void setName(String realCardName) {
213 this.name = realCardName;
214 }
215
216 /***
217 * @param amount
218 */
219 public void setAmount(int amount) {
220 this.amount = amount;
221 }
222
223 /***
224 * @param mdbOffset
225 * The mdbOffset to set.
226 */
227 public void setMdbOffset(long mdbOffset) {
228 this.mdbOffset = mdbOffset;
229 }
230
231 /***
232 * Return an implementation of card corresponding to this card name. The
233 * returned instance cannot be modified and only contains the database data.
234 *
235 * @return a lightweight implementation of a card corresponding to this card
236 * name.
237 * @param dbStream
238 * the mdb stream's header.
239 * @throws IOException
240 * if io exception occurred.
241 */
242 public Targetable getCard(FileInputStream dbStream) throws IOException {
243 if (card == null) {
244 dbStream.getChannel().position(this.mdbOffset);
245 card = new MCard(this.name, dbStream, null, null, constraints);
246 }
247 return card;
248 }
249
250 /***
251 * Return an implementation of card corresponding to this card name. The
252 * returned instance cannot be modified and only contains the database data.
253 *
254 * @return a lightweight implementation of a card corresponding to this card
255 * name.
256 * @param dbStream
257 * the mdb stream's header.
258 * @throws IOException
259 * if io exception occurred.
260 */
261 public CardModel getModel(FileInputStream dbStream) throws IOException {
262 dbStream.getChannel().position(this.mdbOffset);
263 return CardFactory.getCardModel(name, dbStream);
264 }
265
266 @Override
267 public MCardCompare clone() {
268 MCardCompare clone = new MCardCompare(name, amount, constraints, mdbOffset);
269 clone.card = card;
270 return clone;
271 }
272
273 /***
274 * A lightweight implementation of a card corresponding to this card name.
275 */
276 private MCard card = null;
277 }