1 | package de.ugoe.cs.eventbench.data;
|
---|
2 |
|
---|
3 | import java.io.Serializable;
|
---|
4 | import java.security.InvalidParameterException;
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * <p>
|
---|
8 | * Base class for all events. An event is described by its {@link #type} and its
|
---|
9 | * {@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
|
---|
17 | * without subclassing.
|
---|
18 | */
|
---|
19 | public class Event<T> 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<Object> STARTEVENT = new Event<Object>("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<Object> ENDEVENT = new Event<Object>("END");
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * <p>
|
---|
41 | * Type of the event.
|
---|
42 | * </p>
|
---|
43 | */
|
---|
44 | protected String type;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * </p> Target of the event.
|
---|
48 | */
|
---|
49 | protected String target = null;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * <p>
|
---|
53 | * Short description of the event target.
|
---|
54 | * </p>
|
---|
55 | */
|
---|
56 | protected String targetShort = null;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Further information about the event that shall be included in its Id.
|
---|
60 | */
|
---|
61 | protected String idInfo = "";
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * <p>
|
---|
65 | * Constructor. Creates a new Event with a given type.
|
---|
66 | * </p>
|
---|
67 | *
|
---|
68 | * @param type
|
---|
69 | * type of the event
|
---|
70 | */
|
---|
71 | public Event(String type) {
|
---|
72 | if (type == null) {
|
---|
73 | throw new InvalidParameterException("Event type must not be null");
|
---|
74 | }
|
---|
75 | this.type = type;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * <p>
|
---|
80 | * Two events are equal, if their {@link #type} and {@link #target} are
|
---|
81 | * equal.
|
---|
82 | * </p>
|
---|
83 | * <p>
|
---|
84 | * See {@link Object#equals(Object)} for further information.
|
---|
85 | * </p>
|
---|
86 | *
|
---|
87 | * @param other
|
---|
88 | * Event that is compared to this
|
---|
89 | * @return true, if events are equal, false otherwise
|
---|
90 | */
|
---|
91 | @Override
|
---|
92 | public boolean equals(Object other) {
|
---|
93 | if (this == other) {
|
---|
94 | return true;
|
---|
95 | }
|
---|
96 | if (other instanceof Event<?>) {
|
---|
97 | Event<?> otherEvent = (Event<?>) other;
|
---|
98 | if (otherEvent.canEqual(this)) {
|
---|
99 | if (type != null) {
|
---|
100 | return type.equals(otherEvent.type)
|
---|
101 | && targetEquals(otherEvent.target);
|
---|
102 | } else {
|
---|
103 | return otherEvent.type == null
|
---|
104 | && targetEquals(otherEvent.target);
|
---|
105 | }
|
---|
106 | } else {
|
---|
107 | return false;
|
---|
108 | }
|
---|
109 | } else {
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | public boolean canEqual(Object other) {
|
---|
115 | return (other instanceof Event<?>);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * <p>
|
---|
120 | * Returns {@link #getStandardId()} as String representation of the event.
|
---|
121 | * </p>
|
---|
122 | *
|
---|
123 | * @return String represenation of the event
|
---|
124 | */
|
---|
125 | @Override
|
---|
126 | public String toString() {
|
---|
127 | return getStandardId();
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Informations about the event important for its Id that is neither target
|
---|
132 | * nor type.
|
---|
133 | *
|
---|
134 | * @return {@link #idInfo} of the event
|
---|
135 | */
|
---|
136 | public String getIdInfo() {
|
---|
137 | return idInfo;
|
---|
138 | }
|
---|
139 |
|
---|
140 | /**
|
---|
141 | * <p>
|
---|
142 | * If {@link #targetShort} is set, a shortend version of the Id is returned
|
---|
143 | * of the form {@link #targetShort}.{@link #type}.{@link #idInfo} is
|
---|
144 | * returned. Otherwise the standard Id is returned (see
|
---|
145 | * {@link #getStandardId()}).
|
---|
146 | * </p>
|
---|
147 | *
|
---|
148 | * @return if available, shortend Id string; {@link #getStandardId()}
|
---|
149 | * otherwise
|
---|
150 | */
|
---|
151 | public String getShortId() {
|
---|
152 | String shortId = null;
|
---|
153 | if (targetShort != null) {
|
---|
154 | shortId = targetShort + "." + getType();
|
---|
155 | if (!"".equals(idInfo)) {
|
---|
156 | shortId += "." + idInfo;
|
---|
157 | }
|
---|
158 | } else {
|
---|
159 | shortId = getStandardId();
|
---|
160 | }
|
---|
161 | return shortId;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * <p>
|
---|
166 | * Returns the Id string of the event. It has the form {@link #target}.
|
---|
167 | * {@link #type}.{@link #idInfo};
|
---|
168 | * <p>
|
---|
169 | *
|
---|
170 | * @return Id string of the event
|
---|
171 | */
|
---|
172 | public String getStandardId() {
|
---|
173 | String id = "";
|
---|
174 | if (target != null) {
|
---|
175 | id += target + ".";
|
---|
176 | }
|
---|
177 | id += getType();
|
---|
178 | if (!"".equals(idInfo)) {
|
---|
179 | id += "." + idInfo;
|
---|
180 | }
|
---|
181 | return id;
|
---|
182 | }
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * <p>
|
---|
186 | * Returns the {@link #target} of the event.
|
---|
187 | * </p>
|
---|
188 | *
|
---|
189 | * @return {@link #target} of the event
|
---|
190 | */
|
---|
191 | public String getTarget() {
|
---|
192 | return target;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * <p>
|
---|
197 | * Returns the {@link #targetShort} of the event.
|
---|
198 | * </p>
|
---|
199 | *
|
---|
200 | * @return {@link #targetShort} of the event
|
---|
201 | */
|
---|
202 | protected String getTargetShort() {
|
---|
203 | return targetShort;
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * <p>
|
---|
208 | * Returns the {@link #type} of the event.
|
---|
209 | * </p>
|
---|
210 | *
|
---|
211 | * @return {@link #type} of the event
|
---|
212 | */
|
---|
213 | public String getType() {
|
---|
214 | return type;
|
---|
215 | }
|
---|
216 |
|
---|
217 | /*
|
---|
218 | * (non-Javadoc)
|
---|
219 | *
|
---|
220 | * @see java.lang.Object#hashCode()
|
---|
221 | */
|
---|
222 | @Override
|
---|
223 | public int hashCode() {
|
---|
224 | int multiplier = 17;
|
---|
225 | int hash = 42;
|
---|
226 | if (type != null) {
|
---|
227 | hash = multiplier * hash + type.hashCode();
|
---|
228 | }
|
---|
229 | hash = multiplier * hash + targetHashCode();
|
---|
230 |
|
---|
231 | return hash;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * <p>
|
---|
236 | * Sets the {@link #idInfo} of the event. The idInfo is optional and
|
---|
237 | * contains information important for the event's Id that is neither target
|
---|
238 | * nor type.
|
---|
239 | * </p>
|
---|
240 | *
|
---|
241 | * @param info
|
---|
242 | * {@link #idInfo} of the event
|
---|
243 | */
|
---|
244 | public void setIdInfo(String info) {
|
---|
245 | idInfo = info;
|
---|
246 | }
|
---|
247 |
|
---|
248 | /**
|
---|
249 | * <p>
|
---|
250 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
251 | * </p>
|
---|
252 | *
|
---|
253 | * @param target
|
---|
254 | * target of the event
|
---|
255 | * @return true, if target was changed, false otherwise
|
---|
256 | */
|
---|
257 | public boolean setTarget(String target) {
|
---|
258 | if (this.target != null) {
|
---|
259 | return false;
|
---|
260 | }
|
---|
261 | this.target = target;
|
---|
262 | return true;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * <p>
|
---|
267 | * Sets the short description of the event target. Once set, the target
|
---|
268 | * cannot be changed.
|
---|
269 | * </p>
|
---|
270 | *
|
---|
271 | * @param targetShort
|
---|
272 | * short target description
|
---|
273 | * @return true, if target was changed, false otherwise
|
---|
274 | */
|
---|
275 | public boolean setTargetShort(String targetShort) {
|
---|
276 | if (this.targetShort != null) {
|
---|
277 | return false;
|
---|
278 | }
|
---|
279 | this.targetShort = targetShort;
|
---|
280 | return true;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * <p>
|
---|
285 | * This function is used by {@link #equals(Object)} to determine if the
|
---|
286 | * targets of both events are equal. The standard implementation provided by
|
---|
287 | * this class performs a String comparison between the target strings.
|
---|
288 | * </p>
|
---|
289 | * <p>
|
---|
290 | * Subclasses can override this method to implemented more sophisticated
|
---|
291 | * means for the target comparison, e.g., to account for changes in the
|
---|
292 | * title of a widget.
|
---|
293 | * </p>
|
---|
294 | *
|
---|
295 | * @param otherTarget
|
---|
296 | * other target string to which the target if this event is
|
---|
297 | * compared to
|
---|
298 | * @return true if the targets are equals; false otherwise
|
---|
299 | */
|
---|
300 | protected boolean targetEquals(String otherTarget) {
|
---|
301 | boolean retVal;
|
---|
302 | if (target != null) {
|
---|
303 | retVal = target.equals(otherTarget);
|
---|
304 | } else {
|
---|
305 | retVal = (otherTarget == null);
|
---|
306 | }
|
---|
307 | return retVal;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | * <p>
|
---|
312 | * This function is used by {@link #hashCode()} to determine how the hash of
|
---|
313 | * the {@link #target}. It has to be overridden by subclasses that implement
|
---|
314 | * {@link #targetEquals(String)}, to ensure that the equals/hashCode
|
---|
315 | * contract remains valid.
|
---|
316 | * </p>
|
---|
317 | *
|
---|
318 | * @return hash of the target
|
---|
319 | */
|
---|
320 | protected int targetHashCode() {
|
---|
321 | if (target != null) {
|
---|
322 | return target.hashCode();
|
---|
323 | } else {
|
---|
324 | return 0;
|
---|
325 | }
|
---|
326 | }
|
---|
327 | }
|
---|