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.clickable.ability.Ability;
29 import net.sf.magicproject.clickable.targetable.Targetable;
30 import net.sf.magicproject.expression.Expression;
31
32 /***
33 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
34 * @since 0.54
35 */
36 public class Or extends BinaryTest {
37
38 /***
39 * Create an instance of Or by reading a file. Offset's file must pointing on
40 * the first byte of this test <br>
41 * <ul>
42 * Structure of InputStream : Data[size]
43 * <li>left expression [...]</li>
44 * <li>right expression [...]</li>
45 * </ul>
46 *
47 * @param inputFile
48 * is the file containing this event
49 * @throws IOException
50 * if error occurred during the reading process from the specified
51 * input stream
52 */
53 Or(InputStream inputFile) throws IOException {
54 super(inputFile);
55 }
56
57 /***
58 * Creates a new instance of Or specifying all attributes of this class. All
59 * parameters are copied, not cloned. So this new object shares the card and
60 * the specified codes
61 *
62 * @param leftTest
63 * the left boolean expression
64 * @param rightTest
65 * the right boolean expression
66 */
67 public Or(Test leftTest, Test rightTest) {
68 super(leftTest, rightTest);
69 }
70
71 /***
72 * Return this test where values depending on values of this action have been
73 * replaced.
74 *
75 * @param values
76 * are referecable values.
77 * @return a parsed test.
78 * @since 0.85
79 */
80 @Override
81 public Test getConstraintTest(HashMap<String, Expression> values) {
82 return new Or(leftTest.getConstraintTest(values), rightTest
83 .getConstraintTest(values));
84 }
85
86 @Override
87 public boolean test(Ability ability, Targetable tested) {
88 return leftTest.test(ability, tested) || rightTest.test(ability, tested);
89 }
90
91 /***
92 * Return a new test equals to "leftTest || rightTest"
93 *
94 * @param leftTest
95 * the left test
96 * @param rightTest
97 * the right test
98 * @return a new test equals to "leftTest || rightTest"
99 */
100 public static Test append(Test leftTest, Test rightTest) {
101 if (leftTest == True.getInstance()) {
102 return rightTest;
103 }
104 if (rightTest == True.getInstance()) {
105 return leftTest;
106 }
107 return new Or(leftTest, rightTest);
108 }
109
110 @Override
111 public boolean testPreemption(Ability ability, Targetable tested) {
112 return leftTest.testPreemption(ability, tested)
113 || rightTest.testPreemption(ability, tested);
114 }
115
116 @Override
117 public String toString() {
118 return "(" + leftTest.toString() + " OR " + rightTest.toString() + ")";
119 }
120 }