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

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