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