1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 }