View Javadoc

1   /*
2    * Created on Nov 8, 2004 
3    * Original filename was UnaryExpression.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.io.IOException;
26  import java.io.InputStream;
27  import java.util.List;
28  
29  import net.sf.magicproject.clickable.ability.Ability;
30  import net.sf.magicproject.clickable.targetable.Targetable;
31  import net.sf.magicproject.clickable.targetable.card.MCard;
32  import net.sf.magicproject.event.MEventListener;
33  import net.sf.magicproject.event.context.ContextEventListener;
34  import net.sf.magicproject.operation.Operation;
35  import net.sf.magicproject.test.Test;
36  
37  /***
38   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
39   * @since 0.80
40   */
41  public class UnaryExpression extends Expression {
42  
43  	/***
44  	 * Creates a new instance of UnaryExpression <br>
45  	 * 
46  	 * @param op
47  	 *          the operation used by this expression.
48  	 * @param expr
49  	 *          the expression used for this expression
50  	 */
51  	public UnaryExpression(Operation op, Expression expr) {
52  		super();
53  		this.op = op;
54  		this.expr = expr;
55  	}
56  
57  	/***
58  	 * Create a new instance of this class.
59  	 * 
60  	 * @param inputFile
61  	 *          file containing this action
62  	 * @param op
63  	 *          optional operation.
64  	 * @throws IOException
65  	 *           if error occurred during the reading process from the specified
66  	 *           input stream
67  	 */
68  
69  	public UnaryExpression(InputStream inputFile, Operation op)
70  			throws IOException {
71  		expr = ExpressionFactory.readNextExpression(inputFile);
72  		this.op = op;
73  	}
74  
75  	@Override
76  	public int getValue(Ability ability, Targetable tested,
77  			ContextEventListener context) {
78  		return op.process(expr.getValue(ability, tested, context), 0);
79  	}
80  
81  	@Override
82  	public void extractTriggeredEvents(List<MEventListener> res, MCard source,
83  			Test globalTest) {
84  		expr.extractTriggeredEvents(res, source, globalTest);
85  	}
86  
87  	@Override
88  	public boolean canBePreempted() {
89  		return expr.canBePreempted();
90  	}
91  
92  	/***
93  	 * The operation used by this expression
94  	 */
95  	private final Operation op;
96  
97  	/***
98  	 * The expression used for this expression
99  	 */
100 	protected final Expression expr;
101 
102 }