Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / types / StructDefinition.java
CommitLineData
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
f357bcd4 13package org.eclipse.tracecompass.ctf.core.event.types;
866e5b51 14
a4fa4e36
MK
15import java.util.Collections;
16import java.util.LinkedList;
2b7f6f09 17import java.util.List;
843f986b 18import java.util.Map;
866e5b51 19
a4fa4e36 20import org.eclipse.jdt.annotation.NonNull;
f357bcd4
AM
21import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
22import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
a4fa4e36
MK
23
24import com.google.common.base.Joiner;
25import com.google.common.collect.ImmutableList;
26import com.google.common.collect.ImmutableMap;
27import 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 42public 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
2db699c2
AM
73 */
74 public StructDefinition(@NonNull StructDeclaration declaration,
75 IDefinitionScope definitionScope,
76 @NonNull String structFieldName,
77 List<String> fieldNames,
78 Definition[] definitions) {
79 this(declaration, definitionScope, structFieldName, (Iterable<String>) fieldNames, definitions);
80 }
81
9ac2eb62
MK
82 /**
83 * Constructor
84 *
85 * @param declaration
86 * the parent declaration
87 * @param definitionScope
88 * the parent scope
be6df2d8 89 * @param structFieldName
9ac2eb62 90 * the field name
a4fa4e36
MK
91 * @param fieldNames
92 * the list of fields
93 * @param definitions
94 * the definitions
9ac2eb62 95 */
a4fa4e36 96 public StructDefinition(@NonNull StructDeclaration declaration,
2db699c2
AM
97 IDefinitionScope definitionScope,
98 @NonNull String structFieldName,
99 Iterable<String> fieldNames,
100 Definition[] definitions) {
a4fa4e36
MK
101 super(declaration, definitionScope, structFieldName);
102 fFieldNames = ImmutableList.copyOf(fieldNames);
103 fDefinitions = definitions;
70f60307
MK
104 if (fFieldNames.isEmpty()) {
105 fDefinitionsMap = Collections.EMPTY_MAP;
106 }
107 }
108
109 /**
110 * Constructor This one takes the scope and thus speeds up definition
111 * creation
112 *
113 * @param declaration
114 * the parent declaration
115 * @param definitionScope
116 * the parent scope
117 * @param scope
118 * the scope of this variable
119 * @param structFieldName
120 * the field name
121 * @param fieldNames
122 * the list of fields
123 * @param definitions
124 * the definitions
70f60307
MK
125 */
126 public StructDefinition(@NonNull StructDeclaration declaration,
127 IDefinitionScope definitionScope, @NonNull LexicalScope scope,
128 @NonNull String structFieldName, @NonNull Iterable<String> fieldNames, Definition[] definitions) {
129 super(declaration, definitionScope, structFieldName, scope);
130 fFieldNames = ImmutableList.copyOf(fieldNames);
131 fDefinitions = definitions;
009883d7 132 if (fFieldNames.isEmpty()) {
a4fa4e36 133 fDefinitionsMap = Collections.EMPTY_MAP;
866e5b51
FC
134 }
135 }
136
137 // ------------------------------------------------------------------------
138 // Getters/Setters/Predicates
139 // ------------------------------------------------------------------------
140
009883d7 141 @Override
a4fa4e36
MK
142 public Definition getDefinition(String fieldName) {
143 if (fDefinitionsMap == null) {
2db699c2
AM
144 /* Build the definitions map */
145 Builder<String, Definition> mapBuilder = new ImmutableMap.Builder<>();
146 for (int i = 0; i < fFieldNames.size(); i++) {
147 if (fDefinitions[i] != null) {
148 mapBuilder.put(fFieldNames.get(i), fDefinitions[i]);
149 }
a4fa4e36 150 }
2db699c2 151 fDefinitionsMap = mapBuilder.build();
a4fa4e36 152 }
2db699c2 153 return fDefinitionsMap.get(fieldName);
a4fa4e36
MK
154 }
155
009883d7 156 @Override
a4fa4e36
MK
157 public List<String> getFieldNames() {
158 return fFieldNames;
866e5b51
FC
159 }
160
9ac2eb62 161 @Override
866e5b51 162 public StructDeclaration getDeclaration() {
a4fa4e36 163 return (StructDeclaration) super.getDeclaration();
866e5b51
FC
164 }
165
166 // ------------------------------------------------------------------------
167 // Operations
168 // ------------------------------------------------------------------------
169
866e5b51
FC
170 @Override
171 public Definition lookupDefinition(String lookupPath) {
172 /*
173 * The fields are created in order of appearance, so if a variant or
174 * sequence refers to a field that is after it, the field's definition
175 * will not be there yet in the hashmap.
176 */
a4fa4e36
MK
177 int val = fFieldNames.indexOf(lookupPath);
178 if (val != -1) {
179 return fDefinitions[val];
91847dfc 180 }
a4fa4e36
MK
181 String lookupUnderscored = "_" + lookupPath; //$NON-NLS-1$
182 val = fFieldNames.indexOf(lookupUnderscored);
183 if (val != -1) {
184 return fDefinitions[val];
185 }
186 return null;
866e5b51
FC
187 }
188
866e5b51
FC
189 @Override
190 public String toString() {
191 StringBuilder builder = new StringBuilder();
192
419f09a8 193 builder.append("{ "); //$NON-NLS-1$
866e5b51 194
a4fa4e36
MK
195 if (fFieldNames != null) {
196 List<String> fields = new LinkedList<>();
197 for (String field : fFieldNames) {
198 String appendee = field + " = " + lookupDefinition(field).toString(); //$NON-NLS-1$
199 fields.add(appendee);
866e5b51 200 }
a4fa4e36
MK
201 Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
202 builder.append(joiner.join(fields));
866e5b51
FC
203 }
204
419f09a8 205 builder.append(" }"); //$NON-NLS-1$
866e5b51
FC
206
207 return builder.toString();
208 }
a4fa4e36 209
866e5b51 210}
This page took 0.070574 seconds and 5 git commands to generate.