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 | * | |
d4a8d935 BH |
9 | * Contributors: |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | * Alexendre Montplaisir - Initial API and implementation | |
12 | * Bernd Hufmann - Add Enum field handling | |
13 | * | |
a3fc8213 AM |
14 | *******************************************************************************/ |
15 | ||
16 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; | |
17 | ||
18 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; | |
19 | import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; | |
20 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; | |
21fb02fa | 21 | import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition; |
a04464b1 | 22 | import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition; |
a3fc8213 AM |
23 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; |
24 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; | |
25 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; | |
26 | import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; | |
27 | import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; | |
28 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
29 | ||
30 | /** | |
7558a1e1 AM |
31 | * The CTF implementation of the TMF event field model |
32 | * | |
d4a8d935 | 33 | * @version 2.0 |
7558a1e1 | 34 | * @author Matthew Khouzam |
d09f973b | 35 | * @author Alexandre Montplaisir |
a3fc8213 AM |
36 | */ |
37 | public abstract class CtfTmfEventField implements ITmfEventField { | |
38 | ||
a1c6baa7 AM |
39 | // ------------------------------------------------------------------------ |
40 | // Class attributes | |
41 | // ------------------------------------------------------------------------ | |
42 | ||
273c7f30 | 43 | /** @since 2.0 */ |
a1c6baa7 AM |
44 | protected static final int FIELDTYPE_INTEGER = 0; |
45 | ||
273c7f30 | 46 | /** @since 2.0 */ |
a1c6baa7 AM |
47 | protected static final int FIELDTYPE_STRING = 1; |
48 | ||
273c7f30 | 49 | /** @since 2.0 */ |
a1c6baa7 AM |
50 | protected static final int FIELDTYPE_INTEGER_ARRAY = 2; |
51 | ||
273c7f30 | 52 | /** @since 2.0 */ |
a1c6baa7 AM |
53 | protected static final int FIELDTYPE_FLOAT = 3; |
54 | ||
d4a8d935 BH |
55 | /** @since 2.0 */ |
56 | protected static final int FIELDTYPE_ENUM = 4; | |
57 | ||
a3fc8213 AM |
58 | // ------------------------------------------------------------------------ |
59 | // Attributes | |
60 | // ------------------------------------------------------------------------ | |
61 | ||
6f4e8ec0 | 62 | /** The name of this field */ |
a3fc8213 AM |
63 | protected final String name; |
64 | ||
65 | // ------------------------------------------------------------------------ | |
7558a1e1 | 66 | // Constructor |
a3fc8213 AM |
67 | // ------------------------------------------------------------------------ |
68 | ||
b1baa808 | 69 | /** |
7558a1e1 AM |
70 | * Standard constructor. Only to be used internally, call parseField() to |
71 | * generate a new field object. | |
72 | * | |
73 | * @param name | |
74 | * The name of this field | |
b1baa808 | 75 | */ |
a3fc8213 | 76 | protected CtfTmfEventField(String name) { |
7558a1e1 | 77 | /* Strip the underscore */ |
21fb02fa | 78 | if (name.startsWith("_")) { //$NON-NLS-1$ |
f13dfe18 AM |
79 | this.name = name.substring(1); |
80 | } else { | |
81 | this.name = name; | |
82 | } | |
a3fc8213 AM |
83 | } |
84 | ||
85 | // ------------------------------------------------------------------------ | |
86 | // Getters/Setters/Predicates | |
87 | // ------------------------------------------------------------------------ | |
88 | ||
89 | @Override | |
90 | public String getName() { | |
91 | return this.name; | |
92 | } | |
93 | ||
94 | // ------------------------------------------------------------------------ | |
95 | // Operations | |
96 | // ------------------------------------------------------------------------ | |
97 | ||
b1baa808 | 98 | /** |
7558a1e1 AM |
99 | * Factory method to instantiate CtfTmfEventField objects. |
100 | * | |
101 | * @param fieldDef | |
102 | * The CTF Definition of this event field | |
103 | * @param fieldName | |
104 | * String The name to assign to this field | |
105 | * @return The resulting CtfTmfEventField object | |
b1baa808 | 106 | */ |
a3fc8213 AM |
107 | public static CtfTmfEventField parseField(Definition fieldDef, |
108 | String fieldName) { | |
109 | CtfTmfEventField field = null; | |
110 | ||
111 | /* Determine the Definition type */ | |
112 | if (fieldDef instanceof IntegerDefinition) { | |
367bcd2b AM |
113 | IntegerDefinition intDef = (IntegerDefinition) fieldDef; |
114 | int base = intDef.getDeclaration().getBase(); | |
115 | field = new CTFIntegerField(intDef.getValue(), fieldName, base); | |
a3fc8213 | 116 | |
21fb02fa MK |
117 | } else if (fieldDef instanceof EnumDefinition) { |
118 | EnumDefinition enumDef = (EnumDefinition) fieldDef; | |
d4a8d935 | 119 | field = new CTFEnumField(new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()), fieldName); |
21fb02fa | 120 | |
a3fc8213 AM |
121 | } else if (fieldDef instanceof StringDefinition) { |
122 | field = new CTFStringField( | |
123 | ((StringDefinition) fieldDef).getValue(), fieldName); | |
124 | ||
125 | } else if (fieldDef instanceof ArrayDefinition) { | |
126 | ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; | |
127 | ArrayDeclaration arrayDecl = arrayDef.getDeclaration(); | |
128 | ||
129 | if (arrayDef.isString()) { | |
130 | /* This is an array of UTF-8 bytes, a.k.a. a String! */ | |
131 | field = new CTFStringField(fieldDef.toString(), fieldName); | |
132 | ||
133 | } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) { | |
134 | /* This is a an array of CTF Integers */ | |
135 | long[] values = new long[arrayDecl.getLength()]; | |
136 | for (int i = 0; i < arrayDecl.getLength(); i++) { | |
21fb02fa MK |
137 | values[i] = ((IntegerDefinition) arrayDef.getElem(i)) |
138 | .getValue(); | |
a3fc8213 AM |
139 | } |
140 | field = new CTFIntegerArrayField(values, fieldName); | |
141 | } | |
142 | /* Add other types of arrays here */ | |
143 | ||
144 | } else if (fieldDef instanceof SequenceDefinition) { | |
145 | SequenceDefinition seqDef = (SequenceDefinition) fieldDef; | |
146 | SequenceDeclaration seqDecl = seqDef.getDeclaration(); | |
147 | ||
148 | if (seqDef.getLength() == 0) { | |
149 | /* Some sequences have length = 0. Simply use an empty string */ | |
150 | field = new CTFStringField("", fieldName); //$NON-NLS-1$ | |
151 | } else if (seqDef.isString()) { | |
152 | /* Interpret this sequence as a String */ | |
153 | field = new CTFStringField(seqDef.toString(), fieldName); | |
154 | } else if (seqDecl.getElementType() instanceof IntegerDeclaration) { | |
155 | /* Sequence of integers => CTFIntegerArrayField */ | |
156 | long[] values = new long[seqDef.getLength()]; | |
157 | for (int i = 0; i < seqDef.getLength(); i++) { | |
21fb02fa MK |
158 | values[i] = ((IntegerDefinition) seqDef.getElem(i)) |
159 | .getValue(); | |
a3fc8213 AM |
160 | } |
161 | field = new CTFIntegerArrayField(values, fieldName); | |
162 | } | |
163 | /* Add other Sequence types here */ | |
367bcd2b | 164 | |
21fb02fa | 165 | } else if (fieldDef instanceof FloatDefinition) { |
a04464b1 | 166 | FloatDefinition floatDef = (FloatDefinition) fieldDef; |
21fb02fa | 167 | field = new CTFFloatField(floatDef.getValue(), fieldName); |
a3fc8213 | 168 | } |
a04464b1 | 169 | |
a3fc8213 AM |
170 | return field; |
171 | } | |
172 | ||
b1baa808 | 173 | /** |
7558a1e1 AM |
174 | * Copy factory. Create a new field by (deep-) copying the information in an |
175 | * existing one. | |
176 | * | |
177 | * @param other | |
178 | * The other CtfTmfEventField to copy | |
179 | * @return The new CtfTmfEventField | |
b1baa808 | 180 | */ |
a3fc8213 AM |
181 | public static CtfTmfEventField copyFrom(CtfTmfEventField other) { |
182 | switch (other.getFieldType()) { | |
a1c6baa7 | 183 | case FIELDTYPE_INTEGER: |
367bcd2b | 184 | CTFIntegerField intOther = (CTFIntegerField) other; |
21fb02fa MK |
185 | return new CTFIntegerField(intOther.getValue(), intOther.name, |
186 | intOther.getBase()); | |
a1c6baa7 | 187 | case FIELDTYPE_STRING: |
21fb02fa MK |
188 | return new CTFStringField(((CTFStringField) other).getValue(), |
189 | other.name); | |
a1c6baa7 | 190 | case FIELDTYPE_INTEGER_ARRAY: |
21fb02fa MK |
191 | return new CTFIntegerArrayField( |
192 | ((CTFIntegerArrayField) other).getValue(), other.name); | |
a1c6baa7 | 193 | case FIELDTYPE_FLOAT: |
21fb02fa MK |
194 | return new CTFFloatField(((CTFFloatField) other).getValue(), |
195 | other.name); | |
d4a8d935 BH |
196 | case FIELDTYPE_ENUM: |
197 | return new CTFEnumField(((CTFEnumField) other).getValue(), other.name); | |
a3fc8213 AM |
198 | default: |
199 | return null; | |
200 | } | |
201 | } | |
202 | ||
203 | @Override | |
204 | public CtfTmfEventField clone() { | |
205 | return CtfTmfEventField.copyFrom(this); | |
206 | } | |
207 | ||
7558a1e1 AM |
208 | // ------------------------------------------------------------------------ |
209 | // Abstract methods (to be implemented by each specific field type) | |
210 | // ------------------------------------------------------------------------ | |
211 | ||
a3fc8213 AM |
212 | /** |
213 | * Return the int representing this field's value type | |
ce2388e0 | 214 | * |
7558a1e1 AM |
215 | * @return The field type |
216 | */ | |
a3fc8213 AM |
217 | public abstract int getFieldType(); |
218 | ||
219 | /** | |
220 | * Return this field's value. You can cast it to the correct type depending | |
221 | * on what getFieldType says. | |
ce2388e0 | 222 | * |
7558a1e1 | 223 | * @return The field's value |
a3fc8213 AM |
224 | */ |
225 | @Override | |
226 | public abstract Object getValue(); | |
227 | ||
7558a1e1 AM |
228 | // ------------------------------------------------------------------------ |
229 | // Other methods defined by ITmfEventField, but not used here. | |
230 | // CTF fields do not have sub-fields (yet!) | |
231 | // ------------------------------------------------------------------------ | |
81c8e6f7 | 232 | |
a3fc8213 AM |
233 | @Override |
234 | public String[] getFieldNames() { | |
235 | return null; | |
236 | } | |
237 | ||
238 | @Override | |
239 | public String getFieldName(int index) { | |
240 | return null; | |
241 | } | |
242 | ||
243 | @Override | |
244 | public ITmfEventField[] getFields() { | |
245 | return null; | |
246 | } | |
247 | ||
248 | @Override | |
249 | public ITmfEventField getField(String fieldName) { | |
250 | return null; | |
251 | } | |
252 | ||
253 | @Override | |
254 | public ITmfEventField getField(int index) { | |
255 | return null; | |
256 | } | |
257 | } | |
258 | ||
259 | /** | |
7558a1e1 AM |
260 | * The CTF field implementation for integer fields. |
261 | * | |
262 | * @author alexmont | |
a3fc8213 AM |
263 | */ |
264 | final class CTFIntegerField extends CtfTmfEventField { | |
265 | ||
266 | private final long longValue; | |
367bcd2b | 267 | private final int base; |
a3fc8213 AM |
268 | |
269 | /** | |
270 | * A CTF "IntegerDefinition" can be an integer of any byte size, so in the | |
271 | * Java parser this is interpreted as a long. | |
7558a1e1 AM |
272 | * |
273 | * @param longValue | |
274 | * The integer value of this field | |
275 | * @param name | |
276 | * The name of this field | |
a3fc8213 | 277 | */ |
367bcd2b | 278 | CTFIntegerField(long longValue, String name, int base) { |
a3fc8213 AM |
279 | super(name); |
280 | this.longValue = longValue; | |
367bcd2b AM |
281 | this.base = base; |
282 | } | |
283 | ||
284 | /** | |
285 | * Return the integer's base. (Not made public until it's needed.) | |
286 | * | |
287 | * @return The base, usually 10 or 16. | |
288 | */ | |
289 | int getBase() { | |
290 | return base; | |
a3fc8213 AM |
291 | } |
292 | ||
293 | @Override | |
294 | public int getFieldType() { | |
a1c6baa7 | 295 | return FIELDTYPE_INTEGER; |
a3fc8213 AM |
296 | } |
297 | ||
298 | @Override | |
299 | public Long getValue() { | |
300 | return this.longValue; | |
301 | } | |
302 | ||
a3fc8213 AM |
303 | @Override |
304 | public String toString() { | |
367bcd2b AM |
305 | StringBuilder sb = new StringBuilder(name); |
306 | sb.append('='); | |
307 | ||
308 | /* Format the number correctly according to the integer's base */ | |
309 | switch (base) { | |
310 | case 2: | |
311 | sb.append("0b"); //$NON-NLS-1$ | |
312 | sb.append(Long.toBinaryString(longValue)); | |
313 | break; | |
314 | case 8: | |
315 | sb.append('0'); | |
316 | sb.append(Long.toOctalString(longValue)); | |
317 | break; | |
318 | case 10: | |
319 | sb.append(longValue); | |
320 | break; | |
321 | case 16: | |
322 | sb.append("0x"); //$NON-NLS-1$ | |
323 | sb.append(Long.toHexString(longValue)); | |
324 | break; | |
325 | default: | |
326 | /* Non-standard base, we'll just print it as a decimal number */ | |
327 | sb.append(longValue); | |
328 | break; | |
329 | } | |
330 | return sb.toString(); | |
a3fc8213 AM |
331 | } |
332 | } | |
333 | ||
334 | /** | |
7558a1e1 AM |
335 | * The CTF field implementation for string fields |
336 | * | |
337 | * @author alexmont | |
a3fc8213 AM |
338 | */ |
339 | final class CTFStringField extends CtfTmfEventField { | |
340 | ||
341 | private final String strValue; | |
342 | ||
b1baa808 MK |
343 | /** |
344 | * Constructor for CTFStringField. | |
7558a1e1 AM |
345 | * |
346 | * @param strValue | |
347 | * The string value of this field | |
348 | * @param name | |
349 | * The name of this field | |
b1baa808 | 350 | */ |
a3fc8213 AM |
351 | CTFStringField(String strValue, String name) { |
352 | super(name); | |
353 | this.strValue = strValue; | |
354 | } | |
355 | ||
356 | @Override | |
357 | public int getFieldType() { | |
a1c6baa7 | 358 | return FIELDTYPE_STRING; |
a3fc8213 AM |
359 | } |
360 | ||
361 | @Override | |
362 | public String getValue() { | |
363 | return this.strValue; | |
364 | } | |
365 | ||
a3fc8213 AM |
366 | @Override |
367 | public String toString() { | |
368 | return name + '=' + strValue; | |
369 | } | |
370 | } | |
371 | ||
372 | /** | |
7558a1e1 AM |
373 | * CTF field implementation for arrays of integers. |
374 | * | |
375 | * @author alexmont | |
a3fc8213 AM |
376 | */ |
377 | final class CTFIntegerArrayField extends CtfTmfEventField { | |
378 | ||
379 | private final long[] longValues; | |
380 | ||
b1baa808 MK |
381 | /** |
382 | * Constructor for CTFIntegerArrayField. | |
7558a1e1 AM |
383 | * |
384 | * @param longValues | |
385 | * The array of integers (as longs) that compose this field's | |
386 | * value | |
387 | * @param name | |
388 | * The name of this field | |
b1baa808 | 389 | */ |
a3fc8213 AM |
390 | CTFIntegerArrayField(long[] longValues, String name) { |
391 | super(name); | |
392 | this.longValues = longValues; | |
393 | } | |
394 | ||
395 | @Override | |
396 | public int getFieldType() { | |
a1c6baa7 | 397 | return FIELDTYPE_INTEGER_ARRAY; |
a3fc8213 AM |
398 | } |
399 | ||
400 | @Override | |
401 | public long[] getValue() { | |
402 | return this.longValues; | |
403 | } | |
404 | ||
405 | @Override | |
406 | public String toString() { | |
407 | StringBuffer buffer = new StringBuffer(); | |
408 | buffer.append("{ "); //$NON-NLS-1$ | |
409 | ||
410 | buffer.append(longValues[0]); | |
411 | for (int i = 1; i < longValues.length; i++) { | |
412 | buffer.append(", " + longValues[i]); //$NON-NLS-1$ | |
413 | } | |
414 | buffer.append('}'); | |
415 | return name + '=' + buffer.toString(); | |
416 | } | |
417 | } | |
418 | ||
b1baa808 | 419 | /** |
7558a1e1 AM |
420 | * CTF field implementation for floats. |
421 | * | |
422 | * @author emathko | |
b1baa808 | 423 | */ |
a04464b1 MK |
424 | final class CTFFloatField extends CtfTmfEventField { |
425 | ||
7558a1e1 AM |
426 | private final Double value; |
427 | ||
b1baa808 MK |
428 | /** |
429 | * Constructor for CTFFloatField. | |
7558a1e1 AM |
430 | * |
431 | * @param value | |
432 | * The float value (actually a double) of this field | |
433 | * @param name | |
434 | * The name of this field | |
b1baa808 | 435 | */ |
21fb02fa | 436 | protected CTFFloatField(double value, String name) { |
a04464b1 MK |
437 | super(name); |
438 | this.value = value; | |
439 | } | |
440 | ||
441 | @Override | |
442 | public int getFieldType() { | |
a1c6baa7 | 443 | return FIELDTYPE_FLOAT; |
a04464b1 MK |
444 | } |
445 | ||
446 | @Override | |
81c8e6f7 | 447 | public Double getValue() { |
a04464b1 MK |
448 | return this.value; |
449 | } | |
450 | ||
451 | @Override | |
21fb02fa | 452 | public String toString() { |
a04464b1 MK |
453 | return name + '=' + value; |
454 | } | |
a04464b1 | 455 | } |
7558a1e1 | 456 | |
d4a8d935 BH |
457 | /** |
458 | * The CTF field implementation for Enum fields | |
459 | * | |
460 | * @author Bernd Hufmann | |
461 | */ | |
462 | final class CTFEnumField extends CtfTmfEventField { | |
463 | ||
464 | private final CtfEnumPair value; | |
465 | ||
466 | /** | |
467 | * Constructor for CTFEnumField. | |
468 | * | |
469 | * @param enumValue | |
470 | * The Enum value consisting of a pair of Enum value name and its long value | |
471 | * @param name | |
472 | * The name of this field | |
473 | */ | |
474 | CTFEnumField(CtfEnumPair enumValue, String name) { | |
475 | super(name); | |
476 | this.value = new CtfEnumPair(enumValue.getFirst(), enumValue.getSecond().longValue()); | |
477 | } | |
478 | ||
479 | @Override | |
480 | public int getFieldType() { | |
481 | return FIELDTYPE_ENUM; | |
482 | } | |
483 | ||
484 | @Override | |
485 | public CtfEnumPair getValue() { | |
486 | return this.value; | |
487 | } | |
488 | ||
489 | @Override | |
490 | public String toString() { | |
491 | return name + '=' + value.toString(); | |
492 | } | |
493 | } | |
494 | ||
a3fc8213 | 495 | /* Implement other possible fields types here... */ |