Improve test cases. Coverage of 85%+ and fix bugs.
[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 * <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
41 /**
42 * Constructor for CtfTmfEventField.
43 * @param name String
44 */
45 protected CtfTmfEventField(String name) {
46 /* Strip the underscore*/
47 if ( name.startsWith("_") ) { //$NON-NLS-1$
48 this.name = name.substring(1);
49 } else {
50 this.name = name;
51 }
52 }
53
54 // ------------------------------------------------------------------------
55 // Getters/Setters/Predicates
56 // ------------------------------------------------------------------------
57
58 /**
59 * Method getName.
60 * @return String
61 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getName()
62 */
63 @Override
64 public String getName() {
65 return this.name;
66 }
67
68 // ------------------------------------------------------------------------
69 // Operations
70 // ------------------------------------------------------------------------
71
72 /**
73 * Method parseField.
74 * @param fieldDef Definition
75 * @param fieldName String
76 * @return CtfTmfEventField
77 */
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 */
128 } else if (fieldDef instanceof FloatDefinition){
129 FloatDefinition floatDef = (FloatDefinition) fieldDef;
130 field = new CTFFloatField( floatDef.getValue(), fieldName);
131 }
132
133
134 return field;
135 }
136
137 /**
138 * Method copyFrom.
139 * @param other CtfTmfEventField
140 * @return CtfTmfEventField
141 */
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);
153 case 3:
154 return new CTFFloatField(
155 ((CTFFloatField) other).getValue(), other.name);
156 default:
157 return null;
158 }
159 }
160
161 /**
162 * Method clone.
163 * @return CtfTmfEventField
164 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#clone()
165 */
166 @Override
167 public CtfTmfEventField clone() {
168 return CtfTmfEventField.copyFrom(this);
169 }
170
171 /**
172 * Return the int representing this field's value type
173 *
174
175 * @return the field type */
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.
181 *
182
183 * @return the field value * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
184 */
185 @Override
186 public abstract Object getValue();
187
188 /**
189 * Other methods defined by ITmfEventField, but not used here: the CTF
190 * fields do not have sub-fields (yet!)
191 *
192
193 * @return the field names * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldNames()
194 */
195 @Override
196 public String[] getFieldNames() {
197 return null;
198 }
199
200 /**
201 * Method getFieldName.
202 * @param index int
203 * @return String
204 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFieldName(int)
205 */
206 @SuppressWarnings("unused")
207 @Override
208 public String getFieldName(int index) {
209 return null;
210 }
211
212 /**
213 * Method getFields.
214 * @return ITmfEventField[]
215 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getFields()
216 */
217 @Override
218 public ITmfEventField[] getFields() {
219 return null;
220 }
221
222 /**
223 * Method getField.
224 * @param fieldName String
225 * @return ITmfEventField
226 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(String)
227 */
228 @SuppressWarnings("unused")
229 @Override
230 public ITmfEventField getField(String fieldName) {
231 return null;
232 }
233
234 /**
235 * Method getField.
236 * @param index int
237 * @return ITmfEventField
238 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getField(int)
239 */
240 @SuppressWarnings("unused")
241 @Override
242 public ITmfEventField getField(int index) {
243 return null;
244 }
245 }
246
247 /**
248 * <b><u>CTFIntegerField</u></b>
249 * @author ematkho
250 * @version $Revision: 1.0 $
251 */
252 final class CTFIntegerField extends CtfTmfEventField {
253
254 private final long longValue;
255
256 /**
257 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
258 * Java parser this is interpreted as a long.
259 * @param longValue long
260 * @param name String
261 */
262 CTFIntegerField(long longValue, String name) {
263 super(name);
264 this.longValue = longValue;
265 }
266
267 /**
268 * Method getFieldType.
269 * @return int
270 */
271 @Override
272 public int getFieldType() {
273 return 0;
274 }
275
276 /**
277 * Method getValue.
278 * @return Long
279 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
280 */
281 @Override
282 public Long getValue() {
283 return this.longValue;
284 }
285
286 /*
287 * (non-Javadoc)
288 *
289 * @see java.lang.Object#toString()
290 */
291 @Override
292 public String toString() {
293 return name + '=' + longValue;
294 }
295 }
296
297 /**
298 * <b><u>CTFStringField</u></b>
299 * @author ematkho
300 * @version $Revision: 1.0 $
301 */
302 final class CTFStringField extends CtfTmfEventField {
303
304 private final String strValue;
305
306 /**
307 * Constructor for CTFStringField.
308 * @param strValue String
309 * @param name String
310 */
311 CTFStringField(String strValue, String name) {
312 super(name);
313 this.strValue = strValue;
314 }
315
316 /**
317 * Method getFieldType.
318 * @return int
319 */
320 @Override
321 public int getFieldType() {
322 return 1;
323 }
324
325 /**
326 * Method getValue.
327 * @return String
328 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
329 */
330 @Override
331 public String getValue() {
332 return this.strValue;
333 }
334
335 /*
336 * (non-Javadoc)
337 *
338 * @see java.lang.Object#toString()
339 */
340 @Override
341 public String toString() {
342 return name + '=' + strValue;
343 }
344 }
345
346 /**
347 * <b><u>CTFIntegerArrayField</u></b>
348 * @author ematkho
349 * @version $Revision: 1.0 $
350 */
351 final class CTFIntegerArrayField extends CtfTmfEventField {
352
353 private final long[] longValues;
354
355 /**
356 * Constructor for CTFIntegerArrayField.
357 * @param longValues long[]
358 * @param name String
359 */
360 CTFIntegerArrayField(long[] longValues, String name) {
361 super(name);
362 this.longValues = longValues;
363 }
364
365 /**
366 * Method getFieldType.
367 * @return int
368 */
369 @Override
370 public int getFieldType() {
371 return 2;
372 }
373
374 /**
375 * Method getValue.
376 * @return long[]
377 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
378 */
379 @Override
380 public long[] getValue() {
381 return this.longValues;
382 }
383
384 /**
385 * Method toString.
386 * @return String
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 final class CTFFloatField extends CtfTmfEventField {
405
406 Double value;
407 /**
408 * Constructor for CTFFloatField.
409 * @param value double
410 * @param name String
411 */
412 protected CTFFloatField(double value ,String name) {
413 super(name);
414 this.value = value;
415 }
416
417 /**
418 * Method getFieldType.
419 * @return int
420 */
421 @Override
422 public int getFieldType() {
423 return 3;
424 }
425
426 /**
427 * Method getValue.
428 * @return Object
429 * @see org.eclipse.linuxtools.tmf.core.event.ITmfEventField#getValue()
430 */
431 @Override
432 public Double getValue() {
433 return this.value;
434 }
435
436 /**
437 * Method toString.
438 * @return String
439 */
440 @Override
441 public String toString(){
442 return name + '=' + value;
443 }
444
445 }
446 /* Implement other possible fields types here... */
This page took 0.042786 seconds and 6 git commands to generate.