View Javadoc

1   /*
2    *   Magic-Project 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.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  		// Nothing to do
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  		// Add the 'none' deck constraint
89  		constraints.clear();
90  		constraints.add(new DeckConstraint(DECK_CONSTRAINT_NAME_NONE, True
91  				.getInstance()));
92  
93  		// Read the user defined constraints
94  		final int count = dbStream.read();
95  		for (int i = count; i-- > 0;) {
96  			// Read constraint name
97  			String deckName = MToolKit.readString(dbStream);
98  
99  			// Read extend
100 			String extend = MToolKit.readString(dbStream);
101 
102 			// Read constraint
103 			Test constraint = TestFactory.readNextTest(dbStream);
104 
105 			// Create the deck constraint with extend
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 }