View Javadoc

1   /*
2    * Created on Jul 5, 2004 
3    * Original filename was MContextCardCardIntInt.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  package net.sf.magicproject.event.context;
23  
24  import net.sf.magicproject.clickable.targetable.Targetable;
25  import net.sf.magicproject.clickable.targetable.card.MCard;
26  
27  /***
28   * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
29   * @since 0.63
30   */
31  public class MContextCardCardIntInt extends MContextMtargetable {
32  
33  	/***
34  	 * Creates a new instance of MContextCardCardIntInt <br>
35  	 * 
36  	 * @param dest
37  	 *          the MTargetable object to save to this context.
38  	 */
39  	public MContextCardCardIntInt(Targetable dest) {
40  		this(dest, null, 0, 0, -1, -1);
41  	}
42  
43  	/***
44  	 * Creates a new instance of MContextCardCardIntInt <br>
45  	 * 
46  	 * @param dest
47  	 *          the MTargetable object to save to this context.
48  	 * @param value
49  	 *          the integer to save to this context.
50  	 */
51  	public MContextCardCardIntInt(Targetable dest, int value) {
52  		this(dest, null, value, 0, -1, -1);
53  	}
54  
55  	/***
56  	 * Creates a new instance of MContextCardCardIntInt <br>
57  	 * 
58  	 * @param dest
59  	 *          the MTargetable object to save to this context.
60  	 * @param source
61  	 *          the Mcard object to save to this context.
62  	 */
63  	public MContextCardCardIntInt(Targetable dest, MCard source) {
64  		this(dest, source, 0, 0, -1, -1);
65  	}
66  
67  	/***
68  	 * Creates a new instance of MContextCardCardIntInt <br>
69  	 * 
70  	 * @param dest
71  	 *          the MTargetable object to save to this context.
72  	 * @param source
73  	 *          the Mcard object to save to this context.
74  	 * @param value
75  	 *          the integer to save to this context.
76  	 * @param value2
77  	 *          the integer to save to this context.
78  	 */
79  	public MContextCardCardIntInt(Targetable dest, MCard source, int value,
80  			int value2) {
81  		this(dest, source, value, value2, -1, -1);
82  	}
83  
84  	/***
85  	 * Creates a new instance of MContextCardCardIntInt <br>
86  	 * 
87  	 * @param dest
88  	 *          the MTargetable object to save to this context.
89  	 * @param source
90  	 *          the Mcard object to save to this context.
91  	 * @param value
92  	 *          the integer to save to this context.
93  	 * @param value2
94  	 *          the integer to save to this context.
95  	 * @param maxTimeStamp1
96  	 *          is the maximum timestamp allowed for destination card during the
97  	 *          resolution.
98  	 * @param maxTimeStamp2
99  	 *          is the maximum timestamp allowed for source card during the
100 	 *          resolution.
101 	 */
102 	public MContextCardCardIntInt(Targetable dest, MCard source, int value,
103 			int value2, int maxTimeStamp1, int maxTimeStamp2) {
104 		super(dest, maxTimeStamp1);
105 		if (source != null) {
106 			this.card = (MCard) source.getOriginalTargetable();
107 			this.card.addTimestampReference();
108 		}
109 		if (maxTimeStamp2 == -1 && source != null) {
110 			this.timeStamp2 = this.card.getTimestamp();
111 		} else {
112 			this.timeStamp2 = maxTimeStamp2;
113 		}
114 		this.intToSave = value;
115 		this.value2 = value2;
116 	}
117 
118 	/***
119 	 * Return the MTargetable cast to MCard object of this context considering
120 	 * it's timstamp. The returned object is the same as it was when this context
121 	 * has been created.
122 	 * 
123 	 * @return The other Card object that was saved.
124 	 */
125 	public MCard getCard2() {
126 		if (card == null) {
127 			return null;
128 		}
129 		if (timeStamp2 > card.getTimestamp()) {
130 			return (MCard) card.getLastKnownTargetable(card.getTimestamp());
131 		}
132 		return (MCard) card.getLastKnownTargetable(timeStamp2);
133 	}
134 
135 	/***
136 	 * Return the MTargetable cast to MCard object of this context without
137 	 * considering it's timstamp.
138 	 * 
139 	 * @return the MTargetable cast to MCard object of this context without
140 	 *         considering it's timstamp.
141 	 */
142 	public MCard getOriginalCard2() {
143 		return card;
144 	}
145 
146 	@Override
147 	public void removeTimestamp() {
148 		super.removeTimestamp();
149 		if (card != null) {
150 			if (timeStamp2 > card.getTimestamp()) {
151 				card.decrementTimestampReference(card.getTimestamp());
152 			} else {
153 				card.decrementTimestampReference(timeStamp2);
154 			}
155 		}
156 	}
157 
158 	/***
159 	 * Returns The integer that was saved.
160 	 * 
161 	 * @return The integer that was saved.
162 	 */
163 	public final int getValue() {
164 		return intToSave;
165 	}
166 
167 	/***
168 	 * Returns The other integer that was saved.
169 	 * 
170 	 * @return The other integer that was saved.
171 	 */
172 	public final int getValue2() {
173 		return value2;
174 	}
175 
176 	@Override
177 	public boolean checkTimeStamp(MCard card) {
178 		if (this.card == card) {
179 			return card.getTimestamp() <= timeStamp2;
180 		}
181 		return super.checkTimeStamp(card);
182 	}
183 
184 	/***
185 	 * Indicates wether the second card is null or not.
186 	 * 
187 	 * @return true if the second card is null.
188 	 */
189 	public boolean isNull2() {
190 		return card == null;
191 	}
192 
193 	@Override
194 	public boolean equals(Object context) {
195 		return super.equals(context) && context instanceof MContextCardCardIntInt
196 				&& ((MContextCardCardIntInt) context).card == card
197 				&& ((MContextCardCardIntInt) context).intToSave == intToSave
198 				&& ((MContextCardCardIntInt) context).value2 == value2
199 				&& ((MContextCardCardIntInt) context).timeStamp2 == timeStamp2;
200 	}
201 
202 	@Override
203 	public int hashCode() {
204 		return card.hashCode();
205 	}
206 
207 	@Override
208 	public String toString() {
209 		return new StringBuilder("{source=").append(eventSourceCard).append(
210 				",context1=").append(targetToSave).append(",context2=").append(card)
211 				.append(",int1=").append(intToSave + ",int2=").append(value2).append(
212 						"}").toString();
213 	}
214 
215 	/***
216 	 * This timestamp corresponds to the amount of card movements.
217 	 */
218 	private int timeStamp2;
219 
220 	/***
221 	 * The second card that was saved.
222 	 */
223 	private MCard card;
224 
225 	/***
226 	 * The integer that was saved.
227 	 */
228 	private int intToSave;
229 
230 	/***
231 	 * The other integer that was saved.
232 	 */
233 	private int value2;
234 
235 }