1 | package de.ugoe.cs.eventbench.data;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 |
|
---|
5 |
|
---|
6 |
|
---|
7 | public class Event<T> {
|
---|
8 |
|
---|
9 | public static final Event<Object> STARTEVENT = new Event<Object>("START");
|
---|
10 | public static final Event<Object> ENDEVENT = new Event<Object>("END");
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * <p>
|
---|
14 | * Type of the event.
|
---|
15 | * </p>
|
---|
16 | */
|
---|
17 | private String type;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * </p>
|
---|
21 | * Target of the event.
|
---|
22 | */
|
---|
23 | private String target = null;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * <p>
|
---|
27 | * Short description of the event target.
|
---|
28 | * </p>
|
---|
29 | */
|
---|
30 | private String targetShort = null;
|
---|
31 |
|
---|
32 |
|
---|
33 | private String idInfo = "";
|
---|
34 |
|
---|
35 | public Event(String type) {
|
---|
36 | if( type==null ) {
|
---|
37 | throw new InvalidParameterException("Event type must not be null");
|
---|
38 | }
|
---|
39 | this.type = type;
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | public boolean equals(Object other) {
|
---|
44 | if( this==other ) {
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 | if (other instanceof Event<?>) {
|
---|
48 | Event<?> otherEvent = (Event<?>) other;
|
---|
49 | if( target!=null ) {
|
---|
50 | return type.equals(otherEvent.type)
|
---|
51 | && target.equals(otherEvent.target);
|
---|
52 | } else {
|
---|
53 | return type.equals(otherEvent.type)
|
---|
54 | && otherEvent.target==null;
|
---|
55 | }
|
---|
56 | } else {
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | @Override
|
---|
62 | public String toString() {
|
---|
63 | return getStandardId();
|
---|
64 | }
|
---|
65 |
|
---|
66 | public String getIdInfo() {
|
---|
67 | return idInfo;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public String getShortId() {
|
---|
71 | String shortId = null;
|
---|
72 | if (targetShort!=null) {
|
---|
73 | shortId = targetShort+"."+getType();
|
---|
74 | if ( !"".equals(idInfo)) {
|
---|
75 | shortId += "."+idInfo;
|
---|
76 | }
|
---|
77 | } else {
|
---|
78 | shortId = getStandardId();
|
---|
79 | }
|
---|
80 | return shortId;
|
---|
81 | }
|
---|
82 |
|
---|
83 | public String getStandardId() {
|
---|
84 | String id = "";
|
---|
85 | if( target!=null ) {
|
---|
86 | id += target + ".";
|
---|
87 | }
|
---|
88 | id += getType();
|
---|
89 | if ( !"".equals(idInfo) ) {
|
---|
90 | id += "." + idInfo;
|
---|
91 | }
|
---|
92 | return id;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public String getTarget() {
|
---|
96 | return target;
|
---|
97 | }
|
---|
98 |
|
---|
99 | public String getTargetShort() {
|
---|
100 | return targetShort;
|
---|
101 | }
|
---|
102 |
|
---|
103 | public String getType() {
|
---|
104 | return type;
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public int hashCode() {
|
---|
109 | int multiplier = 17;
|
---|
110 | int hash = 42;
|
---|
111 | hash = multiplier * hash + type.hashCode();
|
---|
112 | if( target!=null ) {
|
---|
113 | hash = multiplier * hash + target.hashCode();
|
---|
114 | }
|
---|
115 |
|
---|
116 | return hash;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void setIdInfo(String info) {
|
---|
120 | idInfo = info;
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * <p>
|
---|
125 | * Sets the target of the event. Once set, the target cannot be changed.
|
---|
126 | * </p>
|
---|
127 | * @param target target of the event
|
---|
128 | * @return true, if target was changed, false otherwise
|
---|
129 | */
|
---|
130 | public boolean setTarget(String target) {
|
---|
131 | if( this.target!=null ) {
|
---|
132 | return false;
|
---|
133 | }
|
---|
134 | this.target = target;
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * <p>
|
---|
140 | * Sets the short description of the event target. Once set, the target cannot be changed.
|
---|
141 | * </p>
|
---|
142 | * @param targetShort short target description
|
---|
143 | * @return true, if target was changed, false otherwise
|
---|
144 | */
|
---|
145 | public boolean setTargetShort(String targetShort) {
|
---|
146 | if( this.targetShort!=null ) {
|
---|
147 | return false;
|
---|
148 | }
|
---|
149 | this.targetShort = targetShort;
|
---|
150 | return true;
|
---|
151 | }
|
---|
152 | }
|
---|