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