[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 | */
|
---|
| 83 | private Map<String, String> parameters = new HashMap<String, String>();
|
---|
[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 | */
|
---|
[958] | 92 | private List<IReplayable> replay = new LinkedList<IReplayable>();
|
---|
[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 | }
|
---|
| 252 | replay.add(replayable);
|
---|
| 253 | }
|
---|
[79] | 254 |
|
---|
[541] | 255 | /**
|
---|
| 256 | * <p>
|
---|
[958] | 257 | * Adds a {@link List} of {@link IReplayable} to the replay sequence.
|
---|
[541] | 258 | * </p>
|
---|
| 259 | *
|
---|
| 260 | * @param generatedReplaySeq
|
---|
| 261 | * {@link List} that is added to the sequence
|
---|
[766] | 262 | * @throws IllegalArgumentException
|
---|
[541] | 263 | * thrown if generatedReplaySeq is null
|
---|
| 264 | */
|
---|
[566] | 265 | public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
|
---|
[541] | 266 | if (generatedReplaySeq == null) {
|
---|
[766] | 267 | throw new IllegalArgumentException("generatedReplaySeq must not be null");
|
---|
[541] | 268 | }
|
---|
| 269 | replay.addAll(generatedReplaySeq);
|
---|
| 270 | }
|
---|
[958] | 271 |
|
---|
| 272 | /**
|
---|
| 273 | * <p>
|
---|
| 274 | * Adds a parameter to the event or sets it to a new value. The key must not be null. If a
|
---|
| 275 | * parameter with the specified key already exists, its value is replaced with the new one.
|
---|
| 276 | * If the value is null, the parameter with the specified key is removed, if it exists.
|
---|
| 277 | * </p>
|
---|
| 278 | *
|
---|
| 279 | * @param key the key of the parameter
|
---|
| 280 | * @param value the value of the parameter
|
---|
| 281 | *
|
---|
| 282 | * @throws IllegalArgumentException
|
---|
| 283 | * if the provided key is null
|
---|
| 284 | */
|
---|
| 285 | public void setParameter(String key, String value) {
|
---|
| 286 | if (key == null) {
|
---|
| 287 | throw new IllegalArgumentException("key must not be null");
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | if (value == null) {
|
---|
| 291 | parameters.remove(key);
|
---|
| 292 | }
|
---|
| 293 | else {
|
---|
| 294 | parameters.put(key, value);
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
[1] | 297 |
|
---|
[541] | 298 | /**
|
---|
| 299 | * <p>
|
---|
[958] | 300 | * Returns the event parameter with the specified key if it exists or null if not. The key
|
---|
| 301 | * must not be null.
|
---|
| 302 | * </p>
|
---|
| 303 | *
|
---|
| 304 | * @param key the key of the parameter to be returned
|
---|
| 305 | *
|
---|
| 306 | * @return the value of the parameter with the specified key or null if there is no parameter
|
---|
| 307 | * with that key
|
---|
| 308 | *
|
---|
| 309 | * @throws IllegalArgumentException
|
---|
| 310 | * if the provided key is null
|
---|
| 311 | */
|
---|
| 312 | public String getParameter(String key) {
|
---|
| 313 | if (key == null) {
|
---|
| 314 | throw new IllegalArgumentException("key must not be null");
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | return parameters.get(key);
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
| 321 | * <p>
|
---|
[996] | 322 | * Sets the timestamp of the event, i.e. when the event occurred. A timestamp for events is
|
---|
| 323 | * optional. Therefore it is also ignored by the {@link #equals(Object)}-method. A timestamp
|
---|
| 324 | * with a value smaller 0 means, that now timestamp for the event exists.
|
---|
| 325 | * </p>
|
---|
| 326 | *
|
---|
| 327 | * @param timestamp the new value for the timestamp
|
---|
| 328 | */
|
---|
| 329 | public void setTimestamp(long timestamp) {
|
---|
| 330 | this.timestamp = timestamp;
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | /**
|
---|
| 334 | * <p>
|
---|
| 335 | * Returns the timestamp of the event or a value lower than 0 if no timestamp for the event
|
---|
| 336 | * exists.
|
---|
| 337 | * </p>
|
---|
| 338 | *
|
---|
| 339 | * @return the timestamp of the event or a value lower than 0 if no timestamp exists
|
---|
| 340 | */
|
---|
| 341 | public long getTimestamp() {
|
---|
| 342 | return timestamp;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | /**
|
---|
| 346 | * <p>
|
---|
[541] | 347 | * Returns a the list of replay events.
|
---|
| 348 | * </p>
|
---|
| 349 | * <p>
|
---|
| 350 | * The return value is a copy of the list used internally!
|
---|
| 351 | * </p>
|
---|
| 352 | *
|
---|
| 353 | * @return list of replay events.
|
---|
| 354 | */
|
---|
| 355 | public List<IReplayable> getReplayables() {
|
---|
| 356 | return new LinkedList<IReplayable>(replay);
|
---|
| 357 | }
|
---|
[1] | 358 | }
|
---|