[1] | 1 | package de.ugoe.cs.eventbench.data;
|
---|
| 2 |
|
---|
[86] | 3 | import java.io.Serializable;
|
---|
[12] | 4 | import java.security.InvalidParameterException;
|
---|
[1] | 5 |
|
---|
[79] | 6 | /**
|
---|
| 7 | * <p>
|
---|
| 8 | * Base class for all events. An event is described by its {@link #type} and its
|
---|
| 9 | * {@link #target}.
|
---|
| 10 | * </p>
|
---|
| 11 | *
|
---|
| 12 | * @author Steffen Herbold
|
---|
| 13 | * @version 1.0
|
---|
| 14 | *
|
---|
| 15 | * @param <T>
|
---|
| 16 | * Can be used to declare that events belong to a specific platform
|
---|
| 17 | * without subclassing.
|
---|
| 18 | */
|
---|
[86] | 19 | public class Event<T> implements Serializable {
|
---|
[1] | 20 |
|
---|
[79] | 21 | /**
|
---|
[86] | 22 | * Id for object serialization.
|
---|
| 23 | */
|
---|
| 24 | private static final long serialVersionUID = 1L;
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
[79] | 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");
|
---|
[12] | 32 |
|
---|
[79] | 33 | /**
|
---|
| 34 | * <p>
|
---|
| 35 | * Global end event that can be used to indicate the end of a sequence.
|
---|
| 36 | */
|
---|
[7] | 37 | public static final Event<Object> ENDEVENT = new Event<Object>("END");
|
---|
[79] | 38 |
|
---|
[1] | 39 | /**
|
---|
| 40 | * <p>
|
---|
| 41 | * Type of the event.
|
---|
| 42 | * </p>
|
---|
| 43 | */
|
---|
[335] | 44 | protected String type;
|
---|
[79] | 45 |
|
---|
[1] | 46 | /**
|
---|
[79] | 47 | * </p> Target of the event.
|
---|
[1] | 48 | */
|
---|
[335] | 49 | protected String target = null;
|
---|
[79] | 50 |
|
---|
[1] | 51 | /**
|
---|
| 52 | * <p>
|
---|
| 53 | * Short description of the event target.
|
---|
| 54 | * </p>
|
---|
| 55 | */
|
---|
[335] | 56 | protected String targetShort = null;
|
---|
[1] | 57 |
|
---|
[79] | 58 | /**
|
---|
| 59 | * Further information about the event that shall be included in its Id.
|
---|
| 60 | */
|
---|
[335] | 61 | protected String idInfo = "";
|
---|
[1] | 62 |
|
---|
[79] | 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 | */
|
---|
[1] | 71 | public Event(String type) {
|
---|
[79] | 72 | if (type == null) {
|
---|
[12] | 73 | throw new InvalidParameterException("Event type must not be null");
|
---|
| 74 | }
|
---|
[1] | 75 | this.type = type;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
[79] | 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 | */
|
---|
[1] | 91 | @Override
|
---|
| 92 | public boolean equals(Object other) {
|
---|
[79] | 93 | if (this == other) {
|
---|
[1] | 94 | return true;
|
---|
| 95 | }
|
---|
| 96 | if (other instanceof Event<?>) {
|
---|
[12] | 97 | Event<?> otherEvent = (Event<?>) other;
|
---|
[336] | 98 | if (otherEvent.canEqual(this)) {
|
---|
[373] | 99 | if (type != null) {
|
---|
[336] | 100 | return type.equals(otherEvent.type)
|
---|
[373] | 101 | && targetEquals(otherEvent.target);
|
---|
| 102 | } else {
|
---|
[336] | 103 | return otherEvent.type == null
|
---|
[373] | 104 | && targetEquals(otherEvent.target);
|
---|
[336] | 105 | }
|
---|
[12] | 106 | } else {
|
---|
[336] | 107 | return false;
|
---|
[12] | 108 | }
|
---|
[1] | 109 | } else {
|
---|
| 110 | return false;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
[79] | 113 |
|
---|
[336] | 114 | public boolean canEqual(Object other) {
|
---|
| 115 | return (other instanceof Event<?>);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[79] | 118 | /**
|
---|
| 119 | * <p>
|
---|
| 120 | * Returns {@link #getStandardId()} as String representation of the event.
|
---|
| 121 | * </p>
|
---|
| 122 | *
|
---|
| 123 | * @return String represenation of the event
|
---|
| 124 | */
|
---|
[2] | 125 | @Override
|
---|
| 126 | public String toString() {
|
---|
| 127 | return getStandardId();
|
---|
| 128 | }
|
---|
[1] | 129 |
|
---|
[79] | 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 | */
|
---|
[1] | 136 | public String getIdInfo() {
|
---|
| 137 | return idInfo;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[79] | 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 | */
|
---|
[1] | 151 | public String getShortId() {
|
---|
| 152 | String shortId = null;
|
---|
[79] | 153 | if (targetShort != null) {
|
---|
| 154 | shortId = targetShort + "." + getType();
|
---|
| 155 | if (!"".equals(idInfo)) {
|
---|
| 156 | shortId += "." + idInfo;
|
---|
[1] | 157 | }
|
---|
[16] | 158 | } else {
|
---|
| 159 | shortId = getStandardId();
|
---|
[1] | 160 | }
|
---|
| 161 | return shortId;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
[79] | 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 | */
|
---|
[1] | 172 | public String getStandardId() {
|
---|
[12] | 173 | String id = "";
|
---|
[79] | 174 | if (target != null) {
|
---|
[12] | 175 | id += target + ".";
|
---|
| 176 | }
|
---|
| 177 | id += getType();
|
---|
[79] | 178 | if (!"".equals(idInfo)) {
|
---|
[1] | 179 | id += "." + idInfo;
|
---|
| 180 | }
|
---|
| 181 | return id;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[79] | 184 | /**
|
---|
| 185 | * <p>
|
---|
| 186 | * Returns the {@link #target} of the event.
|
---|
| 187 | * </p>
|
---|
| 188 | *
|
---|
| 189 | * @return {@link #target} of the event
|
---|
| 190 | */
|
---|
[1] | 191 | public String getTarget() {
|
---|
| 192 | return target;
|
---|
| 193 | }
|
---|
[79] | 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() {
|
---|
[1] | 203 | return targetShort;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[79] | 206 | /**
|
---|
| 207 | * <p>
|
---|
| 208 | * Returns the {@link #type} of the event.
|
---|
| 209 | * </p>
|
---|
| 210 | *
|
---|
| 211 | * @return {@link #type} of the event
|
---|
| 212 | */
|
---|
[1] | 213 | public String getType() {
|
---|
| 214 | return type;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
[79] | 217 | /*
|
---|
| 218 | * (non-Javadoc)
|
---|
| 219 | *
|
---|
| 220 | * @see java.lang.Object#hashCode()
|
---|
| 221 | */
|
---|
[1] | 222 | @Override
|
---|
| 223 | public int hashCode() {
|
---|
| 224 | int multiplier = 17;
|
---|
| 225 | int hash = 42;
|
---|
[336] | 226 | if (type != null) {
|
---|
| 227 | hash = multiplier * hash + type.hashCode();
|
---|
| 228 | }
|
---|
[375] | 229 | hash = multiplier * hash + targetHashCode();
|
---|
[1] | 230 |
|
---|
| 231 | return hash;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[79] | 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 | */
|
---|
[1] | 244 | public void setIdInfo(String info) {
|
---|
| 245 | idInfo = info;
|
---|
| 246 | }
|
---|
[79] | 247 |
|
---|
[1] | 248 | /**
|
---|
| 249 | * <p>
|
---|
| 250 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
[79] | 251 | * </p>
|
---|
| 252 | *
|
---|
| 253 | * @param target
|
---|
| 254 | * target of the event
|
---|
[1] | 255 | * @return true, if target was changed, false otherwise
|
---|
| 256 | */
|
---|
| 257 | public boolean setTarget(String target) {
|
---|
[79] | 258 | if (this.target != null) {
|
---|
[1] | 259 | return false;
|
---|
| 260 | }
|
---|
| 261 | this.target = target;
|
---|
| 262 | return true;
|
---|
| 263 | }
|
---|
[79] | 264 |
|
---|
[1] | 265 | /**
|
---|
| 266 | * <p>
|
---|
[79] | 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
|
---|
[1] | 273 | * @return true, if target was changed, false otherwise
|
---|
| 274 | */
|
---|
| 275 | public boolean setTargetShort(String targetShort) {
|
---|
[79] | 276 | if (this.targetShort != null) {
|
---|
[1] | 277 | return false;
|
---|
| 278 | }
|
---|
| 279 | this.targetShort = targetShort;
|
---|
| 280 | return true;
|
---|
| 281 | }
|
---|
[373] | 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
|
---|
[375] | 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
|
---|
[373] | 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 | }
|
---|
[375] | 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 | }
|
---|
[1] | 327 | }
|
---|