1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
2 | //
|
---|
3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
4 | // you may not use this file except in compliance with the License.
|
---|
5 | // You may obtain a copy of the License at
|
---|
6 | //
|
---|
7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
8 | //
|
---|
9 | // Unless required by applicable law or agreed to in writing, software
|
---|
10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
12 | // See the License for the specific language governing permissions and
|
---|
13 | // limitations under the License.
|
---|
14 |
|
---|
15 | package de.ugoe.cs.autoquest.eventcore;
|
---|
16 |
|
---|
17 | import java.io.Serializable;
|
---|
18 | import java.util.HashMap;
|
---|
19 | import java.util.LinkedList;
|
---|
20 | import java.util.List;
|
---|
21 | import java.util.Map;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * <p>
|
---|
25 | * Base class for all events. An event is described by its {@link #type} and its {@link #target}.
|
---|
26 | * An event may be provided with a timestamp and further parameters.
|
---|
27 | * </p>
|
---|
28 | *
|
---|
29 | * @author Steffen Herbold
|
---|
30 | * @version 1.0
|
---|
31 | *
|
---|
32 | * @param <T>
|
---|
33 | * Can be used to declare that events belong to a specific platform without subclassing.
|
---|
34 | */
|
---|
35 | public final class Event implements Serializable {
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * <p>
|
---|
39 | * Id for object serialization.
|
---|
40 | * </p>
|
---|
41 | */
|
---|
42 | private static final long serialVersionUID = 1L;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * <p>
|
---|
46 | * Global start event that can be used to indicate the start of a sequence.
|
---|
47 | * </p>
|
---|
48 | */
|
---|
49 | public static final Event STARTEVENT = new Event(new StringEventType("START"));
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * <p>
|
---|
53 | * Global end event that can be used to indicate the end of a sequence.
|
---|
54 | */
|
---|
55 | public static final Event ENDEVENT = new Event(new StringEventType("END"));
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <p>
|
---|
59 | * Type of the event.
|
---|
60 | * </p>
|
---|
61 | */
|
---|
62 | private IEventType type;
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * <p>
|
---|
66 | * Target of the event.
|
---|
67 | * </p>
|
---|
68 | */
|
---|
69 | private IEventTarget target = null;
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * <p>
|
---|
73 | * Timestamp of when the event took place.
|
---|
74 | * </p>
|
---|
75 | */
|
---|
76 | private long timestamp = Long.MIN_VALUE;
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * <p>
|
---|
80 | * Map with further parameters of the event, which are not belonging to the type or target.
|
---|
81 | * </p>
|
---|
82 | */
|
---|
83 | private Map<String, String> parameters = new HashMap<String, String>();
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * <p>
|
---|
87 | * List of {@link IReplayable}s of type T that describes the replay of an event. The
|
---|
88 | * {@link IReplayable}s can be interpreted as <it>sub-events</it> on the platform level that
|
---|
89 | * make up the abstract event.
|
---|
90 | * </p>
|
---|
91 | */
|
---|
92 | private List<IReplayable> replay = new LinkedList<IReplayable>();
|
---|
93 |
|
---|
94 | /**
|
---|
95 | * <p>
|
---|
96 | * Constructor. Creates a new Event with a given type. The type must not be null.
|
---|
97 | * </p>
|
---|
98 | *
|
---|
99 | * @param type
|
---|
100 | * type of the event
|
---|
101 | */
|
---|
102 | public Event(IEventType type) {
|
---|
103 | if (type == null) {
|
---|
104 | throw new IllegalArgumentException("Event type must not be null");
|
---|
105 | }
|
---|
106 | this.type = type;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * <p>
|
---|
111 | * Constructor. Creates a new Event with a given type and target. The type must not be null.
|
---|
112 | * </p>
|
---|
113 | *
|
---|
114 | * @param type
|
---|
115 | * type of the event
|
---|
116 | * @param target
|
---|
117 | * target of the event
|
---|
118 | */
|
---|
119 | public Event(IEventType type, IEventTarget target) {
|
---|
120 | this(type);
|
---|
121 | this.target = target;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * <p>
|
---|
126 | * Two events are equal, if their {@link #type} and {@link #target} are equal. The timestamp
|
---|
127 | * and other parameters are ignored.
|
---|
128 | * </p>
|
---|
129 | * <p>
|
---|
130 | * See {@link Object#equals(Object)} for further information.
|
---|
131 | * </p>
|
---|
132 | *
|
---|
133 | * @param other
|
---|
134 | * Event that is compared to this
|
---|
135 | * @return true, if events are equal, false otherwise
|
---|
136 | */
|
---|
137 | @Override
|
---|
138 | public boolean equals(Object other) {
|
---|
139 | if (this == other) {
|
---|
140 | return true;
|
---|
141 | }
|
---|
142 | if (other instanceof Event) {
|
---|
143 | Event otherEvent = (Event) other;
|
---|
144 | if (target != null) {
|
---|
145 | return type.equals(otherEvent.type) && target.equals(otherEvent.target);
|
---|
146 | }
|
---|
147 | else {
|
---|
148 | return type.equals(otherEvent.type) && otherEvent.target == null;
|
---|
149 | }
|
---|
150 | }
|
---|
151 | return false;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * <p>
|
---|
156 | * Returns {@link #getId()} as String representation of the event.
|
---|
157 | * </p>
|
---|
158 | *
|
---|
159 | * @return String representation of the event
|
---|
160 | */
|
---|
161 | @Override
|
---|
162 | public String toString() {
|
---|
163 | return getId();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * <p>
|
---|
168 | * Returns the {@link #target} of the event.
|
---|
169 | * </p>
|
---|
170 | *
|
---|
171 | * @return {@link #target} of the event
|
---|
172 | */
|
---|
173 | public IEventTarget getTarget() {
|
---|
174 | return target;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * <p>
|
---|
179 | * Returns the {@link #type} of the event.
|
---|
180 | * </p>
|
---|
181 | *
|
---|
182 | * @return {@link #type} of the event
|
---|
183 | */
|
---|
184 | public IEventType getType() {
|
---|
185 | return type;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * <p>
|
---|
190 | * Returns the combination of {@link #type} and {@link #target} as id.
|
---|
191 | * </p>
|
---|
192 | *
|
---|
193 | * @return string of the form (type,target)
|
---|
194 | */
|
---|
195 | public String getId() {
|
---|
196 | String id = type.toString();
|
---|
197 | if( target!=null ) {
|
---|
198 | id += "." + target.getStringIdentifier();
|
---|
199 | }
|
---|
200 | return id;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * (non-Javadoc)
|
---|
205 | *
|
---|
206 | * @see java.lang.Object#hashCode()
|
---|
207 | */
|
---|
208 | @Override
|
---|
209 | public int hashCode() {
|
---|
210 | int multiplier = 17;
|
---|
211 | int hash = 42;
|
---|
212 | if (type != null) {
|
---|
213 | hash = multiplier * hash + type.hashCode();
|
---|
214 | }
|
---|
215 | if (target != null) {
|
---|
216 | hash = multiplier * hash + target.hashCode();
|
---|
217 | }
|
---|
218 | return hash;
|
---|
219 | }
|
---|
220 |
|
---|
221 | /**
|
---|
222 | * <p>
|
---|
223 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
224 | * </p>
|
---|
225 | *
|
---|
226 | * @param target
|
---|
227 | * target of the event
|
---|
228 | * @return true, if target was changed, false otherwise
|
---|
229 | */
|
---|
230 | public boolean setTarget(IEventTarget target) {
|
---|
231 | if (this.target != null) {
|
---|
232 | return false;
|
---|
233 | }
|
---|
234 | this.target = target;
|
---|
235 | return true;
|
---|
236 | }
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * <p>
|
---|
240 | * Adds a new {@link IReplayable} of type T to the replay sequence.
|
---|
241 | * </p>
|
---|
242 | *
|
---|
243 | * @param replayable
|
---|
244 | * element that is added to the sequence
|
---|
245 | * @throws IllegalArgumentException
|
---|
246 | * thrown is replayable is null
|
---|
247 | */
|
---|
248 | public void addReplayable(IReplayable replayable) {
|
---|
249 | if (replayable == null) {
|
---|
250 | throw new IllegalArgumentException("replayble must not be null");
|
---|
251 | }
|
---|
252 | replay.add(replayable);
|
---|
253 | }
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * <p>
|
---|
257 | * Adds a {@link List} of {@link IReplayable} to the replay sequence.
|
---|
258 | * </p>
|
---|
259 | *
|
---|
260 | * @param generatedReplaySeq
|
---|
261 | * {@link List} that is added to the sequence
|
---|
262 | * @throws IllegalArgumentException
|
---|
263 | * thrown if generatedReplaySeq is null
|
---|
264 | */
|
---|
265 | public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
|
---|
266 | if (generatedReplaySeq == null) {
|
---|
267 | throw new IllegalArgumentException("generatedReplaySeq must not be null");
|
---|
268 | }
|
---|
269 | replay.addAll(generatedReplaySeq);
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * <p>
|
---|
274 | * Adds a parameter to the event or sets it to a new value. The key must not be null. If a
|
---|
275 | * parameter with the specified key already exists, its value is replaced with the new one.
|
---|
276 | * If the value is null, the parameter with the specified key is removed, if it exists.
|
---|
277 | * </p>
|
---|
278 | *
|
---|
279 | * @param key the key of the parameter
|
---|
280 | * @param value the value of the parameter
|
---|
281 | *
|
---|
282 | * @throws IllegalArgumentException
|
---|
283 | * if the provided key is null
|
---|
284 | */
|
---|
285 | public void setParameter(String key, String value) {
|
---|
286 | if (key == null) {
|
---|
287 | throw new IllegalArgumentException("key must not be null");
|
---|
288 | }
|
---|
289 |
|
---|
290 | if (value == null) {
|
---|
291 | parameters.remove(key);
|
---|
292 | }
|
---|
293 | else {
|
---|
294 | parameters.put(key, value);
|
---|
295 | }
|
---|
296 | }
|
---|
297 |
|
---|
298 | /**
|
---|
299 | * <p>
|
---|
300 | * Returns the event parameter with the specified key if it exists or null if not. The key
|
---|
301 | * must not be null.
|
---|
302 | * </p>
|
---|
303 | *
|
---|
304 | * @param key the key of the parameter to be returned
|
---|
305 | *
|
---|
306 | * @return the value of the parameter with the specified key or null if there is no parameter
|
---|
307 | * with that key
|
---|
308 | *
|
---|
309 | * @throws IllegalArgumentException
|
---|
310 | * if the provided key is null
|
---|
311 | */
|
---|
312 | public String getParameter(String key) {
|
---|
313 | if (key == null) {
|
---|
314 | throw new IllegalArgumentException("key must not be null");
|
---|
315 | }
|
---|
316 |
|
---|
317 | return parameters.get(key);
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * <p>
|
---|
322 | * Sets the timestamp of the event, i.e. when the event occurred. A timestamp for events is
|
---|
323 | * optional. Therefore it is also ignored by the {@link #equals(Object)}-method. A timestamp
|
---|
324 | * with a value smaller 0 means, that now timestamp for the event exists.
|
---|
325 | * </p>
|
---|
326 | *
|
---|
327 | * @param timestamp the new value for the timestamp
|
---|
328 | */
|
---|
329 | public void setTimestamp(long timestamp) {
|
---|
330 | this.timestamp = timestamp;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * <p>
|
---|
335 | * Returns the timestamp of the event or a value lower than 0 if no timestamp for the event
|
---|
336 | * exists.
|
---|
337 | * </p>
|
---|
338 | *
|
---|
339 | * @return the timestamp of the event or a value lower than 0 if no timestamp exists
|
---|
340 | */
|
---|
341 | public long getTimestamp() {
|
---|
342 | return timestamp;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * <p>
|
---|
347 | * Returns a the list of replay events.
|
---|
348 | * </p>
|
---|
349 | * <p>
|
---|
350 | * The return value is a copy of the list used internally!
|
---|
351 | * </p>
|
---|
352 | *
|
---|
353 | * @return list of replay events.
|
---|
354 | */
|
---|
355 | public List<IReplayable> getReplayables() {
|
---|
356 | return new LinkedList<IReplayable>(replay);
|
---|
357 | }
|
---|
358 | }
|
---|