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 * <b><u>CTFEventField</u></b>
29 public abstract class CtfTmfEventField
implements ITmfEventField
{
31 // ------------------------------------------------------------------------
33 // ------------------------------------------------------------------------
35 protected final String name
;
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 * Constructor for CtfTmfEventField.
45 protected CtfTmfEventField(String name
) {
46 /* Strip the underscore*/
47 if ( name
.startsWith("_") ) { //$NON-NLS-1$
48 this.name
= name
.substring(1);
54 // ------------------------------------------------------------------------
55 // Getters/Setters/Predicates
56 // ------------------------------------------------------------------------
61 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
64 public String
getName() {
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
74 * @param fieldDef Definition
75 * @param fieldName String
76 * @return CtfTmfEventField
78 public static CtfTmfEventField
parseField(Definition fieldDef
,
80 CtfTmfEventField field
= null;
82 /* Determine the Definition type */
83 if (fieldDef
instanceof IntegerDefinition
) {
84 field
= new CTFIntegerField(
85 ((IntegerDefinition
) fieldDef
).getValue(), fieldName
);
87 } else if (fieldDef
instanceof StringDefinition
) {
88 field
= new CTFStringField(
89 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
91 } else if (fieldDef
instanceof ArrayDefinition
) {
92 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
93 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
95 if (arrayDef
.isString()) {
96 /* This is an array of UTF-8 bytes, a.k.a. a String! */
97 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
99 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
100 /* This is a an array of CTF Integers */
101 long[] values
= new long[arrayDecl
.getLength()];
102 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
103 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
)).getValue();
105 field
= new CTFIntegerArrayField(values
, fieldName
);
107 /* Add other types of arrays here */
109 } else if (fieldDef
instanceof SequenceDefinition
) {
110 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
111 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
113 if (seqDef
.getLength() == 0) {
114 /* Some sequences have length = 0. Simply use an empty string */
115 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
116 } else if (seqDef
.isString()) {
117 /* Interpret this sequence as a String */
118 field
= new CTFStringField(seqDef
.toString(), fieldName
);
119 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
120 /* Sequence of integers => CTFIntegerArrayField */
121 long[] values
= new long[seqDef
.getLength()];
122 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
123 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
)).getValue();
125 field
= new CTFIntegerArrayField(values
, fieldName
);
127 /* Add other Sequence types here */
128 } else if (fieldDef
instanceof FloatDefinition
){
129 FloatDefinition floatDef
= (FloatDefinition
) fieldDef
;
130 field
= new CTFFloatField( floatDef
.getValue(), fieldName
);
139 * @param other CtfTmfEventField
140 * @return CtfTmfEventField
142 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
143 switch (other
.getFieldType()) {
145 return new CTFIntegerField(((CTFIntegerField
) other
).getValue(),
148 return new CTFStringField(((CTFStringField
) other
).getValue(),
151 return new CTFIntegerArrayField(
152 ((CTFIntegerArrayField
) other
).getValue(), other
.name
);
154 return new CTFFloatField(
155 ((CTFFloatField
) other
).getValue(), other
.name
);
163 * @return CtfTmfEventField
164 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#clone()
167 public CtfTmfEventField
clone() {
168 return CtfTmfEventField
.copyFrom(this);
172 * Return the int representing this field's value type
175 * @return the field type */
176 public abstract int getFieldType();
179 * Return this field's value. You can cast it to the correct type depending
180 * on what getFieldType says.
183 * @return the field value * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
186 public abstract Object
getValue();
189 * Other methods defined by ITmfEventField, but not used here: the CTF
190 * fields do not have sub-fields (yet!)
193 * @return the field names * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
196 public String
[] getFieldNames() {
201 * Method getFieldName.
204 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
206 @SuppressWarnings("unused")
208 public String
getFieldName(int index
) {
214 * @return ITmfEventField[]
215 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
218 public ITmfEventField
[] getFields() {
224 * @param fieldName String
225 * @return ITmfEventField
226 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(String)
228 @SuppressWarnings("unused")
230 public ITmfEventField
getField(String fieldName
) {
237 * @return ITmfEventField
238 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
240 @SuppressWarnings("unused")
242 public ITmfEventField
getField(int index
) {
248 * <b><u>CTFIntegerField</u></b>
250 * @version $Revision: 1.0 $
252 final class CTFIntegerField
extends CtfTmfEventField
{
254 private final long longValue
;
257 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
258 * Java parser this is interpreted as a long.
259 * @param longValue long
262 CTFIntegerField(long longValue
, String name
) {
264 this.longValue
= longValue
;
268 * Method getFieldType.
272 public int getFieldType() {
279 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
282 public Long
getValue() {
283 return this.longValue
;
289 * @see java.lang.Object#toString()
292 public String
toString() {
293 return name
+ '=' + longValue
;
298 * <b><u>CTFStringField</u></b>
300 * @version $Revision: 1.0 $
302 final class CTFStringField
extends CtfTmfEventField
{
304 private final String strValue
;
307 * Constructor for CTFStringField.
308 * @param strValue String
311 CTFStringField(String strValue
, String name
) {
313 this.strValue
= strValue
;
317 * Method getFieldType.
321 public int getFieldType() {
328 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
331 public String
getValue() {
332 return this.strValue
;
338 * @see java.lang.Object#toString()
341 public String
toString() {
342 return name
+ '=' + strValue
;
347 * <b><u>CTFIntegerArrayField</u></b>
349 * @version $Revision: 1.0 $
351 final class CTFIntegerArrayField
extends CtfTmfEventField
{
353 private final long[] longValues
;
356 * Constructor for CTFIntegerArrayField.
357 * @param longValues long[]
360 CTFIntegerArrayField(long[] longValues
, String name
) {
362 this.longValues
= longValues
;
366 * Method getFieldType.
370 public int getFieldType() {
377 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
380 public long[] getValue() {
381 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 final class CTFFloatField
extends CtfTmfEventField
{
408 * Constructor for CTFFloatField.
409 * @param value double
412 protected CTFFloatField(double value
,String name
) {
418 * Method getFieldType.
422 public int getFieldType() {
429 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
432 public Double
getValue() {
441 public String
toString(){
442 return name
+ '=' + value
;
446 /* Implement other possible fields types here... */