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