[1] | 1 | package de.ugoe.cs.eventbench.data;
|
---|
| 2 |
|
---|
[12] | 3 | import java.security.InvalidParameterException;
|
---|
[1] | 4 |
|
---|
[79] | 5 | /**
|
---|
| 6 | * <p>
|
---|
| 7 | * Base class for all events. An event is described by its {@link #type} and its
|
---|
| 8 | * {@link #target}.
|
---|
| 9 | * </p>
|
---|
| 10 | *
|
---|
| 11 | * @author Steffen Herbold
|
---|
| 12 | * @version 1.0
|
---|
| 13 | *
|
---|
| 14 | * @param <T>
|
---|
| 15 | * Can be used to declare that events belong to a specific platform
|
---|
| 16 | * without subclassing.
|
---|
| 17 | */
|
---|
| 18 | public class Event<T> {
|
---|
[1] | 19 |
|
---|
[79] | 20 | /**
|
---|
| 21 | * <p>
|
---|
| 22 | * Global start event that can be used to indicate the start of a sequence.
|
---|
| 23 | * </p>
|
---|
| 24 | */
|
---|
| 25 | public static final Event<Object> STARTEVENT = new Event<Object>("START");
|
---|
[12] | 26 |
|
---|
[79] | 27 | /**
|
---|
| 28 | * <p>
|
---|
| 29 | * Global end event that can be used to indicate the end of a sequence.
|
---|
| 30 | */
|
---|
[7] | 31 | public static final Event<Object> ENDEVENT = new Event<Object>("END");
|
---|
[79] | 32 |
|
---|
[1] | 33 | /**
|
---|
| 34 | * <p>
|
---|
| 35 | * Type of the event.
|
---|
| 36 | * </p>
|
---|
| 37 | */
|
---|
| 38 | private String type;
|
---|
[79] | 39 |
|
---|
[1] | 40 | /**
|
---|
[79] | 41 | * </p> Target of the event.
|
---|
[1] | 42 | */
|
---|
| 43 | private String target = null;
|
---|
[79] | 44 |
|
---|
[1] | 45 | /**
|
---|
| 46 | * <p>
|
---|
| 47 | * Short description of the event target.
|
---|
| 48 | * </p>
|
---|
| 49 | */
|
---|
| 50 | private String targetShort = null;
|
---|
| 51 |
|
---|
[79] | 52 | /**
|
---|
| 53 | * Further information about the event that shall be included in its Id.
|
---|
| 54 | */
|
---|
| 55 | private String idInfo = "";
|
---|
[1] | 56 |
|
---|
[79] | 57 | /**
|
---|
| 58 | * <p>
|
---|
| 59 | * Constructor. Creates a new Event with a given type.
|
---|
| 60 | * </p>
|
---|
| 61 | *
|
---|
| 62 | * @param type
|
---|
| 63 | * type of the event
|
---|
| 64 | */
|
---|
[1] | 65 | public Event(String type) {
|
---|
[79] | 66 | if (type == null) {
|
---|
[12] | 67 | throw new InvalidParameterException("Event type must not be null");
|
---|
| 68 | }
|
---|
[1] | 69 | this.type = type;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[79] | 72 | /**
|
---|
| 73 | * <p>
|
---|
| 74 | * Two events are equal, if their {@link #type} and {@link #target} are
|
---|
| 75 | * equal.
|
---|
| 76 | * </p>
|
---|
| 77 | * <p>
|
---|
| 78 | * See {@link Object#equals(Object)} for further information.
|
---|
| 79 | * </p>
|
---|
| 80 | *
|
---|
| 81 | * @param other
|
---|
| 82 | * Event that is compared to this
|
---|
| 83 | * @return true, if events are equal, false otherwise
|
---|
| 84 | */
|
---|
[1] | 85 | @Override
|
---|
| 86 | public boolean equals(Object other) {
|
---|
[79] | 87 | if (this == other) {
|
---|
[1] | 88 | return true;
|
---|
| 89 | }
|
---|
| 90 | if (other instanceof Event<?>) {
|
---|
[12] | 91 | Event<?> otherEvent = (Event<?>) other;
|
---|
[79] | 92 | if (target != null) {
|
---|
[12] | 93 | return type.equals(otherEvent.type)
|
---|
[79] | 94 | && target.equals(otherEvent.target);
|
---|
[12] | 95 | } else {
|
---|
| 96 | return type.equals(otherEvent.type)
|
---|
[79] | 97 | && otherEvent.target == null;
|
---|
[12] | 98 | }
|
---|
[1] | 99 | } else {
|
---|
| 100 | return false;
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[79] | 103 |
|
---|
| 104 | /**
|
---|
| 105 | * <p>
|
---|
| 106 | * Returns {@link #getStandardId()} as String representation of the event.
|
---|
| 107 | * </p>
|
---|
| 108 | *
|
---|
| 109 | * @return String represenation of the event
|
---|
| 110 | */
|
---|
[2] | 111 | @Override
|
---|
| 112 | public String toString() {
|
---|
| 113 | return getStandardId();
|
---|
| 114 | }
|
---|
[1] | 115 |
|
---|
[79] | 116 | /**
|
---|
| 117 | * Informations about the event important for its Id that is neither target
|
---|
| 118 | * nor type.
|
---|
| 119 | *
|
---|
| 120 | * @return {@link #idInfo} of the event
|
---|
| 121 | */
|
---|
[1] | 122 | public String getIdInfo() {
|
---|
| 123 | return idInfo;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[79] | 126 | /**
|
---|
| 127 | * <p>
|
---|
| 128 | * If {@link #targetShort} is set, a shortend version of the Id is returned
|
---|
| 129 | * of the form {@link #targetShort}.{@link #type}.{@link #idInfo} is
|
---|
| 130 | * returned. Otherwise the standard Id is returned (see
|
---|
| 131 | * {@link #getStandardId()}).
|
---|
| 132 | * </p>
|
---|
| 133 | *
|
---|
| 134 | * @return if available, shortend Id string; {@link #getStandardId()}
|
---|
| 135 | * otherwise
|
---|
| 136 | */
|
---|
[1] | 137 | public String getShortId() {
|
---|
| 138 | String shortId = null;
|
---|
[79] | 139 | if (targetShort != null) {
|
---|
| 140 | shortId = targetShort + "." + getType();
|
---|
| 141 | if (!"".equals(idInfo)) {
|
---|
| 142 | shortId += "." + idInfo;
|
---|
[1] | 143 | }
|
---|
[16] | 144 | } else {
|
---|
| 145 | shortId = getStandardId();
|
---|
[1] | 146 | }
|
---|
| 147 | return shortId;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[79] | 150 | /**
|
---|
| 151 | * <p>
|
---|
| 152 | * Returns the Id string of the event. It has the form {@link #target}.
|
---|
| 153 | * {@link #type}.{@link #idInfo};
|
---|
| 154 | * <p>
|
---|
| 155 | *
|
---|
| 156 | * @return Id string of the event
|
---|
| 157 | */
|
---|
[1] | 158 | public String getStandardId() {
|
---|
[12] | 159 | String id = "";
|
---|
[79] | 160 | if (target != null) {
|
---|
[12] | 161 | id += target + ".";
|
---|
| 162 | }
|
---|
| 163 | id += getType();
|
---|
[79] | 164 | if (!"".equals(idInfo)) {
|
---|
[1] | 165 | id += "." + idInfo;
|
---|
| 166 | }
|
---|
| 167 | return id;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[79] | 170 | /**
|
---|
| 171 | * <p>
|
---|
| 172 | * Returns the {@link #target} of the event.
|
---|
| 173 | * </p>
|
---|
| 174 | *
|
---|
| 175 | * @return {@link #target} of the event
|
---|
| 176 | */
|
---|
[1] | 177 | public String getTarget() {
|
---|
| 178 | return target;
|
---|
| 179 | }
|
---|
[79] | 180 |
|
---|
| 181 | /**
|
---|
| 182 | * <p>
|
---|
| 183 | * Returns the {@link #targetShort} of the event.
|
---|
| 184 | * </p>
|
---|
| 185 | *
|
---|
| 186 | * @return {@link #targetShort} of the event
|
---|
| 187 | */
|
---|
| 188 | protected String getTargetShort() {
|
---|
[1] | 189 | return targetShort;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[79] | 192 | /**
|
---|
| 193 | * <p>
|
---|
| 194 | * Returns the {@link #type} of the event.
|
---|
| 195 | * </p>
|
---|
| 196 | *
|
---|
| 197 | * @return {@link #type} of the event
|
---|
| 198 | */
|
---|
[1] | 199 | public String getType() {
|
---|
| 200 | return type;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[79] | 203 | /*
|
---|
| 204 | * (non-Javadoc)
|
---|
| 205 | *
|
---|
| 206 | * @see java.lang.Object#hashCode()
|
---|
| 207 | */
|
---|
[1] | 208 | @Override
|
---|
| 209 | public int hashCode() {
|
---|
| 210 | int multiplier = 17;
|
---|
| 211 | int hash = 42;
|
---|
| 212 | hash = multiplier * hash + type.hashCode();
|
---|
[79] | 213 | if (target != null) {
|
---|
[1] | 214 | hash = multiplier * hash + target.hashCode();
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | return hash;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[79] | 220 | /**
|
---|
| 221 | * <p>
|
---|
| 222 | * Sets the {@link #idInfo} of the event. The idInfo is optional and
|
---|
| 223 | * contains information important for the event's Id that is neither target
|
---|
| 224 | * nor type.
|
---|
| 225 | * </p>
|
---|
| 226 | *
|
---|
| 227 | * @param info
|
---|
| 228 | * {@link #idInfo} of the event
|
---|
| 229 | */
|
---|
[1] | 230 | public void setIdInfo(String info) {
|
---|
| 231 | idInfo = info;
|
---|
| 232 | }
|
---|
[79] | 233 |
|
---|
[1] | 234 | /**
|
---|
| 235 | * <p>
|
---|
| 236 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
[79] | 237 | * </p>
|
---|
| 238 | *
|
---|
| 239 | * @param target
|
---|
| 240 | * target of the event
|
---|
[1] | 241 | * @return true, if target was changed, false otherwise
|
---|
| 242 | */
|
---|
| 243 | public boolean setTarget(String target) {
|
---|
[79] | 244 | if (this.target != null) {
|
---|
[1] | 245 | return false;
|
---|
| 246 | }
|
---|
| 247 | this.target = target;
|
---|
| 248 | return true;
|
---|
| 249 | }
|
---|
[79] | 250 |
|
---|
[1] | 251 | /**
|
---|
| 252 | * <p>
|
---|
[79] | 253 | * Sets the short description of the event target. Once set, the target
|
---|
| 254 | * cannot be changed.
|
---|
| 255 | * </p>
|
---|
| 256 | *
|
---|
| 257 | * @param targetShort
|
---|
| 258 | * short target description
|
---|
[1] | 259 | * @return true, if target was changed, false otherwise
|
---|
| 260 | */
|
---|
| 261 | public boolean setTargetShort(String targetShort) {
|
---|
[79] | 262 | if (this.targetShort != null) {
|
---|
[1] | 263 | return false;
|
---|
| 264 | }
|
---|
| 265 | this.targetShort = targetShort;
|
---|
| 266 | return true;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|