Commit | Line | Data |
---|---|---|
bcb8c2cb PT |
1 | /******************************************************************************* |
2 | * Copyright (c) 2012, 2014 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Patrick Tasse - Initial API and implementation | |
11 | * Bernd Hufmann - Updated equals, clone and hashCode to consider StringBuffer values | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.tmf.core.trace.text; | |
15 | ||
16 | import java.util.Arrays; | |
17 | ||
18 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
19 | ||
20 | /** | |
21 | * Implementation of ITmfEventField for Text Traces. | |
22 | * | |
23 | * @since 3.0 | |
24 | */ | |
25 | public class TextTraceEventContent implements ITmfEventField, Cloneable { | |
26 | ||
27 | private String fName; | |
28 | private Object fValue; | |
29 | private TextTraceEventContent[] fFields; | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Constructors | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
35 | /** | |
36 | * Constructor for a root event content | |
37 | * | |
38 | * @param fieldNames | |
39 | * the array of field names | |
40 | */ | |
41 | public TextTraceEventContent(String[] fieldNames) { | |
42 | this(ITmfEventField.ROOT_FIELD_ID); | |
43 | fFields = new TextTraceEventContent[fieldNames.length]; | |
44 | for (int i = 0; i < fFields.length; i++) { | |
45 | fFields[i] = new TextTraceEventContent(fieldNames[i]); | |
46 | } | |
47 | } | |
48 | ||
49 | /** | |
50 | * Constructor for a subfield | |
51 | * | |
52 | * @param fieldNames | |
53 | * the array of field names | |
54 | */ | |
55 | private TextTraceEventContent(String fieldName) { | |
56 | fName = fieldName; | |
57 | } | |
58 | ||
59 | // ------------------------------------------------------------------------ | |
60 | // ITmfEventField | |
61 | // ------------------------------------------------------------------------ | |
62 | ||
63 | @Override | |
64 | public String getName() { | |
65 | return fName; | |
66 | } | |
67 | ||
68 | @Override | |
69 | public Object getValue() { | |
70 | return fValue; | |
71 | } | |
72 | ||
73 | @Override | |
74 | public String[] getFieldNames() { | |
75 | String[] fieldNames = new String[fFields.length]; | |
76 | for (int i = 0; i < fieldNames.length; i++) { | |
77 | fieldNames[i] = fFields[i].getName(); | |
78 | } | |
79 | return fieldNames; | |
80 | } | |
81 | ||
82 | @Override | |
83 | public String getFieldName(int index) { | |
84 | if (index >= 0 && index < fFields.length) { | |
85 | return fFields[index].getName(); | |
86 | } | |
87 | return null; | |
88 | } | |
89 | ||
90 | @Override | |
91 | public ITmfEventField[] getFields() { | |
92 | return fFields; | |
93 | } | |
94 | ||
95 | @Override | |
96 | public ITmfEventField getField(String name) { | |
97 | for (int i = 0; i < fFields.length; i++) { | |
98 | if (fFields[i].getName().equals(name)) { | |
99 | return fFields[i]; | |
100 | } | |
101 | } | |
102 | return null; | |
103 | } | |
104 | ||
105 | @Override | |
106 | public ITmfEventField getField(int index) { | |
107 | if (index >= 0 && index < fFields.length) { | |
108 | return fFields[index]; | |
109 | } | |
110 | return null; | |
111 | } | |
112 | ||
113 | @Override | |
114 | public String getFormattedValue() { | |
115 | return fValue.toString(); | |
116 | } | |
117 | ||
118 | @Override | |
119 | public ITmfEventField getSubField(String[] names) { | |
eadf9801 BH |
120 | // There are no sub fields |
121 | if (names.length == 1) { | |
122 | return getField(names[0]); | |
bcb8c2cb | 123 | } |
eadf9801 | 124 | return null; |
bcb8c2cb PT |
125 | } |
126 | ||
127 | // ------------------------------------------------------------------------ | |
128 | // Convenience getters and setters | |
129 | // ------------------------------------------------------------------------ | |
130 | ||
131 | /** | |
132 | * Get a subfield value by name | |
133 | * | |
134 | * @param name | |
135 | * a subfield name | |
136 | * @return field value object | |
137 | */ | |
138 | public Object getFieldValue(String name) { | |
139 | for (int i = 0; i < fFields.length; i++) { | |
140 | if (fFields[i].getName().equals(name)) { | |
141 | return fFields[i].getValue(); | |
142 | } | |
143 | } | |
144 | return null; | |
145 | } | |
146 | ||
147 | /** | |
148 | * Get a subfield value by index | |
149 | * | |
150 | * @param index | |
151 | * a subfield index | |
152 | * @return field value object | |
153 | */ | |
154 | public Object getFieldValue(int index) { | |
155 | if (index >= 0 && index < fFields.length) { | |
156 | return fFields[index].getValue(); | |
157 | } | |
158 | return null; | |
159 | } | |
160 | ||
161 | /** | |
162 | * Set the content value | |
163 | * | |
164 | * @param value | |
165 | * the content value | |
166 | */ | |
167 | public void setValue(Object value) { | |
168 | fValue = value; | |
169 | } | |
170 | ||
171 | /** | |
172 | * Set a subfield value by name | |
173 | * | |
174 | * @param name | |
175 | * a subfield name | |
176 | * @param value | |
177 | * the subfield value | |
178 | */ | |
179 | public void setFieldValue(String name, Object value) { | |
180 | for (int i = 0; i < fFields.length; i++) { | |
181 | if (fFields[i].getName().equals(name)) { | |
182 | fFields[i].fValue = value; | |
183 | } | |
184 | } | |
185 | } | |
186 | ||
187 | /** | |
188 | * Set a subfield value by index | |
189 | * | |
190 | * @param index | |
191 | * a subfield index | |
192 | * @param value | |
193 | * the subfield value | |
194 | */ | |
195 | public void setFieldValue(int index, Object value) { | |
196 | if (index >= 0 && index < fFields.length) { | |
197 | fFields[index].fValue = value; | |
198 | } | |
199 | } | |
200 | ||
201 | // ------------------------------------------------------------------------ | |
202 | // Cloneable | |
203 | // ------------------------------------------------------------------------ | |
204 | ||
205 | @Override | |
206 | public TextTraceEventContent clone() { | |
207 | TextTraceEventContent clone = null; | |
208 | try { | |
209 | clone = (TextTraceEventContent) super.clone(); | |
210 | clone.fName = fName; | |
211 | if (fValue instanceof StringBuffer) { | |
212 | StringBuffer value = new StringBuffer(fValue.toString()); | |
213 | clone.fValue = value; | |
214 | } else { | |
215 | clone.fValue = fValue; | |
216 | } | |
217 | clone.fFields = (fFields != null) ? fFields.clone() : null; | |
218 | if (fFields != null) { | |
219 | for (int i = 0; i < fFields.length; i++) { | |
220 | clone.fFields[i] = fFields[i].clone(); | |
221 | } | |
222 | } | |
223 | } catch (CloneNotSupportedException e) { | |
224 | } | |
225 | return clone; | |
226 | } | |
227 | ||
228 | // ------------------------------------------------------------------------ | |
229 | // Object | |
230 | // ------------------------------------------------------------------------ | |
231 | ||
232 | @Override | |
233 | public int hashCode() { | |
234 | final int prime = 31; | |
235 | int result = 1; | |
236 | result = prime * result + Arrays.hashCode(fFields); | |
237 | result = prime * result + ((fName == null) ? 0 : fName.hashCode()); | |
238 | int tmpHash = 0; // initialize for fValue equals null; | |
239 | if (fValue != null) { | |
240 | if (fValue instanceof StringBuffer) { | |
241 | tmpHash = fValue.toString().hashCode(); | |
242 | } else { | |
243 | tmpHash = fValue.hashCode(); | |
244 | } | |
245 | } | |
246 | result = prime * result + tmpHash; | |
247 | return result; | |
248 | } | |
249 | ||
250 | @Override | |
251 | public boolean equals(Object obj) { | |
252 | if (this == obj) { | |
253 | return true; | |
254 | } | |
255 | if (obj == null) { | |
256 | return false; | |
257 | } | |
258 | if (getClass() != obj.getClass()) { | |
259 | return false; | |
260 | } | |
261 | TextTraceEventContent other = (TextTraceEventContent) obj; | |
262 | if (!Arrays.equals(fFields, other.fFields)) { | |
263 | return false; | |
264 | } | |
265 | if (fName == null) { | |
266 | if (other.fName != null) { | |
267 | return false; | |
268 | } | |
269 | } else if (!fName.equals(other.fName)) { | |
270 | return false; | |
271 | } | |
272 | if (fValue == null) { | |
273 | if (other.fValue != null) { | |
274 | return false; | |
275 | } | |
276 | } else { | |
277 | if ((fValue instanceof StringBuffer) && (other.fValue instanceof StringBuffer)) { | |
278 | if (!fValue.toString().equals(other.fValue.toString())) { | |
279 | return false; | |
280 | } | |
281 | } else if (!fValue.equals(other.fValue)) { | |
282 | return false; | |
283 | } | |
284 | } | |
285 | return true; | |
286 | } | |
287 | ||
288 | @Override | |
289 | public String toString() { | |
290 | StringBuilder sb = new StringBuilder(); | |
291 | if (fName == ITmfEventField.ROOT_FIELD_ID) { | |
292 | for (int i = 0; i < getFields().length; i++) { | |
293 | ITmfEventField field = getFields()[i]; | |
294 | if (i != 0) { | |
295 | sb.append(", "); //$NON-NLS-1$ | |
296 | } | |
297 | sb.append(field.toString()); | |
298 | } | |
299 | } else { | |
300 | sb.append(fName); | |
301 | sb.append('='); | |
302 | sb.append(fValue); | |
303 | } | |
304 | return sb.toString(); | |
305 | } | |
306 | ||
307 | } |