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