tmf: Bring back "@since 1.2" annotations in master
[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:
10 * Matthew Khouzam - Initial API and implementation
11 * Alexendre Montplaisir - Initial API and implementation
12 * Bernd Hufmann - Add Enum field handling
13 *
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;
21 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
22 import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
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 /**
31 * The CTF implementation of the TMF event field model
32 *
33 * @version 2.0
34 * @author Matthew Khouzam
35 * @author Alexandre Montplaisir
36 */
37 public abstract class CtfTmfEventField implements ITmfEventField {
38
39 // ------------------------------------------------------------------------
40 // Class attributes
41 // ------------------------------------------------------------------------
42
43 /** @since 1.2 */
44 protected static final int FIELDTYPE_INTEGER = 0;
45
46 /** @since 1.2 */
47 protected static final int FIELDTYPE_STRING = 1;
48
49 /** @since 1.2 */
50 protected static final int FIELDTYPE_INTEGER_ARRAY = 2;
51
52 /** @since 1.2 */
53 protected static final int FIELDTYPE_FLOAT = 3;
54
55 /** @since 2.0 */
56 protected static final int FIELDTYPE_ENUM = 4;
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61
62 /** The name of this field */
63 protected final String name;
64
65 // ------------------------------------------------------------------------
66 // Constructor
67 // ------------------------------------------------------------------------
68
69 /**
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
75 */
76 protected CtfTmfEventField(String name) {
77 /* Strip the underscore */
78 if (name.startsWith("_")) { //$NON-NLS-1$
79 this.name = name.substring(1);
80 } else {
81 this.name = name;
82 }
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
98 /**
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
106 */
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) {
113 IntegerDefinition intDef = (IntegerDefinition) fieldDef;
114 int base = intDef.getDeclaration().getBase();
115 field = new CTFIntegerField(intDef.getValue(), fieldName, base);
116
117 } else if (fieldDef instanceof EnumDefinition) {
118 EnumDefinition enumDef = (EnumDefinition) fieldDef;
119 field = new CTFEnumField(new CtfEnumPair(enumDef.getValue(), enumDef.getIntegerValue()), fieldName);
120
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++) {
137 values[i] = ((IntegerDefinition) arrayDef.getElem(i))
138 .getValue();
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++) {
158 values[i] = ((IntegerDefinition) seqDef.getElem(i))
159 .getValue();
160 }
161 field = new CTFIntegerArrayField(values, fieldName);
162 }
163 /* Add other Sequence types here */
164
165 } else if (fieldDef instanceof FloatDefinition) {
166 FloatDefinition floatDef = (FloatDefinition) fieldDef;
167 field = new CTFFloatField(floatDef.getValue(), fieldName);
168 }
169
170 return field;
171 }
172
173 /**
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
180 */
181 public static CtfTmfEventField copyFrom(CtfTmfEventField other) {
182 switch (other.getFieldType()) {
183 case FIELDTYPE_INTEGER:
184 CTFIntegerField intOther = (CTFIntegerField) other;
185 return new CTFIntegerField(intOther.getValue(), intOther.name,
186 intOther.getBase());
187 case FIELDTYPE_STRING:
188 return new CTFStringField(((CTFStringField) other).getValue(),
189 other.name);
190 case FIELDTYPE_INTEGER_ARRAY:
191 return new CTFIntegerArrayField(
192 ((CTFIntegerArrayField) other).getValue(), other.name);
193 case FIELDTYPE_FLOAT:
194 return new CTFFloatField(((CTFFloatField) other).getValue(),
195 other.name);
196 case FIELDTYPE_ENUM:
197 return new CTFEnumField(((CTFEnumField) other).getValue(), other.name);
198 default:
199 return null;
200 }
201 }
202
203 @Override
204 public CtfTmfEventField clone() {
205 return CtfTmfEventField.copyFrom(this);
206 }
207
208 // ------------------------------------------------------------------------
209 // Abstract methods (to be implemented by each specific field type)
210 // ------------------------------------------------------------------------
211
212 /**
213 * Return the int representing this field's value type
214 *
215 * @return The field type
216 */
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.
222 *
223 * @return The field's value
224 */
225 @Override
226 public abstract Object getValue();
227
228 // ------------------------------------------------------------------------
229 // Other methods defined by ITmfEventField, but not used here.
230 // CTF fields do not have sub-fields (yet!)
231 // ------------------------------------------------------------------------
232
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 /**
260 * The CTF field implementation for integer fields.
261 *
262 * @author alexmont
263 */
264 final class CTFIntegerField extends CtfTmfEventField {
265
266 private final long longValue;
267 private final int base;
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.
272 *
273 * @param longValue
274 * The integer value of this field
275 * @param name
276 * The name of this field
277 */
278 CTFIntegerField(long longValue, String name, int base) {
279 super(name);
280 this.longValue = longValue;
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;
291 }
292
293 @Override
294 public int getFieldType() {
295 return FIELDTYPE_INTEGER;
296 }
297
298 @Override
299 public Long getValue() {
300 return this.longValue;
301 }
302
303 @Override
304 public String toString() {
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();
331 }
332 }
333
334 /**
335 * The CTF field implementation for string fields
336 *
337 * @author alexmont
338 */
339 final class CTFStringField extends CtfTmfEventField {
340
341 private final String strValue;
342
343 /**
344 * Constructor for CTFStringField.
345 *
346 * @param strValue
347 * The string value of this field
348 * @param name
349 * The name of this field
350 */
351 CTFStringField(String strValue, String name) {
352 super(name);
353 this.strValue = strValue;
354 }
355
356 @Override
357 public int getFieldType() {
358 return FIELDTYPE_STRING;
359 }
360
361 @Override
362 public String getValue() {
363 return this.strValue;
364 }
365
366 @Override
367 public String toString() {
368 return name + '=' + strValue;
369 }
370 }
371
372 /**
373 * CTF field implementation for arrays of integers.
374 *
375 * @author alexmont
376 */
377 final class CTFIntegerArrayField extends CtfTmfEventField {
378
379 private final long[] longValues;
380
381 /**
382 * Constructor for CTFIntegerArrayField.
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
389 */
390 CTFIntegerArrayField(long[] longValues, String name) {
391 super(name);
392 this.longValues = longValues;
393 }
394
395 @Override
396 public int getFieldType() {
397 return FIELDTYPE_INTEGER_ARRAY;
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
419 /**
420 * CTF field implementation for floats.
421 *
422 * @author emathko
423 */
424 final class CTFFloatField extends CtfTmfEventField {
425
426 private final Double value;
427
428 /**
429 * Constructor for CTFFloatField.
430 *
431 * @param value
432 * The float value (actually a double) of this field
433 * @param name
434 * The name of this field
435 */
436 protected CTFFloatField(double value, String name) {
437 super(name);
438 this.value = value;
439 }
440
441 @Override
442 public int getFieldType() {
443 return FIELDTYPE_FLOAT;
444 }
445
446 @Override
447 public Double getValue() {
448 return this.value;
449 }
450
451 @Override
452 public String toString() {
453 return name + '=' + value;
454 }
455 }
456
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
495 /* Implement other possible fields types here... */
This page took 0.055566 seconds and 6 git commands to generate.