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