btf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDefinition.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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.Collections;
16 import java.util.LinkedList;
17 import java.util.List;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNull;
21 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
22 import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
23
24 import com.google.common.base.Joiner;
25 import com.google.common.collect.ImmutableList;
26 import com.google.common.collect.ImmutableMap;
27 import com.google.common.collect.ImmutableMap.Builder;
28
29 /**
30 * A CTF structure definition (similar to a C structure).
31 *
32 * A structure is similar to a C structure, it is a compound data type that
33 * contains other datatypes in fields. they are stored in an hashmap and indexed
34 * by names which are strings.
35 *
36 * TODO: move me to internal
37 *
38 * @version 1.0
39 * @author Matthew Khouzam
40 * @author Simon Marchi
41 */
42 public final class StructDefinition extends ScopedDefinition implements ICompositeDefinition {
43
44 // ------------------------------------------------------------------------
45 // Attributes
46 // ------------------------------------------------------------------------
47
48 private final ImmutableList<String> fFieldNames;
49 private final Definition[] fDefinitions;
50 private Map<String, Definition> fDefinitionsMap = null;
51
52 // ------------------------------------------------------------------------
53 // Constructors
54 // ------------------------------------------------------------------------
55
56 /**
57 * *DEPRECATED* TODO: To remove once we break the API...
58 *
59 * Not marked with the annotation to not annoy callers using a List, which
60 * is still as valid with the new constructor. But the compiler gives an
61 * error even though a Iterable is a List too...
62 *
63 * @param declaration
64 * the parent declaration
65 * @param definitionScope
66 * the parent scope
67 * @param structFieldName
68 * the field name
69 * @param fieldNames
70 * the list of fields
71 * @param definitions
72 * the definitions
73 * @since 3.1
74 */
75 public StructDefinition(@NonNull StructDeclaration declaration,
76 IDefinitionScope definitionScope,
77 @NonNull String structFieldName,
78 List<String> fieldNames,
79 Definition[] definitions) {
80 this(declaration, definitionScope, structFieldName, (Iterable<String>) fieldNames, definitions);
81 }
82
83 /**
84 * Constructor
85 *
86 * @param declaration
87 * the parent declaration
88 * @param definitionScope
89 * the parent scope
90 * @param structFieldName
91 * the field name
92 * @param fieldNames
93 * the list of fields
94 * @param definitions
95 * the definitions
96 * @since 3.1
97 */
98 public StructDefinition(@NonNull StructDeclaration declaration,
99 IDefinitionScope definitionScope,
100 @NonNull String structFieldName,
101 Iterable<String> fieldNames,
102 Definition[] definitions) {
103 super(declaration, definitionScope, structFieldName);
104 fFieldNames = ImmutableList.copyOf(fieldNames);
105 fDefinitions = definitions;
106 if (fFieldNames.isEmpty()) {
107 fDefinitionsMap = Collections.EMPTY_MAP;
108 }
109 }
110
111 /**
112 * Constructor This one takes the scope and thus speeds up definition
113 * creation
114 *
115 * @param declaration
116 * the parent declaration
117 * @param definitionScope
118 * the parent scope
119 * @param scope
120 * the scope of this variable
121 * @param structFieldName
122 * the field name
123 * @param fieldNames
124 * the list of fields
125 * @param definitions
126 * the definitions
127 * @since 3.1
128 */
129 public StructDefinition(@NonNull StructDeclaration declaration,
130 IDefinitionScope definitionScope, @NonNull LexicalScope scope,
131 @NonNull String structFieldName, @NonNull Iterable<String> fieldNames, Definition[] definitions) {
132 super(declaration, definitionScope, structFieldName, scope);
133 fFieldNames = ImmutableList.copyOf(fieldNames);
134 fDefinitions = definitions;
135 if (fFieldNames.isEmpty()) {
136 fDefinitionsMap = Collections.EMPTY_MAP;
137 }
138 }
139
140 // ------------------------------------------------------------------------
141 // Getters/Setters/Predicates
142 // ------------------------------------------------------------------------
143
144 @Override
145 public Definition getDefinition(String fieldName) {
146 if (fDefinitionsMap == null) {
147 /* Build the definitions map */
148 Builder<String, Definition> mapBuilder = new ImmutableMap.Builder<>();
149 for (int i = 0; i < fFieldNames.size(); i++) {
150 if (fDefinitions[i] != null) {
151 mapBuilder.put(fFieldNames.get(i), fDefinitions[i]);
152 }
153 }
154 fDefinitionsMap = mapBuilder.build();
155 }
156 return fDefinitionsMap.get(fieldName);
157 }
158
159 @Override
160 public List<String> getFieldNames() {
161 return fFieldNames;
162 }
163
164 @Override
165 public StructDeclaration getDeclaration() {
166 return (StructDeclaration) super.getDeclaration();
167 }
168
169 // ------------------------------------------------------------------------
170 // Operations
171 // ------------------------------------------------------------------------
172
173 @Override
174 public Definition lookupDefinition(String lookupPath) {
175 /*
176 * The fields are created in order of appearance, so if a variant or
177 * sequence refers to a field that is after it, the field's definition
178 * will not be there yet in the hashmap.
179 */
180 int val = fFieldNames.indexOf(lookupPath);
181 if (val != -1) {
182 return fDefinitions[val];
183 }
184 String lookupUnderscored = "_" + lookupPath; //$NON-NLS-1$
185 val = fFieldNames.indexOf(lookupUnderscored);
186 if (val != -1) {
187 return fDefinitions[val];
188 }
189 return null;
190 }
191
192 @Override
193 public String toString() {
194 StringBuilder builder = new StringBuilder();
195
196 builder.append("{ "); //$NON-NLS-1$
197
198 if (fFieldNames != null) {
199 List<String> fields = new LinkedList<>();
200 for (String field : fFieldNames) {
201 String appendee = field + " = " + lookupDefinition(field).toString(); //$NON-NLS-1$
202 fields.add(appendee);
203 }
204 Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
205 builder.append(joiner.join(fields));
206 }
207
208 builder.append(" }"); //$NON-NLS-1$
209
210 return builder.toString();
211 }
212
213 }
This page took 0.036514 seconds and 5 git commands to generate.