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 | */
|
---|
33 | public final class Event implements Serializable {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * <p>
|
---|
37 | * Id for object serialization.
|
---|
38 | * </p>
|
---|
39 | */
|
---|
40 | private static final long serialVersionUID = 1L;
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * <p>
|
---|
44 | * Global start event that can be used to indicate the start of a sequence.
|
---|
45 | * </p>
|
---|
46 | */
|
---|
47 | public static final Event STARTEVENT = new Event(new StringEventType("START"));
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * <p>
|
---|
51 | * Global end event that can be used to indicate the end of a sequence.
|
---|
52 | */
|
---|
53 | public static final Event ENDEVENT = new Event(new StringEventType("END"));
|
---|
54 |
|
---|
55 | /**
|
---|
56 | * <p>
|
---|
57 | * Type of the event.
|
---|
58 | * </p>
|
---|
59 | */
|
---|
60 | private IEventType type;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * <p>
|
---|
64 | * Target of the event.
|
---|
65 | * </p>
|
---|
66 | */
|
---|
67 | private IEventTarget target = null;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * <p>
|
---|
71 | * Timestamp of when the event took place.
|
---|
72 | * </p>
|
---|
73 | */
|
---|
74 | private long timestamp = Long.MIN_VALUE;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * <p>
|
---|
78 | * Map with further parameters of the event, which are not belonging to the type or target.
|
---|
79 | * </p>
|
---|
80 | */
|
---|
81 | private Map<String, String> parameters;
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * <p>
|
---|
85 | * List of {@link IReplayable}s of type T that describes the replay of an event. The
|
---|
86 | * {@link IReplayable}s can be interpreted as sub-events on the platform level that
|
---|
87 | * make up the abstract event.
|
---|
88 | * </p>
|
---|
89 | */
|
---|
90 | private List<IReplayable> replay;
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * <p>
|
---|
94 | * Constructor. Creates a new Event with a given type. The type must not be null.
|
---|
95 | * </p>
|
---|
96 | *
|
---|
97 | * @param type
|
---|
98 | * type of the event
|
---|
99 | */
|
---|
100 | public Event(IEventType type) {
|
---|
101 | if (type == null) {
|
---|
102 | throw new IllegalArgumentException("Event type must not be null");
|
---|
103 | }
|
---|
104 | this.type = type;
|
---|
105 | }
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * <p>
|
---|
109 | * Constructor. Creates a new Event with a given type and target. The type must not be null.
|
---|
110 | * </p>
|
---|
111 | *
|
---|
112 | * @param type
|
---|
113 | * type of the event
|
---|
114 | * @param target
|
---|
115 | * target of the event
|
---|
116 | */
|
---|
117 | public Event(IEventType type, IEventTarget target) {
|
---|
118 | this(type);
|
---|
119 | this.target = target;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * <p>
|
---|
124 | * Two events are equal, if their {@link #type} and {@link #target} are equal. The timestamp
|
---|
125 | * and other parameters are ignored.
|
---|
126 | * </p>
|
---|
127 | * <p>
|
---|
128 | * See {@link Object#equals(Object)} for further information.
|
---|
129 | * </p>
|
---|
130 | *
|
---|
131 | * @param other
|
---|
132 | * Event that is compared to this
|
---|
133 | * @return true, if events are equal, false otherwise
|
---|
134 | */
|
---|
135 | @Override
|
---|
136 | public boolean equals(Object other) {
|
---|
137 | if (this == other) {
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 | if (other instanceof Event) {
|
---|
141 | Event otherEvent = (Event) other;
|
---|
142 | if (target != null) {
|
---|
143 | return type.equals(otherEvent.type) && target.equals(otherEvent.target);
|
---|
144 | }
|
---|
145 | else {
|
---|
146 | return type.equals(otherEvent.type) && otherEvent.target == null;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | return false;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * <p>
|
---|
154 | * Returns {@link #getId()} as String representation of the event.
|
---|
155 | * </p>
|
---|
156 | *
|
---|
157 | * @return String representation of the event
|
---|
158 | */
|
---|
159 | @Override
|
---|
160 | public String toString() {
|
---|
161 | return getId();
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * <p>
|
---|
166 | * Returns the {@link #target} of the event.
|
---|
167 | * </p>
|
---|
168 | *
|
---|
169 | * @return {@link #target} of the event
|
---|
170 | */
|
---|
171 | public IEventTarget getTarget() {
|
---|
172 | return target;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * <p>
|
---|
177 | * Returns the {@link #type} of the event.
|
---|
178 | * </p>
|
---|
179 | *
|
---|
180 | * @return {@link #type} of the event
|
---|
181 | */
|
---|
182 | public IEventType getType() {
|
---|
183 | return type;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * <p>
|
---|
188 | * Returns the combination of {@link #type} and {@link #target} as id.
|
---|
189 | * </p>
|
---|
190 | *
|
---|
191 | * @return string of the form (type,target)
|
---|
192 | */
|
---|
193 | public String getId() {
|
---|
194 | String id = type.toString();
|
---|
195 | if( target!=null ) {
|
---|
196 | id += "." + target.getStringIdentifier();
|
---|
197 | }
|
---|
198 | return id;
|
---|
199 | }
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * (non-Javadoc)
|
---|
203 | *
|
---|
204 | * @see java.lang.Object#hashCode()
|
---|
205 | */
|
---|
206 | @Override
|
---|
207 | public int hashCode() {
|
---|
208 | int multiplier = 17;
|
---|
209 | int hash = 42;
|
---|
210 | if (type != null) {
|
---|
211 | hash = multiplier * hash + type.hashCode();
|
---|
212 | }
|
---|
213 | if (target != null) {
|
---|
214 | hash = multiplier * hash + target.hashCode();
|
---|
215 | }
|
---|
216 | return hash;
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * <p>
|
---|
221 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
222 | * </p>
|
---|
223 | *
|
---|
224 | * @param target
|
---|
225 | * target of the event
|
---|
226 | * @return true, if target was changed, false otherwise
|
---|
227 | */
|
---|
228 | public boolean setTarget(IEventTarget target) {
|
---|
229 | if (this.target != null) {
|
---|
230 | return false;
|
---|
231 | }
|
---|
232 | this.target = target;
|
---|
233 | return true;
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * <p>
|
---|
238 | * Adds a new {@link IReplayable} of type T to the replay sequence.
|
---|
239 | * </p>
|
---|
240 | *
|
---|
241 | * @param replayable
|
---|
242 | * element that is added to the sequence
|
---|
243 | * @throws IllegalArgumentException
|
---|
244 | * thrown is replayable is null
|
---|
245 | */
|
---|
246 | public void addReplayable(IReplayable replayable) {
|
---|
247 | if (replayable == null) {
|
---|
248 | throw new IllegalArgumentException("replayble must not be null");
|
---|
249 | }
|
---|
250 |
|
---|
251 | if (replay == null) {
|
---|
252 | replay = new LinkedList<IReplayable>();
|
---|
253 | }
|
---|
254 |
|
---|
255 | replay.add(replayable);
|
---|
256 | }
|
---|
257 |
|
---|
258 | /**
|
---|
259 | * <p>
|
---|
260 | * Adds a {@link List} of {@link IReplayable} to the replay sequence.
|
---|
261 | * </p>
|
---|
262 | *
|
---|
263 | * @param generatedReplaySeq
|
---|
264 | * {@link List} that is added to the sequence
|
---|
265 | * @throws IllegalArgumentException
|
---|
266 | * thrown if generatedReplaySeq is null
|
---|
267 | */
|
---|
268 | public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
|
---|
269 | if (generatedReplaySeq == null) {
|
---|
270 | throw new IllegalArgumentException("generatedReplaySeq must not be null");
|
---|
271 | }
|
---|
272 |
|
---|
273 | if (replay == null) {
|
---|
274 | replay = new LinkedList<IReplayable>();
|
---|
275 | }
|
---|
276 |
|
---|
277 | replay.addAll(generatedReplaySeq);
|
---|
278 | }
|
---|
279 |
|
---|
280 | /**
|
---|
281 | * <p>
|
---|
282 | * Adds a parameter to the event or sets it to a new value. The key must not be null. If a
|
---|
283 | * parameter with the specified key already exists, its value is replaced with the new one.
|
---|
284 | * If the value is null, the parameter with the specified key is removed, if it exists.
|
---|
285 | * </p>
|
---|
286 | *
|
---|
287 | * @param key the key of the parameter
|
---|
288 | * @param value the value of the parameter
|
---|
289 | *
|
---|
290 | * @throws IllegalArgumentException
|
---|
291 | * if the provided key is null
|
---|
292 | */
|
---|
293 | public void setParameter(String key, String value) {
|
---|
294 | if (key == null) {
|
---|
295 | throw new IllegalArgumentException("key must not be null");
|
---|
296 | }
|
---|
297 |
|
---|
298 | if (parameters == null) {
|
---|
299 | parameters = new HashMap<String, String>();
|
---|
300 | }
|
---|
301 |
|
---|
302 | if (value == null) {
|
---|
303 | parameters.remove(key);
|
---|
304 | }
|
---|
305 | else {
|
---|
306 | parameters.put(key, value);
|
---|
307 | }
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * <p>
|
---|
312 | * Returns the event parameter with the specified key if it exists or null if not. The key
|
---|
313 | * must not be null.
|
---|
314 | * </p>
|
---|
315 | *
|
---|
316 | * @param key the key of the parameter to be returned
|
---|
317 | *
|
---|
318 | * @return the value of the parameter with the specified key or null if there is no parameter
|
---|
319 | * with that key
|
---|
320 | *
|
---|
321 | * @throws IllegalArgumentException
|
---|
322 | * if the provided key is null
|
---|
323 | */
|
---|
324 | public String getParameter(String key) {
|
---|
325 | if (key == null) {
|
---|
326 | throw new IllegalArgumentException("key must not be null");
|
---|
327 | }
|
---|
328 |
|
---|
329 | if (parameters != null) {
|
---|
330 | return parameters.get(key);
|
---|
331 | }
|
---|
332 | else {
|
---|
333 | return null;
|
---|
334 | }
|
---|
335 | }
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * <p>
|
---|
339 | * Sets the timestamp of the event, i.e. when the event occurred. A timestamp for events is
|
---|
340 | * optional. Therefore it is also ignored by the {@link #equals(Object)}-method. A timestamp
|
---|
341 | * with a value smaller 0 means, that now timestamp for the event exists.
|
---|
342 | * </p>
|
---|
343 | *
|
---|
344 | * @param timestamp the new value for the timestamp
|
---|
345 | */
|
---|
346 | public void setTimestamp(long timestamp) {
|
---|
347 | this.timestamp = timestamp;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /**
|
---|
351 | * <p>
|
---|
352 | * Returns the timestamp of the event or a value lower than 0 if no timestamp for the event
|
---|
353 | * exists.
|
---|
354 | * </p>
|
---|
355 | *
|
---|
356 | * @return the timestamp of the event or a value lower than 0 if no timestamp exists
|
---|
357 | */
|
---|
358 | public long getTimestamp() {
|
---|
359 | return timestamp;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * <p>
|
---|
364 | * Returns a the list of replay events.
|
---|
365 | * </p>
|
---|
366 | * <p>
|
---|
367 | * The return value is a copy of the list used internally!
|
---|
368 | * </p>
|
---|
369 | *
|
---|
370 | * @return list of replay events.
|
---|
371 | */
|
---|
372 | public List<IReplayable> getReplayables() {
|
---|
373 | if (replay != null) {
|
---|
374 | return new LinkedList<IReplayable>(replay);
|
---|
375 | }
|
---|
376 | else {
|
---|
377 | return new LinkedList<IReplayable>();
|
---|
378 | }
|
---|
379 | }
|
---|
380 | }
|
---|