source: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/jfc/data/JFCEvent.java @ 390

Last change on this file since 390 was 390, checked in by sherbold, 12 years ago
  • all hashValues are now only computed once, stored and only recomputed when something changes.
  • de.ugoe.cs.eventbench.jfc.data.JFCTargetComparator now allows precomputation of equalities
  • Property svn:mime-type set to text/plain
File size: 5.5 KB
Line 
1package de.ugoe.cs.eventbench.jfc.data;
2
3import java.util.HashMap;
4import java.util.Map;
5
6import de.ugoe.cs.eventbench.data.IReplayable;
7import de.ugoe.cs.eventbench.data.ReplayableEvent;
8import de.ugoe.cs.eventbench.jfc.JFCLogParser;
9
10/**
11 * <p>
12 * This class defines JFC events.
13 * </p>
14 *
15 * @author Steffen Herbold
16 * @version 1.0
17 */
18public 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>
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;
47       
48        private boolean targetChanged = false;
49       
50        private int targetHash = -1;
51
52        /**
53         * <p>
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);
62                parameters = new HashMap<String, String>();
63                sourceParameters = new HashMap<String, String>();
64                parentParameters = new HashMap<String, String>();
65        }
66
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>
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                }
151                targetChanged = true;
152        }
153
154        /**
155         * <p>
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
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>
175         *
176         * @see de.ugoe.cs.eventbench.data.Event#targetEquals(java.lang.String)
177         */
178        @Override
179        protected boolean targetEquals(String otherTarget) {
180                return JFCTargetComparator.compare(target, otherTarget);
181        }
182
183        /**
184         * <p>
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.
188         * </p>
189         *
190         * @see de.ugoe.cs.eventbench.data.Event#targetHashCode()
191         */
192        @Override
193        protected int targetHashCode() {
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                                        }
206                                }
207                        }
208                        targetChanged = false;
209                }
210                return targetHash;
211        }
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         */
223        private int widgetHashCode(String widget) {
224                int hashCode = 0;
225                int multiplier = 37;
226                String[] widgetInfo = widget.split("','");
227                if (widgetInfo.length == 5) {
228                        hashCode = hashCode * multiplier + widgetInfo[1].hashCode();
229                        hashCode = hashCode * multiplier + widgetInfo[2].hashCode();
230                        //hashCode = hashCode * multiplier + widgetInfo[3].hashCode();
231                }
232                return hashCode;
233        }
234
235}
Note: See TracBrowser for help on using the repository browser.