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