[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}.
|
---|
[79] | 26 | * </p>
|
---|
| 27 | *
|
---|
| 28 | * @author Steffen Herbold
|
---|
| 29 | * @version 1.0
|
---|
| 30 | *
|
---|
| 31 | * @param <T>
|
---|
[541] | 32 | * Can be used to declare that events belong to a specific platform without subclassing.
|
---|
[79] | 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 | /**
|
---|
| 64 | * </p> Target of the event.
|
---|
| 65 | */
|
---|
[958] | 66 | private IEventTarget target = null;
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * <p>
|
---|
| 70 | * Map with further parameters of the event, which are not belonging to the type or target.
|
---|
| 71 | * </p>
|
---|
| 72 | */
|
---|
| 73 | private Map<String, String> parameters = new HashMap<String, String>();
|
---|
[79] | 74 |
|
---|
[541] | 75 | /**
|
---|
| 76 | * <p>
|
---|
| 77 | * List of {@link IReplayable}s of type T that describes the replay of an event. The
|
---|
| 78 | * {@link IReplayable}s can be interpreted as <it>sub-events</it> on the platform level that
|
---|
| 79 | * make up the abstract event.
|
---|
| 80 | * </p>
|
---|
| 81 | */
|
---|
[958] | 82 | private List<IReplayable> replay = new LinkedList<IReplayable>();
|
---|
[1] | 83 |
|
---|
[541] | 84 | /**
|
---|
| 85 | * <p>
|
---|
| 86 | * Constructor. Creates a new Event with a given type. The type must not be null.
|
---|
| 87 | * </p>
|
---|
| 88 | *
|
---|
| 89 | * @param type
|
---|
| 90 | * type of the event
|
---|
| 91 | */
|
---|
| 92 | public Event(IEventType type) {
|
---|
| 93 | if (type == null) {
|
---|
[766] | 94 | throw new IllegalArgumentException("Event type must not be null");
|
---|
[541] | 95 | }
|
---|
| 96 | this.type = type;
|
---|
| 97 | }
|
---|
[1] | 98 |
|
---|
[541] | 99 | /**
|
---|
| 100 | * <p>
|
---|
| 101 | * Constructor. Creates a new Event with a given type and target. The type must not be null.
|
---|
| 102 | * </p>
|
---|
| 103 | *
|
---|
| 104 | * @param type
|
---|
| 105 | * type of the event
|
---|
| 106 | * @param target
|
---|
| 107 | * target of the event
|
---|
| 108 | */
|
---|
| 109 | public Event(IEventType type, IEventTarget target) {
|
---|
| 110 | this(type);
|
---|
| 111 | this.target = target;
|
---|
| 112 | }
|
---|
[1] | 113 |
|
---|
[541] | 114 | /**
|
---|
| 115 | * <p>
|
---|
| 116 | * Two events are equal, if their {@link #type} and {@link #target} are equal.
|
---|
| 117 | * </p>
|
---|
| 118 | * <p>
|
---|
| 119 | * See {@link Object#equals(Object)} for further information.
|
---|
| 120 | * </p>
|
---|
| 121 | *
|
---|
| 122 | * @param other
|
---|
| 123 | * Event that is compared to this
|
---|
| 124 | * @return true, if events are equal, false otherwise
|
---|
| 125 | */
|
---|
| 126 | @Override
|
---|
| 127 | public boolean equals(Object other) {
|
---|
| 128 | if (this == other) {
|
---|
| 129 | return true;
|
---|
| 130 | }
|
---|
| 131 | if (other instanceof Event) {
|
---|
| 132 | Event otherEvent = (Event) other;
|
---|
| 133 | if (target != null) {
|
---|
| 134 | return type.equals(otherEvent.type) && target.equals(otherEvent.target);
|
---|
| 135 | }
|
---|
| 136 | else {
|
---|
| 137 | return type.equals(otherEvent.type) && otherEvent.target == null;
|
---|
| 138 | }
|
---|
| 139 | }
|
---|
| 140 | return false;
|
---|
| 141 | }
|
---|
[79] | 142 |
|
---|
[541] | 143 | /**
|
---|
| 144 | * <p>
|
---|
[551] | 145 | * Returns {@link #getId()} as String representation of the event.
|
---|
[541] | 146 | * </p>
|
---|
| 147 | *
|
---|
| 148 | * @return String representation of the event
|
---|
| 149 | */
|
---|
| 150 | @Override
|
---|
| 151 | public String toString() {
|
---|
[551] | 152 | return getId();
|
---|
[541] | 153 | }
|
---|
[336] | 154 |
|
---|
[541] | 155 | /**
|
---|
| 156 | * <p>
|
---|
| 157 | * Returns the {@link #target} of the event.
|
---|
| 158 | * </p>
|
---|
| 159 | *
|
---|
| 160 | * @return {@link #target} of the event
|
---|
| 161 | */
|
---|
| 162 | public IEventTarget getTarget() {
|
---|
| 163 | return target;
|
---|
| 164 | }
|
---|
[1] | 165 |
|
---|
[541] | 166 | /**
|
---|
| 167 | * <p>
|
---|
| 168 | * Returns the {@link #type} of the event.
|
---|
| 169 | * </p>
|
---|
| 170 | *
|
---|
| 171 | * @return {@link #type} of the event
|
---|
| 172 | */
|
---|
| 173 | public IEventType getType() {
|
---|
| 174 | return type;
|
---|
| 175 | }
|
---|
[551] | 176 |
|
---|
| 177 | /**
|
---|
| 178 | * <p>
|
---|
| 179 | * Returns the combination of {@link #type} and {@link #target} as id.
|
---|
| 180 | * </p>
|
---|
| 181 | *
|
---|
| 182 | * @return string of the form (type,target)
|
---|
| 183 | */
|
---|
| 184 | public String getId() {
|
---|
[553] | 185 | String id = type.toString();
|
---|
| 186 | if( target!=null ) {
|
---|
[681] | 187 | id += "." + target.getStringIdentifier();
|
---|
[553] | 188 | }
|
---|
| 189 | return id;
|
---|
[551] | 190 | }
|
---|
[1] | 191 |
|
---|
[541] | 192 | /*
|
---|
| 193 | * (non-Javadoc)
|
---|
| 194 | *
|
---|
| 195 | * @see java.lang.Object#hashCode()
|
---|
| 196 | */
|
---|
| 197 | @Override
|
---|
| 198 | public int hashCode() {
|
---|
| 199 | int multiplier = 17;
|
---|
| 200 | int hash = 42;
|
---|
| 201 | if (type != null) {
|
---|
| 202 | hash = multiplier * hash + type.hashCode();
|
---|
| 203 | }
|
---|
| 204 | if (target != null) {
|
---|
| 205 | hash = multiplier * hash + target.hashCode();
|
---|
| 206 | }
|
---|
| 207 | return hash;
|
---|
| 208 | }
|
---|
[1] | 209 |
|
---|
[541] | 210 | /**
|
---|
| 211 | * <p>
|
---|
| 212 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
| 213 | * </p>
|
---|
| 214 | *
|
---|
| 215 | * @param target
|
---|
| 216 | * target of the event
|
---|
| 217 | * @return true, if target was changed, false otherwise
|
---|
| 218 | */
|
---|
| 219 | public boolean setTarget(IEventTarget target) {
|
---|
| 220 | if (this.target != null) {
|
---|
| 221 | return false;
|
---|
| 222 | }
|
---|
| 223 | this.target = target;
|
---|
| 224 | return true;
|
---|
| 225 | }
|
---|
[1] | 226 |
|
---|
[541] | 227 | /**
|
---|
| 228 | * <p>
|
---|
| 229 | * Adds a new {@link IReplayable} of type T to the replay sequence.
|
---|
| 230 | * </p>
|
---|
| 231 | *
|
---|
| 232 | * @param replayable
|
---|
| 233 | * element that is added to the sequence
|
---|
[766] | 234 | * @throws IllegalArgumentException
|
---|
[541] | 235 | * thrown is replayable is null
|
---|
| 236 | */
|
---|
| 237 | public void addReplayable(IReplayable replayable) {
|
---|
| 238 | if (replayable == null) {
|
---|
[766] | 239 | throw new IllegalArgumentException("replayble must not be null");
|
---|
[541] | 240 | }
|
---|
| 241 | replay.add(replayable);
|
---|
| 242 | }
|
---|
[79] | 243 |
|
---|
[541] | 244 | /**
|
---|
| 245 | * <p>
|
---|
[958] | 246 | * Adds a {@link List} of {@link IReplayable} to the replay sequence.
|
---|
[541] | 247 | * </p>
|
---|
| 248 | *
|
---|
| 249 | * @param generatedReplaySeq
|
---|
| 250 | * {@link List} that is added to the sequence
|
---|
[766] | 251 | * @throws IllegalArgumentException
|
---|
[541] | 252 | * thrown if generatedReplaySeq is null
|
---|
| 253 | */
|
---|
[566] | 254 | public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
|
---|
[541] | 255 | if (generatedReplaySeq == null) {
|
---|
[766] | 256 | throw new IllegalArgumentException("generatedReplaySeq must not be null");
|
---|
[541] | 257 | }
|
---|
| 258 | replay.addAll(generatedReplaySeq);
|
---|
| 259 | }
|
---|
[958] | 260 |
|
---|
| 261 | /**
|
---|
| 262 | * <p>
|
---|
| 263 | * Adds a parameter to the event or sets it to a new value. The key must not be null. If a
|
---|
| 264 | * parameter with the specified key already exists, its value is replaced with the new one.
|
---|
| 265 | * If the value is null, the parameter with the specified key is removed, if it exists.
|
---|
| 266 | * </p>
|
---|
| 267 | *
|
---|
| 268 | * @param key the key of the parameter
|
---|
| 269 | * @param value the value of the parameter
|
---|
| 270 | *
|
---|
| 271 | * @throws IllegalArgumentException
|
---|
| 272 | * if the provided key is null
|
---|
| 273 | */
|
---|
| 274 | public void setParameter(String key, String value) {
|
---|
| 275 | if (key == null) {
|
---|
| 276 | throw new IllegalArgumentException("key must not be null");
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | if (value == null) {
|
---|
| 280 | parameters.remove(key);
|
---|
| 281 | }
|
---|
| 282 | else {
|
---|
| 283 | parameters.put(key, value);
|
---|
| 284 | }
|
---|
| 285 | }
|
---|
[1] | 286 |
|
---|
[541] | 287 | /**
|
---|
| 288 | * <p>
|
---|
[958] | 289 | * Returns the event parameter with the specified key if it exists or null if not. The key
|
---|
| 290 | * must not be null.
|
---|
| 291 | * </p>
|
---|
| 292 | *
|
---|
| 293 | * @param key the key of the parameter to be returned
|
---|
| 294 | *
|
---|
| 295 | * @return the value of the parameter with the specified key or null if there is no parameter
|
---|
| 296 | * with that key
|
---|
| 297 | *
|
---|
| 298 | * @throws IllegalArgumentException
|
---|
| 299 | * if the provided key is null
|
---|
| 300 | */
|
---|
| 301 | public String getParameter(String key) {
|
---|
| 302 | if (key == null) {
|
---|
| 303 | throw new IllegalArgumentException("key must not be null");
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | return parameters.get(key);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /**
|
---|
| 310 | * <p>
|
---|
[541] | 311 | * Returns a the list of replay events.
|
---|
| 312 | * </p>
|
---|
| 313 | * <p>
|
---|
| 314 | * The return value is a copy of the list used internally!
|
---|
| 315 | * </p>
|
---|
| 316 | *
|
---|
| 317 | * @return list of replay events.
|
---|
| 318 | */
|
---|
| 319 | public List<IReplayable> getReplayables() {
|
---|
| 320 | return new LinkedList<IReplayable>(replay);
|
---|
| 321 | }
|
---|
[1] | 322 | }
|
---|