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

Last change on this file since 376 was 376, checked in by sherbold, 12 years ago
  • the class de.ugoe.cs.eventbench.jfc.data.JFCEvents overrides the functions targetEquals and targetHashCode. The adapted comparison allows that either the hashCode or the title of a widget my defer, without a change of equality.
  • Property svn:mime-type set to text/plain
File size: 6.1 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        /**
49         * <p>
50         * Constructor. Creates a new JFCEvent.
51         * </p>
52         *
53         * @param type
54         *            type of the event
55         */
56        public JFCEvent(String type) {
57                super(type);
58                parameters = new HashMap<String, String>();
59                sourceParameters = new HashMap<String, String>();
60                parentParameters = new HashMap<String, String>();
61        }
62
63        /**
64         * <p>
65         * Adds a new parameter to the event.
66         * </p>
67         *
68         * @param name
69         *            name of the parameter
70         * @param value
71         *            value of the parameter
72         */
73        public void addParameter(String name, String value) {
74                parameters.put(name, value);
75        }
76
77        /**
78         * <p>
79         * Retrieves the value of a parameter.
80         * </p>
81         *
82         * @param name
83         *            name of the parameter
84         * @return value of the parameter
85         */
86        public String getParameter(String name) {
87                return parameters.get(name);
88        }
89
90        /**
91         * <p>
92         * Adds new information about the source of the event.
93         * </p>
94         *
95         * @param name
96         *            name of the information
97         * @param value
98         *            value of the information
99         */
100        public void addSourceInformation(String name, String value) {
101                sourceParameters.put(name, value);
102        }
103
104        /**
105         * <p>
106         * Retrieves information about the source of the event.
107         * </p>
108         *
109         * @param name
110         *            name of the information
111         * @return value of the information
112         */
113        public String getSourceInformation(String name) {
114                return sourceParameters.get(name);
115        }
116
117        /**
118         * <p>
119         * Adds new information about the parent of the source of the event.
120         * </p>
121         *
122         * @param name
123         *            name of the information
124         * @param value
125         *            value of the information
126         */
127        public void addParentInformation(String name, String value) {
128                parentParameters.put(name, value);
129        }
130
131        /**
132         * <p>
133         * Used by the {@link JFCLogParser} to extend the target string of the
134         * current event with a further ancestor. The resulting target string will
135         * have the structure {@code etc.grandparent.parent.eventtarget}.
136         * </p>
137         *
138         * @param extension
139         *            extension for the target.
140         */
141        public void extendTarget(String extension) {
142                if (target == null || "".equals(target)) {
143                        target = extension;
144                } else {
145                        target += "." + extension;
146                }
147        }
148
149        /**
150         * <p>
151         * Retrieves information about the parent of the source of the event.
152         * </p>
153         *
154         * @param name
155         *            name of the information
156         * @return value of the information
157         */
158        public String getParentInformation(String name) {
159                return parentParameters.get(name);
160        }
161
162        /**
163         * <p>
164         * This method implements the comparison between two targets of JFCEvents.
165         * The targets are equal, if they have the same placement in the widget
166         * hierarchy, i.e., the target strings describe the same widgets, according
167         * to the implementation of widget equality provided by
168         * {@link #compareWidgets(String, String)}.
169         * </p>
170         *
171         * @see de.ugoe.cs.eventbench.data.Event#targetEquals(java.lang.String)
172         */
173        @Override
174        protected boolean targetEquals(String otherTarget) {
175                if( target==null || otherTarget==null ) {
176                        return target==otherTarget;
177                }
178                String[] targetParts = target.split("\\]\\.\\[");
179                String[] otherParts = otherTarget.split("\\]\\.\\[");
180                if (targetParts.length != otherParts.length) {
181                        return false;
182                }
183               
184                boolean retVal;
185                if (targetParts.length == 0) {
186                        retVal = compareWidgets(target, otherTarget);
187                } else {
188                        retVal = true;
189                        for (int i = 0; retVal && i < targetParts.length; i++) {
190                                retVal &= compareWidgets(targetParts[i], otherParts[i]);
191                        }
192                }
193                return retVal;
194        }
195
196        /**
197         * <p>
198         * Compares two widget strings of the form
199         * {@code ['title','class','index','text','hashCode']}.
200         * </p>
201         *
202         * @param widget1
203         * @param widget2
204         * @return
205         */
206        private boolean compareWidgets(String widget1, String widget2) {
207                String[] widgetInfo1 = widget1.split("','");
208                String[] widgetInfo2 = widget2.split("','");
209                // ensure size is equal
210                if (widgetInfo1.length != 5 || widgetInfo2.length != 5) {
211                        return false;
212                }
213                // ensure that class [1], index [2], and text [3] are equal
214                // and title [0] or hashCode [4] are equal
215                return (widgetInfo1[1].equals(widgetInfo2[1])
216                                && widgetInfo1[2].equals(widgetInfo2[2]) && widgetInfo1[3]
217                                        .equals(widgetInfo2[3]))
218                                && (widgetInfo1[0].equals(widgetInfo2[0]) || widgetInfo1[4]
219                                                .equals(widgetInfo2[4]));
220
221        }
222       
223        @Override
224        protected int targetHashCode() {
225                int hashCode = 0;
226                int multiplier = 29;
227                if( target!=null ) {
228                        String[] targetParts = target.split("\\[\\.\\[");
229                        if( targetParts.length==0 ) {
230                                hashCode = widgetHashCode(target);
231                        } else {
232                                for( String widgetString : targetParts ) {
233                                        hashCode = hashCode * multiplier + widgetHashCode(widgetString);
234                                }
235                        }
236                }
237               
238                return hashCode;
239        }
240       
241        private int widgetHashCode(String widget) {
242                int hashCode = 0;
243                int multiplier = 37;
244                String[] widgetInfo = widget.split("','");
245                if( widgetInfo.length==5 ) {
246                        hashCode = hashCode * multiplier + widgetInfo[1].hashCode();
247                        hashCode = hashCode * multiplier + widgetInfo[2].hashCode();
248                        hashCode = hashCode * multiplier + widgetInfo[3].hashCode();
249                }
250                return hashCode;
251        }
252
253}
Note: See TracBrowser for help on using the repository browser.