View Javadoc

1   /*
2    * Or.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.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 }