1 | package de.ugoe.cs.eventbench.data;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * <p>
|
---|
7 | * Base class for all events. An event is described by its {@link #type} and its
|
---|
8 | * {@link #target}.
|
---|
9 | * </p>
|
---|
10 | *
|
---|
11 | * @author Steffen Herbold
|
---|
12 | * @version 1.0
|
---|
13 | *
|
---|
14 | * @param <T>
|
---|
15 | * Can be used to declare that events belong to a specific platform
|
---|
16 | * without subclassing.
|
---|
17 | */
|
---|
18 | public class Event<T> {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * <p>
|
---|
22 | * Global start event that can be used to indicate the start of a sequence.
|
---|
23 | * </p>
|
---|
24 | */
|
---|
25 | public static final Event<Object> STARTEVENT = new Event<Object>("START");
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * <p>
|
---|
29 | * Global end event that can be used to indicate the end of a sequence.
|
---|
30 | */
|
---|
31 | public static final Event<Object> ENDEVENT = new Event<Object>("END");
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * <p>
|
---|
35 | * Type of the event.
|
---|
36 | * </p>
|
---|
37 | */
|
---|
38 | private String type;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * </p> Target of the event.
|
---|
42 | */
|
---|
43 | private String target = null;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>
|
---|
47 | * Short description of the event target.
|
---|
48 | * </p>
|
---|
49 | */
|
---|
50 | private String targetShort = null;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Further information about the event that shall be included in its Id.
|
---|
54 | */
|
---|
55 | private String idInfo = "";
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * <p>
|
---|
59 | * Constructor. Creates a new Event with a given type.
|
---|
60 | * </p>
|
---|
61 | *
|
---|
62 | * @param type
|
---|
63 | * type of the event
|
---|
64 | */
|
---|
65 | public Event(String type) {
|
---|
66 | if (type == null) {
|
---|
67 | throw new InvalidParameterException("Event type must not be null");
|
---|
68 | }
|
---|
69 | this.type = type;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * <p>
|
---|
74 | * Two events are equal, if their {@link #type} and {@link #target} are
|
---|
75 | * equal.
|
---|
76 | * </p>
|
---|
77 | * <p>
|
---|
78 | * See {@link Object#equals(Object)} for further information.
|
---|
79 | * </p>
|
---|
80 | *
|
---|
81 | * @param other
|
---|
82 | * Event that is compared to this
|
---|
83 | * @return true, if events are equal, false otherwise
|
---|
84 | */
|
---|
85 | @Override
|
---|
86 | public boolean equals(Object other) {
|
---|
87 | if (this == other) {
|
---|
88 | return true;
|
---|
89 | }
|
---|
90 | if (other instanceof Event<?>) {
|
---|
91 | Event<?> otherEvent = (Event<?>) other;
|
---|
92 | if (target != null) {
|
---|
93 | return type.equals(otherEvent.type)
|
---|
94 | && target.equals(otherEvent.target);
|
---|
95 | } else {
|
---|
96 | return type.equals(otherEvent.type)
|
---|
97 | && otherEvent.target == null;
|
---|
98 | }
|
---|
99 | } else {
|
---|
100 | return false;
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * <p>
|
---|
106 | * Returns {@link #getStandardId()} as String representation of the event.
|
---|
107 | * </p>
|
---|
108 | *
|
---|
109 | * @return String represenation of the event
|
---|
110 | */
|
---|
111 | @Override
|
---|
112 | public String toString() {
|
---|
113 | return getStandardId();
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Informations about the event important for its Id that is neither target
|
---|
118 | * nor type.
|
---|
119 | *
|
---|
120 | * @return {@link #idInfo} of the event
|
---|
121 | */
|
---|
122 | public String getIdInfo() {
|
---|
123 | return idInfo;
|
---|
124 | }
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * <p>
|
---|
128 | * If {@link #targetShort} is set, a shortend version of the Id is returned
|
---|
129 | * of the form {@link #targetShort}.{@link #type}.{@link #idInfo} is
|
---|
130 | * returned. Otherwise the standard Id is returned (see
|
---|
131 | * {@link #getStandardId()}).
|
---|
132 | * </p>
|
---|
133 | *
|
---|
134 | * @return if available, shortend Id string; {@link #getStandardId()}
|
---|
135 | * otherwise
|
---|
136 | */
|
---|
137 | public String getShortId() {
|
---|
138 | String shortId = null;
|
---|
139 | if (targetShort != null) {
|
---|
140 | shortId = targetShort + "." + getType();
|
---|
141 | if (!"".equals(idInfo)) {
|
---|
142 | shortId += "." + idInfo;
|
---|
143 | }
|
---|
144 | } else {
|
---|
145 | shortId = getStandardId();
|
---|
146 | }
|
---|
147 | return shortId;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * <p>
|
---|
152 | * Returns the Id string of the event. It has the form {@link #target}.
|
---|
153 | * {@link #type}.{@link #idInfo};
|
---|
154 | * <p>
|
---|
155 | *
|
---|
156 | * @return Id string of the event
|
---|
157 | */
|
---|
158 | public String getStandardId() {
|
---|
159 | String id = "";
|
---|
160 | if (target != null) {
|
---|
161 | id += target + ".";
|
---|
162 | }
|
---|
163 | id += getType();
|
---|
164 | if (!"".equals(idInfo)) {
|
---|
165 | id += "." + idInfo;
|
---|
166 | }
|
---|
167 | return id;
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * <p>
|
---|
172 | * Returns the {@link #target} of the event.
|
---|
173 | * </p>
|
---|
174 | *
|
---|
175 | * @return {@link #target} of the event
|
---|
176 | */
|
---|
177 | public String getTarget() {
|
---|
178 | return target;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * <p>
|
---|
183 | * Returns the {@link #targetShort} of the event.
|
---|
184 | * </p>
|
---|
185 | *
|
---|
186 | * @return {@link #targetShort} of the event
|
---|
187 | */
|
---|
188 | protected String getTargetShort() {
|
---|
189 | return targetShort;
|
---|
190 | }
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * <p>
|
---|
194 | * Returns the {@link #type} of the event.
|
---|
195 | * </p>
|
---|
196 | *
|
---|
197 | * @return {@link #type} of the event
|
---|
198 | */
|
---|
199 | public String getType() {
|
---|
200 | return type;
|
---|
201 | }
|
---|
202 |
|
---|
203 | /*
|
---|
204 | * (non-Javadoc)
|
---|
205 | *
|
---|
206 | * @see java.lang.Object#hashCode()
|
---|
207 | */
|
---|
208 | @Override
|
---|
209 | public int hashCode() {
|
---|
210 | int multiplier = 17;
|
---|
211 | int hash = 42;
|
---|
212 | hash = multiplier * hash + type.hashCode();
|
---|
213 | if (target != null) {
|
---|
214 | hash = multiplier * hash + target.hashCode();
|
---|
215 | }
|
---|
216 |
|
---|
217 | return hash;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * <p>
|
---|
222 | * Sets the {@link #idInfo} of the event. The idInfo is optional and
|
---|
223 | * contains information important for the event's Id that is neither target
|
---|
224 | * nor type.
|
---|
225 | * </p>
|
---|
226 | *
|
---|
227 | * @param info
|
---|
228 | * {@link #idInfo} of the event
|
---|
229 | */
|
---|
230 | public void setIdInfo(String info) {
|
---|
231 | idInfo = info;
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * <p>
|
---|
236 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
237 | * </p>
|
---|
238 | *
|
---|
239 | * @param target
|
---|
240 | * target of the event
|
---|
241 | * @return true, if target was changed, false otherwise
|
---|
242 | */
|
---|
243 | public boolean setTarget(String target) {
|
---|
244 | if (this.target != null) {
|
---|
245 | return false;
|
---|
246 | }
|
---|
247 | this.target = target;
|
---|
248 | return true;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * <p>
|
---|
253 | * Sets the short description of the event target. Once set, the target
|
---|
254 | * cannot be changed.
|
---|
255 | * </p>
|
---|
256 | *
|
---|
257 | * @param targetShort
|
---|
258 | * short target description
|
---|
259 | * @return true, if target was changed, false otherwise
|
---|
260 | */
|
---|
261 | public boolean setTargetShort(String targetShort) {
|
---|
262 | if (this.targetShort != null) {
|
---|
263 | return false;
|
---|
264 | }
|
---|
265 | this.targetShort = targetShort;
|
---|
266 | return true;
|
---|
267 | }
|
---|
268 | }
|
---|