Improve javadoc for ctfAdapter in Tmf.Core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDefinition.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.List;
17 import java.util.ListIterator;
18
19 import org.eclipse.linuxtools.internal.ctf.core.event.io.BitBuffer;
20
21 /**
22 * <b><u>StructDefinition</u></b>
23 */
24 public class StructDefinition extends Definition implements IDefinitionScope {
25
26 // ------------------------------------------------------------------------
27 // Attributes
28 // ------------------------------------------------------------------------
29
30 private final StructDeclaration declaration;
31 private final HashMap<String, Definition> definitions = new HashMap<String, Definition>();
32
33 // ------------------------------------------------------------------------
34 // Constructors
35 // ------------------------------------------------------------------------
36
37 /**
38 * Constructor
39 *
40 * @param declaration
41 * the parent declaration
42 * @param definitionScope
43 * the parent scope
44 * @param fieldName
45 * the field name
46 */
47 public StructDefinition(StructDeclaration declaration,
48 IDefinitionScope definitionScope, String structFieldName) {
49 super(definitionScope, structFieldName);
50
51 this.declaration = declaration;
52
53 for (String fName : declaration.getFieldsList()) {
54 IDeclaration fieldDecl = declaration.getFields().get(fName);
55 assert (fieldDecl != null);
56
57 Definition def = fieldDecl.createDefinition(this, fName);
58 definitions.put(fName, def);
59 }
60 }
61
62 // ------------------------------------------------------------------------
63 // Getters/Setters/Predicates
64 // ------------------------------------------------------------------------
65
66 @Override
67 public String getPath() {
68 return path;
69 }
70
71 /**
72 * @return The definitions of all the fields
73 */
74 public HashMap<String, Definition> getDefinitions() {
75 return definitions;
76 }
77
78 @Override
79 public StructDeclaration getDeclaration() {
80 return declaration;
81 }
82
83 // ------------------------------------------------------------------------
84 // Operations
85 // ------------------------------------------------------------------------
86
87 @Override
88 public void read(BitBuffer input) {
89 final int align = (int) declaration.getAlignment();
90 int pos = input.position()
91 + ((align - (input.position() % align)) % align);
92 input.position(pos);
93 final List<String> fieldList = declaration.getFieldsList();
94 for (String fName : fieldList) {
95 Definition def = definitions.get(fName);
96 assert (def != null);
97 def.read(input);
98 }
99 }
100
101 @Override
102 public Definition lookupDefinition(String lookupPath) {
103 /*
104 * The fields are created in order of appearance, so if a variant or
105 * sequence refers to a field that is after it, the field's definition
106 * will not be there yet in the hashmap.
107 */
108 Definition retVal = definitions.get(lookupPath);
109 if (retVal == null) {
110 retVal = definitions.get("_" + lookupPath); //$NON-NLS-1$
111 }
112 return retVal;
113 }
114
115 /**
116 * Lookup an array in a struct. if the name returns a non-array (like an
117 * int) than the method returns null
118 *
119 * @param name
120 * the name of the array
121 * @return the array or null.
122 */
123 public ArrayDefinition lookupArray(String name) {
124 Definition def = lookupDefinition(name);
125 return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
126 }
127
128 /**
129 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
130 * than the method returns null
131 *
132 * @param name
133 * the name of the enum
134 * @return the enum or null.
135 */
136 public EnumDefinition lookupEnum(String name) {
137 Definition def = lookupDefinition(name);
138 return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
139 }
140
141 /**
142 * Lookup an integer in a struct. if the name returns a non-integer (like an
143 * float) than the method returns null
144 *
145 * @param name
146 * the name of the integer
147 * @return the integer or null.
148 */
149 public IntegerDefinition lookupInteger(String name) {
150 Definition def = lookupDefinition(name);
151 return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
152 : null);
153 }
154
155 /**
156 * Lookup a sequence in a struct. if the name returns a non-sequence (like
157 * an int) than the method returns null
158 *
159 * @param name
160 * the name of the sequence
161 * @return the sequence or null.
162 */
163 public SequenceDefinition lookupSequence(String name) {
164 Definition def = lookupDefinition(name);
165 return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
166 : null);
167 }
168
169 /**
170 * Lookup a string in a struct. if the name returns a non-string (like
171 * an int) than the method returns null
172 *
173 * @param name
174 * the name of the string
175 * @return the string or null.
176 */
177 public StringDefinition lookupString(String name) {
178 Definition def = lookupDefinition(name);
179 return (StringDefinition) ((def instanceof StringDefinition) ? def
180 : null);
181 }
182
183 /**
184 * Lookup a struct in a struct. if the name returns a non-struct (like
185 * an int) than the method returns null
186 *
187 * @param name
188 * the name of the struct
189 * @return the struct or null.
190 */
191 public StructDefinition lookupStruct(String name) {
192 Definition def = lookupDefinition(name);
193 return (StructDefinition) ((def instanceof StructDefinition) ? def
194 : null);
195 }
196
197 /**
198 * Lookup a variant in a struct. if the name returns a non-variant (like
199 * an int) than the method returns null
200 *
201 * @param name
202 * the name of the variant
203 * @return the variant or null.
204 */
205 public VariantDefinition lookupVariant(String name) {
206 Definition def = lookupDefinition(name);
207 return (VariantDefinition) ((def instanceof VariantDefinition) ? def
208 : null);
209 }
210
211 @Override
212 public String toString() {
213 StringBuilder builder = new StringBuilder();
214
215 int size = this.declaration.getFieldsList().size();
216 int n = 0;
217
218 if (size > 1) {
219 builder.append("{ "); //$NON-NLS-1$
220 }
221
222 ListIterator<String> listIterator = this.declaration.getFieldsList()
223 .listIterator();
224
225 while (listIterator.hasNext()) {
226 String field = listIterator.next();
227 builder.append(lookupDefinition(field).toString());
228 n++;
229 if (n != size) {
230 builder.append(", "); //$NON-NLS-1$
231 }
232 }
233
234 if (size > 1) {
235 builder.append(" }"); //$NON-NLS-1$
236 }
237
238 return builder.toString();
239 }
240 }
This page took 0.037747 seconds and 6 git commands to generate.