View Javadoc

1   /*
2    * And.java 
3    * Created on 25 feb. 2004
4    * 
5    *   Magic-Project is a turn based strategy simulator
6    *   Copyright (C) 2003-2007 Fabrice Daugan
7    *
8    *   This program is free software; you can redistribute it and/or modify it 
9    * under the terms of the GNU General Public License as published by the Free 
10   * Software Foundation; either version 2 of the License, or (at your option) any
11   * later version.
12   *
13   *   This program is distributed in the hope that it will be useful, but WITHOUT 
14   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
16   * details.
17   *
18   *   You should have received a copy of the GNU General Public License along  
19   * with this program; if not, write to the Free Software Foundation, Inc., 
20   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21   */
22  package net.sf.magicproject.test;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.util.HashMap;
27  
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.Targetable;
30  import net.sf.magicproject.clickable.targetable.player.Player;
31  import net.sf.magicproject.event.context.ContextEventListener;
32  import net.sf.magicproject.expression.Expression;
33  
34  /***
35   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
36   * @since 0.54
37   */
38  public class And extends BinaryTest {
39  
40  	/***
41  	 * Create an instance of And by reading a file. Offset's file must pointing on
42  	 * the first byte of this test <br>
43  	 * <ul>
44  	 * Structure of InputStream : Data[size]
45  	 * <li>left expression [...]</li>
46  	 * <li>right expression [...]</li>
47  	 * </ul>
48  	 * 
49  	 * @param inputFile
50  	 *          is the file containing this event
51  	 * @throws IOException
52  	 *           if error occurred during the reading process from the specified
53  	 *           input stream
54  	 */
55  	And(InputStream inputFile) throws IOException {
56  		super(inputFile);
57  	}
58  
59  	/***
60  	 * Return a new test equals to "leftTest && rightTest"
61  	 * 
62  	 * @param tests
63  	 *          the list of tests to append
64  	 * @return a new test equals to "leftTest && rightTest"
65  	 */
66  	public static Test append(Test... tests) {
67  		Test workingTest = null;
68  		for (Test test : tests) {
69  			if (test != null && test != True.getInstance()) {
70  				if (workingTest != null)
71  					workingTest = new And(workingTest, test);
72  				else
73  					workingTest = test;
74  			}
75  		}
76  		if (workingTest == null)
77  			return True.getInstance();
78  		return workingTest;
79  	}
80  
81  	/***
82  	 * Creates a new instance of And specifying all attributes of this class. All
83  	 * parameters are copied, not cloned. So this new object shares the card and
84  	 * the specified codes
85  	 * 
86  	 * @param leftTest
87  	 *          the left boolean expression
88  	 * @param rightTest
89  	 *          the right boolean expression
90  	 */
91  	private And(Test leftTest, Test rightTest) {
92  		super(leftTest, rightTest);
93  	}
94  
95  	@Override
96  	public Test getConstraintTest(HashMap<String, Expression> values) {
97  		return new And(leftTest.getConstraintTest(values), rightTest
98  				.getConstraintTest(values));
99  	}
100 
101 	@Override
102 	public boolean test(Ability ability, Targetable tested) {
103 		return leftTest.test(ability, tested) && rightTest.test(ability, tested);
104 	}
105 
106 	@Override
107 	public Player getOptimizedController(Ability ability,
108 			ContextEventListener context) {
109 		final Player controller = leftTest.getOptimizedController(ability, context);
110 		if (controller == null) {
111 			return rightTest.getOptimizedController(ability, context);
112 		}
113 		if (rightTest.getOptimizedController(ability, context) == null) {
114 			return controller;
115 		}
116 		return null;
117 	}
118 
119 	@Override
120 	public boolean testPreemption(Ability ability, Targetable tested) {
121 		return leftTest.testPreemption(ability, tested)
122 				&& rightTest.testPreemption(ability, tested);
123 	}
124 
125 	@Override
126 	public String toString() {
127 		return "(" + leftTest.toString() + " AND " + rightTest.toString() + ")";
128 	}
129 
130 }