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 | private String type;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * </p> Target of the event.
|
---|
48 | */
|
---|
49 | private String target = null;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * <p>
|
---|
53 | * Short description of the event target.
|
---|
54 | * </p>
|
---|
55 | */
|
---|
56 | private String targetShort = null;
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Further information about the event that shall be included in its Id.
|
---|
60 | */
|
---|
61 | private 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 (target != null) {
|
---|
99 | return type.equals(otherEvent.type)
|
---|
100 | && target.equals(otherEvent.target);
|
---|
101 | } else {
|
---|
102 | return type.equals(otherEvent.type)
|
---|
103 | && otherEvent.target == null;
|
---|
104 | }
|
---|
105 | } else {
|
---|
106 | return false;
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * <p>
|
---|
112 | * Returns {@link #getStandardId()} as String representation of the event.
|
---|
113 | * </p>
|
---|
114 | *
|
---|
115 | * @return String represenation of the event
|
---|
116 | */
|
---|
117 | @Override
|
---|
118 | public String toString() {
|
---|
119 | return getStandardId();
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Informations about the event important for its Id that is neither target
|
---|
124 | * nor type.
|
---|
125 | *
|
---|
126 | * @return {@link #idInfo} of the event
|
---|
127 | */
|
---|
128 | public String getIdInfo() {
|
---|
129 | return idInfo;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * <p>
|
---|
134 | * If {@link #targetShort} is set, a shortend version of the Id is returned
|
---|
135 | * of the form {@link #targetShort}.{@link #type}.{@link #idInfo} is
|
---|
136 | * returned. Otherwise the standard Id is returned (see
|
---|
137 | * {@link #getStandardId()}).
|
---|
138 | * </p>
|
---|
139 | *
|
---|
140 | * @return if available, shortend Id string; {@link #getStandardId()}
|
---|
141 | * otherwise
|
---|
142 | */
|
---|
143 | public String getShortId() {
|
---|
144 | String shortId = null;
|
---|
145 | if (targetShort != null) {
|
---|
146 | shortId = targetShort + "." + getType();
|
---|
147 | if (!"".equals(idInfo)) {
|
---|
148 | shortId += "." + idInfo;
|
---|
149 | }
|
---|
150 | } else {
|
---|
151 | shortId = getStandardId();
|
---|
152 | }
|
---|
153 | return shortId;
|
---|
154 | }
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * <p>
|
---|
158 | * Returns the Id string of the event. It has the form {@link #target}.
|
---|
159 | * {@link #type}.{@link #idInfo};
|
---|
160 | * <p>
|
---|
161 | *
|
---|
162 | * @return Id string of the event
|
---|
163 | */
|
---|
164 | public String getStandardId() {
|
---|
165 | String id = "";
|
---|
166 | if (target != null) {
|
---|
167 | id += target + ".";
|
---|
168 | }
|
---|
169 | id += getType();
|
---|
170 | if (!"".equals(idInfo)) {
|
---|
171 | id += "." + idInfo;
|
---|
172 | }
|
---|
173 | return id;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * <p>
|
---|
178 | * Returns the {@link #target} of the event.
|
---|
179 | * </p>
|
---|
180 | *
|
---|
181 | * @return {@link #target} of the event
|
---|
182 | */
|
---|
183 | public String getTarget() {
|
---|
184 | return target;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * <p>
|
---|
189 | * Returns the {@link #targetShort} of the event.
|
---|
190 | * </p>
|
---|
191 | *
|
---|
192 | * @return {@link #targetShort} of the event
|
---|
193 | */
|
---|
194 | protected String getTargetShort() {
|
---|
195 | return targetShort;
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * <p>
|
---|
200 | * Returns the {@link #type} of the event.
|
---|
201 | * </p>
|
---|
202 | *
|
---|
203 | * @return {@link #type} of the event
|
---|
204 | */
|
---|
205 | public String getType() {
|
---|
206 | return type;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /*
|
---|
210 | * (non-Javadoc)
|
---|
211 | *
|
---|
212 | * @see java.lang.Object#hashCode()
|
---|
213 | */
|
---|
214 | @Override
|
---|
215 | public int hashCode() {
|
---|
216 | int multiplier = 17;
|
---|
217 | int hash = 42;
|
---|
218 | hash = multiplier * hash + type.hashCode();
|
---|
219 | if (target != null) {
|
---|
220 | hash = multiplier * hash + target.hashCode();
|
---|
221 | }
|
---|
222 |
|
---|
223 | return hash;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * <p>
|
---|
228 | * Sets the {@link #idInfo} of the event. The idInfo is optional and
|
---|
229 | * contains information important for the event's Id that is neither target
|
---|
230 | * nor type.
|
---|
231 | * </p>
|
---|
232 | *
|
---|
233 | * @param info
|
---|
234 | * {@link #idInfo} of the event
|
---|
235 | */
|
---|
236 | public void setIdInfo(String info) {
|
---|
237 | idInfo = info;
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * <p>
|
---|
242 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
243 | * </p>
|
---|
244 | *
|
---|
245 | * @param target
|
---|
246 | * target of the event
|
---|
247 | * @return true, if target was changed, false otherwise
|
---|
248 | */
|
---|
249 | public boolean setTarget(String target) {
|
---|
250 | if (this.target != null) {
|
---|
251 | return false;
|
---|
252 | }
|
---|
253 | this.target = target;
|
---|
254 | return true;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * <p>
|
---|
259 | * Sets the short description of the event target. Once set, the target
|
---|
260 | * cannot be changed.
|
---|
261 | * </p>
|
---|
262 | *
|
---|
263 | * @param targetShort
|
---|
264 | * short target description
|
---|
265 | * @return true, if target was changed, false otherwise
|
---|
266 | */
|
---|
267 | public boolean setTargetShort(String targetShort) {
|
---|
268 | if (this.targetShort != null) {
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 | this.targetShort = targetShort;
|
---|
272 | return true;
|
---|
273 | }
|
---|
274 | }
|
---|