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

Last change on this file since 958 was 958, checked in by pharms, 12 years ago
  • made event final, so that it can't be derived of
  • added support for adding further event parameters
File size: 8.8 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 * </p>
27 *
28 * @author Steffen Herbold
29 * @version 1.0
30 *
31 * @param <T>
32 *            Can be used to declare that events belong to a specific platform without subclassing.
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> Target of the event.
65     */
66    private IEventTarget target = null;
67   
68    /**
69     * <p>
70     * Map with further parameters of the event, which are not belonging to the type or target.
71     * </p>
72     */
73    private Map<String, String> parameters = new HashMap<String, String>();
74
75    /**
76     * <p>
77     * List of {@link IReplayable}s of type T that describes the replay of an event. The
78     * {@link IReplayable}s can be interpreted as <it>sub-events</it> on the platform level that
79     * make up the abstract event.
80     * </p>
81     */
82    private List<IReplayable> replay = new LinkedList<IReplayable>();
83
84    /**
85     * <p>
86     * Constructor. Creates a new Event with a given type. The type must not be null.
87     * </p>
88     *
89     * @param type
90     *            type of the event
91     */
92    public Event(IEventType type) {
93        if (type == null) {
94            throw new IllegalArgumentException("Event type must not be null");
95        }
96        this.type = type;
97    }
98
99    /**
100     * <p>
101     * Constructor. Creates a new Event with a given type and target. The type must not be null.
102     * </p>
103     *
104     * @param type
105     *            type of the event
106     * @param target
107     *            target of the event
108     */
109    public Event(IEventType type, IEventTarget target) {
110        this(type);
111        this.target = target;
112    }
113
114    /**
115     * <p>
116     * Two events are equal, if their {@link #type} and {@link #target} are equal.
117     * </p>
118     * <p>
119     * See {@link Object#equals(Object)} for further information.
120     * </p>
121     *
122     * @param other
123     *            Event that is compared to this
124     * @return true, if events are equal, false otherwise
125     */
126    @Override
127    public boolean equals(Object other) {
128        if (this == other) {
129            return true;
130        }
131        if (other instanceof Event) {
132            Event otherEvent = (Event) other;
133            if (target != null) {
134                return type.equals(otherEvent.type) && target.equals(otherEvent.target);
135            }
136            else {
137                return type.equals(otherEvent.type) && otherEvent.target == null;
138            }
139        }
140        return false;
141    }
142
143    /**
144     * <p>
145     * Returns {@link #getId()} as String representation of the event.
146     * </p>
147     *
148     * @return String representation of the event
149     */
150    @Override
151    public String toString() {
152        return getId();
153    }
154
155    /**
156     * <p>
157     * Returns the {@link #target} of the event.
158     * </p>
159     *
160     * @return {@link #target} of the event
161     */
162    public IEventTarget getTarget() {
163        return target;
164    }
165
166    /**
167     * <p>
168     * Returns the {@link #type} of the event.
169     * </p>
170     *
171     * @return {@link #type} of the event
172     */
173    public IEventType getType() {
174        return type;
175    }
176   
177    /**
178     * <p>
179     * Returns the combination of {@link #type} and {@link #target} as id.
180     * </p>
181     *
182     * @return string of the form (type,target)
183     */
184    public String getId() {
185        String id = type.toString();
186        if( target!=null ) {
187            id += "." + target.getStringIdentifier();
188        }
189        return id;
190    }
191
192    /*
193     * (non-Javadoc)
194     *
195     * @see java.lang.Object#hashCode()
196     */
197    @Override
198    public int hashCode() {
199        int multiplier = 17;
200        int hash = 42;
201        if (type != null) {
202            hash = multiplier * hash + type.hashCode();
203        }
204        if (target != null) {
205            hash = multiplier * hash + target.hashCode();
206        }
207        return hash;
208    }
209
210    /**
211     * <p>
212     * Sets the target of the event. Once set, the target cannot be changed.
213     * </p>
214     *
215     * @param target
216     *            target of the event
217     * @return true, if target was changed, false otherwise
218     */
219    public boolean setTarget(IEventTarget target) {
220        if (this.target != null) {
221            return false;
222        }
223        this.target = target;
224        return true;
225    }
226
227    /**
228     * <p>
229     * Adds a new {@link IReplayable} of type T to the replay sequence.
230     * </p>
231     *
232     * @param replayable
233     *            element that is added to the sequence
234     * @throws IllegalArgumentException
235     *             thrown is replayable is null
236     */
237    public void addReplayable(IReplayable replayable) {
238        if (replayable == null) {
239            throw new IllegalArgumentException("replayble must not be null");
240        }
241        replay.add(replayable);
242    }
243
244    /**
245     * <p>
246     * Adds a {@link List} of {@link IReplayable} to the replay sequence.
247     * </p>
248     *
249     * @param generatedReplaySeq
250     *            {@link List} that is added to the sequence
251     * @throws IllegalArgumentException
252     *             thrown if generatedReplaySeq is null
253     */
254    public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
255        if (generatedReplaySeq == null) {
256            throw new IllegalArgumentException("generatedReplaySeq must not be null");
257        }
258        replay.addAll(generatedReplaySeq);
259    }
260   
261    /**
262     * <p>
263     * Adds a parameter to the event or sets it to a new value. The key must not be null. If a
264     * parameter with the specified key already exists, its value is replaced with the new one.
265     * If the value is null, the parameter with the specified key is removed, if it exists.
266     * </p>
267     *
268     * @param key   the key of the parameter
269     * @param value the value of the parameter
270     *
271     * @throws IllegalArgumentException
272     *             if the provided key is null
273     */
274    public void setParameter(String key, String value) {
275        if (key == null) {
276            throw new IllegalArgumentException("key must not be null");
277        }
278       
279        if (value == null) {
280            parameters.remove(key);
281        }
282        else {
283            parameters.put(key, value);
284        }
285    }
286
287    /**
288     * <p>
289     * Returns the event parameter with the specified key if it exists or null if not. The key
290     * must not be null.
291     * </p>
292     *
293     * @param key the key of the parameter to be returned
294     *
295     * @return the value of the parameter with the specified key or null if there is no parameter
296     *         with that key
297     *
298     * @throws IllegalArgumentException
299     *             if the provided key is null
300     */
301    public String getParameter(String key) {
302        if (key == null) {
303            throw new IllegalArgumentException("key must not be null");
304        }
305       
306        return parameters.get(key);
307    }
308
309    /**
310     * <p>
311     * Returns a the list of replay events.
312     * </p>
313     * <p>
314     * The return value is a copy of the list used internally!
315     * </p>
316     *
317     * @return list of replay events.
318     */
319    public List<IReplayable> getReplayables() {
320        return new LinkedList<IReplayable>(replay);
321    }
322}
Note: See TracBrowser for help on using the repository browser.