Commit | Line | Data |
---|---|---|
a3fc8213 AM |
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; | |
a04464b1 | 18 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; |
a3fc8213 AM |
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; | |
25 | ||
26 | /** | |
27 | * <b><u>CTFEventField</u></b> | |
28 | */ | |
29 | public abstract class CtfTmfEventField implements ITmfEventField { | |
30 | ||
31 | // ------------------------------------------------------------------------ | |
32 | // Attributes | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
35 | protected final String name; | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Constructors | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
b1baa808 MK |
41 | /** |
42 | * Constructor for CtfTmfEventField. | |
43 | * @param name String | |
44 | */ | |
a3fc8213 | 45 | protected CtfTmfEventField(String name) { |
81c8e6f7 | 46 | /* Strip the underscore*/ |
f13dfe18 AM |
47 | if ( name.startsWith("_") ) { //$NON-NLS-1$ |
48 | this.name = name.substring(1); | |
49 | } else { | |
50 | this.name = name; | |
51 | } | |
a3fc8213 AM |
52 | } |
53 | ||
54 | // ------------------------------------------------------------------------ | |
55 | // Getters/Setters/Predicates | |
56 | // ------------------------------------------------------------------------ | |
57 | ||
b1baa808 MK |
58 | /** |
59 | * Method getName. | |
60 | * @return String | |
61 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName() | |
62 | */ | |
a3fc8213 AM |
63 | @Override |
64 | public String getName() { | |
65 | return this.name; | |
66 | } | |
67 | ||
68 | // ------------------------------------------------------------------------ | |
69 | // Operations | |
70 | // ------------------------------------------------------------------------ | |
71 | ||
b1baa808 MK |
72 | /** |
73 | * Method parseField. | |
74 | * @param fieldDef Definition | |
75 | * @param fieldName String | |
76 | * @return CtfTmfEventField | |
77 | */ | |
a3fc8213 AM |
78 | public static CtfTmfEventField parseField(Definition fieldDef, |
79 | String fieldName) { | |
80 | CtfTmfEventField field = null; | |
81 | ||
82 | /* Determine the Definition type */ | |
83 | if (fieldDef instanceof IntegerDefinition) { | |
84 | field = new CTFIntegerField( | |
85 | ((IntegerDefinition) fieldDef).getValue(), fieldName); | |
86 | ||
87 | } else if (fieldDef instanceof StringDefinition) { | |
88 | field = new CTFStringField( | |
89 | ((StringDefinition) fieldDef).getValue(), fieldName); | |
90 | ||
91 | } else if (fieldDef instanceof ArrayDefinition) { | |
92 | ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; | |
93 | ArrayDeclaration arrayDecl = arrayDef.getDeclaration(); | |
94 | ||
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); | |
98 | ||
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(); | |
104 | } | |
105 | field = new CTFIntegerArrayField(values, fieldName); | |
106 | } | |
107 | /* Add other types of arrays here */ | |
108 | ||
109 | } else if (fieldDef instanceof SequenceDefinition) { | |
110 | SequenceDefinition seqDef = (SequenceDefinition) fieldDef; | |
111 | SequenceDeclaration seqDecl = seqDef.getDeclaration(); | |
112 | ||
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(); | |
124 | } | |
125 | field = new CTFIntegerArrayField(values, fieldName); | |
126 | } | |
127 | /* Add other Sequence types here */ | |
a04464b1 MK |
128 | } else if (fieldDef instanceof FloatDefinition){ |
129 | FloatDefinition floatDef = (FloatDefinition) fieldDef; | |
130 | field = new CTFFloatField( floatDef.getValue(), fieldName); | |
a3fc8213 | 131 | } |
a04464b1 | 132 | |
a3fc8213 AM |
133 | |
134 | return field; | |
135 | } | |
136 | ||
b1baa808 MK |
137 | /** |
138 | * Method copyFrom. | |
139 | * @param other CtfTmfEventField | |
140 | * @return CtfTmfEventField | |
141 | */ | |
a3fc8213 AM |
142 | public static CtfTmfEventField copyFrom(CtfTmfEventField other) { |
143 | switch (other.getFieldType()) { | |
144 | case 0: | |
145 | return new CTFIntegerField(((CTFIntegerField) other).getValue(), | |
146 | other.name); | |
147 | case 1: | |
148 | return new CTFStringField(((CTFStringField) other).getValue(), | |
149 | other.name); | |
150 | case 2: | |
151 | return new CTFIntegerArrayField( | |
152 | ((CTFIntegerArrayField) other).getValue(), other.name); | |
81c8e6f7 MK |
153 | case 3: |
154 | return new CTFFloatField( | |
155 | ((CTFFloatField) other).getValue(), other.name); | |
a3fc8213 AM |
156 | default: |
157 | return null; | |
158 | } | |
159 | } | |
160 | ||
b1baa808 MK |
161 | /** |
162 | * Method clone. | |
163 | * @return CtfTmfEventField | |
164 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#clone() | |
165 | */ | |
a3fc8213 AM |
166 | @Override |
167 | public CtfTmfEventField clone() { | |
168 | return CtfTmfEventField.copyFrom(this); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Return the int representing this field's value type | |
ce2388e0 | 173 | * |
81c8e6f7 | 174 | |
b1baa808 | 175 | * @return the field type */ |
a3fc8213 AM |
176 | public abstract int getFieldType(); |
177 | ||
178 | /** | |
179 | * Return this field's value. You can cast it to the correct type depending | |
180 | * on what getFieldType says. | |
ce2388e0 | 181 | * |
81c8e6f7 | 182 | |
b1baa808 | 183 | * @return the field value * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue() |
a3fc8213 AM |
184 | */ |
185 | @Override | |
186 | public abstract Object getValue(); | |
187 | ||
188 | /** | |
0d9a6d76 | 189 | * Other methods defined by ITmfEventField, but not used here: the CTF |
a3fc8213 | 190 | * fields do not have sub-fields (yet!) |
ce2388e0 | 191 | * |
81c8e6f7 | 192 | |
b1baa808 | 193 | * @return the field names * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames() |
a3fc8213 | 194 | */ |
a3fc8213 AM |
195 | @Override |
196 | public String[] getFieldNames() { | |
197 | return null; | |
198 | } | |
199 | ||
b1baa808 MK |
200 | /** |
201 | * Method getFieldName. | |
202 | * @param index int | |
203 | * @return String | |
204 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int) | |
205 | */ | |
a3fc8213 AM |
206 | @Override |
207 | public String getFieldName(int index) { | |
208 | return null; | |
209 | } | |
210 | ||
b1baa808 MK |
211 | /** |
212 | * Method getFields. | |
213 | * @return ITmfEventField[] | |
214 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields() | |
215 | */ | |
a3fc8213 AM |
216 | @Override |
217 | public ITmfEventField[] getFields() { | |
218 | return null; | |
219 | } | |
220 | ||
b1baa808 MK |
221 | /** |
222 | * Method getField. | |
223 | * @param fieldName String | |
224 | * @return ITmfEventField | |
225 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(String) | |
226 | */ | |
a3fc8213 AM |
227 | @Override |
228 | public ITmfEventField getField(String fieldName) { | |
229 | return null; | |
230 | } | |
231 | ||
b1baa808 MK |
232 | /** |
233 | * Method getField. | |
234 | * @param index int | |
235 | * @return ITmfEventField | |
236 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int) | |
237 | */ | |
a3fc8213 AM |
238 | @Override |
239 | public ITmfEventField getField(int index) { | |
240 | return null; | |
241 | } | |
242 | } | |
243 | ||
244 | /** | |
245 | * <b><u>CTFIntegerField</u></b> | |
b1baa808 MK |
246 | * @author ematkho |
247 | * @version $Revision: 1.0 $ | |
a3fc8213 AM |
248 | */ |
249 | final class CTFIntegerField extends CtfTmfEventField { | |
250 | ||
251 | private final long longValue; | |
252 | ||
253 | /** | |
254 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the | |
255 | * Java parser this is interpreted as a long. | |
b1baa808 MK |
256 | * @param longValue long |
257 | * @param name String | |
a3fc8213 AM |
258 | */ |
259 | CTFIntegerField(long longValue, String name) { | |
260 | super(name); | |
261 | this.longValue = longValue; | |
262 | } | |
263 | ||
b1baa808 MK |
264 | /** |
265 | * Method getFieldType. | |
266 | * @return int | |
267 | */ | |
a3fc8213 AM |
268 | @Override |
269 | public int getFieldType() { | |
270 | return 0; | |
271 | } | |
272 | ||
b1baa808 MK |
273 | /** |
274 | * Method getValue. | |
275 | * @return Long | |
276 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue() | |
277 | */ | |
a3fc8213 AM |
278 | @Override |
279 | public Long getValue() { | |
280 | return this.longValue; | |
281 | } | |
282 | ||
283 | /* | |
284 | * (non-Javadoc) | |
ce2388e0 | 285 | * |
a3fc8213 AM |
286 | * @see java.lang.Object#toString() |
287 | */ | |
288 | @Override | |
289 | public String toString() { | |
290 | return name + '=' + longValue; | |
291 | } | |
292 | } | |
293 | ||
294 | /** | |
295 | * <b><u>CTFStringField</u></b> | |
b1baa808 MK |
296 | * @author ematkho |
297 | * @version $Revision: 1.0 $ | |
a3fc8213 AM |
298 | */ |
299 | final class CTFStringField extends CtfTmfEventField { | |
300 | ||
301 | private final String strValue; | |
302 | ||
b1baa808 MK |
303 | /** |
304 | * Constructor for CTFStringField. | |
305 | * @param strValue String | |
306 | * @param name String | |
307 | */ | |
a3fc8213 AM |
308 | CTFStringField(String strValue, String name) { |
309 | super(name); | |
310 | this.strValue = strValue; | |
311 | } | |
312 | ||
b1baa808 MK |
313 | /** |
314 | * Method getFieldType. | |
315 | * @return int | |
316 | */ | |
a3fc8213 AM |
317 | @Override |
318 | public int getFieldType() { | |
319 | return 1; | |
320 | } | |
321 | ||
b1baa808 MK |
322 | /** |
323 | * Method getValue. | |
324 | * @return String | |
325 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue() | |
326 | */ | |
a3fc8213 AM |
327 | @Override |
328 | public String getValue() { | |
329 | return this.strValue; | |
330 | } | |
331 | ||
332 | /* | |
333 | * (non-Javadoc) | |
ce2388e0 | 334 | * |
a3fc8213 AM |
335 | * @see java.lang.Object#toString() |
336 | */ | |
337 | @Override | |
338 | public String toString() { | |
339 | return name + '=' + strValue; | |
340 | } | |
341 | } | |
342 | ||
343 | /** | |
344 | * <b><u>CTFIntegerArrayField</u></b> | |
b1baa808 MK |
345 | * @author ematkho |
346 | * @version $Revision: 1.0 $ | |
a3fc8213 AM |
347 | */ |
348 | final class CTFIntegerArrayField extends CtfTmfEventField { | |
349 | ||
350 | private final long[] longValues; | |
351 | ||
b1baa808 MK |
352 | /** |
353 | * Constructor for CTFIntegerArrayField. | |
354 | * @param longValues long[] | |
355 | * @param name String | |
356 | */ | |
a3fc8213 AM |
357 | CTFIntegerArrayField(long[] longValues, String name) { |
358 | super(name); | |
359 | this.longValues = longValues; | |
360 | } | |
361 | ||
b1baa808 MK |
362 | /** |
363 | * Method getFieldType. | |
364 | * @return int | |
365 | */ | |
a3fc8213 AM |
366 | @Override |
367 | public int getFieldType() { | |
368 | return 2; | |
369 | } | |
370 | ||
b1baa808 MK |
371 | /** |
372 | * Method getValue. | |
373 | * @return long[] | |
374 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue() | |
375 | */ | |
a3fc8213 AM |
376 | @Override |
377 | public long[] getValue() { | |
378 | return this.longValues; | |
379 | } | |
380 | ||
b1baa808 MK |
381 | /** |
382 | * Method toString. | |
383 | * @return String | |
384 | */ | |
a3fc8213 AM |
385 | @Override |
386 | public String toString() { | |
387 | StringBuffer buffer = new StringBuffer(); | |
388 | buffer.append("{ "); //$NON-NLS-1$ | |
389 | ||
390 | buffer.append(longValues[0]); | |
391 | for (int i = 1; i < longValues.length; i++) { | |
392 | buffer.append(", " + longValues[i]); //$NON-NLS-1$ | |
393 | } | |
394 | buffer.append('}'); | |
395 | return name + '=' + buffer.toString(); | |
396 | } | |
397 | } | |
398 | ||
b1baa808 MK |
399 | /** |
400 | */ | |
a04464b1 MK |
401 | final class CTFFloatField extends CtfTmfEventField { |
402 | ||
403 | Double value; | |
b1baa808 MK |
404 | /** |
405 | * Constructor for CTFFloatField. | |
406 | * @param value double | |
407 | * @param name String | |
408 | */ | |
a04464b1 MK |
409 | protected CTFFloatField(double value ,String name) { |
410 | super(name); | |
411 | this.value = value; | |
412 | } | |
413 | ||
b1baa808 MK |
414 | /** |
415 | * Method getFieldType. | |
416 | * @return int | |
417 | */ | |
a04464b1 MK |
418 | @Override |
419 | public int getFieldType() { | |
420 | return 3; | |
421 | } | |
422 | ||
b1baa808 MK |
423 | /** |
424 | * Method getValue. | |
425 | * @return Object | |
426 | * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue() | |
427 | */ | |
a04464b1 | 428 | @Override |
81c8e6f7 | 429 | public Double getValue() { |
a04464b1 MK |
430 | return this.value; |
431 | } | |
432 | ||
b1baa808 MK |
433 | /** |
434 | * Method toString. | |
435 | * @return String | |
436 | */ | |
a04464b1 MK |
437 | @Override |
438 | public String toString(){ | |
439 | return name + '=' + value; | |
440 | } | |
441 | ||
442 | } | |
a3fc8213 | 443 | /* Implement other possible fields types here... */ |