[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;
|
---|
[541] | 18 | import java.util.LinkedList;
|
---|
| 19 | import java.util.List;
|
---|
[1] | 20 |
|
---|
[79] | 21 | /**
|
---|
| 22 | * <p>
|
---|
[541] | 23 | * Base class for all events. An event is described by its {@link #type} and its {@link #target}.
|
---|
[79] | 24 | * </p>
|
---|
| 25 | *
|
---|
| 26 | * @author Steffen Herbold
|
---|
| 27 | * @version 1.0
|
---|
| 28 | *
|
---|
| 29 | * @param <T>
|
---|
[541] | 30 | * Can be used to declare that events belong to a specific platform without subclassing.
|
---|
[79] | 31 | */
|
---|
[541] | 32 | public class Event implements Serializable {
|
---|
[1] | 33 |
|
---|
[541] | 34 | /**
|
---|
[784] | 35 | * <p>
|
---|
[541] | 36 | * Id for object serialization.
|
---|
[784] | 37 | * </p>
|
---|
[541] | 38 | */
|
---|
| 39 | private static final long serialVersionUID = 1L;
|
---|
[86] | 40 |
|
---|
[541] | 41 | /**
|
---|
| 42 | * <p>
|
---|
| 43 | * Global start event that can be used to indicate the start of a sequence.
|
---|
| 44 | * </p>
|
---|
| 45 | */
|
---|
[553] | 46 | public static final Event STARTEVENT = new Event(new StringEventType("START"));
|
---|
[12] | 47 |
|
---|
[541] | 48 | /**
|
---|
| 49 | * <p>
|
---|
| 50 | * Global end event that can be used to indicate the end of a sequence.
|
---|
| 51 | */
|
---|
[553] | 52 | public static final Event ENDEVENT = new Event(new StringEventType("END"));
|
---|
[79] | 53 |
|
---|
[541] | 54 | /**
|
---|
| 55 | * <p>
|
---|
| 56 | * Type of the event.
|
---|
| 57 | * </p>
|
---|
| 58 | */
|
---|
| 59 | protected IEventType type;
|
---|
[79] | 60 |
|
---|
[541] | 61 | /**
|
---|
| 62 | * </p> Target of the event.
|
---|
| 63 | */
|
---|
| 64 | protected IEventTarget target = null;
|
---|
[79] | 65 |
|
---|
[541] | 66 | /**
|
---|
| 67 | * <p>
|
---|
| 68 | * List of {@link IReplayable}s of type T that describes the replay of an event. The
|
---|
| 69 | * {@link IReplayable}s can be interpreted as <it>sub-events</it> on the platform level that
|
---|
| 70 | * make up the abstract event.
|
---|
| 71 | * </p>
|
---|
| 72 | */
|
---|
| 73 | protected List<IReplayable> replay = new LinkedList<IReplayable>();
|
---|
[1] | 74 |
|
---|
[541] | 75 | /**
|
---|
| 76 | * <p>
|
---|
| 77 | * Constructor. Creates a new Event with a given type. The type must not be null.
|
---|
| 78 | * </p>
|
---|
| 79 | *
|
---|
| 80 | * @param type
|
---|
| 81 | * type of the event
|
---|
| 82 | */
|
---|
| 83 | public Event(IEventType type) {
|
---|
| 84 | if (type == null) {
|
---|
[766] | 85 | throw new IllegalArgumentException("Event type must not be null");
|
---|
[541] | 86 | }
|
---|
| 87 | this.type = type;
|
---|
| 88 | }
|
---|
[1] | 89 |
|
---|
[541] | 90 | /**
|
---|
| 91 | * <p>
|
---|
| 92 | * Constructor. Creates a new Event with a given type and target. The type must not be null.
|
---|
| 93 | * </p>
|
---|
| 94 | *
|
---|
| 95 | * @param type
|
---|
| 96 | * type of the event
|
---|
| 97 | * @param target
|
---|
| 98 | * target of the event
|
---|
| 99 | */
|
---|
| 100 | public Event(IEventType type, IEventTarget target) {
|
---|
| 101 | this(type);
|
---|
| 102 | this.target = target;
|
---|
| 103 | }
|
---|
[1] | 104 |
|
---|
[541] | 105 | /**
|
---|
| 106 | * <p>
|
---|
| 107 | * Two events are equal, if their {@link #type} and {@link #target} are equal.
|
---|
| 108 | * </p>
|
---|
| 109 | * <p>
|
---|
| 110 | * See {@link Object#equals(Object)} for further information.
|
---|
| 111 | * </p>
|
---|
| 112 | *
|
---|
| 113 | * @param other
|
---|
| 114 | * Event that is compared to this
|
---|
| 115 | * @return true, if events are equal, false otherwise
|
---|
| 116 | */
|
---|
| 117 | @Override
|
---|
| 118 | public boolean equals(Object other) {
|
---|
| 119 | if (this == other) {
|
---|
| 120 | return true;
|
---|
| 121 | }
|
---|
| 122 | if (other instanceof Event) {
|
---|
| 123 | Event otherEvent = (Event) other;
|
---|
| 124 | if (target != null) {
|
---|
| 125 | return type.equals(otherEvent.type) && target.equals(otherEvent.target);
|
---|
| 126 | }
|
---|
| 127 | else {
|
---|
| 128 | return type.equals(otherEvent.type) && otherEvent.target == null;
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 | return false;
|
---|
| 132 | }
|
---|
[79] | 133 |
|
---|
[541] | 134 | /**
|
---|
| 135 | * <p>
|
---|
[551] | 136 | * Returns {@link #getId()} as String representation of the event.
|
---|
[541] | 137 | * </p>
|
---|
| 138 | *
|
---|
| 139 | * @return String representation of the event
|
---|
| 140 | */
|
---|
| 141 | @Override
|
---|
| 142 | public String toString() {
|
---|
[551] | 143 | return getId();
|
---|
[541] | 144 | }
|
---|
[336] | 145 |
|
---|
[541] | 146 | /**
|
---|
| 147 | * <p>
|
---|
| 148 | * Returns the {@link #target} of the event.
|
---|
| 149 | * </p>
|
---|
| 150 | *
|
---|
| 151 | * @return {@link #target} of the event
|
---|
| 152 | */
|
---|
| 153 | public IEventTarget getTarget() {
|
---|
| 154 | return target;
|
---|
| 155 | }
|
---|
[1] | 156 |
|
---|
[541] | 157 | /**
|
---|
| 158 | * <p>
|
---|
| 159 | * Returns the {@link #type} of the event.
|
---|
| 160 | * </p>
|
---|
| 161 | *
|
---|
| 162 | * @return {@link #type} of the event
|
---|
| 163 | */
|
---|
| 164 | public IEventType getType() {
|
---|
| 165 | return type;
|
---|
| 166 | }
|
---|
[551] | 167 |
|
---|
| 168 | /**
|
---|
| 169 | * <p>
|
---|
| 170 | * Returns the combination of {@link #type} and {@link #target} as id.
|
---|
| 171 | * </p>
|
---|
| 172 | *
|
---|
| 173 | * @return string of the form (type,target)
|
---|
| 174 | */
|
---|
| 175 | public String getId() {
|
---|
[553] | 176 | String id = type.toString();
|
---|
| 177 | if( target!=null ) {
|
---|
[681] | 178 | id += "." + target.getStringIdentifier();
|
---|
[553] | 179 | }
|
---|
| 180 | return id;
|
---|
[551] | 181 | }
|
---|
[1] | 182 |
|
---|
[541] | 183 | /*
|
---|
| 184 | * (non-Javadoc)
|
---|
| 185 | *
|
---|
| 186 | * @see java.lang.Object#hashCode()
|
---|
| 187 | */
|
---|
| 188 | @Override
|
---|
| 189 | public int hashCode() {
|
---|
| 190 | int multiplier = 17;
|
---|
| 191 | int hash = 42;
|
---|
| 192 | if (type != null) {
|
---|
| 193 | hash = multiplier * hash + type.hashCode();
|
---|
| 194 | }
|
---|
| 195 | if (target != null) {
|
---|
| 196 | hash = multiplier * hash + target.hashCode();
|
---|
| 197 | }
|
---|
| 198 | return hash;
|
---|
| 199 | }
|
---|
[1] | 200 |
|
---|
[541] | 201 | /**
|
---|
| 202 | * <p>
|
---|
| 203 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
| 204 | * </p>
|
---|
| 205 | *
|
---|
| 206 | * @param target
|
---|
| 207 | * target of the event
|
---|
| 208 | * @return true, if target was changed, false otherwise
|
---|
| 209 | */
|
---|
| 210 | public boolean setTarget(IEventTarget target) {
|
---|
| 211 | if (this.target != null) {
|
---|
| 212 | return false;
|
---|
| 213 | }
|
---|
| 214 | this.target = target;
|
---|
| 215 | return true;
|
---|
| 216 | }
|
---|
[1] | 217 |
|
---|
[541] | 218 | /**
|
---|
| 219 | * <p>
|
---|
| 220 | * Adds a new {@link IReplayable} of type T to the replay sequence.
|
---|
| 221 | * </p>
|
---|
| 222 | *
|
---|
| 223 | * @param replayable
|
---|
| 224 | * element that is added to the sequence
|
---|
[766] | 225 | * @throws IllegalArgumentException
|
---|
[541] | 226 | * thrown is replayable is null
|
---|
| 227 | */
|
---|
| 228 | public void addReplayable(IReplayable replayable) {
|
---|
| 229 | if (replayable == null) {
|
---|
[766] | 230 | throw new IllegalArgumentException("replayble must not be null");
|
---|
[541] | 231 | }
|
---|
| 232 | replay.add(replayable);
|
---|
| 233 | }
|
---|
[79] | 234 |
|
---|
[541] | 235 | /**
|
---|
| 236 | * <p>
|
---|
| 237 | * Adds a {@link List}ist of {@link IReplayable} to the replay sequence.
|
---|
| 238 | * </p>
|
---|
| 239 | *
|
---|
| 240 | * @param generatedReplaySeq
|
---|
| 241 | * {@link List} that is added to the sequence
|
---|
[766] | 242 | * @throws IllegalArgumentException
|
---|
[541] | 243 | * thrown if generatedReplaySeq is null
|
---|
| 244 | */
|
---|
[566] | 245 | public void addReplayableSequence(List<? extends IReplayable> generatedReplaySeq) {
|
---|
[541] | 246 | if (generatedReplaySeq == null) {
|
---|
[766] | 247 | throw new IllegalArgumentException("generatedReplaySeq must not be null");
|
---|
[541] | 248 | }
|
---|
| 249 | replay.addAll(generatedReplaySeq);
|
---|
| 250 | }
|
---|
[1] | 251 |
|
---|
[541] | 252 | /**
|
---|
| 253 | * <p>
|
---|
| 254 | * Returns a the list of replay events.
|
---|
| 255 | * </p>
|
---|
| 256 | * <p>
|
---|
| 257 | * The return value is a copy of the list used internally!
|
---|
| 258 | * </p>
|
---|
| 259 | *
|
---|
| 260 | * @return list of replay events.
|
---|
| 261 | */
|
---|
| 262 | public List<IReplayable> getReplayables() {
|
---|
| 263 | return new LinkedList<IReplayable>(replay);
|
---|
| 264 | }
|
---|
[1] | 265 | }
|
---|