View Javadoc

1   /*
2    *   Firemox is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  package net.sf.firemox.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.firemox.expression.ExpressionFactory;
28  import net.sf.firemox.test.And;
29  import net.sf.firemox.test.Test;
30  import net.sf.firemox.test.TestFactory;
31  import net.sf.firemox.test.True;
32  import net.sf.firemox.tools.MToolKit;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.94
37   */
38  public class DeckConstraints {
39  
40  	/***
41  	 * The null deck constraint key name.
42  	 */
43  	public static final String DECK_CONSTRAINT_NAME_NONE = "none";
44  
45  	/***
46  	 * The defined deck constraints.
47  	 */
48  	private static final SortedSet<DeckConstraint> CONSTRAINTS = new TreeSet<DeckConstraint>();
49  
50  	private static int minProperty;
51  
52  	private static int maxProperty;
53  
54  	/***
55  	 * MASTER property for the game. Example: MTG -> Changeling
56  	 */
57  	public static int MASTER;
58  
59  	/***
60  	 * Singleton constructor.
61  	 */
62  	private DeckConstraints() {
63  		// Nothing to do
64  	}
65  
66  	/***
67  	 * Return the constraint associated to the given name.
68  	 * 
69  	 * @param deckConstraint
70  	 *          the constraint name.
71  	 * @return the constraint associated to the given name.
72  	 */
73  	public static DeckConstraint getDeckConstraint(String deckConstraint) {
74  		for (DeckConstraint constraint : CONSTRAINTS) {
75  			if (constraint.getName().equalsIgnoreCase(deckConstraint))
76  				return constraint;
77  		}
78  		return null;
79  	}
80  
81  	/***
82  	 * Return the available constraints.
83  	 * 
84  	 * @return the available constraints.
85  	 */
86  	public static Collection<DeckConstraint> getDeckConstraints() {
87  		return CONSTRAINTS;
88  	}
89  
90  	/***
91  	 * Initialize the deck constraints.
92  	 * 
93  	 * @param dbStream
94  	 * @throws IOException
95  	 *           If some other I/O error occurs
96  	 */
97  	public static void init(FileInputStream dbStream) throws IOException {
98  		// Add the 'none' deck constraint
99  		CONSTRAINTS.clear();
100 		CONSTRAINTS.add(new DeckConstraint(DECK_CONSTRAINT_NAME_NONE, True
101 				.getInstance()));
102 
103 		// Read the user defined constraints
104 		minProperty = ExpressionFactory.readNextExpression(dbStream).getValue(null,
105 				null, null);
106 		maxProperty = ExpressionFactory.readNextExpression(dbStream).getValue(null,
107 				null, null);
108 		MASTER = ExpressionFactory.readNextExpression(dbStream).getValue(null,
109 				null, null);
110 		final int count = dbStream.read();
111 		for (int i = count; i-- > 0;) {
112 			// Read constraint name
113 			String deckName = MToolKit.readString(dbStream);
114 
115 			// Read extend
116 			String extend = MToolKit.readString(dbStream);
117 
118 			// Read constraint
119 			Test constraint = TestFactory.readNextTest(dbStream);
120 
121 			// Create the deck constraint with extend
122 			if (extend.length() > 0) {
123 				DeckConstraint extendConstraint = getDeckConstraint(extend);
124 				if (extendConstraint == null) {
125 					throw new RuntimeException(
126 							"'"
127 									+ deckName
128 									+ "' is supposed extending '"
129 									+ extend
130 									+ "' but has not been found. Note that declaration order is important.");
131 				}
132 				constraint = And.append(getDeckConstraint(extend).getConstraint(),
133 						constraint);
134 			}
135 			CONSTRAINTS.add(new DeckConstraint(deckName, constraint));
136 		}
137 	}
138 
139 	/***
140 	 * Return
141 	 * 
142 	 * @return Returns the minProperty.
143 	 */
144 	public static int getMinProperty() {
145 		return minProperty;
146 	}
147 
148 	/***
149 	 * Return
150 	 * 
151 	 * @return Returns the maxProperty.
152 	 */
153 	public static int getMaxProperty() {
154 		return maxProperty;
155 	}
156 }