1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 * Alexendre Montplaisir - Initial API and implementation
12 * Bernd Hufmann - Add Enum field handling
14 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDefinition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
31 * The CTF implementation of the TMF event field model
34 * @author Matthew Khouzam
35 * @author Alexandre Montplaisir
37 public abstract class CtfTmfEventField
implements ITmfEventField
{
39 // ------------------------------------------------------------------------
41 // ------------------------------------------------------------------------
44 protected static final int FIELDTYPE_INTEGER
= 0;
47 protected static final int FIELDTYPE_STRING
= 1;
50 protected static final int FIELDTYPE_INTEGER_ARRAY
= 2;
53 protected static final int FIELDTYPE_FLOAT
= 3;
56 protected static final int FIELDTYPE_ENUM
= 4;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 /** The name of this field */
63 protected final String name
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * Standard constructor. Only to be used internally, call parseField() to
71 * generate a new field object.
74 * The name of this field
76 protected CtfTmfEventField(String name
) {
77 /* Strip the underscore */
78 if (name
.startsWith("_")) { //$NON-NLS-1$
79 this.name
= name
.substring(1);
85 // ------------------------------------------------------------------------
86 // Getters/Setters/Predicates
87 // ------------------------------------------------------------------------
90 public String
getName() {
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
99 * Factory method to instantiate CtfTmfEventField objects.
102 * The CTF Definition of this event field
104 * String The name to assign to this field
105 * @return The resulting CtfTmfEventField object
107 public static CtfTmfEventField
parseField(Definition fieldDef
,
109 CtfTmfEventField field
= null;
111 /* Determine the Definition type */
112 if (fieldDef
instanceof IntegerDefinition
) {
113 IntegerDefinition intDef
= (IntegerDefinition
) fieldDef
;
114 int base
= intDef
.getDeclaration().getBase();
115 field
= new CTFIntegerField(intDef
.getValue(), fieldName
, base
);
117 } else if (fieldDef
instanceof EnumDefinition
) {
118 EnumDefinition enumDef
= (EnumDefinition
) fieldDef
;
119 field
= new CTFEnumField(new CtfEnumPair(enumDef
.getValue(), enumDef
.getIntegerValue()), fieldName
);
121 } else if (fieldDef
instanceof StringDefinition
) {
122 field
= new CTFStringField(
123 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
125 } else if (fieldDef
instanceof ArrayDefinition
) {
126 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
127 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
129 if (arrayDef
.isString()) {
130 /* This is an array of UTF-8 bytes, a.k.a. a String! */
131 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
133 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
134 /* This is a an array of CTF Integers */
135 long[] values
= new long[arrayDecl
.getLength()];
136 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
137 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
))
140 field
= new CTFIntegerArrayField(values
, fieldName
);
142 /* Add other types of arrays here */
144 } else if (fieldDef
instanceof SequenceDefinition
) {
145 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
146 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
148 if (seqDef
.getLength() == 0) {
149 /* Some sequences have length = 0. Simply use an empty string */
150 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
151 } else if (seqDef
.isString()) {
152 /* Interpret this sequence as a String */
153 field
= new CTFStringField(seqDef
.toString(), fieldName
);
154 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
155 /* Sequence of integers => CTFIntegerArrayField */
156 long[] values
= new long[seqDef
.getLength()];
157 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
158 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
))
161 field
= new CTFIntegerArrayField(values
, fieldName
);
163 /* Add other Sequence types here */
165 } else if (fieldDef
instanceof FloatDefinition
) {
166 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
167 field
= new CTFFloatField(floatDef
.getValue(), fieldName
);
174 * Copy factory. Create a new field by (deep-) copying the information in an
178 * The other CtfTmfEventField to copy
179 * @return The new CtfTmfEventField
181 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
182 switch (other
.getFieldType()) {
183 case FIELDTYPE_INTEGER
:
184 CTFIntegerField intOther
= (CTFIntegerField
) other
;
185 return new CTFIntegerField(intOther
.getValue(), intOther
.name
,
187 case FIELDTYPE_STRING
:
188 return new CTFStringField(((CTFStringField
) other
).getValue(),
190 case FIELDTYPE_INTEGER_ARRAY
:
191 return new CTFIntegerArrayField(
192 ((CTFIntegerArrayField
) other
).getValue(), other
.name
);
193 case FIELDTYPE_FLOAT
:
194 return new CTFFloatField(((CTFFloatField
) other
).getValue(),
197 return new CTFEnumField(((CTFEnumField
) other
).getValue(), other
.name
);
204 public CtfTmfEventField
clone() {
205 return CtfTmfEventField
.copyFrom(this);
208 // ------------------------------------------------------------------------
209 // Abstract methods (to be implemented by each specific field type)
210 // ------------------------------------------------------------------------
213 * Return the int representing this field's value type
215 * @return The field type
217 public abstract int getFieldType();
220 * Return this field's value. You can cast it to the correct type depending
221 * on what getFieldType says.
223 * @return The field's value
226 public abstract Object
getValue();
228 // ------------------------------------------------------------------------
229 // Other methods defined by ITmfEventField, but not used here.
230 // CTF fields do not have sub-fields (yet!)
231 // ------------------------------------------------------------------------
234 public String
[] getFieldNames() {
239 public String
getFieldName(int index
) {
244 public ITmfEventField
[] getFields() {
249 public ITmfEventField
getField(String fieldName
) {
254 public ITmfEventField
getField(int index
) {
260 * The CTF field implementation for integer fields.
264 final class CTFIntegerField
extends CtfTmfEventField
{
266 private final long longValue
;
267 private final int base
;
270 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
271 * Java parser this is interpreted as a long.
274 * The integer value of this field
276 * The name of this field
278 CTFIntegerField(long longValue
, String name
, int base
) {
280 this.longValue
= longValue
;
285 * Return the integer's base. (Not made public until it's needed.)
287 * @return The base, usually 10 or 16.
294 public int getFieldType() {
295 return FIELDTYPE_INTEGER
;
299 public Long
getValue() {
300 return this.longValue
;
304 public String
toString() {
305 StringBuilder sb
= new StringBuilder(name
);
308 /* Format the number correctly according to the integer's base */
311 sb
.append("0b"); //$NON-NLS-1$
312 sb
.append(Long
.toBinaryString(longValue
));
316 sb
.append(Long
.toOctalString(longValue
));
319 sb
.append(longValue
);
322 sb
.append("0x"); //$NON-NLS-1$
323 sb
.append(Long
.toHexString(longValue
));
326 /* Non-standard base, we'll just print it as a decimal number */
327 sb
.append(longValue
);
330 return sb
.toString();
335 * The CTF field implementation for string fields
339 final class CTFStringField
extends CtfTmfEventField
{
341 private final String strValue
;
344 * Constructor for CTFStringField.
347 * The string value of this field
349 * The name of this field
351 CTFStringField(String strValue
, String name
) {
353 this.strValue
= strValue
;
357 public int getFieldType() {
358 return FIELDTYPE_STRING
;
362 public String
getValue() {
363 return this.strValue
;
367 public String
toString() {
368 return name
+ '=' + strValue
;
373 * CTF field implementation for arrays of integers.
377 final class CTFIntegerArrayField
extends CtfTmfEventField
{
379 private final long[] longValues
;
382 * Constructor for CTFIntegerArrayField.
385 * The array of integers (as longs) that compose this field's
388 * The name of this field
390 CTFIntegerArrayField(long[] longValues
, String name
) {
392 this.longValues
= longValues
;
396 public int getFieldType() {
397 return FIELDTYPE_INTEGER_ARRAY
;
401 public long[] getValue() {
402 return this.longValues
;
406 public String
toString() {
407 StringBuffer buffer
= new StringBuffer();
408 buffer
.append("{ "); //$NON-NLS-1$
410 buffer
.append(longValues
[0]);
411 for (int i
= 1; i
< longValues
.length
; i
++) {
412 buffer
.append(", " + longValues
[i
]); //$NON-NLS-1$
415 return name
+ '=' + buffer
.toString();
420 * CTF field implementation for floats.
424 final class CTFFloatField
extends CtfTmfEventField
{
426 private final Double value
;
429 * Constructor for CTFFloatField.
432 * The float value (actually a double) of this field
434 * The name of this field
436 protected CTFFloatField(double value
, String name
) {
442 public int getFieldType() {
443 return FIELDTYPE_FLOAT
;
447 public Double
getValue() {
452 public String
toString() {
453 return name
+ '=' + value
;
458 * The CTF field implementation for Enum fields
460 * @author Bernd Hufmann
462 final class CTFEnumField
extends CtfTmfEventField
{
464 private final CtfEnumPair value
;
467 * Constructor for CTFEnumField.
470 * The Enum value consisting of a pair of Enum value name and its long value
472 * The name of this field
474 CTFEnumField(CtfEnumPair enumValue
, String name
) {
476 this.value
= new CtfEnumPair(enumValue
.getFirst(), enumValue
.getSecond().longValue());
480 public int getFieldType() {
481 return FIELDTYPE_ENUM
;
485 public CtfEnumPair
getValue() {
490 public String
toString() {
491 return name
+ '=' + value
.toString();
495 /* Implement other possible fields types here... */