View Javadoc

1   /*
2    * Created on 14 mars 2005
3    * 
4    *   Magic-Project is a turn based strategy simulator
5    *   Copyright (C) 2003-2007 Fabrice Daugan
6    *
7    *   This program is free software; you can redistribute it and/or modify it 
8    * under the terms of the GNU General Public License as published by the Free 
9    * Software Foundation; either version 2 of the License, or (at your option) any
10   * later version.
11   *
12   *   This program is distributed in the hope that it will be useful, but WITHOUT 
13   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
15   * details.
16   *
17   *   You should have received a copy of the GNU General Public License along  
18   * with this program; if not, write to the Free Software Foundation, Inc., 
19   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }