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