View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * 
19   */
20  package net.sf.magicproject.expression;
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import net.sf.magicproject.clickable.ability.Ability;
26  import net.sf.magicproject.clickable.targetable.Targetable;
27  import net.sf.magicproject.event.context.ContextEventListener;
28  import net.sf.magicproject.expression.intlist.ListType;
29  
30  /***
31   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
32   * @since 0.92
33   */
34  public class ListExpression {
35  
36  	/***
37  	 * Creates a new instance of ListExpression <br>
38  	 * <ul>
39  	 * Structure of InputStream : Data[size]
40  	 * <li>list type [1]</li>
41  	 * <li>nb values : [1]</li>
42  	 * <li>value i : Expression [...]</li>
43  	 * </ul>
44  	 * 
45  	 * @param inputFile
46  	 *          file containing this action
47  	 * @throws IOException
48  	 *           if error occurred during the reading process from the specified
49  	 *           input stream
50  	 */
51  	public ListExpression(InputStream inputFile) throws IOException {
52  		super();
53  		listType = ListType.values()[inputFile.read()];
54  		values = new Expression[inputFile.read()];
55  		for (int i = 0; i < values.length; i++) {
56  			values[i] = ExpressionFactory.readNextExpression(inputFile);
57  		}
58  	}
59  
60  	/***
61  	 * Return the computed integer list.
62  	 * 
63  	 * @param ability
64  	 *          is the ability owning this test. The card component of this
65  	 *          ability should correspond to the card owning this test too.
66  	 * @param tested
67  	 *          the tested card
68  	 * @param context
69  	 *          is the context attached to this test.
70  	 * @return the computed integer list.
71  	 */
72  	public int[] getList(Ability ability, Targetable tested,
73  			ContextEventListener context) {
74  		final int[] result = new int[values.length];
75  		for (int i = 0; i < values.length; i++) {
76  			result[i] = values[i].getValue(ability, ability.getCard(), context);
77  		}
78  		return listType.getList(result);
79  	}
80  
81  	/***
82  	 * Is this list is empty.
83  	 * 
84  	 * @return true if this list is empty.
85  	 */
86  	public boolean isEmpty() {
87  		return values.length == 0;
88  	}
89  
90  	/***
91  	 * The integer values of this expression
92  	 */
93  	private final Expression[] values;
94  
95  	/***
96  	 * The type of this list
97  	 */
98  	private final ListType listType;
99  
100 }