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