[305] | 1 | package de.ugoe.cs.eventbench.jfc.data;
|
---|
[299] | 2 |
|
---|
[300] | 3 | import java.util.HashMap;
|
---|
| 4 | import java.util.Map;
|
---|
| 5 |
|
---|
[299] | 6 | import de.ugoe.cs.eventbench.data.IReplayable;
|
---|
| 7 | import de.ugoe.cs.eventbench.data.ReplayableEvent;
|
---|
[359] | 8 | import de.ugoe.cs.eventbench.jfc.JFCLogParser;
|
---|
[299] | 9 |
|
---|
| 10 | /**
|
---|
| 11 | * <p>
|
---|
| 12 | * This class defines JFC events.
|
---|
| 13 | * </p>
|
---|
| 14 | *
|
---|
| 15 | * @author Steffen Herbold
|
---|
| 16 | * @version 1.0
|
---|
| 17 | */
|
---|
| 18 | public class JFCEvent extends ReplayableEvent<IReplayable> {
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * <p>
|
---|
| 22 | * Id for object serialization.
|
---|
| 23 | * </p>
|
---|
| 24 | */
|
---|
| 25 | private static final long serialVersionUID = 1L;
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * <p>
|
---|
[300] | 29 | * Internal map of parameters associated with the event.
|
---|
| 30 | * </p>
|
---|
| 31 | */
|
---|
| 32 | private Map<String, String> parameters;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * <p>
|
---|
| 36 | * Information about the event source.
|
---|
| 37 | * </p>
|
---|
| 38 | */
|
---|
| 39 | private Map<String, String> sourceParameters;
|
---|
| 40 |
|
---|
| 41 | /**
|
---|
| 42 | * <p>
|
---|
| 43 | * Information about the parent of the event source.
|
---|
| 44 | * </p>
|
---|
| 45 | */
|
---|
| 46 | private Map<String, String> parentParameters;
|
---|
[390] | 47 |
|
---|
| 48 | private boolean targetChanged = false;
|
---|
| 49 |
|
---|
| 50 | private int targetHash = -1;
|
---|
[300] | 51 |
|
---|
| 52 | /**
|
---|
| 53 | * <p>
|
---|
[299] | 54 | * Constructor. Creates a new JFCEvent.
|
---|
| 55 | * </p>
|
---|
| 56 | *
|
---|
| 57 | * @param type
|
---|
| 58 | * type of the event
|
---|
| 59 | */
|
---|
| 60 | public JFCEvent(String type) {
|
---|
| 61 | super(type);
|
---|
[300] | 62 | parameters = new HashMap<String, String>();
|
---|
[304] | 63 | sourceParameters = new HashMap<String, String>();
|
---|
| 64 | parentParameters = new HashMap<String, String>();
|
---|
[299] | 65 | }
|
---|
| 66 |
|
---|
[300] | 67 | /**
|
---|
| 68 | * <p>
|
---|
| 69 | * Adds a new parameter to the event.
|
---|
| 70 | * </p>
|
---|
| 71 | *
|
---|
| 72 | * @param name
|
---|
| 73 | * name of the parameter
|
---|
| 74 | * @param value
|
---|
| 75 | * value of the parameter
|
---|
| 76 | */
|
---|
| 77 | public void addParameter(String name, String value) {
|
---|
| 78 | parameters.put(name, value);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | /**
|
---|
| 82 | * <p>
|
---|
| 83 | * Retrieves the value of a parameter.
|
---|
| 84 | * </p>
|
---|
| 85 | *
|
---|
| 86 | * @param name
|
---|
| 87 | * name of the parameter
|
---|
| 88 | * @return value of the parameter
|
---|
| 89 | */
|
---|
| 90 | public String getParameter(String name) {
|
---|
| 91 | return parameters.get(name);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | /**
|
---|
| 95 | * <p>
|
---|
| 96 | * Adds new information about the source of the event.
|
---|
| 97 | * </p>
|
---|
| 98 | *
|
---|
| 99 | * @param name
|
---|
| 100 | * name of the information
|
---|
| 101 | * @param value
|
---|
| 102 | * value of the information
|
---|
| 103 | */
|
---|
| 104 | public void addSourceInformation(String name, String value) {
|
---|
| 105 | sourceParameters.put(name, value);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * <p>
|
---|
| 110 | * Retrieves information about the source of the event.
|
---|
| 111 | * </p>
|
---|
| 112 | *
|
---|
| 113 | * @param name
|
---|
| 114 | * name of the information
|
---|
| 115 | * @return value of the information
|
---|
| 116 | */
|
---|
| 117 | public String getSourceInformation(String name) {
|
---|
| 118 | return sourceParameters.get(name);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | /**
|
---|
| 122 | * <p>
|
---|
| 123 | * Adds new information about the parent of the source of the event.
|
---|
| 124 | * </p>
|
---|
| 125 | *
|
---|
| 126 | * @param name
|
---|
| 127 | * name of the information
|
---|
| 128 | * @param value
|
---|
| 129 | * value of the information
|
---|
| 130 | */
|
---|
| 131 | public void addParentInformation(String name, String value) {
|
---|
| 132 | parentParameters.put(name, value);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /**
|
---|
| 136 | * <p>
|
---|
[359] | 137 | * Used by the {@link JFCLogParser} to extend the target string of the
|
---|
| 138 | * current event with a further ancestor. The resulting target string will
|
---|
| 139 | * have the structure {@code etc.grandparent.parent.eventtarget}.
|
---|
| 140 | * </p>
|
---|
| 141 | *
|
---|
| 142 | * @param extension
|
---|
| 143 | * extension for the target.
|
---|
| 144 | */
|
---|
| 145 | public void extendTarget(String extension) {
|
---|
| 146 | if (target == null || "".equals(target)) {
|
---|
| 147 | target = extension;
|
---|
| 148 | } else {
|
---|
| 149 | target += "." + extension;
|
---|
| 150 | }
|
---|
[390] | 151 | targetChanged = true;
|
---|
[359] | 152 | }
|
---|
| 153 |
|
---|
| 154 | /**
|
---|
| 155 | * <p>
|
---|
[300] | 156 | * Retrieves information about the parent of the source of the event.
|
---|
| 157 | * </p>
|
---|
| 158 | *
|
---|
| 159 | * @param name
|
---|
| 160 | * name of the information
|
---|
| 161 | * @return value of the information
|
---|
| 162 | */
|
---|
| 163 | public String getParentInformation(String name) {
|
---|
| 164 | return parentParameters.get(name);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[376] | 167 | /**
|
---|
| 168 | * <p>
|
---|
| 169 | * This method implements the comparison between two targets of JFCEvents.
|
---|
| 170 | * The targets are equal, if they have the same placement in the widget
|
---|
| 171 | * hierarchy, i.e., the target strings describe the same widgets, according
|
---|
| 172 | * to the implementation of widget equality provided by
|
---|
| 173 | * {@link #compareWidgets(String, String)}.
|
---|
| 174 | * </p>
|
---|
[300] | 175 | *
|
---|
[376] | 176 | * @see de.ugoe.cs.eventbench.data.Event#targetEquals(java.lang.String)
|
---|
[300] | 177 | */
|
---|
| 178 | @Override
|
---|
[376] | 179 | protected boolean targetEquals(String otherTarget) {
|
---|
[380] | 180 | return JFCTargetComparator.compare(target, otherTarget);
|
---|
[300] | 181 | }
|
---|
| 182 |
|
---|
[376] | 183 | /**
|
---|
| 184 | * <p>
|
---|
[380] | 185 | * The targetHashCode ignores the parts of the target that describe the
|
---|
| 186 | * title and hash of a widget, to ensure that the equals/hashCode contract
|
---|
| 187 | * is fulfilled.
|
---|
[376] | 188 | * </p>
|
---|
[300] | 189 | *
|
---|
[380] | 190 | * @see de.ugoe.cs.eventbench.data.Event#targetHashCode()
|
---|
[300] | 191 | */
|
---|
| 192 | @Override
|
---|
[376] | 193 | protected int targetHashCode() {
|
---|
[390] | 194 | if( targetChanged || targetHash==-1 ) {
|
---|
| 195 | targetHash = 0;
|
---|
| 196 | int multiplier = 29;
|
---|
| 197 | if (target != null) {
|
---|
| 198 | String[] targetParts = target.split("\\]\\.\\[");
|
---|
| 199 | if (targetParts.length == 0) {
|
---|
| 200 | targetHash = widgetHashCode(target);
|
---|
| 201 | } else {
|
---|
| 202 | for (String widgetString : targetParts) {
|
---|
| 203 | targetHash = targetHash * multiplier
|
---|
| 204 | + widgetHashCode(widgetString);
|
---|
| 205 | }
|
---|
[376] | 206 | }
|
---|
| 207 | }
|
---|
[390] | 208 | targetChanged = false;
|
---|
[376] | 209 | }
|
---|
[390] | 210 | return targetHash;
|
---|
[300] | 211 | }
|
---|
[380] | 212 |
|
---|
| 213 | /**
|
---|
| 214 | * <p>
|
---|
| 215 | * This method calculates the hashCode for a a widget. If is used by
|
---|
| 216 | * {@link #targetHashCode()} to build the complete hashCode.
|
---|
| 217 | * </p>
|
---|
| 218 | *
|
---|
| 219 | * @param widget
|
---|
| 220 | * string describing the widget
|
---|
| 221 | * @return hashCode of the widget
|
---|
| 222 | */
|
---|
[376] | 223 | private int widgetHashCode(String widget) {
|
---|
| 224 | int hashCode = 0;
|
---|
| 225 | int multiplier = 37;
|
---|
| 226 | String[] widgetInfo = widget.split("','");
|
---|
[380] | 227 | if (widgetInfo.length == 5) {
|
---|
[376] | 228 | hashCode = hashCode * multiplier + widgetInfo[1].hashCode();
|
---|
| 229 | hashCode = hashCode * multiplier + widgetInfo[2].hashCode();
|
---|
[390] | 230 | //hashCode = hashCode * multiplier + widgetInfo[3].hashCode();
|
---|
[376] | 231 | }
|
---|
| 232 | return hashCode;
|
---|
| 233 | }
|
---|
[300] | 234 |
|
---|
[299] | 235 | }
|
---|