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.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 }