Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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 | ||
a4fa4e36 MK |
15 | import java.util.Collections; |
16 | import java.util.LinkedList; | |
2b7f6f09 | 17 | import java.util.List; |
843f986b | 18 | import java.util.Map; |
866e5b51 | 19 | |
a4fa4e36 MK |
20 | import org.eclipse.jdt.annotation.NonNull; |
21 | import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope; | |
70f60307 | 22 | import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope; |
a4fa4e36 MK |
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; | |
866e5b51 FC |
28 | |
29 | /** | |
d37aaa7f | 30 | * A CTF structure definition (similar to a C structure). |
486efb2e | 31 | * |
d37aaa7f FC |
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 | * | |
6c7592e1 MK |
36 | * TODO: move me to internal |
37 | * | |
d37aaa7f FC |
38 | * @version 1.0 |
39 | * @author Matthew Khouzam | |
40 | * @author Simon Marchi | |
866e5b51 | 41 | */ |
009883d7 | 42 | public final class StructDefinition extends ScopedDefinition implements ICompositeDefinition { |
866e5b51 FC |
43 | |
44 | // ------------------------------------------------------------------------ | |
45 | // Attributes | |
46 | // ------------------------------------------------------------------------ | |
47 | ||
a4fa4e36 MK |
48 | private final ImmutableList<String> fFieldNames; |
49 | private final Definition[] fDefinitions; | |
50 | private Map<String, Definition> fDefinitionsMap = null; | |
866e5b51 FC |
51 | |
52 | // ------------------------------------------------------------------------ | |
53 | // Constructors | |
54 | // ------------------------------------------------------------------------ | |
55 | ||
2db699c2 AM |
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 | ||
9ac2eb62 MK |
83 | /** |
84 | * Constructor | |
85 | * | |
86 | * @param declaration | |
87 | * the parent declaration | |
88 | * @param definitionScope | |
89 | * the parent scope | |
be6df2d8 | 90 | * @param structFieldName |
9ac2eb62 | 91 | * the field name |
a4fa4e36 MK |
92 | * @param fieldNames |
93 | * the list of fields | |
94 | * @param definitions | |
95 | * the definitions | |
2db699c2 | 96 | * @since 3.1 |
9ac2eb62 | 97 | */ |
a4fa4e36 | 98 | public StructDefinition(@NonNull StructDeclaration declaration, |
2db699c2 AM |
99 | IDefinitionScope definitionScope, |
100 | @NonNull String structFieldName, | |
101 | Iterable<String> fieldNames, | |
102 | Definition[] definitions) { | |
a4fa4e36 MK |
103 | super(declaration, definitionScope, structFieldName); |
104 | fFieldNames = ImmutableList.copyOf(fieldNames); | |
105 | fDefinitions = definitions; | |
70f60307 MK |
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; | |
009883d7 | 135 | if (fFieldNames.isEmpty()) { |
a4fa4e36 | 136 | fDefinitionsMap = Collections.EMPTY_MAP; |
866e5b51 FC |
137 | } |
138 | } | |
139 | ||
140 | // ------------------------------------------------------------------------ | |
141 | // Getters/Setters/Predicates | |
142 | // ------------------------------------------------------------------------ | |
143 | ||
009883d7 | 144 | @Override |
a4fa4e36 MK |
145 | public Definition getDefinition(String fieldName) { |
146 | if (fDefinitionsMap == null) { | |
2db699c2 AM |
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 | } | |
a4fa4e36 | 153 | } |
2db699c2 | 154 | fDefinitionsMap = mapBuilder.build(); |
a4fa4e36 | 155 | } |
2db699c2 | 156 | return fDefinitionsMap.get(fieldName); |
a4fa4e36 MK |
157 | } |
158 | ||
009883d7 | 159 | @Override |
a4fa4e36 MK |
160 | public List<String> getFieldNames() { |
161 | return fFieldNames; | |
866e5b51 FC |
162 | } |
163 | ||
9ac2eb62 | 164 | @Override |
866e5b51 | 165 | public StructDeclaration getDeclaration() { |
a4fa4e36 | 166 | return (StructDeclaration) super.getDeclaration(); |
866e5b51 FC |
167 | } |
168 | ||
169 | // ------------------------------------------------------------------------ | |
170 | // Operations | |
171 | // ------------------------------------------------------------------------ | |
172 | ||
866e5b51 FC |
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 | */ | |
a4fa4e36 MK |
180 | int val = fFieldNames.indexOf(lookupPath); |
181 | if (val != -1) { | |
182 | return fDefinitions[val]; | |
91847dfc | 183 | } |
a4fa4e36 MK |
184 | String lookupUnderscored = "_" + lookupPath; //$NON-NLS-1$ |
185 | val = fFieldNames.indexOf(lookupUnderscored); | |
186 | if (val != -1) { | |
187 | return fDefinitions[val]; | |
188 | } | |
189 | return null; | |
866e5b51 FC |
190 | } |
191 | ||
866e5b51 FC |
192 | @Override |
193 | public String toString() { | |
194 | StringBuilder builder = new StringBuilder(); | |
195 | ||
419f09a8 | 196 | builder.append("{ "); //$NON-NLS-1$ |
866e5b51 | 197 | |
a4fa4e36 MK |
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); | |
866e5b51 | 203 | } |
a4fa4e36 MK |
204 | Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$ |
205 | builder.append(joiner.join(fields)); | |
866e5b51 FC |
206 | } |
207 | ||
419f09a8 | 208 | builder.append(" }"); //$NON-NLS-1$ |
866e5b51 FC |
209 | |
210 | return builder.toString(); | |
211 | } | |
a4fa4e36 | 212 | |
866e5b51 | 213 | } |