source: trunk/autoquest-core-events/src/main/java/de/ugoe/cs/autoquest/eventcore/Event.java @ 1873

Last change on this file since 1873 was 1873, checked in by pharms, 9 years ago
  • implemented lazy instantiation of parameters and replay
File size: 10.4 KB
Line 
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
15package de.ugoe.cs.autoquest.eventcore;
16
17import java.io.Serializable;
18import java.util.HashMap;
19import java.util.LinkedList;
20import java.util.List;
21import 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 */
35public 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;
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;
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       
253        if (replay == null) {
254            replay = new LinkedList<IReplayable>();
255        }
256       
257        replay.add(replayable);
258    }
259
260    /**
261     * <p>
262     * Adds a {@link List} of {@link IReplayable} to the replay sequence.
263     * </p>
264     *
265     * @param generatedReplaySeq
266     *            {@link List} that is added to the sequence
267     * @throws IllegalArgumentException
268     *             thrown if generatedReplaySeq is null
269     */
270    public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
271        if (generatedReplaySeq == null) {
272            throw new IllegalArgumentException("generatedReplaySeq must not be null");
273        }
274       
275        if (replay == null) {
276            replay = new LinkedList<IReplayable>();
277        }
278       
279        replay.addAll(generatedReplaySeq);
280    }
281   
282    /**
283     * <p>
284     * Adds a parameter to the event or sets it to a new value. The key must not be null. If a
285     * parameter with the specified key already exists, its value is replaced with the new one.
286     * If the value is null, the parameter with the specified key is removed, if it exists.
287     * </p>
288     *
289     * @param key   the key of the parameter
290     * @param value the value of the parameter
291     *
292     * @throws IllegalArgumentException
293     *             if the provided key is null
294     */
295    public void setParameter(String key, String value) {
296        if (key == null) {
297            throw new IllegalArgumentException("key must not be null");
298        }
299       
300        if (parameters == null) {
301            parameters = new HashMap<String, String>();
302        }
303       
304        if (value == null) {
305            parameters.remove(key);
306        }
307        else {
308            parameters.put(key, value);
309        }
310    }
311
312    /**
313     * <p>
314     * Returns the event parameter with the specified key if it exists or null if not. The key
315     * must not be null.
316     * </p>
317     *
318     * @param key the key of the parameter to be returned
319     *
320     * @return the value of the parameter with the specified key or null if there is no parameter
321     *         with that key
322     *
323     * @throws IllegalArgumentException
324     *             if the provided key is null
325     */
326    public String getParameter(String key) {
327        if (key == null) {
328            throw new IllegalArgumentException("key must not be null");
329        }
330       
331        if (parameters != null) {
332            return parameters.get(key);
333        }
334        else {
335            return null;
336        }
337    }
338
339    /**
340     * <p>
341     * Sets the timestamp of the event, i.e. when the event occurred. A timestamp for events is
342     * optional. Therefore it is also ignored by the {@link #equals(Object)}-method. A timestamp
343     * with a value smaller 0 means, that now timestamp for the event exists.
344     * </p>
345     *
346     * @param timestamp the new value for the timestamp
347     */
348    public void setTimestamp(long timestamp) {
349        this.timestamp = timestamp;
350    }
351
352    /**
353     * <p>
354     * Returns the timestamp of the event or a value lower than 0 if no timestamp for the event
355     * exists.
356     * </p>
357     *
358     * @return the timestamp of the event or a value lower than 0 if no timestamp exists
359     */
360    public long getTimestamp() {
361        return timestamp;
362    }
363
364    /**
365     * <p>
366     * Returns a the list of replay events.
367     * </p>
368     * <p>
369     * The return value is a copy of the list used internally!
370     * </p>
371     *
372     * @return list of replay events.
373     */
374    public List<IReplayable> getReplayables() {
375        if (replay != null) {
376            return new LinkedList<IReplayable>(replay);
377        }
378        else {
379            return new LinkedList<IReplayable>();
380        }
381    }
382}
Note: See TracBrowser for help on using the repository browser.