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

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