1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.deckbuilder;
20
21 import java.io.FileInputStream;
22 import java.io.IOException;
23 import java.util.Collection;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26
27 import net.sf.magicproject.test.And;
28 import net.sf.magicproject.test.Test;
29 import net.sf.magicproject.test.TestFactory;
30 import net.sf.magicproject.test.True;
31 import net.sf.magicproject.tools.MToolKit;
32
33 /***
34 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35 * @since 0.94
36 */
37 public class DeckConstraints {
38
39 /***
40 * The null deck constraint key name.
41 */
42 public final static String DECK_CONSTRAINT_NAME_NONE = "none";
43
44 /***
45 * The defined deck constraints.
46 */
47 private final static SortedSet<DeckConstraint> constraints = new TreeSet<DeckConstraint>();;
48
49 /***
50 * Singleton constructor.
51 */
52 private DeckConstraints() {
53
54 }
55
56 /***
57 * Return the constraint associated to the given name.
58 *
59 * @param deckConstraint
60 * the constraint name.
61 * @return the constraint associated to the given name.
62 */
63 public static DeckConstraint getDeckConstraint(String deckConstraint) {
64 for (DeckConstraint constraint : constraints) {
65 if (constraint.getName().equalsIgnoreCase(deckConstraint))
66 return constraint;
67 }
68 return null;
69 }
70
71 /***
72 * Return the available constraints.
73 *
74 * @return the available constraints.
75 */
76 public static Collection<DeckConstraint> getDeckConstraints() {
77 return constraints;
78 }
79
80 /***
81 * Initialize the deck constraints.
82 *
83 * @param dbStream
84 * @throws IOException
85 * If some other I/O error occurs
86 */
87 public static void init(FileInputStream dbStream) throws IOException {
88
89 constraints.clear();
90 constraints.add(new DeckConstraint(DECK_CONSTRAINT_NAME_NONE, True
91 .getInstance()));
92
93
94 final int count = dbStream.read();
95 for (int i = count; i-- > 0;) {
96
97 String deckName = MToolKit.readString(dbStream);
98
99
100 String extend = MToolKit.readString(dbStream);
101
102
103 Test constraint = TestFactory.readNextTest(dbStream);
104
105
106 if (extend.length() > 0) {
107 DeckConstraint extendConstraint = getDeckConstraint(extend);
108 if (extendConstraint == null) {
109 throw new RuntimeException(
110 "'"
111 + deckName
112 + "' is supposed extending '"
113 + extend
114 + "' but has not been found. Note that declaration order is important.");
115 }
116 constraint = And.append(getDeckConstraint(extend).getConstraint(),
117 constraint);
118 }
119 constraints.add(new DeckConstraint(deckName, constraint));
120 }
121 }
122 }