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. | |
28 | * | |
29 | * @since 3.0 | |
30 | */ | |
b742c196 | 31 | public class TextTraceEventContent implements ITmfEventField { |
bcb8c2cb | 32 | |
b742c196 AM |
33 | private final @NonNull String fName; |
34 | private final @NonNull List<TextTraceEventContent> fFields; | |
35 | ||
36 | private @Nullable Object fValue; | |
bcb8c2cb PT |
37 | |
38 | // ------------------------------------------------------------------------ | |
39 | // Constructors | |
40 | // ------------------------------------------------------------------------ | |
41 | ||
42 | /** | |
43 | * Constructor for a root event content | |
44 | * | |
45 | * @param fieldNames | |
46 | * the array of field names | |
47 | */ | |
48 | public TextTraceEventContent(String[] fieldNames) { | |
b742c196 AM |
49 | fName = ITmfEventField.ROOT_FIELD_ID; |
50 | fValue = null; | |
51 | fFields = new ArrayList<>(fieldNames.length); | |
52 | for (String fieldName : fieldNames) { | |
53 | if (fieldName == null) { | |
54 | throw new IllegalArgumentException("Null field name not allowed"); //$NON-NLS-1$ | |
55 | } | |
56 | fFields.add(new TextTraceEventContent(fieldName)); | |
bcb8c2cb PT |
57 | } |
58 | } | |
59 | ||
60 | /** | |
61 | * Constructor for a subfield | |
62 | * | |
63 | * @param fieldNames | |
64 | * the array of field names | |
65 | */ | |
b742c196 | 66 | private TextTraceEventContent(@NonNull String fieldName) { |
bcb8c2cb | 67 | fName = fieldName; |
b742c196 | 68 | fValue = null; |
5db5a3a4 | 69 | fFields = checkNotNull(Collections.EMPTY_LIST); |
bcb8c2cb PT |
70 | } |
71 | ||
72 | // ------------------------------------------------------------------------ | |
73 | // ITmfEventField | |
74 | // ------------------------------------------------------------------------ | |
75 | ||
76 | @Override | |
77 | public String getName() { | |
78 | return fName; | |
79 | } | |
80 | ||
81 | @Override | |
82 | public Object getValue() { | |
83 | return fValue; | |
84 | } | |
85 | ||
86 | @Override | |
b742c196 AM |
87 | public List<String> getFieldNames() { |
88 | List<String> fieldNames = new ArrayList<>(fFields.size()); | |
89 | for (TextTraceEventContent field : fFields) { | |
90 | fieldNames.add(field.getName()); | |
bcb8c2cb PT |
91 | } |
92 | return fieldNames; | |
93 | } | |
94 | ||
bcb8c2cb | 95 | @Override |
b742c196 AM |
96 | public List<TextTraceEventContent> getFields() { |
97 | return new ArrayList<>(fFields); | |
bcb8c2cb PT |
98 | } |
99 | ||
100 | @Override | |
97de0bca PT |
101 | public ITmfEventField getField(String... path) { |
102 | if (path.length == 0) { | |
103 | return this; | |
104 | } | |
105 | // There are no sub fields | |
106 | if (path.length == 1) { | |
107 | for (TextTraceEventContent field : fFields) { | |
108 | if (field.getName().equals(path[0])) { | |
109 | return field; | |
110 | } | |
bcb8c2cb PT |
111 | } |
112 | } | |
113 | return null; | |
114 | } | |
115 | ||
bcb8c2cb PT |
116 | @Override |
117 | public String getFormattedValue() { | |
b742c196 AM |
118 | Object value = fValue; |
119 | if (value == null) { | |
120 | return null; | |
121 | } | |
122 | return value.toString(); | |
bcb8c2cb PT |
123 | } |
124 | ||
bcb8c2cb PT |
125 | // ------------------------------------------------------------------------ |
126 | // Convenience getters and setters | |
127 | // ------------------------------------------------------------------------ | |
128 | ||
99f21044 AM |
129 | /** |
130 | * Get a field name by index | |
131 | * | |
132 | * @param index | |
133 | * The index of the field | |
134 | * @return The name of the field at that index | |
135 | */ | |
136 | public String getFieldName(int index) { | |
137 | if (index >= 0 && index < fFields.size()) { | |
138 | return fFields.get(index).getName(); | |
139 | } | |
140 | return null; | |
141 | } | |
142 | ||
143 | /** | |
144 | * Get a field by index | |
145 | * | |
146 | * @param index | |
147 | * The index of the field | |
148 | * @return The field object at the requested index | |
149 | */ | |
150 | public ITmfEventField getField(int index) { | |
151 | if (index >= 0 && index < fFields.size()) { | |
152 | return fFields.get(index); | |
153 | } | |
154 | return null; | |
155 | } | |
156 | ||
bcb8c2cb PT |
157 | /** |
158 | * Get a subfield value by name | |
159 | * | |
160 | * @param name | |
161 | * a subfield name | |
162 | * @return field value object | |
163 | */ | |
164 | public Object getFieldValue(String name) { | |
b742c196 AM |
165 | for (int i = 0; i < fFields.size(); i++) { |
166 | if (fFields.get(i).getName().equals(name)) { | |
167 | return fFields.get(i).getValue(); | |
bcb8c2cb PT |
168 | } |
169 | } | |
170 | return null; | |
171 | } | |
172 | ||
173 | /** | |
174 | * Get a subfield value by index | |
175 | * | |
176 | * @param index | |
177 | * a subfield index | |
178 | * @return field value object | |
179 | */ | |
180 | public Object getFieldValue(int index) { | |
b742c196 AM |
181 | if (index >= 0 && index < fFields.size()) { |
182 | return fFields.get(index).getValue(); | |
bcb8c2cb PT |
183 | } |
184 | return null; | |
185 | } | |
186 | ||
187 | /** | |
188 | * Set the content value | |
189 | * | |
190 | * @param value | |
191 | * the content value | |
192 | */ | |
193 | public void setValue(Object value) { | |
194 | fValue = value; | |
195 | } | |
196 | ||
197 | /** | |
198 | * Set a subfield value by name | |
199 | * | |
200 | * @param name | |
201 | * a subfield name | |
202 | * @param value | |
203 | * the subfield value | |
204 | */ | |
205 | public void setFieldValue(String name, Object value) { | |
b742c196 AM |
206 | for (int i = 0; i < fFields.size(); i++) { |
207 | if (fFields.get(i).getName().equals(name)) { | |
208 | fFields.get(i).fValue = value; | |
bcb8c2cb PT |
209 | } |
210 | } | |
211 | } | |
212 | ||
213 | /** | |
214 | * Set a subfield value by index | |
215 | * | |
216 | * @param index | |
217 | * a subfield index | |
218 | * @param value | |
219 | * the subfield value | |
220 | */ | |
221 | public void setFieldValue(int index, Object value) { | |
b742c196 AM |
222 | if (index >= 0 && index < fFields.size()) { |
223 | fFields.get(index).fValue = value; | |
bcb8c2cb PT |
224 | } |
225 | } | |
226 | ||
bcb8c2cb PT |
227 | // ------------------------------------------------------------------------ |
228 | // Object | |
229 | // ------------------------------------------------------------------------ | |
230 | ||
231 | @Override | |
232 | public int hashCode() { | |
233 | final int prime = 31; | |
234 | int result = 1; | |
b742c196 AM |
235 | result = prime * result + fFields.hashCode(); |
236 | result = prime * result + fName.hashCode(); | |
bcb8c2cb | 237 | int tmpHash = 0; // initialize for fValue equals null; |
b742c196 AM |
238 | Object value = fValue; |
239 | if (value != null) { | |
240 | if (value instanceof StringBuffer) { | |
241 | tmpHash = value.toString().hashCode(); | |
bcb8c2cb | 242 | } else { |
b742c196 | 243 | tmpHash = value.hashCode(); |
bcb8c2cb PT |
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; | |
b742c196 | 262 | if (!fFields.equals(other.fFields)) { |
bcb8c2cb PT |
263 | return false; |
264 | } | |
b742c196 | 265 | if (!fName.equals(other.fName)) { |
bcb8c2cb PT |
266 | return false; |
267 | } | |
b742c196 AM |
268 | |
269 | Object value = fValue; | |
270 | if (value == null) { | |
bcb8c2cb PT |
271 | if (other.fValue != null) { |
272 | return false; | |
273 | } | |
274 | } else { | |
b742c196 AM |
275 | if ((value instanceof StringBuffer) && (other.fValue instanceof StringBuffer)) { |
276 | Object otherValue = other.getValue(); | |
277 | if (otherValue == null) { | |
278 | return false; | |
279 | } | |
280 | if (!value.toString().equals(otherValue.toString())) { | |
bcb8c2cb PT |
281 | return false; |
282 | } | |
b742c196 | 283 | } else if (!value.equals(other.fValue)) { |
bcb8c2cb PT |
284 | return false; |
285 | } | |
286 | } | |
287 | return true; | |
288 | } | |
289 | ||
290 | @Override | |
291 | public String toString() { | |
292 | StringBuilder sb = new StringBuilder(); | |
293 | if (fName == ITmfEventField.ROOT_FIELD_ID) { | |
b742c196 AM |
294 | for (int i = 0; i < getFields().size(); i++) { |
295 | ITmfEventField field = getFields().get(i); | |
bcb8c2cb PT |
296 | if (i != 0) { |
297 | sb.append(", "); //$NON-NLS-1$ | |
298 | } | |
299 | sb.append(field.toString()); | |
300 | } | |
301 | } else { | |
302 | sb.append(fName); | |
303 | sb.append('='); | |
304 | sb.append(fValue); | |
305 | } | |
306 | return sb.toString(); | |
307 | } | |
308 | ||
309 | } |