Integrate CtfAdaptor package in TMF
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventField.java
1 /*******************************************************************************
2 * Copyright (c) 2011 Ericsson
3 *
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
8 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Alexendre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
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;
24
25 /**
26 * <b><u>CTFEventField</u></b>
27 */
28 public abstract class CtfTmfEventField implements ITmfEventField {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 protected final String name;
35
36 // ------------------------------------------------------------------------
37 // Constructors
38 // ------------------------------------------------------------------------
39
40 protected CtfTmfEventField(String name) {
41 this.name = name;
42 }
43
44 // ------------------------------------------------------------------------
45 // Getters/Setters/Predicates
46 // ------------------------------------------------------------------------
47
48 @Override
49 public String getName() {
50 return this.name;
51 }
52
53 // ------------------------------------------------------------------------
54 // Operations
55 // ------------------------------------------------------------------------
56
57 public static CtfTmfEventField parseField(Definition fieldDef,
58 String fieldName) {
59 CtfTmfEventField field = null;
60
61 /* Determine the Definition type */
62 if (fieldDef instanceof IntegerDefinition) {
63 field = new CTFIntegerField(
64 ((IntegerDefinition) fieldDef).getValue(), fieldName);
65
66 } else if (fieldDef instanceof StringDefinition) {
67 field = new CTFStringField(
68 ((StringDefinition) fieldDef).getValue(), fieldName);
69
70 } else if (fieldDef instanceof ArrayDefinition) {
71 ArrayDefinition arrayDef = (ArrayDefinition) fieldDef;
72 ArrayDeclaration arrayDecl = arrayDef.getDeclaration();
73
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);
77
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();
83 }
84 field = new CTFIntegerArrayField(values, fieldName);
85 }
86 /* Add other types of arrays here */
87
88 } else if (fieldDef instanceof SequenceDefinition) {
89 SequenceDefinition seqDef = (SequenceDefinition) fieldDef;
90 SequenceDeclaration seqDecl = seqDef.getDeclaration();
91
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();
103 }
104 field = new CTFIntegerArrayField(values, fieldName);
105 }
106 /* Add other Sequence types here */
107 }
108 /* Add other field types here */
109
110 return field;
111 }
112
113 public static CtfTmfEventField copyFrom(CtfTmfEventField other) {
114 switch (other.getFieldType()) {
115 case 0:
116 return new CTFIntegerField(((CTFIntegerField) other).getValue(),
117 other.name);
118 case 1:
119 return new CTFStringField(((CTFStringField) other).getValue(),
120 other.name);
121 case 2:
122 return new CTFIntegerArrayField(
123 ((CTFIntegerArrayField) other).getValue(), other.name);
124 default:
125 return null;
126 }
127 }
128
129 @Override
130 public CtfTmfEventField clone() {
131 return CtfTmfEventField.copyFrom(this);
132 }
133
134 /**
135 * Return the int representing this field's value type
136 *
137 * @return
138 */
139 public abstract int getFieldType();
140
141 /**
142 * Return this field's value. You can cast it to the correct type depending
143 * on what getFieldType says.
144 *
145 * @return
146 */
147 @Override
148 public abstract Object getValue();
149
150 /**
151 * @name Other methods defined by ITmfEventField, but not used here: the CTF
152 * fields do not have sub-fields (yet!)
153 */
154
155 @Override
156 public String[] getFieldNames() {
157 return null;
158 }
159
160 @Override
161 public String getFieldName(int index) {
162 return null;
163 }
164
165 @Override
166 public ITmfEventField[] getFields() {
167 return null;
168 }
169
170 @Override
171 public ITmfEventField getField(String fieldName) {
172 return null;
173 }
174
175 @Override
176 public ITmfEventField getField(int index) {
177 return null;
178 }
179 }
180
181 /**
182 * <b><u>CTFIntegerField</u></b>
183 */
184 final class CTFIntegerField extends CtfTmfEventField {
185
186 private final long longValue;
187
188 /**
189 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
190 * Java parser this is interpreted as a long.
191 */
192 CTFIntegerField(long longValue, String name) {
193 super(name);
194 this.longValue = longValue;
195 }
196
197 @Override
198 public int getFieldType() {
199 return 0;
200 }
201
202 @Override
203 public Long getValue() {
204 return this.longValue;
205 }
206
207 /*
208 * (non-Javadoc)
209 *
210 * @see java.lang.Object#toString()
211 */
212 @Override
213 public String toString() {
214 return name + '=' + longValue;
215 }
216 }
217
218 /**
219 * <b><u>CTFStringField</u></b>
220 */
221 final class CTFStringField extends CtfTmfEventField {
222
223 private final String strValue;
224
225 CTFStringField(String strValue, String name) {
226 super(name);
227 this.strValue = strValue;
228 }
229
230 @Override
231 public int getFieldType() {
232 return 1;
233 }
234
235 @Override
236 public String getValue() {
237 return this.strValue;
238 }
239
240 /*
241 * (non-Javadoc)
242 *
243 * @see java.lang.Object#toString()
244 */
245 @Override
246 public String toString() {
247 return name + '=' + strValue;
248 }
249 }
250
251 /**
252 * <b><u>CTFIntegerArrayField</u></b>
253 */
254 final class CTFIntegerArrayField extends CtfTmfEventField {
255
256 private final long[] longValues;
257
258 CTFIntegerArrayField(long[] longValues, String name) {
259 super(name);
260 this.longValues = longValues;
261 }
262
263 @Override
264 public int getFieldType() {
265 return 2;
266 }
267
268 @Override
269 public long[] getValue() {
270 return this.longValues;
271 }
272
273 @Override
274 public String toString() {
275 StringBuffer buffer = new StringBuffer();
276 buffer.append("{ "); //$NON-NLS-1$
277
278 buffer.append(longValues[0]);
279 for (int i = 1; i < longValues.length; i++) {
280 buffer.append(", " + longValues[i]); //$NON-NLS-1$
281 }
282 buffer.append('}');
283 return name + '=' + buffer.toString();
284 }
285 }
286
287 /* Implement other possible fields types here... */
This page took 0.038902 seconds and 6 git commands to generate.