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