[927] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany
|
---|
| 2 | //
|
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License");
|
---|
| 4 | // you may not use this file except in compliance with the License.
|
---|
| 5 | // You may obtain a copy of the License at
|
---|
| 6 | //
|
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0
|
---|
| 8 | //
|
---|
| 9 | // Unless required by applicable law or agreed to in writing, software
|
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS,
|
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
| 12 | // See the License for the specific language governing permissions and
|
---|
| 13 | // limitations under the License.
|
---|
| 14 |
|
---|
[922] | 15 | package de.ugoe.cs.autoquest.eventcore;
|
---|
[1] | 16 |
|
---|
[86] | 17 | import java.io.Serializable;
|
---|
[2252] | 18 | import java.util.Collections;
|
---|
[958] | 19 | import java.util.HashMap;
|
---|
[541] | 20 | import java.util.LinkedList;
|
---|
| 21 | import java.util.List;
|
---|
[958] | 22 | import java.util.Map;
|
---|
[1] | 23 |
|
---|
[79] | 24 | /**
|
---|
| 25 | * <p>
|
---|
[541] | 26 | * Base class for all events. An event is described by its {@link #type} and its {@link #target}.
|
---|
[996] | 27 | * An event may be provided with a timestamp and further parameters.
|
---|
[79] | 28 | * </p>
|
---|
| 29 | *
|
---|
| 30 | * @author Steffen Herbold
|
---|
| 31 | * @version 1.0
|
---|
| 32 | *
|
---|
| 33 | */
|
---|
[958] | 34 | public final class Event implements Serializable {
|
---|
[1] | 35 |
|
---|
[541] | 36 | /**
|
---|
[784] | 37 | * <p>
|
---|
[541] | 38 | * Id for object serialization.
|
---|
[784] | 39 | * </p>
|
---|
[541] | 40 | */
|
---|
| 41 | private static final long serialVersionUID = 1L;
|
---|
[86] | 42 |
|
---|
[541] | 43 | /**
|
---|
| 44 | * <p>
|
---|
| 45 | * Global start event that can be used to indicate the start of a sequence.
|
---|
| 46 | * </p>
|
---|
| 47 | */
|
---|
[553] | 48 | public static final Event STARTEVENT = new Event(new StringEventType("START"));
|
---|
[12] | 49 |
|
---|
[541] | 50 | /**
|
---|
| 51 | * <p>
|
---|
| 52 | * Global end event that can be used to indicate the end of a sequence.
|
---|
| 53 | */
|
---|
[553] | 54 | public static final Event ENDEVENT = new Event(new StringEventType("END"));
|
---|
[79] | 55 |
|
---|
[541] | 56 | /**
|
---|
| 57 | * <p>
|
---|
| 58 | * Type of the event.
|
---|
| 59 | * </p>
|
---|
| 60 | */
|
---|
[958] | 61 | private IEventType type;
|
---|
[79] | 62 |
|
---|
[541] | 63 | /**
|
---|
[996] | 64 | * <p>
|
---|
| 65 | * Target of the event.
|
---|
| 66 | * </p>
|
---|
[541] | 67 | */
|
---|
[958] | 68 | private IEventTarget target = null;
|
---|
| 69 |
|
---|
| 70 | /**
|
---|
| 71 | * <p>
|
---|
[996] | 72 | * Timestamp of when the event took place.
|
---|
| 73 | * </p>
|
---|
| 74 | */
|
---|
| 75 | private long timestamp = Long.MIN_VALUE;
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * <p>
|
---|
[958] | 79 | * Map with further parameters of the event, which are not belonging to the type or target.
|
---|
| 80 | * </p>
|
---|
| 81 | */
|
---|
[1873] | 82 | private Map<String, String> parameters;
|
---|
[79] | 83 |
|
---|
[541] | 84 | /**
|
---|
| 85 | * <p>
|
---|
| 86 | * List of {@link IReplayable}s of type T that describes the replay of an event. The
|
---|
[2218] | 87 | * {@link IReplayable}s can be interpreted as sub-events on the platform level that
|
---|
[541] | 88 | * make up the abstract event.
|
---|
| 89 | * </p>
|
---|
| 90 | */
|
---|
[1873] | 91 | private List<IReplayable> replay;
|
---|
[1] | 92 |
|
---|
[541] | 93 | /**
|
---|
| 94 | * <p>
|
---|
| 95 | * Constructor. Creates a new Event with a given type. The type must not be null.
|
---|
| 96 | * </p>
|
---|
| 97 | *
|
---|
| 98 | * @param type
|
---|
| 99 | * type of the event
|
---|
| 100 | */
|
---|
| 101 | public Event(IEventType type) {
|
---|
| 102 | if (type == null) {
|
---|
[766] | 103 | throw new IllegalArgumentException("Event type must not be null");
|
---|
[541] | 104 | }
|
---|
| 105 | this.type = type;
|
---|
| 106 | }
|
---|
[1] | 107 |
|
---|
[541] | 108 | /**
|
---|
| 109 | * <p>
|
---|
| 110 | * Constructor. Creates a new Event with a given type and target. The type must not be null.
|
---|
| 111 | * </p>
|
---|
| 112 | *
|
---|
| 113 | * @param type
|
---|
| 114 | * type of the event
|
---|
| 115 | * @param target
|
---|
| 116 | * target of the event
|
---|
| 117 | */
|
---|
| 118 | public Event(IEventType type, IEventTarget target) {
|
---|
| 119 | this(type);
|
---|
| 120 | this.target = target;
|
---|
| 121 | }
|
---|
[1] | 122 |
|
---|
[541] | 123 | /**
|
---|
| 124 | * <p>
|
---|
[996] | 125 | * Two events are equal, if their {@link #type} and {@link #target} are equal. The timestamp
|
---|
| 126 | * and other parameters are ignored.
|
---|
[541] | 127 | * </p>
|
---|
| 128 | * <p>
|
---|
| 129 | * See {@link Object#equals(Object)} for further information.
|
---|
| 130 | * </p>
|
---|
| 131 | *
|
---|
| 132 | * @param other
|
---|
| 133 | * Event that is compared to this
|
---|
| 134 | * @return true, if events are equal, false otherwise
|
---|
| 135 | */
|
---|
| 136 | @Override
|
---|
| 137 | public boolean equals(Object other) {
|
---|
| 138 | if (this == other) {
|
---|
| 139 | return true;
|
---|
| 140 | }
|
---|
| 141 | if (other instanceof Event) {
|
---|
| 142 | Event otherEvent = (Event) other;
|
---|
| 143 | if (target != null) {
|
---|
| 144 | return type.equals(otherEvent.type) && target.equals(otherEvent.target);
|
---|
| 145 | }
|
---|
| 146 | else {
|
---|
| 147 | return type.equals(otherEvent.type) && otherEvent.target == null;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | return false;
|
---|
| 151 | }
|
---|
[79] | 152 |
|
---|
[541] | 153 | /**
|
---|
| 154 | * <p>
|
---|
[551] | 155 | * Returns {@link #getId()} as String representation of the event.
|
---|
[541] | 156 | * </p>
|
---|
| 157 | *
|
---|
| 158 | * @return String representation of the event
|
---|
| 159 | */
|
---|
| 160 | @Override
|
---|
| 161 | public String toString() {
|
---|
[551] | 162 | return getId();
|
---|
[541] | 163 | }
|
---|
[336] | 164 |
|
---|
[541] | 165 | /**
|
---|
| 166 | * <p>
|
---|
| 167 | * Returns the {@link #target} of the event.
|
---|
| 168 | * </p>
|
---|
| 169 | *
|
---|
| 170 | * @return {@link #target} of the event
|
---|
| 171 | */
|
---|
| 172 | public IEventTarget getTarget() {
|
---|
| 173 | return target;
|
---|
| 174 | }
|
---|
[1] | 175 |
|
---|
[541] | 176 | /**
|
---|
| 177 | * <p>
|
---|
| 178 | * Returns the {@link #type} of the event.
|
---|
| 179 | * </p>
|
---|
| 180 | *
|
---|
| 181 | * @return {@link #type} of the event
|
---|
| 182 | */
|
---|
| 183 | public IEventType getType() {
|
---|
| 184 | return type;
|
---|
| 185 | }
|
---|
[551] | 186 |
|
---|
| 187 | /**
|
---|
| 188 | * <p>
|
---|
| 189 | * Returns the combination of {@link #type} and {@link #target} as id.
|
---|
| 190 | * </p>
|
---|
| 191 | *
|
---|
| 192 | * @return string of the form (type,target)
|
---|
| 193 | */
|
---|
| 194 | public String getId() {
|
---|
[553] | 195 | String id = type.toString();
|
---|
| 196 | if( target!=null ) {
|
---|
[681] | 197 | id += "." + target.getStringIdentifier();
|
---|
[553] | 198 | }
|
---|
| 199 | return id;
|
---|
[551] | 200 | }
|
---|
[1] | 201 |
|
---|
[541] | 202 | /*
|
---|
| 203 | * (non-Javadoc)
|
---|
| 204 | *
|
---|
| 205 | * @see java.lang.Object#hashCode()
|
---|
| 206 | */
|
---|
| 207 | @Override
|
---|
| 208 | public int hashCode() {
|
---|
| 209 | int multiplier = 17;
|
---|
| 210 | int hash = 42;
|
---|
| 211 | if (type != null) {
|
---|
| 212 | hash = multiplier * hash + type.hashCode();
|
---|
| 213 | }
|
---|
| 214 | if (target != null) {
|
---|
| 215 | hash = multiplier * hash + target.hashCode();
|
---|
| 216 | }
|
---|
| 217 | return hash;
|
---|
| 218 | }
|
---|
[1] | 219 |
|
---|
[541] | 220 | /**
|
---|
| 221 | * <p>
|
---|
| 222 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
| 223 | * </p>
|
---|
| 224 | *
|
---|
| 225 | * @param target
|
---|
| 226 | * target of the event
|
---|
| 227 | * @return true, if target was changed, false otherwise
|
---|
| 228 | */
|
---|
| 229 | public boolean setTarget(IEventTarget target) {
|
---|
| 230 | if (this.target != null) {
|
---|
| 231 | return false;
|
---|
| 232 | }
|
---|
| 233 | this.target = target;
|
---|
| 234 | return true;
|
---|
| 235 | }
|
---|
[1] | 236 |
|
---|
[541] | 237 | /**
|
---|
| 238 | * <p>
|
---|
| 239 | * Adds a new {@link IReplayable} of type T to the replay sequence.
|
---|
| 240 | * </p>
|
---|
| 241 | *
|
---|
| 242 | * @param replayable
|
---|
| 243 | * element that is added to the sequence
|
---|
[766] | 244 | * @throws IllegalArgumentException
|
---|
[541] | 245 | * thrown is replayable is null
|
---|
| 246 | */
|
---|
| 247 | public void addReplayable(IReplayable replayable) {
|
---|
| 248 | if (replayable == null) {
|
---|
[766] | 249 | throw new IllegalArgumentException("replayble must not be null");
|
---|
[541] | 250 | }
|
---|
[1873] | 251 |
|
---|
| 252 | if (replay == null) {
|
---|
| 253 | replay = new LinkedList<IReplayable>();
|
---|
| 254 | }
|
---|
| 255 |
|
---|
[541] | 256 | replay.add(replayable);
|
---|
| 257 | }
|
---|
[79] | 258 |
|
---|
[541] | 259 | /**
|
---|
| 260 | * <p>
|
---|
[958] | 261 | * Adds a {@link List} of {@link IReplayable} to the replay sequence.
|
---|
[541] | 262 | * </p>
|
---|
| 263 | *
|
---|
| 264 | * @param generatedReplaySeq
|
---|
| 265 | * {@link List} that is added to the sequence
|
---|
[766] | 266 | * @throws IllegalArgumentException
|
---|
[541] | 267 | * thrown if generatedReplaySeq is null
|
---|
| 268 | */
|
---|
[566] | 269 | public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
|
---|
[541] | 270 | if (generatedReplaySeq == null) {
|
---|
[766] | 271 | throw new IllegalArgumentException("generatedReplaySeq must not be null");
|
---|
[541] | 272 | }
|
---|
[1873] | 273 |
|
---|
| 274 | if (replay == null) {
|
---|
| 275 | replay = new LinkedList<IReplayable>();
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[541] | 278 | replay.addAll(generatedReplaySeq);
|
---|
| 279 | }
|
---|
[958] | 280 |
|
---|
| 281 | /**
|
---|
| 282 | * <p>
|
---|
| 283 | * Adds a parameter to the event or sets it to a new value. The key must not be null. If a
|
---|
| 284 | * parameter with the specified key already exists, its value is replaced with the new one.
|
---|
| 285 | * If the value is null, the parameter with the specified key is removed, if it exists.
|
---|
| 286 | * </p>
|
---|
| 287 | *
|
---|
| 288 | * @param key the key of the parameter
|
---|
| 289 | * @param value the value of the parameter
|
---|
| 290 | *
|
---|
| 291 | * @throws IllegalArgumentException
|
---|
| 292 | * if the provided key is null
|
---|
| 293 | */
|
---|
| 294 | public void setParameter(String key, String value) {
|
---|
| 295 | if (key == null) {
|
---|
| 296 | throw new IllegalArgumentException("key must not be null");
|
---|
| 297 | }
|
---|
| 298 |
|
---|
[1873] | 299 | if (parameters == null) {
|
---|
| 300 | parameters = new HashMap<String, String>();
|
---|
| 301 | }
|
---|
| 302 |
|
---|
[958] | 303 | if (value == null) {
|
---|
| 304 | parameters.remove(key);
|
---|
| 305 | }
|
---|
| 306 | else {
|
---|
| 307 | parameters.put(key, value);
|
---|
| 308 | }
|
---|
| 309 | }
|
---|
[2252] | 310 |
|
---|
| 311 | /**
|
---|
| 312 | * <p>
|
---|
| 313 | * Returns a map containing the parameters attached to the event. It returns null, if there
|
---|
| 314 | * are no parameters.
|
---|
| 315 | * </p>
|
---|
| 316 | */
|
---|
| 317 | public Map<String, String> getParameters() {
|
---|
| 318 | if (parameters == null) {
|
---|
| 319 | return null;
|
---|
| 320 | }
|
---|
| 321 | else {
|
---|
| 322 | return Collections.unmodifiableMap(parameters);
|
---|
| 323 | }
|
---|
| 324 | }
|
---|
[1] | 325 |
|
---|
[541] | 326 | /**
|
---|
| 327 | * <p>
|
---|
[958] | 328 | * Returns the event parameter with the specified key if it exists or null if not. The key
|
---|
| 329 | * must not be null.
|
---|
| 330 | * </p>
|
---|
| 331 | *
|
---|
| 332 | * @param key the key of the parameter to be returned
|
---|
| 333 | *
|
---|
| 334 | * @return the value of the parameter with the specified key or null if there is no parameter
|
---|
| 335 | * with that key
|
---|
| 336 | *
|
---|
| 337 | * @throws IllegalArgumentException
|
---|
| 338 | * if the provided key is null
|
---|
| 339 | */
|
---|
| 340 | public String getParameter(String key) {
|
---|
| 341 | if (key == null) {
|
---|
| 342 | throw new IllegalArgumentException("key must not be null");
|
---|
| 343 | }
|
---|
| 344 |
|
---|
[1873] | 345 | if (parameters != null) {
|
---|
| 346 | return parameters.get(key);
|
---|
| 347 | }
|
---|
| 348 | else {
|
---|
| 349 | return null;
|
---|
| 350 | }
|
---|
[958] | 351 | }
|
---|
| 352 |
|
---|
| 353 | /**
|
---|
| 354 | * <p>
|
---|
[996] | 355 | * Sets the timestamp of the event, i.e. when the event occurred. A timestamp for events is
|
---|
| 356 | * optional. Therefore it is also ignored by the {@link #equals(Object)}-method. A timestamp
|
---|
| 357 | * with a value smaller 0 means, that now timestamp for the event exists.
|
---|
| 358 | * </p>
|
---|
| 359 | *
|
---|
| 360 | * @param timestamp the new value for the timestamp
|
---|
| 361 | */
|
---|
| 362 | public void setTimestamp(long timestamp) {
|
---|
| 363 | this.timestamp = timestamp;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /**
|
---|
| 367 | * <p>
|
---|
| 368 | * Returns the timestamp of the event or a value lower than 0 if no timestamp for the event
|
---|
| 369 | * exists.
|
---|
| 370 | * </p>
|
---|
| 371 | *
|
---|
| 372 | * @return the timestamp of the event or a value lower than 0 if no timestamp exists
|
---|
| 373 | */
|
---|
| 374 | public long getTimestamp() {
|
---|
| 375 | return timestamp;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | /**
|
---|
| 379 | * <p>
|
---|
[541] | 380 | * Returns a the list of replay events.
|
---|
| 381 | * </p>
|
---|
| 382 | * <p>
|
---|
| 383 | * The return value is a copy of the list used internally!
|
---|
| 384 | * </p>
|
---|
| 385 | *
|
---|
| 386 | * @return list of replay events.
|
---|
| 387 | */
|
---|
| 388 | public List<IReplayable> getReplayables() {
|
---|
[1873] | 389 | if (replay != null) {
|
---|
| 390 | return new LinkedList<IReplayable>(replay);
|
---|
| 391 | }
|
---|
| 392 | else {
|
---|
| 393 | return new LinkedList<IReplayable>();
|
---|
| 394 | }
|
---|
[541] | 395 | }
|
---|
[1] | 396 | }
|
---|