tmf: Fix most Number unboxing problems
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventField.java
... / ...
CommitLineData
1/*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson, École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation, extend TmfEventField
12 * Bernd Hufmann - Add Enum field handling
13 * Geneviève Bastien - Add Struct and Variant field handling
14 * Jean-Christian Kouame - Correct handling of unsigned integer fields
15 * François Doray - Add generic array field type
16 *******************************************************************************/
17
18package org.eclipse.linuxtools.tmf.core.ctfadaptor;
19
20import java.util.ArrayList;
21import java.util.Arrays;
22import java.util.List;
23import java.util.Map.Entry;
24
25import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
26import org.eclipse.linuxtools.ctf.core.event.types.Definition;
27import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
28import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
29import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration;
30import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
31import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
32import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
33import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
34import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
35import org.eclipse.linuxtools.ctf.core.event.types.VariantDefinition;
36import org.eclipse.linuxtools.internal.tmf.core.Messages;
37import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
38import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
39
40/**
41 * The CTF implementation of the TMF event field model
42 *
43 * @version 2.0
44 * @author Matthew Khouzam
45 * @author Alexandre Montplaisir
46 */
47public abstract class CtfTmfEventField extends TmfEventField {
48
49 // ------------------------------------------------------------------------
50 // Constructor
51 // ------------------------------------------------------------------------
52
53 /**
54 * Standard constructor. Only to be used internally, call parseField() to
55 * generate a new field object.
56 *
57 * @param name
58 * The name of this field
59 * @param value
60 * The value of this field. Its type should match the field type.
61 * @param fields
62 * The children fields. Useful for composite fields
63 * @since 2.0
64 */
65 protected CtfTmfEventField(String name, Object value, ITmfEventField[] fields) {
66 super(/* Strip the underscore from the field name if there is one */
67 name.startsWith("_") ? name.substring(1) : name, //$NON-NLS-1$
68 value,
69 fields);
70 }
71
72 // ------------------------------------------------------------------------
73 // Operations
74 // ------------------------------------------------------------------------
75
76 /**
77 * Factory method to instantiate CtfTmfEventField objects.
78 *
79 * @param fieldDef
80 * The CTF Definition of this event field
81 * @param fieldName
82 * String The name to assign to this field
83 * @return The resulting CtfTmfEventField object
84 */
85 public static CtfTmfEventField parseField(Definition fieldDef,
86 String fieldName) {
87 CtfTmfEventField field = null;
88
89 /* Determine the Definition type */
90 if (fieldDef instanceof IntegerDefinition) {
91 IntegerDefinition intDef = (IntegerDefinition) fieldDef;
92 int base = intDef.getDeclaration().getBase();
93 field = new CTFIntegerField(fieldName, intDef.getValue(), base, intDef.getDeclaration().isSigned());
94
95 } else if (fieldDef instanceof EnumDefinition) {
96 EnumDefinition enumDef = (EnumDefinition) fieldDef;
97 field = new CTFEnumField(fieldName, new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()));
98
99 } else if (fieldDef instanceof StringDefinition) {
100 field = new CTFStringField(fieldName, ((StringDefinition) fieldDef).getValue());
101
102 } else if (fieldDef instanceof FloatDefinition) {
103 FloatDefinition floatDef = (FloatDefinition) fieldDef;
104 field = new CTFFloatField(fieldName, floatDef.getValue());
105
106 } else if (fieldDef instanceof ArrayDefinition) {
107 ArrayDefinition arrayDef = (ArrayDefinition) fieldDef;
108
109 if (arrayDef.isString()) {
110 /* This is an array of UTF-8 bytes, a.k.a. a String! */
111 field = new CTFStringField(fieldName, fieldDef.toString());
112
113 } else {
114 /* Arrays of elements of any other type */
115 Definition[] definitions = arrayDef.getDefinitions();
116 CtfTmfEventField[] elements = new CtfTmfEventField[definitions.length];
117
118 /* Parse the elements of the array. */
119 for (int i = 0; i < definitions.length; i++) {
120 CtfTmfEventField curField = CtfTmfEventField.parseField(
121 definitions[i], fieldName + '[' + i + ']');
122 elements[i] = curField;
123 }
124
125 field = new CTFArrayField(fieldName, elements);
126 }
127 } else if (fieldDef instanceof SequenceDefinition) {
128 SequenceDefinition seqDef = (SequenceDefinition) fieldDef;
129 SequenceDeclaration seqDecl = seqDef.getDeclaration();
130
131 if (seqDef.getLength() == 0) {
132 /* Some sequences have length = 0. Simply use an empty string */
133 field = new CTFStringField(fieldName, ""); //$NON-NLS-1$
134 } else if (seqDef.isString()) {
135 /* Interpret this sequence as a String */
136 field = new CTFStringField(fieldName, seqDef.toString());
137 } else if (seqDecl.getElementType() instanceof IntegerDeclaration) {
138 /* Sequence of integers => CTFIntegerArrayField */
139 long[] values = new long[seqDef.getLength()];
140 for (int i = 0; i < seqDef.getLength(); i++) {
141 values[i] = ((IntegerDefinition) seqDef.getElem(i)).getValue();
142 }
143 field = new CTFIntegerArrayField(fieldName, values,
144 ((IntegerDeclaration) seqDecl.getElementType()).getBase(),
145 ((IntegerDeclaration) seqDecl.getElementType()).isSigned());
146
147 }
148 /* Add other Sequence types here */
149
150 } else if (fieldDef instanceof StructDefinition) {
151 StructDefinition strDef = (StructDefinition) fieldDef;
152
153 String curFieldName = null;
154 Definition curFieldDef;
155 CtfTmfEventField curField;
156 List<ITmfEventField> list = new ArrayList<>();
157 /* Recursively parse the fields */
158 for (Entry<String, Definition> entry : strDef.getDefinitions().entrySet()) {
159 curFieldName = entry.getKey();
160 curFieldDef = entry.getValue();
161 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
162 list.add(curField);
163 }
164 field = new CTFStructField(fieldName, list.toArray(new CtfTmfEventField[list.size()]));
165
166 } else if (fieldDef instanceof VariantDefinition) {
167 VariantDefinition varDef = (VariantDefinition) fieldDef;
168
169 String curFieldName = varDef.getCurrentFieldName();
170 Definition curFieldDef = varDef.getDefinitions().get(curFieldName);
171 if (curFieldDef != null) {
172 CtfTmfEventField subField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
173 field = new CTFVariantField(fieldName, subField);
174 } else {
175 /* A safe-guard, but curFieldDef should never be null */
176 field = new CTFStringField(curFieldName, ""); //$NON-NLS-1$
177 }
178
179 } else {
180 /*
181 * Safe-guard, to avoid null exceptions later, field is expected not
182 * to be null
183 */
184 field = new CTFStringField(fieldName, Messages.TmfEventField_UnsupportedType + fieldDef.getClass().toString());
185 }
186 return field;
187 }
188
189 @Override
190 public String toString() {
191 return getName() + '=' + getFormattedValue();
192 }
193
194}
195
196/**
197 * The CTF field implementation for integer fields.
198 *
199 * @author alexmont
200 */
201final class CTFIntegerField extends CtfTmfEventField {
202
203 private final int base;
204 private final boolean signed;
205
206 /**
207 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
208 * Java parser this is interpreted as a long.
209 *
210 * @param name
211 * The name of this field
212 * @param longValue
213 * The integer value of this field
214 * @param signed
215 * Is the value signed or not
216 */
217 CTFIntegerField(String name, long longValue, int base, boolean signed) {
218 super(name, longValue, null);
219 this.signed = signed;
220 this.base = base;
221 }
222
223 @Override
224 public Long getValue() {
225 return (Long) super.getValue();
226 }
227
228 @Override
229 public String getFormattedValue() {
230 return IntegerDefinition.formatNumber(getValue(), base, signed);
231 }
232
233}
234
235/**
236 * The CTF field implementation for string fields
237 *
238 * @author alexmont
239 */
240final class CTFStringField extends CtfTmfEventField {
241
242 /**
243 * Constructor for CTFStringField.
244 *
245 * @param strValue
246 * The string value of this field
247 * @param name
248 * The name of this field
249 */
250 CTFStringField(String name, String strValue) {
251 super(name, strValue, null);
252 }
253
254 @Override
255 public String getValue() {
256 return (String) super.getValue();
257 }
258}
259
260/**
261 * CTF field implementation for arrays of integers.
262 *
263 * @author alexmont
264 */
265final class CTFIntegerArrayField extends CtfTmfEventField {
266
267 private final int base;
268 private final boolean signed;
269 private String formattedValue = null;
270
271 /**
272 * Constructor for CTFIntegerArrayField.
273 *
274 * @param name
275 * The name of this field
276 * @param longValues
277 * The array of integers (as longs) that compose this field's
278 * value
279 * @param signed
280 * Are the values in the array signed or not
281 */
282 CTFIntegerArrayField(String name, long[] longValues, int base, boolean signed) {
283 super(name, longValues, null);
284 this.base = base;
285 this.signed = signed;
286 }
287
288 @Override
289 public long[] getValue() {
290 return (long[]) super.getValue();
291 }
292
293 @Override
294 public synchronized String getFormattedValue() {
295 if (formattedValue == null) {
296 List<String> strings = new ArrayList<>();
297 for (long value : getValue()) {
298 strings.add(IntegerDefinition.formatNumber(value, base, signed));
299 }
300 formattedValue = strings.toString();
301 }
302 return formattedValue;
303 }
304
305}
306
307/**
308 * CTF field implementation for arrays of arbitrary types.
309 *
310 * @author fdoray
311 */
312final class CTFArrayField extends CtfTmfEventField {
313
314 private String formattedValue = null;
315
316 /**
317 * Constructor for CTFArrayField.
318 *
319 * @param name
320 * The name of this field
321 * @param elements
322 * The array elements of this field
323 */
324 CTFArrayField(String name, CtfTmfEventField[] elements) {
325 super(name, elements, elements);
326 }
327
328 @Override
329 public CtfTmfEventField[] getValue() {
330 return (CtfTmfEventField[]) super.getValue();
331 }
332
333 @Override
334 public synchronized String getFormattedValue() {
335 if (formattedValue == null) {
336 List<String> strings = new ArrayList<>();
337 for (CtfTmfEventField element : getValue()) {
338 strings.add(element.getFormattedValue());
339 }
340 formattedValue = strings.toString();
341 }
342 return formattedValue;
343 }
344}
345
346/**
347 * CTF field implementation for floats.
348 *
349 * @author emathko
350 */
351final class CTFFloatField extends CtfTmfEventField {
352
353 /**
354 * Constructor for CTFFloatField.
355 *
356 * @param value
357 * The float value (actually a double) of this field
358 * @param name
359 * The name of this field
360 */
361 protected CTFFloatField(String name, double value) {
362 super(name, value, null);
363 }
364
365 @Override
366 public Double getValue() {
367 return (Double) super.getValue();
368 }
369}
370
371/**
372 * The CTF field implementation for Enum fields
373 *
374 * @author Bernd Hufmann
375 */
376final class CTFEnumField extends CtfTmfEventField {
377
378 /**
379 * Constructor for CTFEnumField.
380 *
381 * @param enumValue
382 * The Enum value consisting of a pair of Enum value name and its
383 * long value
384 * @param name
385 * The name of this field
386 */
387 CTFEnumField(String name, CtfEnumPair enumValue) {
388 super(name, new CtfEnumPair(enumValue.getFirst(),
389 enumValue.getSecond()), null);
390 }
391
392 @Override
393 public CtfEnumPair getValue() {
394 return (CtfEnumPair) super.getValue();
395 }
396}
397
398/**
399 * The CTF field implementation for struct fields with sub-fields
400 *
401 * @author gbastien
402 */
403final class CTFStructField extends CtfTmfEventField {
404
405 /**
406 * Constructor for CTFStructField.
407 *
408 * @param fields
409 * The children of this field
410 * @param name
411 * The name of this field
412 */
413 CTFStructField(String name, CtfTmfEventField[] fields) {
414 super(name, fields, fields);
415 }
416
417 @Override
418 public CtfTmfEventField[] getValue() {
419 return (CtfTmfEventField[]) super.getValue();
420 }
421
422 @Override
423 public String getFormattedValue() {
424 return Arrays.toString(getValue());
425 }
426
427}
428
429/**
430 * The CTF field implementation for variant fields its child
431 *
432 * @author gbastien
433 */
434final class CTFVariantField extends CtfTmfEventField {
435
436 /**
437 * Constructor for CTFVariantField.
438 *
439 * @param field
440 * The field selected for this variant
441 * @param name
442 * The name of this field
443 */
444 CTFVariantField(String name, CtfTmfEventField field) {
445 super(name, field, new CtfTmfEventField[] { field });
446 }
447
448 @Override
449 public CtfTmfEventField getValue() {
450 return (CtfTmfEventField) super.getValue();
451 }
452
453}
454
455/* Implement other possible fields types here... */
This page took 0.024232 seconds and 5 git commands to generate.