Improve javadoc for ctfAdapter in Tmf.Core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / VariantDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others
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: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.ctf.core.event.types;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
19
20 /**
21 * <b><u>VariantDefinition</u></b>
22 */
23 public class VariantDefinition extends Definition implements IDefinitionScope {
24
25 // ------------------------------------------------------------------------
26 // Attributes
27 // ------------------------------------------------------------------------
28
29 private VariantDeclaration declaration;
30
31 private EnumDefinition tagDefinition;
32 private HashMap<String, Definition> definitions = new HashMap<String, Definition>();
33 private String currentField;
34
35 // ------------------------------------------------------------------------
36 // Constructors
37 // ------------------------------------------------------------------------
38
39 /**
40 * Constructor
41 * @param declaration the parent declaration
42 * @param definitionScope the parent scope
43 * @param fieldName the field name
44 */
45 public VariantDefinition(VariantDeclaration declaration,
46 IDefinitionScope definitionScope, String fieldName) {
47 super(definitionScope, fieldName);
48
49 this.declaration = declaration;
50
51 Definition tagDef = definitionScope.lookupDefinition(declaration.getTag());
52 /*
53 * if (tagDef == null) { throw new
54 * Exception("Variant tag field not found"); }
55 *
56 * if (!(tagDef instanceof EnumDefinition)) { throw new
57 * Exception("Variant tag field not enum"); }
58 */
59 this.tagDefinition = (EnumDefinition) tagDef;
60
61 for (Map.Entry<String, IDeclaration> field : declaration.getFields().entrySet()) {
62 Definition fieldDef = field.getValue().createDefinition(this,
63 field.getKey());
64 definitions.put(field.getKey(), fieldDef);
65 }
66 }
67
68 // ------------------------------------------------------------------------
69 // Getters/Setters/Predicates
70 // ------------------------------------------------------------------------
71
72 @Override
73 public VariantDeclaration getDeclaration() {
74 return declaration;
75 }
76
77 /**
78 * Sets the variant declaration
79 * @param declaration the variant declaration
80 */
81 public void setDeclaration(VariantDeclaration declaration) {
82 this.declaration = declaration;
83 }
84
85 /**
86 * Gets the tag
87 * @return the tag definition
88 */
89 public EnumDefinition getTagDefinition() {
90 return tagDefinition;
91 }
92
93 /**
94 * Sets the tag
95 * @param tagDefinition the tag
96 */
97 public void setTagDefinition(EnumDefinition tagDefinition) {
98 this.tagDefinition = tagDefinition;
99 }
100
101 /**
102 * Get the definitions in the variant
103 * @return the definitions
104 */
105 public HashMap<String, Definition> getDefinitions() {
106 return definitions;
107 }
108
109 /**
110 * Set the definitions in a variant
111 * @param definitions the definitions
112 */
113 public void setDefinitions(HashMap<String, Definition> definitions) {
114 this.definitions = definitions;
115 }
116
117 /**
118 * Set the current field
119 * @param currentField the current field
120 */
121 public void setCurrentField(String currentField) {
122 this.currentField = currentField;
123 }
124
125 @Override
126 public String getPath() {
127 return path;
128 }
129
130 /**
131 * Get the current field name
132 * @return the current field name
133 */
134 public String getCurrentFieldName() {
135 return currentField;
136 }
137
138 /**
139 * Get the current field
140 * @return the current field
141 */
142 public Definition getCurrentField() {
143 return definitions.get(currentField);
144 }
145
146
147 // ------------------------------------------------------------------------
148 // Operations
149 // ------------------------------------------------------------------------
150
151 @Override
152 public void read(BitBuffer input) {
153 currentField = tagDefinition.getValue();
154
155 Definition field = definitions.get(currentField);
156
157 field.read(input);
158 }
159
160 @Override
161 public Definition lookupDefinition(String lookupPath) {
162 return definitions.get(lookupPath);
163 }
164
165
166 /**
167 * Lookup an array in a struct. if the name returns a non-array (like an
168 * int) than the method returns null
169 *
170 * @param name
171 * the name of the array
172 * @return the array or null.
173 */
174 public ArrayDefinition lookupArray(String name) {
175 Definition def = lookupDefinition(name);
176 return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
177 }
178
179 /**
180 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
181 * than the method returns null
182 *
183 * @param name
184 * the name of the enum
185 * @return the enum or null.
186 */
187 public EnumDefinition lookupEnum(String name) {
188 Definition def = lookupDefinition(name);
189 return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
190 }
191
192 /**
193 * Lookup an integer in a struct. if the name returns a non-integer (like an
194 * float) than the method returns null
195 *
196 * @param name
197 * the name of the integer
198 * @return the integer or null.
199 */
200 public IntegerDefinition lookupInteger(String name) {
201 Definition def = lookupDefinition(name);
202 return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
203 : null);
204 }
205
206 /**
207 * Lookup a sequence in a struct. if the name returns a non-sequence (like
208 * an int) than the method returns null
209 *
210 * @param name
211 * the name of the sequence
212 * @return the sequence or null.
213 */
214 public SequenceDefinition lookupSequence(String name) {
215 Definition def = lookupDefinition(name);
216 return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
217 : null);
218 }
219
220 /**
221 * Lookup a string in a struct. if the name returns a non-string (like
222 * an int) than the method returns null
223 *
224 * @param name
225 * the name of the string
226 * @return the string or null.
227 */
228 public StringDefinition lookupString(String name) {
229 Definition def = lookupDefinition(name);
230 return (StringDefinition) ((def instanceof StringDefinition) ? def
231 : null);
232 }
233
234 /**
235 * Lookup a struct in a struct. if the name returns a non-struct (like
236 * an int) than the method returns null
237 *
238 * @param name
239 * the name of the struct
240 * @return the struct or null.
241 */
242 public StructDefinition lookupStruct(String name) {
243 Definition def = lookupDefinition(name);
244 return (StructDefinition) ((def instanceof StructDefinition) ? def
245 : null);
246 }
247
248 /**
249 * Lookup a variant in a struct. if the name returns a non-variant (like
250 * an int) than the method returns null
251 *
252 * @param name
253 * the name of the variant
254 * @return the variant or null.
255 */
256 public VariantDefinition lookupVariant(String name) {
257 Definition def = lookupDefinition(name);
258 return (VariantDefinition) ((def instanceof VariantDefinition) ? def
259 : null);
260 }
261
262 }
This page took 0.036813 seconds and 5 git commands to generate.