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
.FloatDefinition
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDefinition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
27 * The CTF implementation of the TMF event field model
30 * @author Matthew Khouzam
31 * @author Alexandre Montplaisir
33 public abstract class CtfTmfEventField
implements ITmfEventField
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
40 protected static final int FIELDTYPE_INTEGER
= 0;
43 protected static final int FIELDTYPE_STRING
= 1;
46 protected static final int FIELDTYPE_INTEGER_ARRAY
= 2;
49 protected static final int FIELDTYPE_FLOAT
= 3;
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 protected final String name
;
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 * Standard constructor. Only to be used internally, call parseField() to
63 * generate a new field object.
66 * The name of this field
68 protected CtfTmfEventField(String name
) {
69 /* Strip the underscore */
70 if ( name
.startsWith("_") ) { //$NON-NLS-1$
71 this.name
= name
.substring(1);
77 // ------------------------------------------------------------------------
78 // Getters/Setters/Predicates
79 // ------------------------------------------------------------------------
82 public String
getName() {
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 * Factory method to instantiate CtfTmfEventField objects.
94 * The CTF Definition of this event field
96 * String The name to assign to this field
97 * @return The resulting CtfTmfEventField object
99 public static CtfTmfEventField
parseField(Definition fieldDef
,
101 CtfTmfEventField field
= null;
103 /* Determine the Definition type */
104 if (fieldDef
instanceof IntegerDefinition
) {
105 IntegerDefinition intDef
= (IntegerDefinition
) fieldDef
;
106 int base
= intDef
.getDeclaration().getBase();
107 field
= new CTFIntegerField(intDef
.getValue(), fieldName
, base
);
109 } else if (fieldDef
instanceof StringDefinition
) {
110 field
= new CTFStringField(
111 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
113 } else if (fieldDef
instanceof ArrayDefinition
) {
114 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
115 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
117 if (arrayDef
.isString()) {
118 /* This is an array of UTF-8 bytes, a.k.a. a String! */
119 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
121 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
122 /* This is a an array of CTF Integers */
123 long[] values
= new long[arrayDecl
.getLength()];
124 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
125 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
)).getValue();
127 field
= new CTFIntegerArrayField(values
, fieldName
);
129 /* Add other types of arrays here */
131 } else if (fieldDef
instanceof SequenceDefinition
) {
132 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
133 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
135 if (seqDef
.getLength() == 0) {
136 /* Some sequences have length = 0. Simply use an empty string */
137 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
138 } else if (seqDef
.isString()) {
139 /* Interpret this sequence as a String */
140 field
= new CTFStringField(seqDef
.toString(), fieldName
);
141 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
142 /* Sequence of integers => CTFIntegerArrayField */
143 long[] values
= new long[seqDef
.getLength()];
144 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
145 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
)).getValue();
147 field
= new CTFIntegerArrayField(values
, fieldName
);
149 /* Add other Sequence types here */
151 } else if (fieldDef
instanceof FloatDefinition
){
152 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
153 field
= new CTFFloatField( floatDef
.getValue(), fieldName
);
160 * Copy factory. Create a new field by (deep-) copying the information in an
164 * The other CtfTmfEventField to copy
165 * @return The new CtfTmfEventField
167 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
168 switch (other
.getFieldType()) {
169 case FIELDTYPE_INTEGER
:
170 CTFIntegerField intOther
= (CTFIntegerField
) other
;
171 return new CTFIntegerField(intOther
.getValue(), intOther
.name
, intOther
.getBase());
172 case FIELDTYPE_STRING
:
173 return new CTFStringField(((CTFStringField
) other
).getValue(), other
.name
);
174 case FIELDTYPE_INTEGER_ARRAY
:
175 return new CTFIntegerArrayField(((CTFIntegerArrayField
) other
).getValue(), other
.name
);
176 case FIELDTYPE_FLOAT
:
177 return new CTFFloatField(((CTFFloatField
) other
).getValue(), other
.name
);
184 public CtfTmfEventField
clone() {
185 return CtfTmfEventField
.copyFrom(this);
188 // ------------------------------------------------------------------------
189 // Abstract methods (to be implemented by each specific field type)
190 // ------------------------------------------------------------------------
193 * Return the int representing this field's value type
195 * @return The field type
197 public abstract int getFieldType();
200 * Return this field's value. You can cast it to the correct type depending
201 * on what getFieldType says.
203 * @return The field's value
206 public abstract Object
getValue();
208 // ------------------------------------------------------------------------
209 // Other methods defined by ITmfEventField, but not used here.
210 // CTF fields do not have sub-fields (yet!)
211 // ------------------------------------------------------------------------
214 public String
[] getFieldNames() {
219 public String
getFieldName(int index
) {
224 public ITmfEventField
[] getFields() {
229 public ITmfEventField
getField(String fieldName
) {
234 public ITmfEventField
getField(int index
) {
241 * The CTF field implementation for integer fields.
245 final class CTFIntegerField
extends CtfTmfEventField
{
247 private final long longValue
;
248 private final int base
;
251 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
252 * Java parser this is interpreted as a long.
255 * The integer value of this field
257 * The name of this field
259 CTFIntegerField(long longValue
, String name
, int base
) {
261 this.longValue
= longValue
;
266 * Return the integer's base. (Not made public until it's needed.)
268 * @return The base, usually 10 or 16.
275 public int getFieldType() {
276 return FIELDTYPE_INTEGER
;
280 public Long
getValue() {
281 return this.longValue
;
285 public String
toString() {
286 StringBuilder sb
= new StringBuilder(name
);
289 /* Format the number correctly according to the integer's base */
292 sb
.append("0b"); //$NON-NLS-1$
293 sb
.append(Long
.toBinaryString(longValue
));
297 sb
.append(Long
.toOctalString(longValue
));
300 sb
.append(longValue
);
303 sb
.append("0x"); //$NON-NLS-1$
304 sb
.append(Long
.toHexString(longValue
));
307 /* Non-standard base, we'll just print it as a decimal number */
308 sb
.append(longValue
);
311 return sb
.toString();
317 * The CTF field implementation for string fields
321 final class CTFStringField
extends CtfTmfEventField
{
323 private final String strValue
;
326 * Constructor for CTFStringField.
329 * The string value of this field
331 * The name of this field
333 CTFStringField(String strValue
, String name
) {
335 this.strValue
= strValue
;
339 public int getFieldType() {
340 return FIELDTYPE_STRING
;
344 public String
getValue() {
345 return this.strValue
;
349 public String
toString() {
350 return name
+ '=' + strValue
;
356 * CTF field implementation for arrays of integers.
360 final class CTFIntegerArrayField
extends CtfTmfEventField
{
362 private final long[] longValues
;
365 * Constructor for CTFIntegerArrayField.
368 * The array of integers (as longs) that compose this field's
371 * The name of this field
373 CTFIntegerArrayField(long[] longValues
, String name
) {
375 this.longValues
= longValues
;
379 public int getFieldType() {
380 return FIELDTYPE_INTEGER_ARRAY
;
384 public long[] getValue() {
385 return this.longValues
;
389 public String
toString() {
390 StringBuffer buffer
= new StringBuffer();
391 buffer
.append("{ "); //$NON-NLS-1$
393 buffer
.append(longValues
[0]);
394 for (int i
= 1; i
< longValues
.length
; i
++) {
395 buffer
.append(", " + longValues
[i
]); //$NON-NLS-1$
398 return name
+ '=' + buffer
.toString();
404 * CTF field implementation for floats.
408 final class CTFFloatField
extends CtfTmfEventField
{
410 private final Double value
;
413 * Constructor for CTFFloatField.
416 * The float value (actually a double) of this field
418 * The name of this field
420 protected CTFFloatField(double value
,String name
) {
426 public int getFieldType() {
427 return FIELDTYPE_FLOAT
;
431 public Double
getValue() {
436 public String
toString(){
437 return name
+ '=' + value
;
441 /* Implement other possible fields types here... */