Changeset 541 for trunk/quest-core-events/src/main/java/de/ugoe/cs
- Timestamp:
- 08/16/12 10:55:37 (12 years ago)
- Location:
- trunk/quest-core-events/src/main/java/de/ugoe/cs/quest
- Files:
-
- 3 added
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/SequenceInstanceOf.java
r480 r541 44 44 Object listObj = ((Collection<?>) obj).iterator().next(); 45 45 if (listObj instanceof List<?>) { 46 if (((List<?>) listObj).iterator().next() instanceof Event <?>) {46 if (((List<?>) listObj).iterator().next() instanceof Event) { 47 47 return true; 48 48 } … … 68 68 try { 69 69 if (obj instanceof List<?>) { 70 if (((List<?>) obj).iterator().next() instanceof Event <?>) {70 if (((List<?>) obj).iterator().next() instanceof Event) { 71 71 return true; 72 72 } -
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/Event.java
r433 r541 1 1 2 package de.ugoe.cs.quest.eventcore; 2 3 3 4 import java.io.Serializable; 4 5 import java.security.InvalidParameterException; 6 import java.util.LinkedList; 7 import java.util.List; 5 8 6 9 /** 7 10 * <p> 8 * Base class for all events. An event is described by its {@link #type} and its 9 * {@link #target}. 11 * Base class for all events. An event is described by its {@link #type} and its {@link #target}. 10 12 * </p> 11 13 * … … 14 16 * 15 17 * @param <T> 16 * Can be used to declare that events belong to a specific platform 17 * without subclassing. 18 * Can be used to declare that events belong to a specific platform without subclassing. 18 19 */ 19 public class Event<T> implements Serializable { 20 21 /** 22 * Id for object serialization. 23 */ 24 private static final long serialVersionUID = 1L; 25 26 /** 27 * <p> 28 * Global start event that can be used to indicate the start of a sequence. 29 * </p> 30 */ 31 public static final Event<Object> STARTEVENT = new Event<Object>("START"); 32 33 /** 34 * <p> 35 * Global end event that can be used to indicate the end of a sequence. 36 */ 37 public static final Event<Object> ENDEVENT = new Event<Object>("END"); 38 39 /** 40 * <p> 41 * Type of the event. 42 * </p> 43 */ 44 protected String type; 45 46 /** 47 * </p> Target of the event. 48 */ 49 protected String target = null; 50 51 /** 52 * <p> 53 * Short description of the event target. 54 * </p> 55 */ 56 protected String targetShort = null; 57 58 /** 59 * Further information about the event that shall be included in its Id. 60 */ 61 protected String idInfo = ""; 62 63 /** 64 * <p> 65 * Constructor. Creates a new Event with a given type. 66 * </p> 67 * 68 * @param type 69 * type of the event 70 */ 71 public Event(String type) { 72 if (type == null) { 73 throw new InvalidParameterException("Event type must not be null"); 74 } 75 this.type = type; 76 } 77 78 /** 79 * <p> 80 * Two events are equal, if their {@link #type} and {@link #target} are 81 * equal. 82 * </p> 83 * <p> 84 * See {@link Object#equals(Object)} for further information. 85 * </p> 86 * 87 * @param other 88 * Event that is compared to this 89 * @return true, if events are equal, false otherwise 90 */ 91 @Override 92 public boolean equals(Object other) { 93 if (this == other) { 94 return true; 95 } 96 if (other instanceof Event<?>) { 97 Event<?> otherEvent = (Event<?>) other; 98 if (otherEvent.canEqual(this)) { 99 if (type != null) { 100 return targetEquals(otherEvent.target) 101 && type.equals(otherEvent.type); 102 } else { 103 return targetEquals(otherEvent.target) 104 && otherEvent.type == null; 105 } 106 } else { 107 return false; 108 } 109 } else { 110 return false; 111 } 112 } 113 114 public boolean canEqual(Object other) { 115 return (other instanceof Event<?>); 116 } 117 118 /** 119 * <p> 120 * Returns {@link #getStandardId()} as String representation of the event. 121 * </p> 122 * 123 * @return String represenation of the event 124 */ 125 @Override 126 public String toString() { 127 return getStandardId(); 128 } 129 130 /** 131 * Informations about the event important for its Id that is neither target 132 * nor type. 133 * 134 * @return {@link #idInfo} of the event 135 */ 136 public String getIdInfo() { 137 return idInfo; 138 } 139 140 /** 141 * <p> 142 * If {@link #targetShort} is set, a shortend version of the Id is returned 143 * of the form {@link #targetShort}.{@link #type}.{@link #idInfo} is 144 * returned. Otherwise the standard Id is returned (see 145 * {@link #getStandardId()}). 146 * </p> 147 * 148 * @return if available, shortend Id string; {@link #getStandardId()} 149 * otherwise 150 */ 151 public String getShortId() { 152 String shortId = null; 153 if (targetShort != null) { 154 shortId = targetShort + "." + getType(); 155 if (!"".equals(idInfo)) { 156 shortId += "." + idInfo; 157 } 158 } else { 159 shortId = getStandardId(); 160 } 161 return shortId; 162 } 163 164 /** 165 * <p> 166 * Returns the Id string of the event. It has the form {@link #target}. 167 * {@link #type}.{@link #idInfo}; 168 * <p> 169 * 170 * @return Id string of the event 171 */ 172 public String getStandardId() { 173 String id = ""; 174 if (target != null) { 175 id += target + "."; 176 } 177 id += getType(); 178 if (!"".equals(idInfo)) { 179 id += "." + idInfo; 180 } 181 return id; 182 } 183 184 /** 185 * <p> 186 * Returns the {@link #target} of the event. 187 * </p> 188 * 189 * @return {@link #target} of the event 190 */ 191 public String getTarget() { 192 return target; 193 } 194 195 /** 196 * <p> 197 * Returns the {@link #targetShort} of the event. 198 * </p> 199 * 200 * @return {@link #targetShort} of the event 201 */ 202 protected String getTargetShort() { 203 return targetShort; 204 } 205 206 /** 207 * <p> 208 * Returns the {@link #type} of the event. 209 * </p> 210 * 211 * @return {@link #type} of the event 212 */ 213 public String getType() { 214 return type; 215 } 216 217 /* 218 * (non-Javadoc) 219 * 220 * @see java.lang.Object#hashCode() 221 */ 222 @Override 223 public int hashCode() { 224 int multiplier = 17; 225 int hash = 42; 226 if (type != null) { 227 hash = multiplier * hash + type.hashCode(); 228 } 229 hash = multiplier * hash + targetHashCode(); 230 231 return hash; 232 } 233 234 /** 235 * <p> 236 * Sets the {@link #idInfo} of the event. The idInfo is optional and 237 * contains information important for the event's Id that is neither target 238 * nor type. 239 * </p> 240 * 241 * @param info 242 * {@link #idInfo} of the event 243 */ 244 public void setIdInfo(String info) { 245 idInfo = info; 246 } 247 248 /** 249 * <p> 250 * Sets the target of the event. Once set, the target cannot be changed. 251 * </p> 252 * 253 * @param target 254 * target of the event 255 * @return true, if target was changed, false otherwise 256 */ 257 public boolean setTarget(String target) { 258 if (this.target != null) { 259 return false; 260 } 261 this.target = target; 262 return true; 263 } 264 265 /** 266 * <p> 267 * Sets the short description of the event target. Once set, the target 268 * cannot be changed. 269 * </p> 270 * 271 * @param targetShort 272 * short target description 273 * @return true, if target was changed, false otherwise 274 */ 275 public boolean setTargetShort(String targetShort) { 276 if (this.targetShort != null) { 277 return false; 278 } 279 this.targetShort = targetShort; 280 return true; 281 } 282 283 /** 284 * <p> 285 * This function is used by {@link #equals(Object)} to determine if the 286 * targets of both events are equal. The standard implementation provided by 287 * this class performs a String comparison between the target strings. 288 * </p> 289 * <p> 290 * Subclasses can override this method to implemented more sophisticated 291 * means for the target comparison, e.g., to account for changes in the 292 * title of a widget. 293 * </p> 294 * 295 * @param otherTarget 296 * other target string to which the target if this event is 297 * compared to 298 * @return true if the targets are equals; false otherwise 299 */ 300 protected boolean targetEquals(String otherTarget) { 301 boolean retVal; 302 if (target != null) { 303 retVal = target.equals(otherTarget); 304 } else { 305 retVal = (otherTarget == null); 306 } 307 return retVal; 308 } 309 310 /** 311 * <p> 312 * This function is used by {@link #hashCode()} to determine how the hash of 313 * the {@link #target}. It has to be overridden by subclasses that implement 314 * {@link #targetEquals(String)}, to ensure that the equals/hashCode 315 * contract remains valid. 316 * </p> 317 * 318 * @return hash of the target 319 */ 320 protected int targetHashCode() { 321 if (target != null) { 322 return target.hashCode(); 323 } else { 324 return 0; 325 } 326 } 20 public class Event implements Serializable { 21 22 /** 23 * Id for object serialization. 24 */ 25 private static final long serialVersionUID = 1L; 26 27 /** 28 * <p> 29 * Global start event that can be used to indicate the start of a sequence. 30 * </p> 31 */ 32 public static final Event STARTEVENT = new Event(new DummyEventType()); 33 34 /** 35 * <p> 36 * Global end event that can be used to indicate the end of a sequence. 37 */ 38 public static final Event ENDEVENT = new Event(new DummyEventType()); 39 40 /** 41 * <p> 42 * Type of the event. 43 * </p> 44 */ 45 protected IEventType type; 46 47 /** 48 * </p> Target of the event. 49 */ 50 protected IEventTarget target = null; 51 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>(); 60 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 } 75 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 } 90 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 } 119 120 /** 121 * <p> 122 * Returns {@link #getStandardId()} as String representation of the event. 123 * </p> 124 * 125 * @return String representation of the event 126 */ 127 @Override 128 public String toString() { 129 return "(" + type.toString() + ";" + target.toString() + ")"; 130 } 131 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 } 142 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 } 153 154 /* 155 * (non-Javadoc) 156 * 157 * @see java.lang.Object#hashCode() 158 */ 159 @Override 160 public int hashCode() { 161 int multiplier = 17; 162 int hash = 42; 163 if (type != null) { 164 hash = multiplier * hash + type.hashCode(); 165 } 166 if (target != null) { 167 hash = multiplier * hash + target.hashCode(); 168 } 169 return hash; 170 } 171 172 /** 173 * <p> 174 * Sets the target of the event. Once set, the target cannot be changed. 175 * </p> 176 * 177 * @param target 178 * target of the event 179 * @return true, if target was changed, false otherwise 180 */ 181 public boolean setTarget(IEventTarget target) { 182 if (this.target != null) { 183 return false; 184 } 185 this.target = target; 186 return true; 187 } 188 189 /** 190 * <p> 191 * Adds a new {@link IReplayable} of type T to the replay sequence. 192 * </p> 193 * 194 * @param replayable 195 * element that is added to the sequence 196 * @throws InvalidParameterException 197 * thrown is replayable is null 198 */ 199 public void addReplayable(IReplayable replayable) { 200 if (replayable == null) { 201 throw new InvalidParameterException("replayble must not be null"); 202 } 203 replay.add(replayable); 204 } 205 206 /** 207 * <p> 208 * Adds a {@link List}ist of {@link IReplayable} to the replay sequence. 209 * </p> 210 * 211 * @param generatedReplaySeq 212 * {@link List} that is added to the sequence 213 * @throws InvalidParameterException 214 * thrown if generatedReplaySeq is null 215 */ 216 public void addReplayableSequence(List<IReplayable> generatedReplaySeq) { 217 if (generatedReplaySeq == null) { 218 throw new InvalidParameterException("generatedReplaySeq must not be null"); 219 } 220 replay.addAll(generatedReplaySeq); 221 } 222 223 /** 224 * <p> 225 * Returns a the list of replay events. 226 * </p> 227 * <p> 228 * The return value is a copy of the list used internally! 229 * </p> 230 * 231 * @return list of replay events. 232 */ 233 public List<IReplayable> getReplayables() { 234 return new LinkedList<IReplayable>(replay); 235 } 327 236 } -
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/IReplayable.java
r433 r541 24 24 */ 25 25 String getReplay(); 26 27 /**28 * <p>29 * Returns the target of the replayable.30 * </p>31 *32 * @return target of the replayable33 */34 String getTarget();35 26 }
Note: See TracChangeset
for help on using the changeset viewer.