source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/data/Event.java @ 373

Last change on this file since 373 was 373, checked in by sherbold, 12 years ago
  • extended de.ugoe.cs.eventbench.data.Event with a function targetEquals() that can be overridden by subclasses to allow sophisticated target comparisons instead of simple string comparisons.
File size: 6.6 KB
Line 
1package de.ugoe.cs.eventbench.data;
2
3import java.io.Serializable;
4import 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 */
19public 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                if (target != null) {
230                        hash = multiplier * hash + target.hashCode();
231                }
232
233                return hash;
234        }
235
236        /**
237         * <p>
238         * Sets the {@link #idInfo} of the event. The idInfo is optional and
239         * contains information important for the event's Id that is neither target
240         * nor type.
241         * </p>
242         *
243         * @param info
244         *            {@link #idInfo} of the event
245         */
246        public void setIdInfo(String info) {
247                idInfo = info;
248        }
249
250        /**
251         * <p>
252         * Sets the target of the event. Once set, the target cannot be changed.
253         * </p>
254         *
255         * @param target
256         *            target of the event
257         * @return true, if target was changed, false otherwise
258         */
259        public boolean setTarget(String target) {
260                if (this.target != null) {
261                        return false;
262                }
263                this.target = target;
264                return true;
265        }
266
267        /**
268         * <p>
269         * Sets the short description of the event target. Once set, the target
270         * cannot be changed.
271         * </p>
272         *
273         * @param targetShort
274         *            short target description
275         * @return true, if target was changed, false otherwise
276         */
277        public boolean setTargetShort(String targetShort) {
278                if (this.targetShort != null) {
279                        return false;
280                }
281                this.targetShort = targetShort;
282                return true;
283        }
284
285        /**
286         * <p>
287         * This function is used by {@link #equals(Object)} to determine if the
288         * targets of both events are equal. The standard implementation provided by
289         * this class performs a String comparison between the target strings.
290         * </p>
291         * <p>
292         * Subclasses can override this method to implemented more sophisticated
293         * means for the target comparison, e.g., to account for changes in the
294         * title of a widget.
295         * </p>
296         *
297         * @param otherTarget
298         * @return
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}
Note: See TracBrowser for help on using the repository browser.