View Javadoc

1   /*
2    * Created on Nov 8, 2004 
3    * Original filename was Expression.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   */
23  package net.sf.magicproject.expression;
24  
25  import java.util.List;
26  import java.util.Map;
27  
28  import net.sf.magicproject.clickable.ability.Ability;
29  import net.sf.magicproject.clickable.targetable.Targetable;
30  import net.sf.magicproject.clickable.targetable.card.MCard;
31  import net.sf.magicproject.event.MEventListener;
32  import net.sf.magicproject.event.context.ContextEventListener;
33  import net.sf.magicproject.test.Test;
34  
35  /***
36   * This class represents an expression. Value type is <code>int</code> by
37   * default but it can also be an object. If the method <code>getObject()</code>
38   * is not overridden, the given object value will be the <code>Integer</code>
39   * representation of the <code>int</code> value.
40   * 
41   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
42   * @since 0.80
43   */
44  public abstract class Expression {
45  
46  	/***
47  	 * Creates a new instance of <code>Expression</code>.
48  	 */
49  	protected Expression() {
50  		super();
51  	}
52  
53  	/***
54  	 * Returns the object value of this expression.
55  	 * 
56  	 * @param ability
57  	 *          the ability owning this test. The card component of this ability
58  	 *          should correspond tho the card owning this test too.
59  	 * @param tested
60  	 *          the tested card
61  	 * @param context
62  	 *          the context event listener
63  	 * @return the object value of this expression
64  	 */
65  	public Object getObject(Ability ability, Targetable tested,
66  			ContextEventListener context) {
67  		return new Integer(getValue(ability, tested, context));
68  	}
69  
70  	/***
71  	 * Returns the class of the object value of this expression.
72  	 * 
73  	 * @return the class of the object value of this expression
74  	 */
75  	public Class<?> getObjectClass() {
76  		return int.class;
77  	}
78  
79  	/***
80  	 * Returns the integer value of this expression
81  	 * 
82  	 * @param ability
83  	 *          is the ability owning this test. The card component of this
84  	 *          ability should correspond to the card owning this test too.
85  	 * @param tested
86  	 *          the tested card
87  	 * @param context
88  	 *          is the context attached to this test.
89  	 * @return the integer value of this expression
90  	 */
91  	public abstract int getValue(Ability ability, Targetable tested,
92  			ContextEventListener context);
93  
94  	/***
95  	 * Returns the integer value of this expression exactly as it will be when the
96  	 * ability will be executed.
97  	 * 
98  	 * @param ability
99  	 *          is the ability owning this test. The card component of this
100 	 *          ability should correspond to the card owning this test too.
101 	 * @param tested
102 	 *          the tested card
103 	 * @param context
104 	 *          is the context attached to this test.
105 	 * @return the integer value of this expression. Returns -1 if is not
106 	 *         preemptable.
107 	 */
108 	public int getPreemptionValue(Ability ability, Targetable tested,
109 			ContextEventListener context) {
110 		return getValue(ability, tested, context);
111 	}
112 
113 	/***
114 	 * Returns this expression where values depending on values of this action
115 	 * have been replaced.
116 	 * 
117 	 * @param values
118 	 *          are referecable values.
119 	 * @return a parsed test.
120 	 * @since 0.85
121 	 */
122 	public Expression getConstraintExpression(Map<String, Expression> values) {
123 		return this;
124 	}
125 
126 	/***
127 	 * Adds to the specified list, the events modifying the result of this test.
128 	 * 
129 	 * @param res
130 	 *          is the list of events to fill
131 	 * @param source
132 	 *          is the card source of event
133 	 * @param globalTest
134 	 *          the optional global test to include in the event test.
135 	 */
136 	public void extractTriggeredEvents(List<MEventListener> res, MCard source,
137 			Test globalTest) {
138 		// Nothing to do
139 	}
140 
141 	/***
142 	 * Is this expression is a constant.
143 	 * 
144 	 * @return true if this expression is a constant.
145 	 */
146 	public boolean isConstant() {
147 		return false;
148 	}
149 
150 	/***
151 	 * Return true if the associated value can be evaluated without ability
152 	 * context.
153 	 * 
154 	 * @return true if the associated value can be evaluated without ability
155 	 *         context.
156 	 */
157 	public boolean canBePreempted() {
158 		return true;
159 	}
160 
161 }