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

Last change on this file since 336 was 336, checked in by sherbold, 13 years ago
  • made de.ugoe.cs.eventbench.data.Event and ReplayableEvent? more robust against incorrect usage, with focus on incorrect subclassing. Especially unexpected null values are now handled more reliably.
File size: 6.2 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 && target != null) {
100                                        return type.equals(otherEvent.type)
101                                                        && target.equals(otherEvent.target);
102                                } else if (type != null && target == null) {
103                                        return type.equals(otherEvent.type)
104                                                        && otherEvent.target == null;
105                                } else if (type == null && target != null) {
106                                        return otherEvent.type == null
107                                                        && target.equals(otherEvent.target);
108                                } else {
109                                        return otherEvent.type == null && otherEvent.target == null;
110                                }
111                        } else {
112                                return false;
113                        }
114                } else {
115                        return false;
116                }
117        }
118
119        public boolean canEqual(Object other) {
120                return (other instanceof Event<?>);
121        }
122
123        /**
124         * <p>
125         * Returns {@link #getStandardId()} as String representation of the event.
126         * </p>
127         *
128         * @return String represenation of the event
129         */
130        @Override
131        public String toString() {
132                return getStandardId();
133        }
134
135        /**
136         * Informations about the event important for its Id that is neither target
137         * nor type.
138         *
139         * @return {@link #idInfo} of the event
140         */
141        public String getIdInfo() {
142                return idInfo;
143        }
144
145        /**
146         * <p>
147         * If {@link #targetShort} is set, a shortend version of the Id is returned
148         * of the form {@link #targetShort}.{@link #type}.{@link #idInfo} is
149         * returned. Otherwise the standard Id is returned (see
150         * {@link #getStandardId()}).
151         * </p>
152         *
153         * @return if available, shortend Id string; {@link #getStandardId()}
154         *         otherwise
155         */
156        public String getShortId() {
157                String shortId = null;
158                if (targetShort != null) {
159                        shortId = targetShort + "." + getType();
160                        if (!"".equals(idInfo)) {
161                                shortId += "." + idInfo;
162                        }
163                } else {
164                        shortId = getStandardId();
165                }
166                return shortId;
167        }
168
169        /**
170         * <p>
171         * Returns the Id string of the event. It has the form {@link #target}.
172         * {@link #type}.{@link #idInfo};
173         * <p>
174         *
175         * @return Id string of the event
176         */
177        public String getStandardId() {
178                String id = "";
179                if (target != null) {
180                        id += target + ".";
181                }
182                id += getType();
183                if (!"".equals(idInfo)) {
184                        id += "." + idInfo;
185                }
186                return id;
187        }
188
189        /**
190         * <p>
191         * Returns the {@link #target} of the event.
192         * </p>
193         *
194         * @return {@link #target} of the event
195         */
196        public String getTarget() {
197                return target;
198        }
199
200        /**
201         * <p>
202         * Returns the {@link #targetShort} of the event.
203         * </p>
204         *
205         * @return {@link #targetShort} of the event
206         */
207        protected String getTargetShort() {
208                return targetShort;
209        }
210
211        /**
212         * <p>
213         * Returns the {@link #type} of the event.
214         * </p>
215         *
216         * @return {@link #type} of the event
217         */
218        public String getType() {
219                return type;
220        }
221
222        /*
223         * (non-Javadoc)
224         *
225         * @see java.lang.Object#hashCode()
226         */
227        @Override
228        public int hashCode() {
229                int multiplier = 17;
230                int hash = 42;
231                if (type != null) {
232                        hash = multiplier * hash + type.hashCode();
233                }
234                if (target != null) {
235                        hash = multiplier * hash + target.hashCode();
236                }
237
238                return hash;
239        }
240
241        /**
242         * <p>
243         * Sets the {@link #idInfo} of the event. The idInfo is optional and
244         * contains information important for the event's Id that is neither target
245         * nor type.
246         * </p>
247         *
248         * @param info
249         *            {@link #idInfo} of the event
250         */
251        public void setIdInfo(String info) {
252                idInfo = info;
253        }
254
255        /**
256         * <p>
257         * Sets the target of the event. Once set, the target cannot be changed.
258         * </p>
259         *
260         * @param target
261         *            target of the event
262         * @return true, if target was changed, false otherwise
263         */
264        public boolean setTarget(String target) {
265                if (this.target != null) {
266                        return false;
267                }
268                this.target = target;
269                return true;
270        }
271
272        /**
273         * <p>
274         * Sets the short description of the event target. Once set, the target
275         * cannot be changed.
276         * </p>
277         *
278         * @param targetShort
279         *            short target description
280         * @return true, if target was changed, false otherwise
281         */
282        public boolean setTargetShort(String targetShort) {
283                if (this.targetShort != null) {
284                        return false;
285                }
286                this.targetShort = targetShort;
287                return true;
288        }
289}
Note: See TracBrowser for help on using the repository browser.