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

Last change on this file since 766 was 766, checked in by sherbold, 12 years ago
File size: 6.4 KB
Line 
1package de.ugoe.cs.quest.eventcore;
2
3import java.io.Serializable;
4import java.util.LinkedList;
5import java.util.List;
6
7/**
8 * <p>
9 * Base class for all events. An event is described by its {@link #type} and its {@link #target}.
10 * </p>
11 *
12 * @author Steffen Herbold
13 * @version 1.0
14 *
15 * @param <T>
16 *            Can be used to declare that events belong to a specific platform without subclassing.
17 */
18public class Event implements Serializable {
19
20    /**
21     * Id for object serialization.
22     */
23    private static final long serialVersionUID = 1L;
24
25    /**
26     * <p>
27     * Global start event that can be used to indicate the start of a sequence.
28     * </p>
29     */
30    public static final Event STARTEVENT = new Event(new StringEventType("START"));
31
32    /**
33     * <p>
34     * Global end event that can be used to indicate the end of a sequence.
35     */
36    public static final Event ENDEVENT = new Event(new StringEventType("END"));
37
38    /**
39     * <p>
40     * Type of the event.
41     * </p>
42     */
43    protected IEventType type;
44
45    /**
46     * </p> Target of the event.
47     */
48    protected IEventTarget target = null;
49
50    /**
51     * <p>
52     * List of {@link IReplayable}s of type T that describes the replay of an event. The
53     * {@link IReplayable}s can be interpreted as <it>sub-events</it> on the platform level that
54     * make up the abstract event.
55     * </p>
56     */
57    protected List<IReplayable> replay = new LinkedList<IReplayable>();
58
59    /**
60     * <p>
61     * Constructor. Creates a new Event with a given type. The type must not be null.
62     * </p>
63     *
64     * @param type
65     *            type of the event
66     */
67    public Event(IEventType type) {
68        if (type == null) {
69            throw new IllegalArgumentException("Event type must not be null");
70        }
71        this.type = type;
72    }
73
74    /**
75     * <p>
76     * Constructor. Creates a new Event with a given type and target. The type must not be null.
77     * </p>
78     *
79     * @param type
80     *            type of the event
81     * @param target
82     *            target of the event
83     */
84    public Event(IEventType type, IEventTarget target) {
85        this(type);
86        this.target = target;
87    }
88
89    /**
90     * <p>
91     * Two events are equal, if their {@link #type} and {@link #target} are equal.
92     * </p>
93     * <p>
94     * See {@link Object#equals(Object)} for further information.
95     * </p>
96     *
97     * @param other
98     *            Event that is compared to this
99     * @return true, if events are equal, false otherwise
100     */
101    @Override
102    public boolean equals(Object other) {
103        if (this == other) {
104            return true;
105        }
106        if (other instanceof Event) {
107            Event otherEvent = (Event) other;
108            if (target != null) {
109                return type.equals(otherEvent.type) && target.equals(otherEvent.target);
110            }
111            else {
112                return type.equals(otherEvent.type) && otherEvent.target == null;
113            }
114        }
115        return false;
116    }
117
118    /**
119     * <p>
120     * Returns {@link #getId()} as String representation of the event.
121     * </p>
122     *
123     * @return String representation of the event
124     */
125    @Override
126    public String toString() {
127        return getId();
128    }
129
130    /**
131     * <p>
132     * Returns the {@link #target} of the event.
133     * </p>
134     *
135     * @return {@link #target} of the event
136     */
137    public IEventTarget getTarget() {
138        return target;
139    }
140
141    /**
142     * <p>
143     * Returns the {@link #type} of the event.
144     * </p>
145     *
146     * @return {@link #type} of the event
147     */
148    public IEventType getType() {
149        return type;
150    }
151   
152    /**
153     * <p>
154     * Returns the combination of {@link #type} and {@link #target} as id.
155     * </p>
156     *
157     * @return string of the form (type,target)
158     */
159    public String getId() {
160        String id = type.toString();
161        if( target!=null ) {
162            id += "." + target.getStringIdentifier();
163        }
164        return id;
165    }
166
167    /*
168     * (non-Javadoc)
169     *
170     * @see java.lang.Object#hashCode()
171     */
172    @Override
173    public int hashCode() {
174        int multiplier = 17;
175        int hash = 42;
176        if (type != null) {
177            hash = multiplier * hash + type.hashCode();
178        }
179        if (target != null) {
180            hash = multiplier * hash + target.hashCode();
181        }
182        return hash;
183    }
184
185    /**
186     * <p>
187     * Sets the target of the event. Once set, the target cannot be changed.
188     * </p>
189     *
190     * @param target
191     *            target of the event
192     * @return true, if target was changed, false otherwise
193     */
194    public boolean setTarget(IEventTarget target) {
195        if (this.target != null) {
196            return false;
197        }
198        this.target = target;
199        return true;
200    }
201
202    /**
203     * <p>
204     * Adds a new {@link IReplayable} of type T to the replay sequence.
205     * </p>
206     *
207     * @param replayable
208     *            element that is added to the sequence
209     * @throws IllegalArgumentException
210     *             thrown is replayable is null
211     */
212    public void addReplayable(IReplayable replayable) {
213        if (replayable == null) {
214            throw new IllegalArgumentException("replayble must not be null");
215        }
216        replay.add(replayable);
217    }
218
219    /**
220     * <p>
221     * Adds a {@link List}ist of {@link IReplayable} to the replay sequence.
222     * </p>
223     *
224     * @param generatedReplaySeq
225     *            {@link List} that is added to the sequence
226     * @throws IllegalArgumentException
227     *             thrown if generatedReplaySeq is null
228     */
229    public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
230        if (generatedReplaySeq == null) {
231            throw new IllegalArgumentException("generatedReplaySeq must not be null");
232        }
233        replay.addAll(generatedReplaySeq);
234    }
235
236    /**
237     * <p>
238     * Returns a the list of replay events.
239     * </p>
240     * <p>
241     * The return value is a copy of the list used internally!
242     * </p>
243     *
244     * @return list of replay events.
245     */
246    public List<IReplayable> getReplayables() {
247        return new LinkedList<IReplayable>(replay);
248    }
249}
Note: See TracBrowser for help on using the repository browser.