Commit | Line | Data |
---|---|---|
bcb8c2cb | 1 | /******************************************************************************* |
97de0bca | 2 | * Copyright (c) 2012, 2015 Ericsson |
bcb8c2cb PT |
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 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.core.trace.text; |
bcb8c2cb | 15 | |
5db5a3a4 AM |
16 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
17 | ||
b742c196 AM |
18 | import java.util.ArrayList; |
19 | import java.util.Collections; | |
20 | import java.util.List; | |
bcb8c2cb | 21 | |
b742c196 AM |
22 | import org.eclipse.jdt.annotation.NonNull; |
23 | import org.eclipse.jdt.annotation.Nullable; | |
2bdf0193 | 24 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; |
bcb8c2cb PT |
25 | |
26 | /** | |
27 | * Implementation of ITmfEventField for Text Traces. | |
bcb8c2cb | 28 | */ |
b742c196 | 29 | public class TextTraceEventContent implements ITmfEventField { |
bcb8c2cb | 30 | |
b742c196 AM |
31 | private final @NonNull String fName; |
32 | private final @NonNull List<TextTraceEventContent> fFields; | |
33 | ||
34 | private @Nullable Object fValue; | |
bcb8c2cb PT |
35 | |
36 | // ------------------------------------------------------------------------ | |
37 | // Constructors | |
38 | // ------------------------------------------------------------------------ | |
39 | ||
40 | /** | |
e604cdf6 PT |
41 | * Constructor for a root event content. Subfields with the specified field |
42 | * names are created and initialized with a null value. | |
bcb8c2cb PT |
43 | * |
44 | * @param fieldNames | |
e604cdf6 PT |
45 | * the array of non-null field names |
46 | * @throws IllegalArgumentException | |
47 | * if any one of the field names is null | |
bcb8c2cb | 48 | */ |
e604cdf6 | 49 | public TextTraceEventContent(@NonNull String[] fieldNames) { |
b742c196 AM |
50 | fName = ITmfEventField.ROOT_FIELD_ID; |
51 | fValue = null; | |
52 | fFields = new ArrayList<>(fieldNames.length); | |
53 | for (String fieldName : fieldNames) { | |
54 | if (fieldName == null) { | |
55 | throw new IllegalArgumentException("Null field name not allowed"); //$NON-NLS-1$ | |
56 | } | |
57 | fFields.add(new TextTraceEventContent(fieldName)); | |
bcb8c2cb PT |
58 | } |
59 | } | |
60 | ||
e604cdf6 PT |
61 | /** |
62 | * Constructor for an initial capacity. This should be the expected number | |
63 | * of fields. | |
64 | * | |
65 | * @param initialCapacity | |
66 | * the initial capacity of the field list | |
67 | */ | |
68 | public TextTraceEventContent(int initialCapacity) { | |
69 | fName = ITmfEventField.ROOT_FIELD_ID; | |
70 | fValue = null; | |
71 | fFields = new ArrayList<>(initialCapacity); | |
72 | } | |
73 | ||
bcb8c2cb PT |
74 | /** |
75 | * Constructor for a subfield | |
76 | * | |
e604cdf6 PT |
77 | * @param fieldName |
78 | * the subfield name | |
bcb8c2cb | 79 | */ |
b742c196 | 80 | private TextTraceEventContent(@NonNull String fieldName) { |
bcb8c2cb | 81 | fName = fieldName; |
b742c196 | 82 | fValue = null; |
5db5a3a4 | 83 | fFields = checkNotNull(Collections.EMPTY_LIST); |
bcb8c2cb PT |
84 | } |
85 | ||
86 | // ------------------------------------------------------------------------ | |
87 | // ITmfEventField | |
88 | // ------------------------------------------------------------------------ | |
89 | ||
90 | @Override | |
91 | public String getName() { | |
92 | return fName; | |
93 | } | |
94 | ||
95 | @Override | |
96 | public Object getValue() { | |
97 | return fValue; | |
98 | } | |
99 | ||
100 | @Override | |
b742c196 AM |
101 | public List<String> getFieldNames() { |
102 | List<String> fieldNames = new ArrayList<>(fFields.size()); | |
103 | for (TextTraceEventContent field : fFields) { | |
104 | fieldNames.add(field.getName()); | |
bcb8c2cb PT |
105 | } |
106 | return fieldNames; | |
107 | } | |
108 | ||
bcb8c2cb | 109 | @Override |
b742c196 AM |
110 | public List<TextTraceEventContent> getFields() { |
111 | return new ArrayList<>(fFields); | |
bcb8c2cb PT |
112 | } |
113 | ||
114 | @Override | |
97de0bca PT |
115 | public ITmfEventField getField(String... path) { |
116 | if (path.length == 0) { | |
117 | return this; | |
118 | } | |
119 | // There are no sub fields | |
120 | if (path.length == 1) { | |
121 | for (TextTraceEventContent field : fFields) { | |
122 | if (field.getName().equals(path[0])) { | |
123 | return field; | |
124 | } | |
bcb8c2cb PT |
125 | } |
126 | } | |
127 | return null; | |
128 | } | |
129 | ||
bcb8c2cb PT |
130 | @Override |
131 | public String getFormattedValue() { | |
b742c196 AM |
132 | Object value = fValue; |
133 | if (value == null) { | |
134 | return null; | |
135 | } | |
136 | return value.toString(); | |
bcb8c2cb PT |
137 | } |
138 | ||
bcb8c2cb PT |
139 | // ------------------------------------------------------------------------ |
140 | // Convenience getters and setters | |
141 | // ------------------------------------------------------------------------ | |
142 | ||
99f21044 | 143 | /** |
e604cdf6 | 144 | * Get a field name by index. |
99f21044 AM |
145 | * |
146 | * @param index | |
147 | * The index of the field | |
148 | * @return The name of the field at that index | |
149 | */ | |
150 | public String getFieldName(int index) { | |
151 | if (index >= 0 && index < fFields.size()) { | |
152 | return fFields.get(index).getName(); | |
153 | } | |
154 | return null; | |
155 | } | |
156 | ||
157 | /** | |
e604cdf6 | 158 | * Get a field by index. |
99f21044 AM |
159 | * |
160 | * @param index | |
161 | * The index of the field | |
162 | * @return The field object at the requested index | |
163 | */ | |
164 | public ITmfEventField getField(int index) { | |
165 | if (index >= 0 && index < fFields.size()) { | |
166 | return fFields.get(index); | |
167 | } | |
168 | return null; | |
169 | } | |
170 | ||
bcb8c2cb | 171 | /** |
e604cdf6 | 172 | * Get a subfield value by name. |
bcb8c2cb PT |
173 | * |
174 | * @param name | |
175 | * a subfield name | |
176 | * @return field value object | |
177 | */ | |
e604cdf6 | 178 | public Object getFieldValue(@NonNull String name) { |
b742c196 AM |
179 | for (int i = 0; i < fFields.size(); i++) { |
180 | if (fFields.get(i).getName().equals(name)) { | |
181 | return fFields.get(i).getValue(); | |
bcb8c2cb PT |
182 | } |
183 | } | |
184 | return null; | |
185 | } | |
186 | ||
187 | /** | |
e604cdf6 | 188 | * Get a subfield value by index. |
bcb8c2cb PT |
189 | * |
190 | * @param index | |
191 | * a subfield index | |
192 | * @return field value object | |
193 | */ | |
194 | public Object getFieldValue(int index) { | |
b742c196 AM |
195 | if (index >= 0 && index < fFields.size()) { |
196 | return fFields.get(index).getValue(); | |
bcb8c2cb PT |
197 | } |
198 | return null; | |
199 | } | |
200 | ||
201 | /** | |
e604cdf6 | 202 | * Set the content value. |
bcb8c2cb PT |
203 | * |
204 | * @param value | |
205 | * the content value | |
206 | */ | |
207 | public void setValue(Object value) { | |
208 | fValue = value; | |
209 | } | |
210 | ||
211 | /** | |
e604cdf6 | 212 | * Set a subfield value by name. Adds the subfield if it is new. |
bcb8c2cb PT |
213 | * |
214 | * @param name | |
215 | * a subfield name | |
216 | * @param value | |
217 | * the subfield value | |
218 | */ | |
e604cdf6 PT |
219 | public void setFieldValue(@NonNull String name, Object value) { |
220 | TextTraceEventContent field = null; | |
b742c196 AM |
221 | for (int i = 0; i < fFields.size(); i++) { |
222 | if (fFields.get(i).getName().equals(name)) { | |
e604cdf6 PT |
223 | field = fFields.get(i); |
224 | field.setValue(value); | |
bcb8c2cb PT |
225 | } |
226 | } | |
e604cdf6 PT |
227 | if (field == null) { |
228 | field = new TextTraceEventContent(name); | |
229 | field.setValue(value); | |
230 | fFields.add(field); | |
231 | } | |
bcb8c2cb PT |
232 | } |
233 | ||
234 | /** | |
e604cdf6 | 235 | * Set a subfield value by index. |
bcb8c2cb PT |
236 | * |
237 | * @param index | |
238 | * a subfield index | |
239 | * @param value | |
240 | * the subfield value | |
241 | */ | |
242 | public void setFieldValue(int index, Object value) { | |
b742c196 AM |
243 | if (index >= 0 && index < fFields.size()) { |
244 | fFields.get(index).fValue = value; | |
bcb8c2cb PT |
245 | } |
246 | } | |
247 | ||
e604cdf6 PT |
248 | /** |
249 | * Add a new subfield unconditionally and set its value. Note: This can | |
250 | * create a duplicate subfield. If the subfield already exists, use | |
251 | * {@link #setFieldValue(String, Object)} instead. | |
252 | * | |
253 | * @param name | |
254 | * a subfield name | |
255 | * @param value | |
256 | * the subfield value | |
257 | */ | |
258 | public void addField(@NonNull String name, Object value) { | |
259 | TextTraceEventContent field = new TextTraceEventContent(name); | |
260 | field.setValue(value); | |
261 | fFields.add(field); | |
262 | } | |
263 | ||
bcb8c2cb PT |
264 | // ------------------------------------------------------------------------ |
265 | // Object | |
266 | // ------------------------------------------------------------------------ | |
267 | ||
268 | @Override | |
269 | public int hashCode() { | |
270 | final int prime = 31; | |
271 | int result = 1; | |
b742c196 AM |
272 | result = prime * result + fFields.hashCode(); |
273 | result = prime * result + fName.hashCode(); | |
bcb8c2cb | 274 | int tmpHash = 0; // initialize for fValue equals null; |
b742c196 AM |
275 | Object value = fValue; |
276 | if (value != null) { | |
277 | if (value instanceof StringBuffer) { | |
278 | tmpHash = value.toString().hashCode(); | |
bcb8c2cb | 279 | } else { |
b742c196 | 280 | tmpHash = value.hashCode(); |
bcb8c2cb PT |
281 | } |
282 | } | |
283 | result = prime * result + tmpHash; | |
284 | return result; | |
285 | } | |
286 | ||
287 | @Override | |
288 | public boolean equals(Object obj) { | |
289 | if (this == obj) { | |
290 | return true; | |
291 | } | |
292 | if (obj == null) { | |
293 | return false; | |
294 | } | |
295 | if (getClass() != obj.getClass()) { | |
296 | return false; | |
297 | } | |
298 | TextTraceEventContent other = (TextTraceEventContent) obj; | |
b742c196 | 299 | if (!fFields.equals(other.fFields)) { |
bcb8c2cb PT |
300 | return false; |
301 | } | |
b742c196 | 302 | if (!fName.equals(other.fName)) { |
bcb8c2cb PT |
303 | return false; |
304 | } | |
b742c196 AM |
305 | |
306 | Object value = fValue; | |
307 | if (value == null) { | |
bcb8c2cb PT |
308 | if (other.fValue != null) { |
309 | return false; | |
310 | } | |
311 | } else { | |
b742c196 AM |
312 | if ((value instanceof StringBuffer) && (other.fValue instanceof StringBuffer)) { |
313 | Object otherValue = other.getValue(); | |
314 | if (otherValue == null) { | |
315 | return false; | |
316 | } | |
317 | if (!value.toString().equals(otherValue.toString())) { | |
bcb8c2cb PT |
318 | return false; |
319 | } | |
b742c196 | 320 | } else if (!value.equals(other.fValue)) { |
bcb8c2cb PT |
321 | return false; |
322 | } | |
323 | } | |
324 | return true; | |
325 | } | |
326 | ||
327 | @Override | |
328 | public String toString() { | |
329 | StringBuilder sb = new StringBuilder(); | |
330 | if (fName == ITmfEventField.ROOT_FIELD_ID) { | |
b742c196 AM |
331 | for (int i = 0; i < getFields().size(); i++) { |
332 | ITmfEventField field = getFields().get(i); | |
bcb8c2cb PT |
333 | if (i != 0) { |
334 | sb.append(", "); //$NON-NLS-1$ | |
335 | } | |
336 | sb.append(field.toString()); | |
337 | } | |
338 | } else { | |
339 | sb.append(fName); | |
340 | sb.append('='); | |
341 | sb.append(fValue); | |
342 | } | |
343 | return sb.toString(); | |
344 | } | |
345 | ||
346 | } |