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