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
.IntegerDeclaration
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SequenceDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StringDefinition
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
26 * <b><u>CTFEventField</u></b>
28 public abstract class CtfTmfEventField
implements ITmfEventField
{
30 // ------------------------------------------------------------------------
32 // ------------------------------------------------------------------------
34 protected final String name
;
36 // ------------------------------------------------------------------------
38 // ------------------------------------------------------------------------
40 protected CtfTmfEventField(String name
) {
44 // ------------------------------------------------------------------------
45 // Getters/Setters/Predicates
46 // ------------------------------------------------------------------------
49 public String
getName() {
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 public static CtfTmfEventField
parseField(Definition fieldDef
,
59 CtfTmfEventField field
= null;
61 /* Determine the Definition type */
62 if (fieldDef
instanceof IntegerDefinition
) {
63 field
= new CTFIntegerField(
64 ((IntegerDefinition
) fieldDef
).getValue(), fieldName
);
66 } else if (fieldDef
instanceof StringDefinition
) {
67 field
= new CTFStringField(
68 ((StringDefinition
) fieldDef
).getValue(), fieldName
);
70 } else if (fieldDef
instanceof ArrayDefinition
) {
71 ArrayDefinition arrayDef
= (ArrayDefinition
) fieldDef
;
72 ArrayDeclaration arrayDecl
= arrayDef
.getDeclaration();
74 if (arrayDef
.isString()) {
75 /* This is an array of UTF-8 bytes, a.k.a. a String! */
76 field
= new CTFStringField(fieldDef
.toString(), fieldName
);
78 } else if (arrayDecl
.getElementType() instanceof IntegerDeclaration
) {
79 /* This is a an array of CTF Integers */
80 long[] values
= new long[arrayDecl
.getLength()];
81 for (int i
= 0; i
< arrayDecl
.getLength(); i
++) {
82 values
[i
] = ((IntegerDefinition
) arrayDef
.getElem(i
)).getValue();
84 field
= new CTFIntegerArrayField(values
, fieldName
);
86 /* Add other types of arrays here */
88 } else if (fieldDef
instanceof SequenceDefinition
) {
89 SequenceDefinition seqDef
= (SequenceDefinition
) fieldDef
;
90 SequenceDeclaration seqDecl
= seqDef
.getDeclaration();
92 if (seqDef
.getLength() == 0) {
93 /* Some sequences have length = 0. Simply use an empty string */
94 field
= new CTFStringField("", fieldName
); //$NON-NLS-1$
95 } else if (seqDef
.isString()) {
96 /* Interpret this sequence as a String */
97 field
= new CTFStringField(seqDef
.toString(), fieldName
);
98 } else if (seqDecl
.getElementType() instanceof IntegerDeclaration
) {
99 /* Sequence of integers => CTFIntegerArrayField */
100 long[] values
= new long[seqDef
.getLength()];
101 for (int i
= 0; i
< seqDef
.getLength(); i
++) {
102 values
[i
] = ((IntegerDefinition
) seqDef
.getElem(i
)).getValue();
104 field
= new CTFIntegerArrayField(values
, fieldName
);
106 /* Add other Sequence types here */
108 /* Add other field types here */
113 public static CtfTmfEventField
copyFrom(CtfTmfEventField other
) {
114 switch (other
.getFieldType()) {
116 return new CTFIntegerField(((CTFIntegerField
) other
).getValue(),
119 return new CTFStringField(((CTFStringField
) other
).getValue(),
122 return new CTFIntegerArrayField(
123 ((CTFIntegerArrayField
) other
).getValue(), other
.name
);
130 public CtfTmfEventField
clone() {
131 return CtfTmfEventField
.copyFrom(this);
135 * Return the int representing this field's value type
139 public abstract int getFieldType();
142 * Return this field's value. You can cast it to the correct type depending
143 * on what getFieldType says.
148 public abstract Object
getValue();
151 * @name Other methods defined by ITmfEventField, but not used here: the CTF
152 * fields do not have sub-fields (yet!)
156 public String
[] getFieldNames() {
161 public String
getFieldName(int index
) {
166 public ITmfEventField
[] getFields() {
171 public ITmfEventField
getField(String fieldName
) {
176 public ITmfEventField
getField(int index
) {
182 * <b><u>CTFIntegerField</u></b>
184 final class CTFIntegerField
extends CtfTmfEventField
{
186 private final long longValue
;
189 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
190 * Java parser this is interpreted as a long.
192 CTFIntegerField(long longValue
, String name
) {
194 this.longValue
= longValue
;
198 public int getFieldType() {
203 public Long
getValue() {
204 return this.longValue
;
210 * @see java.lang.Object#toString()
213 public String
toString() {
214 return name
+ '=' + longValue
;
219 * <b><u>CTFStringField</u></b>
221 final class CTFStringField
extends CtfTmfEventField
{
223 private final String strValue
;
225 CTFStringField(String strValue
, String name
) {
227 this.strValue
= strValue
;
231 public int getFieldType() {
236 public String
getValue() {
237 return this.strValue
;
243 * @see java.lang.Object#toString()
246 public String
toString() {
247 return name
+ '=' + strValue
;
252 * <b><u>CTFIntegerArrayField</u></b>
254 final class CTFIntegerArrayField
extends CtfTmfEventField
{
256 private final long[] longValues
;
258 CTFIntegerArrayField(long[] longValues
, String name
) {
260 this.longValues
= longValues
;
264 public int getFieldType() {
269 public long[] getValue() {
270 return this.longValues
;
274 public String
toString() {
275 StringBuffer buffer
= new StringBuffer();
276 buffer
.append("{ "); //$NON-NLS-1$
278 buffer
.append(longValues
[0]);
279 for (int i
= 1; i
< longValues
.length
; i
++) {
280 buffer
.append(", " + longValues
[i
]); //$NON-NLS-1$
283 return name
+ '=' + buffer
.toString();
287 /* Implement other possible fields types here... */