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