Fix warnings
[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.IntegerDeclaration;
19 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
20 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration;
21 import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition;
22 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
24
25 /**
26 * <b><u>CTFEventField</u></b>
27 */
28 public abstract class CtfTmfEventField implements ITmfEventField {
29
30 // ------------------------------------------------------------------------
31 // Attributes
32 // ------------------------------------------------------------------------
33
34 protected final String name;
35
36 // ------------------------------------------------------------------------
37 // Constructors
38 // ------------------------------------------------------------------------
39
40 protected CtfTmfEventField(String name) {
41 /* Strip the damn underscores, screw you CTF */
42 if ( name.startsWith("_") ) { //$NON-NLS-1$
43 this.name = name.substring(1);
44 } else {
45 this.name = name;
46 }
47 }
48
49 // ------------------------------------------------------------------------
50 // Getters/Setters/Predicates
51 // ------------------------------------------------------------------------
52
53 @Override
54 public String getName() {
55 return this.name;
56 }
57
58 // ------------------------------------------------------------------------
59 // Operations
60 // ------------------------------------------------------------------------
61
62 public static CtfTmfEventField parseField(Definition fieldDef,
63 String fieldName) {
64 CtfTmfEventField field = null;
65
66 /* Determine the Definition type */
67 if (fieldDef instanceof IntegerDefinition) {
68 field = new CTFIntegerField(
69 ((IntegerDefinition) fieldDef).getValue(), fieldName);
70
71 } else if (fieldDef instanceof StringDefinition) {
72 field = new CTFStringField(
73 ((StringDefinition) fieldDef).getValue(), fieldName);
74
75 } else if (fieldDef instanceof ArrayDefinition) {
76 ArrayDefinition arrayDef = (ArrayDefinition) fieldDef;
77 ArrayDeclaration arrayDecl = arrayDef.getDeclaration();
78
79 if (arrayDef.isString()) {
80 /* This is an array of UTF-8 bytes, a.k.a. a String! */
81 field = new CTFStringField(fieldDef.toString(), fieldName);
82
83 } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) {
84 /* This is a an array of CTF Integers */
85 long[] values = new long[arrayDecl.getLength()];
86 for (int i = 0; i < arrayDecl.getLength(); i++) {
87 values[i] = ((IntegerDefinition) arrayDef.getElem(i)).getValue();
88 }
89 field = new CTFIntegerArrayField(values, fieldName);
90 }
91 /* Add other types of arrays here */
92
93 } else if (fieldDef instanceof SequenceDefinition) {
94 SequenceDefinition seqDef = (SequenceDefinition) fieldDef;
95 SequenceDeclaration seqDecl = seqDef.getDeclaration();
96
97 if (seqDef.getLength() == 0) {
98 /* Some sequences have length = 0. Simply use an empty string */
99 field = new CTFStringField("", fieldName); //$NON-NLS-1$
100 } else if (seqDef.isString()) {
101 /* Interpret this sequence as a String */
102 field = new CTFStringField(seqDef.toString(), fieldName);
103 } else if (seqDecl.getElementType() instanceof IntegerDeclaration) {
104 /* Sequence of integers => CTFIntegerArrayField */
105 long[] values = new long[seqDef.getLength()];
106 for (int i = 0; i < seqDef.getLength(); i++) {
107 values[i] = ((IntegerDefinition) seqDef.getElem(i)).getValue();
108 }
109 field = new CTFIntegerArrayField(values, fieldName);
110 }
111 /* Add other Sequence types here */
112 }
113 /* Add other field types here */
114
115 return field;
116 }
117
118 public static CtfTmfEventField copyFrom(CtfTmfEventField other) {
119 switch (other.getFieldType()) {
120 case 0:
121 return new CTFIntegerField(((CTFIntegerField) other).getValue(),
122 other.name);
123 case 1:
124 return new CTFStringField(((CTFStringField) other).getValue(),
125 other.name);
126 case 2:
127 return new CTFIntegerArrayField(
128 ((CTFIntegerArrayField) other).getValue(), other.name);
129 default:
130 return null;
131 }
132 }
133
134 @Override
135 public CtfTmfEventField clone() {
136 return CtfTmfEventField.copyFrom(this);
137 }
138
139 /**
140 * Return the int representing this field's value type
141 *
142 * @return the field type
143 */
144 public abstract int getFieldType();
145
146 /**
147 * Return this field's value. You can cast it to the correct type depending
148 * on what getFieldType says.
149 *
150 * @return the field value
151 */
152 @Override
153 public abstract Object getValue();
154
155 /**
156 * Other methods defined by ITmfEventField, but not used here: the CTF
157 * fields do not have sub-fields (yet!)
158 *
159 * @return the field names
160 */
161 @Override
162 public String[] getFieldNames() {
163 return null;
164 }
165
166 @SuppressWarnings("unused")
167 @Override
168 public String getFieldName(int index) {
169 return null;
170 }
171
172 @Override
173 public ITmfEventField[] getFields() {
174 return null;
175 }
176
177 @SuppressWarnings("unused")
178 @Override
179 public ITmfEventField getField(String fieldName) {
180 return null;
181 }
182
183 @SuppressWarnings("unused")
184 @Override
185 public ITmfEventField getField(int index) {
186 return null;
187 }
188 }
189
190 /**
191 * <b><u>CTFIntegerField</u></b>
192 */
193 final class CTFIntegerField extends CtfTmfEventField {
194
195 private final long longValue;
196
197 /**
198 * A CTF "IntegerDefinition" can be an integer of any byte size, so in the
199 * Java parser this is interpreted as a long.
200 */
201 CTFIntegerField(long longValue, String name) {
202 super(name);
203 this.longValue = longValue;
204 }
205
206 @Override
207 public int getFieldType() {
208 return 0;
209 }
210
211 @Override
212 public Long getValue() {
213 return this.longValue;
214 }
215
216 /*
217 * (non-Javadoc)
218 *
219 * @see java.lang.Object#toString()
220 */
221 @Override
222 public String toString() {
223 return name + '=' + longValue;
224 }
225 }
226
227 /**
228 * <b><u>CTFStringField</u></b>
229 */
230 final class CTFStringField extends CtfTmfEventField {
231
232 private final String strValue;
233
234 CTFStringField(String strValue, String name) {
235 super(name);
236 this.strValue = strValue;
237 }
238
239 @Override
240 public int getFieldType() {
241 return 1;
242 }
243
244 @Override
245 public String getValue() {
246 return this.strValue;
247 }
248
249 /*
250 * (non-Javadoc)
251 *
252 * @see java.lang.Object#toString()
253 */
254 @Override
255 public String toString() {
256 return name + '=' + strValue;
257 }
258 }
259
260 /**
261 * <b><u>CTFIntegerArrayField</u></b>
262 */
263 final class CTFIntegerArrayField extends CtfTmfEventField {
264
265 private final long[] longValues;
266
267 CTFIntegerArrayField(long[] longValues, String name) {
268 super(name);
269 this.longValues = longValues;
270 }
271
272 @Override
273 public int getFieldType() {
274 return 2;
275 }
276
277 @Override
278 public long[] getValue() {
279 return this.longValues;
280 }
281
282 @Override
283 public String toString() {
284 StringBuffer buffer = new StringBuffer();
285 buffer.append("{ "); //$NON-NLS-1$
286
287 buffer.append(longValues[0]);
288 for (int i = 1; i < longValues.length; i++) {
289 buffer.append(", " + longValues[i]); //$NON-NLS-1$
290 }
291 buffer.append('}');
292 return name + '=' + buffer.toString();
293 }
294 }
295
296 /* Implement other possible fields types here... */
This page took 0.036169 seconds and 5 git commands to generate.