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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Alexendre Montplaisir - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
15 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDeclaration
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.ArrayDefinition
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.FloatDefinition
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDefinition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
28 * The CTF implementation of the TMF event field model
31 * @author Matthew Khouzam
32 * @author Alexandre Montplaisir
34 public abstract class CtfTmfEventField
implements ITmfEventField
{
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
41 protected static final int FIELDTYPE_INTEGER
= 0;
44 protected static final int FIELDTYPE_STRING
= 1;
47 protected static final int FIELDTYPE_INTEGER_ARRAY
= 2;
50 protected static final int FIELDTYPE_FLOAT
= 3;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
56 protected final String name
;
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
63 * Standard constructor. Only to be used internally, call parseField() to
64 * generate a new field object.
67 * The name of this field
69 protected CtfTmfEventField(String name
) {
70 /* Strip the underscore */
71 if (name
.startsWith("_")) { //$NON-NLS-1$
72 this.name
= name
.substring(1);
78 // ------------------------------------------------------------------------
79 // Getters/Setters/Predicates
80 // ------------------------------------------------------------------------
83 public String
getName() {
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 * Factory method to instantiate CtfTmfEventField objects.
95 * The CTF Definition of this event field
97 * String The name to assign to this field
98 * @return The resulting CtfTmfEventField object
100 public static CtfTmfEventField
parseField(Definition fieldDef
,
102 CtfTmfEventField field
= null;
104 /* Determine the Definition type */
105 if (fieldDef
instanceof IntegerDefinition
) {
106 IntegerDefinition intDef
= (IntegerDefinition
) fieldDef
;
107 int base
= intDef
.getDeclaration().getBase();
108 field
= new CTFIntegerField(intDef
.getValue(), fieldName
, base
);
110 } else if (fieldDef
instanceof EnumDefinition
) {
111 EnumDefinition enumDef
= (EnumDefinition
) fieldDef
;
112 field
= new CTFStringField(enumDef
.getValue(), fieldName
);
114 } else if (fieldDef
instanceof StringDefinition
) {
115 field
= new CTFStringField(
116 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
118 } else if (fieldDef
instanceof ArrayDefinition
) {
119 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
120 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
122 if (arrayDef
.isString()) {
123 /* This is an array of UTF-8 bytes, a.k.a. a String! */
124 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
126 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
127 /* This is a an array of CTF Integers */
128 long[] values
= new long[arrayDecl
.getLength()];
129 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
130 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
))
133 field
= new CTFIntegerArrayField(values
, fieldName
);
135 /* Add other types of arrays here */
137 } else if (fieldDef
instanceof SequenceDefinition
) {
138 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
139 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
141 if (seqDef
.getLength() == 0) {
142 /* Some sequences have length = 0. Simply use an empty string */
143 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
144 } else if (seqDef
.isString()) {
145 /* Interpret this sequence as a String */
146 field
= new CTFStringField(seqDef
.toString(), fieldName
);
147 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
148 /* Sequence of integers => CTFIntegerArrayField */
149 long[] values
= new long[seqDef
.getLength()];
150 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
151 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
))
154 field
= new CTFIntegerArrayField(values
, fieldName
);
156 /* Add other Sequence types here */
158 } else if (fieldDef
instanceof FloatDefinition
) {
159 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
160 field
= new CTFFloatField(floatDef
.getValue(), fieldName
);
167 * Copy factory. Create a new field by (deep-) copying the information in an
171 * The other CtfTmfEventField to copy
172 * @return The new CtfTmfEventField
174 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
175 switch (other
.getFieldType()) {
176 case FIELDTYPE_INTEGER
:
177 CTFIntegerField intOther
= (CTFIntegerField
) other
;
178 return new CTFIntegerField(intOther
.getValue(), intOther
.name
,
180 case FIELDTYPE_STRING
:
181 return new CTFStringField(((CTFStringField
) other
).getValue(),
183 case FIELDTYPE_INTEGER_ARRAY
:
184 return new CTFIntegerArrayField(
185 ((CTFIntegerArrayField
) other
).getValue(), other
.name
);
186 case FIELDTYPE_FLOAT
:
187 return new CTFFloatField(((CTFFloatField
) other
).getValue(),
195 public CtfTmfEventField
clone() {
196 return CtfTmfEventField
.copyFrom(this);
199 // ------------------------------------------------------------------------
200 // Abstract methods (to be implemented by each specific field type)
201 // ------------------------------------------------------------------------
204 * Return the int representing this field's value type
206 * @return The field type
208 public abstract int getFieldType();
211 * Return this field's value. You can cast it to the correct type depending
212 * on what getFieldType says.
214 * @return The field's value
217 public abstract Object
getValue();
219 // ------------------------------------------------------------------------
220 // Other methods defined by ITmfEventField, but not used here.
221 // CTF fields do not have sub-fields (yet!)
222 // ------------------------------------------------------------------------
225 public String
[] getFieldNames() {
230 public String
getFieldName(int index
) {
235 public ITmfEventField
[] getFields() {
240 public ITmfEventField
getField(String fieldName
) {
245 public ITmfEventField
getField(int index
) {
251 * The CTF field implementation for integer fields.
255 final class CTFIntegerField
extends CtfTmfEventField
{
257 private final long longValue
;
258 private final int base
;
261 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
262 * Java parser this is interpreted as a long.
265 * The integer value of this field
267 * The name of this field
269 CTFIntegerField(long longValue
, String name
, int base
) {
271 this.longValue
= longValue
;
276 * Return the integer's base. (Not made public until it's needed.)
278 * @return The base, usually 10 or 16.
285 public int getFieldType() {
286 return FIELDTYPE_INTEGER
;
290 public Long
getValue() {
291 return this.longValue
;
295 public String
toString() {
296 StringBuilder sb
= new StringBuilder(name
);
299 /* Format the number correctly according to the integer's base */
302 sb
.append("0b"); //$NON-NLS-1$
303 sb
.append(Long
.toBinaryString(longValue
));
307 sb
.append(Long
.toOctalString(longValue
));
310 sb
.append(longValue
);
313 sb
.append("0x"); //$NON-NLS-1$
314 sb
.append(Long
.toHexString(longValue
));
317 /* Non-standard base, we'll just print it as a decimal number */
318 sb
.append(longValue
);
321 return sb
.toString();
326 * The CTF field implementation for string fields
330 final class CTFStringField
extends CtfTmfEventField
{
332 private final String strValue
;
335 * Constructor for CTFStringField.
338 * The string value of this field
340 * The name of this field
342 CTFStringField(String strValue
, String name
) {
344 this.strValue
= strValue
;
348 public int getFieldType() {
349 return FIELDTYPE_STRING
;
353 public String
getValue() {
354 return this.strValue
;
358 public String
toString() {
359 return name
+ '=' + strValue
;
364 * CTF field implementation for arrays of integers.
368 final class CTFIntegerArrayField
extends CtfTmfEventField
{
370 private final long[] longValues
;
373 * Constructor for CTFIntegerArrayField.
376 * The array of integers (as longs) that compose this field's
379 * The name of this field
381 CTFIntegerArrayField(long[] longValues
, String name
) {
383 this.longValues
= longValues
;
387 public int getFieldType() {
388 return FIELDTYPE_INTEGER_ARRAY
;
392 public long[] getValue() {
393 return this.longValues
;
397 public String
toString() {
398 StringBuffer buffer
= new StringBuffer();
399 buffer
.append("{ "); //$NON-NLS-1$
401 buffer
.append(longValues
[0]);
402 for (int i
= 1; i
< longValues
.length
; i
++) {
403 buffer
.append(", " + longValues
[i
]); //$NON-NLS-1$
406 return name
+ '=' + buffer
.toString();
411 * CTF field implementation for floats.
415 final class CTFFloatField
extends CtfTmfEventField
{
417 private final Double value
;
420 * Constructor for CTFFloatField.
423 * The float value (actually a double) of this field
425 * The name of this field
427 protected CTFFloatField(double value
, String name
) {
433 public int getFieldType() {
434 return FIELDTYPE_FLOAT
;
438 public Double
getValue() {
443 public String
toString() {
444 return name
+ '=' + value
;
448 /* Implement other possible fields types here... */