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 * Constructor for CtfTmfEventField.
49 protected CtfTmfEventField(String name
) {
50 /* Strip the underscore*/
51 if ( name
.startsWith("_") ) { //$NON-NLS-1$
52 this.name
= name
.substring(1);
58 // ------------------------------------------------------------------------
59 // Getters/Setters/Predicates
60 // ------------------------------------------------------------------------
65 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
68 public String
getName() {
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
78 * @param fieldDef Definition
79 * @param fieldName String
80 * @return CtfTmfEventField
82 public static CtfTmfEventField
parseField(Definition fieldDef
,
84 CtfTmfEventField field
= null;
86 /* Determine the Definition type */
87 if (fieldDef
instanceof IntegerDefinition
) {
88 field
= new CTFIntegerField(
89 ((IntegerDefinition
) fieldDef
).getValue(), fieldName
);
91 } else if (fieldDef
instanceof StringDefinition
) {
92 field
= new CTFStringField(
93 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
95 } else if (fieldDef
instanceof ArrayDefinition
) {
96 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
97 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
99 if (arrayDef
.isString()) {
100 /* This is an array of UTF-8 bytes, a.k.a. a String! */
101 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
103 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
104 /* This is a an array of CTF Integers */
105 long[] values
= new long[arrayDecl
.getLength()];
106 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
107 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
)).getValue();
109 field
= new CTFIntegerArrayField(values
, fieldName
);
111 /* Add other types of arrays here */
113 } else if (fieldDef
instanceof SequenceDefinition
) {
114 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
115 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
117 if (seqDef
.getLength() == 0) {
118 /* Some sequences have length = 0. Simply use an empty string */
119 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
120 } else if (seqDef
.isString()) {
121 /* Interpret this sequence as a String */
122 field
= new CTFStringField(seqDef
.toString(), fieldName
);
123 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
124 /* Sequence of integers => CTFIntegerArrayField */
125 long[] values
= new long[seqDef
.getLength()];
126 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
127 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
)).getValue();
129 field
= new CTFIntegerArrayField(values
, fieldName
);
131 /* Add other Sequence types here */
132 } else if (fieldDef
instanceof FloatDefinition
){
133 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
134 field
= new CTFFloatField( floatDef
.getValue(), fieldName
);
143 * @param other CtfTmfEventField
144 * @return CtfTmfEventField
146 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
147 switch (other
.getFieldType()) {
149 return new CTFIntegerField(((CTFIntegerField
) other
).getValue(),
152 return new CTFStringField(((CTFStringField
) other
).getValue(),
155 return new CTFIntegerArrayField(
156 ((CTFIntegerArrayField
) other
).getValue(), other
.name
);
158 return new CTFFloatField(
159 ((CTFFloatField
) other
).getValue(), other
.name
);
167 * @return CtfTmfEventField
168 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#clone()
171 public CtfTmfEventField
clone() {
172 return CtfTmfEventField
.copyFrom(this);
176 * Return the int representing this field's value type
179 * @return the field type */
180 public abstract int getFieldType();
183 * Return this field's value. You can cast it to the correct type depending
184 * on what getFieldType says.
187 * @return the field value * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
190 public abstract Object
getValue();
193 * Other methods defined by ITmfEventField, but not used here: the CTF
194 * fields do not have sub-fields (yet!)
197 * @return the field names * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
200 public String
[] getFieldNames() {
205 * Method getFieldName.
208 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
211 public String
getFieldName(int index
) {
217 * @return ITmfEventField[]
218 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
221 public ITmfEventField
[] getFields() {
227 * @param fieldName String
228 * @return ITmfEventField
229 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(String)
232 public ITmfEventField
getField(String fieldName
) {
239 * @return ITmfEventField
240 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
243 public ITmfEventField
getField(int index
) {
249 * <b><u>CTFIntegerField</u></b>
251 * @version $Revision: 1.0 $
253 final class CTFIntegerField
extends CtfTmfEventField
{
255 private final long longValue
;
258 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
259 * Java parser this is interpreted as a long.
260 * @param longValue long
263 CTFIntegerField(long longValue
, String name
) {
265 this.longValue
= longValue
;
269 * Method getFieldType.
273 public int getFieldType() {
280 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
283 public Long
getValue() {
284 return this.longValue
;
290 * @see java.lang.Object#toString()
293 public String
toString() {
294 return name
+ '=' + longValue
;
299 * <b><u>CTFStringField</u></b>
301 * @version $Revision: 1.0 $
303 final class CTFStringField
extends CtfTmfEventField
{
305 private final String strValue
;
308 * Constructor for CTFStringField.
309 * @param strValue String
312 CTFStringField(String strValue
, String name
) {
314 this.strValue
= strValue
;
318 * Method getFieldType.
322 public int getFieldType() {
329 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
332 public String
getValue() {
333 return this.strValue
;
339 * @see java.lang.Object#toString()
342 public String
toString() {
343 return name
+ '=' + strValue
;
348 * <b><u>CTFIntegerArrayField</u></b>
350 * @version $Revision: 1.0 $
352 final class CTFIntegerArrayField
extends CtfTmfEventField
{
354 private final long[] longValues
;
357 * Constructor for CTFIntegerArrayField.
358 * @param longValues long[]
361 CTFIntegerArrayField(long[] longValues
, String name
) {
363 this.longValues
= longValues
;
367 * Method getFieldType.
371 public int getFieldType() {
378 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
381 public long[] getValue() {
382 return this.longValues
;
390 public String
toString() {
391 StringBuffer buffer
= new StringBuffer();
392 buffer
.append("{ "); //$NON-NLS-1$
394 buffer
.append(longValues
[0]);
395 for (int i
= 1; i
< longValues
.length
; i
++) {
396 buffer
.append(", " + longValues
[i
]); //$NON-NLS-1$
399 return name
+ '=' + buffer
.toString();
405 final class CTFFloatField
extends CtfTmfEventField
{
409 * Constructor for CTFFloatField.
410 * @param value double
413 protected CTFFloatField(double value
,String name
) {
419 * Method getFieldType.
423 public int getFieldType() {
430 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
433 public Double
getValue() {
442 public String
toString(){
443 return name
+ '=' + value
;
447 /* Implement other possible fields types here... */