1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.magicproject.event.phase;
22
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26
27 import net.sf.magicproject.stack.EventManager;
28
29 /***
30 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
31 * @since 0.83
32 */
33 public enum PhaseFilter {
34
35 /***
36 * This code is used to do a test on phase's index
37 */
38 INDEX_PHASE_FILTER,
39
40 /***
41 * This code is used to do a test on phase's identifiant
42 */
43 ID_PHASE_FILTER;
44
45 /***
46 * Return the identifiant or the index of current phase.
47 *
48 * @return the identifiant or the index of current phase.
49 */
50 public int getPhaseFilter() {
51 switch (this) {
52 case INDEX_PHASE_FILTER:
53 return EventManager.phaseIndex;
54 case ID_PHASE_FILTER:
55 default:
56 return EventManager.currentIdPhase;
57 }
58 }
59
60 /***
61 * Set the identifiant or the index of next phase.
62 *
63 * @param idPhase
64 * the identifiant or the index of next phase.
65 */
66 public void setNextPhase(int idPhase) {
67 switch (this) {
68 case INDEX_PHASE_FILTER:
69 EventManager.nextPhaseIndex = idPhase;
70 break;
71 case ID_PHASE_FILTER:
72 for (int i = 0; i < EventManager.turnStructure.length; i++) {
73 if (EventManager.turnStructure[i].id == idPhase) {
74 EventManager.nextPhaseIndex = i;
75 return;
76 }
77 }
78 throw new InternalError("Unknow idPhase (id) : " + idPhase);
79 default:
80 }
81
82 }
83
84 /***
85 * Wrtite this enum to the given outputstream.
86 *
87 * @param out
88 * the stream ths enum would be written.
89 * @throws IOException
90 * error while writting event's id
91 */
92 public void write(OutputStream out) throws IOException {
93 out.write(ordinal());
94 }
95
96 /***
97 * Read and return the enum from the given inputstream.
98 *
99 * @param input
100 * the stream containing the enum to read.
101 * @return the enum from the given inputstream.
102 * @throws IOException
103 * error while reading event's id.
104 */
105 public static PhaseFilter valueOf(InputStream input) throws IOException {
106 return values()[input.read()];
107 }
108 }