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 // ------------------------------------------------------------------------
39 protected final String name
;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * Standard constructor. Only to be used internally, call parseField() to
47 * generate a new field object.
50 * The name of this field
52 protected CtfTmfEventField(String name
) {
53 /* Strip the underscore */
54 if ( name
.startsWith("_") ) { //$NON-NLS-1$
55 this.name
= name
.substring(1);
61 // ------------------------------------------------------------------------
62 // Getters/Setters/Predicates
63 // ------------------------------------------------------------------------
66 public String
getName() {
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Factory method to instantiate CtfTmfEventField objects.
78 * The CTF Definition of this event field
80 * String The name to assign to this field
81 * @return The resulting CtfTmfEventField object
83 public static CtfTmfEventField
parseField(Definition fieldDef
,
85 CtfTmfEventField field
= null;
87 /* Determine the Definition type */
88 if (fieldDef
instanceof IntegerDefinition
) {
89 field
= new CTFIntegerField(
90 ((IntegerDefinition
) fieldDef
).getValue(), fieldName
);
92 } else if (fieldDef
instanceof StringDefinition
) {
93 field
= new CTFStringField(
94 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
96 } else if (fieldDef
instanceof ArrayDefinition
) {
97 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
98 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
100 if (arrayDef
.isString()) {
101 /* This is an array of UTF-8 bytes, a.k.a. a String! */
102 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
104 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
105 /* This is a an array of CTF Integers */
106 long[] values
= new long[arrayDecl
.getLength()];
107 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
108 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
)).getValue();
110 field
= new CTFIntegerArrayField(values
, fieldName
);
112 /* Add other types of arrays here */
114 } else if (fieldDef
instanceof SequenceDefinition
) {
115 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
116 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
118 if (seqDef
.getLength() == 0) {
119 /* Some sequences have length = 0. Simply use an empty string */
120 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
121 } else if (seqDef
.isString()) {
122 /* Interpret this sequence as a String */
123 field
= new CTFStringField(seqDef
.toString(), fieldName
);
124 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
125 /* Sequence of integers => CTFIntegerArrayField */
126 long[] values
= new long[seqDef
.getLength()];
127 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
128 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
)).getValue();
130 field
= new CTFIntegerArrayField(values
, fieldName
);
132 /* Add other Sequence types here */
133 } else if (fieldDef
instanceof FloatDefinition
){
134 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
135 field
= new CTFFloatField( floatDef
.getValue(), fieldName
);
142 * Copy factory. Create a new field by (deep-) copying the information in an
146 * The other CtfTmfEventField to copy
147 * @return The new CtfTmfEventField
149 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
150 switch (other
.getFieldType()) {
152 return new CTFIntegerField(((CTFIntegerField
) other
).getValue(), other
.name
);
154 return new CTFStringField(((CTFStringField
) other
).getValue(), other
.name
);
156 return new CTFIntegerArrayField(((CTFIntegerArrayField
) other
).getValue(), other
.name
);
158 return new CTFFloatField(((CTFFloatField
) other
).getValue(), other
.name
);
165 public CtfTmfEventField
clone() {
166 return CtfTmfEventField
.copyFrom(this);
169 // ------------------------------------------------------------------------
170 // Abstract methods (to be implemented by each specific field type)
171 // ------------------------------------------------------------------------
174 * Return the int representing this field's value type
176 * @return The field type
178 public abstract int getFieldType();
181 * Return this field's value. You can cast it to the correct type depending
182 * on what getFieldType says.
184 * @return The field's value
187 public abstract Object
getValue();
189 // ------------------------------------------------------------------------
190 // Other methods defined by ITmfEventField, but not used here.
191 // CTF fields do not have sub-fields (yet!)
192 // ------------------------------------------------------------------------
195 public String
[] getFieldNames() {
200 public String
getFieldName(int index
) {
205 public ITmfEventField
[] getFields() {
210 public ITmfEventField
getField(String fieldName
) {
215 public ITmfEventField
getField(int index
) {
222 * The CTF field implementation for integer fields.
226 final class CTFIntegerField
extends CtfTmfEventField
{
228 private final long longValue
;
231 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
232 * Java parser this is interpreted as a long.
235 * The integer value of this field
237 * The name of this field
239 CTFIntegerField(long longValue
, String name
) {
241 this.longValue
= longValue
;
245 public int getFieldType() {
250 public Long
getValue() {
251 return this.longValue
;
255 public String
toString() {
256 return name
+ '=' + longValue
;
262 * The CTF field implementation for string fields
266 final class CTFStringField
extends CtfTmfEventField
{
268 private final String strValue
;
271 * Constructor for CTFStringField.
274 * The string value of this field
276 * The name of this field
278 CTFStringField(String strValue
, String name
) {
280 this.strValue
= strValue
;
284 public int getFieldType() {
289 public String
getValue() {
290 return this.strValue
;
294 public String
toString() {
295 return name
+ '=' + strValue
;
301 * CTF field implementation for arrays of integers.
305 final class CTFIntegerArrayField
extends CtfTmfEventField
{
307 private final long[] longValues
;
310 * Constructor for CTFIntegerArrayField.
313 * The array of integers (as longs) that compose this field's
316 * The name of this field
318 CTFIntegerArrayField(long[] longValues
, String name
) {
320 this.longValues
= longValues
;
324 public int getFieldType() {
329 public long[] getValue() {
330 return this.longValues
;
334 public String
toString() {
335 StringBuffer buffer
= new StringBuffer();
336 buffer
.append("{ "); //$NON-NLS-1$
338 buffer
.append(longValues
[0]);
339 for (int i
= 1; i
< longValues
.length
; i
++) {
340 buffer
.append(", " + longValues
[i
]); //$NON-NLS-1$
343 return name
+ '=' + buffer
.toString();
349 * CTF field implementation for floats.
353 final class CTFFloatField
extends CtfTmfEventField
{
355 private final Double value
;
358 * Constructor for CTFFloatField.
361 * The float value (actually a double) of this field
363 * The name of this field
365 protected CTFFloatField(double value
,String name
) {
371 public int getFieldType() {
376 public Double
getValue() {
381 public String
toString(){
382 return name
+ '=' + value
;
386 /* Implement other possible fields types here... */