[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)) {
|
---|
| 99 | if (type != null && target != null) {
|
---|
| 100 | return type.equals(otherEvent.type)
|
---|
| 101 | && target.equals(otherEvent.target);
|
---|
| 102 | } else if (type != null && target == null) {
|
---|
| 103 | return type.equals(otherEvent.type)
|
---|
| 104 | && otherEvent.target == null;
|
---|
| 105 | } else if (type == null && target != null) {
|
---|
| 106 | return otherEvent.type == null
|
---|
| 107 | && target.equals(otherEvent.target);
|
---|
| 108 | } else {
|
---|
| 109 | return otherEvent.type == null && otherEvent.target == null;
|
---|
| 110 | }
|
---|
[12] | 111 | } else {
|
---|
[336] | 112 | return false;
|
---|
[12] | 113 | }
|
---|
[1] | 114 | } else {
|
---|
| 115 | return false;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
[79] | 118 |
|
---|
[336] | 119 | public boolean canEqual(Object other) {
|
---|
| 120 | return (other instanceof Event<?>);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[79] | 123 | /**
|
---|
| 124 | * <p>
|
---|
| 125 | * Returns {@link #getStandardId()} as String representation of the event.
|
---|
| 126 | * </p>
|
---|
| 127 | *
|
---|
| 128 | * @return String represenation of the event
|
---|
| 129 | */
|
---|
[2] | 130 | @Override
|
---|
| 131 | public String toString() {
|
---|
| 132 | return getStandardId();
|
---|
| 133 | }
|
---|
[1] | 134 |
|
---|
[79] | 135 | /**
|
---|
| 136 | * Informations about the event important for its Id that is neither target
|
---|
| 137 | * nor type.
|
---|
| 138 | *
|
---|
| 139 | * @return {@link #idInfo} of the event
|
---|
| 140 | */
|
---|
[1] | 141 | public String getIdInfo() {
|
---|
| 142 | return idInfo;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
[79] | 145 | /**
|
---|
| 146 | * <p>
|
---|
| 147 | * If {@link #targetShort} is set, a shortend version of the Id is returned
|
---|
| 148 | * of the form {@link #targetShort}.{@link #type}.{@link #idInfo} is
|
---|
| 149 | * returned. Otherwise the standard Id is returned (see
|
---|
| 150 | * {@link #getStandardId()}).
|
---|
| 151 | * </p>
|
---|
| 152 | *
|
---|
| 153 | * @return if available, shortend Id string; {@link #getStandardId()}
|
---|
| 154 | * otherwise
|
---|
| 155 | */
|
---|
[1] | 156 | public String getShortId() {
|
---|
| 157 | String shortId = null;
|
---|
[79] | 158 | if (targetShort != null) {
|
---|
| 159 | shortId = targetShort + "." + getType();
|
---|
| 160 | if (!"".equals(idInfo)) {
|
---|
| 161 | shortId += "." + idInfo;
|
---|
[1] | 162 | }
|
---|
[16] | 163 | } else {
|
---|
| 164 | shortId = getStandardId();
|
---|
[1] | 165 | }
|
---|
| 166 | return shortId;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[79] | 169 | /**
|
---|
| 170 | * <p>
|
---|
| 171 | * Returns the Id string of the event. It has the form {@link #target}.
|
---|
| 172 | * {@link #type}.{@link #idInfo};
|
---|
| 173 | * <p>
|
---|
| 174 | *
|
---|
| 175 | * @return Id string of the event
|
---|
| 176 | */
|
---|
[1] | 177 | public String getStandardId() {
|
---|
[12] | 178 | String id = "";
|
---|
[79] | 179 | if (target != null) {
|
---|
[12] | 180 | id += target + ".";
|
---|
| 181 | }
|
---|
| 182 | id += getType();
|
---|
[79] | 183 | if (!"".equals(idInfo)) {
|
---|
[1] | 184 | id += "." + idInfo;
|
---|
| 185 | }
|
---|
| 186 | return id;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
[79] | 189 | /**
|
---|
| 190 | * <p>
|
---|
| 191 | * Returns the {@link #target} of the event.
|
---|
| 192 | * </p>
|
---|
| 193 | *
|
---|
| 194 | * @return {@link #target} of the event
|
---|
| 195 | */
|
---|
[1] | 196 | public String getTarget() {
|
---|
| 197 | return target;
|
---|
| 198 | }
|
---|
[79] | 199 |
|
---|
| 200 | /**
|
---|
| 201 | * <p>
|
---|
| 202 | * Returns the {@link #targetShort} of the event.
|
---|
| 203 | * </p>
|
---|
| 204 | *
|
---|
| 205 | * @return {@link #targetShort} of the event
|
---|
| 206 | */
|
---|
| 207 | protected String getTargetShort() {
|
---|
[1] | 208 | return targetShort;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
[79] | 211 | /**
|
---|
| 212 | * <p>
|
---|
| 213 | * Returns the {@link #type} of the event.
|
---|
| 214 | * </p>
|
---|
| 215 | *
|
---|
| 216 | * @return {@link #type} of the event
|
---|
| 217 | */
|
---|
[1] | 218 | public String getType() {
|
---|
| 219 | return type;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
[79] | 222 | /*
|
---|
| 223 | * (non-Javadoc)
|
---|
| 224 | *
|
---|
| 225 | * @see java.lang.Object#hashCode()
|
---|
| 226 | */
|
---|
[1] | 227 | @Override
|
---|
| 228 | public int hashCode() {
|
---|
| 229 | int multiplier = 17;
|
---|
| 230 | int hash = 42;
|
---|
[336] | 231 | if (type != null) {
|
---|
| 232 | hash = multiplier * hash + type.hashCode();
|
---|
| 233 | }
|
---|
[79] | 234 | if (target != null) {
|
---|
[1] | 235 | hash = multiplier * hash + target.hashCode();
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | return hash;
|
---|
| 239 | }
|
---|
| 240 |
|
---|
[79] | 241 | /**
|
---|
| 242 | * <p>
|
---|
| 243 | * Sets the {@link #idInfo} of the event. The idInfo is optional and
|
---|
| 244 | * contains information important for the event's Id that is neither target
|
---|
| 245 | * nor type.
|
---|
| 246 | * </p>
|
---|
| 247 | *
|
---|
| 248 | * @param info
|
---|
| 249 | * {@link #idInfo} of the event
|
---|
| 250 | */
|
---|
[1] | 251 | public void setIdInfo(String info) {
|
---|
| 252 | idInfo = info;
|
---|
| 253 | }
|
---|
[79] | 254 |
|
---|
[1] | 255 | /**
|
---|
| 256 | * <p>
|
---|
| 257 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
[79] | 258 | * </p>
|
---|
| 259 | *
|
---|
| 260 | * @param target
|
---|
| 261 | * target of the event
|
---|
[1] | 262 | * @return true, if target was changed, false otherwise
|
---|
| 263 | */
|
---|
| 264 | public boolean setTarget(String target) {
|
---|
[79] | 265 | if (this.target != null) {
|
---|
[1] | 266 | return false;
|
---|
| 267 | }
|
---|
| 268 | this.target = target;
|
---|
| 269 | return true;
|
---|
| 270 | }
|
---|
[79] | 271 |
|
---|
[1] | 272 | /**
|
---|
| 273 | * <p>
|
---|
[79] | 274 | * Sets the short description of the event target. Once set, the target
|
---|
| 275 | * cannot be changed.
|
---|
| 276 | * </p>
|
---|
| 277 | *
|
---|
| 278 | * @param targetShort
|
---|
| 279 | * short target description
|
---|
[1] | 280 | * @return true, if target was changed, false otherwise
|
---|
| 281 | */
|
---|
| 282 | public boolean setTargetShort(String targetShort) {
|
---|
[79] | 283 | if (this.targetShort != null) {
|
---|
[1] | 284 | return false;
|
---|
| 285 | }
|
---|
| 286 | this.targetShort = targetShort;
|
---|
| 287 | return true;
|
---|
| 288 | }
|
---|
| 289 | }
|
---|