View Javadoc

1   /*
2    * Created on Aug 3, 2004 
3    * Original filename was Xor.java
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.annotation.XmlTestElement;
29  import net.sf.magicproject.clickable.ability.Ability;
30  import net.sf.magicproject.clickable.targetable.Targetable;
31  import net.sf.magicproject.expression.Expression;
32  
33  /***
34   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
35   * @since 0.54
36   */
37  @XmlTestElement(id = IdTest.XOR)
38  public class Xor extends BinaryTest {
39  
40  	/***
41  	 * Create an instance of Or 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  	Xor(InputStream inputFile) throws IOException {
56  		super(inputFile);
57  	}
58  
59  	/***
60  	 * Creates a new instance of Xor specifying all attributes of this class. All
61  	 * parameters are copied, not cloned. So this new object shares the card and
62  	 * the specified codes
63  	 * 
64  	 * @param leftTest
65  	 *          the left boolean expression
66  	 * @param rightTest
67  	 *          the right boolean expression
68  	 */
69  	private Xor(Test leftTest, Test rightTest) {
70  		super(leftTest, rightTest);
71  	}
72  
73  	@Override
74  	public Test getConstraintTest(HashMap<String, Expression> values) {
75  		return new Xor(leftTest.getConstraintTest(values), rightTest
76  				.getConstraintTest(values));
77  	}
78  
79  	@Override
80  	public boolean test(Ability ability, Targetable tested) {
81  		boolean left = leftTest.test(ability, tested);
82  		boolean right = rightTest.test(ability, tested);
83  		return left && !right || !left && right;
84  	}
85  
86  	@Override
87  	public boolean testPreemption(Ability ability, Targetable tested) {
88  		boolean left = leftTest.testPreemption(ability, tested);
89  		boolean right = rightTest.testPreemption(ability, tested);
90  		return left && !right || !left && right;
91  	}
92  
93  	@Override
94  	public String toString() {
95  		return "(" + leftTest.toString() + " XOR " + rightTest.toString() + ")";
96  	}
97  }