rcp: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / 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.tracecompass.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.tracecompass.ctf.core.event.scope.IDefinitionScope;
22 import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
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 * Constructor
58 *
59 * @param declaration
60 * the parent declaration
61 * @param definitionScope
62 * the parent scope
63 * @param structFieldName
64 * the field name
65 * @param definitions
66 * the definitions
67 * @since 1.0
68 */
69 public StructDefinition(@NonNull StructDeclaration declaration,
70 IDefinitionScope definitionScope,
71 @NonNull String structFieldName,
72 Definition[] definitions) {
73 super(declaration, definitionScope, structFieldName);
74 fFieldNames = ImmutableList.copyOf(declaration.getFieldsList());
75 fDefinitions = definitions;
76 if (fFieldNames.isEmpty()) {
77 fDefinitionsMap = Collections.EMPTY_MAP;
78 }
79 }
80
81 /**
82 * Constructor This one takes the scope and thus speeds up definition
83 * creation
84 *
85 * @param declaration
86 * the parent declaration
87 * @param definitionScope
88 * the parent scope
89 * @param scope
90 * the scope of this variable
91 * @param structFieldName
92 * the field name
93 * @param fieldNames
94 * the list of fields
95 * @param definitions
96 * the definitions
97 * @since 1.0
98 */
99 public StructDefinition(@NonNull StructDeclaration declaration,
100 IDefinitionScope definitionScope, @NonNull ILexicalScope scope,
101 @NonNull String structFieldName, @NonNull Iterable<String> fieldNames, Definition[] definitions) {
102 super(declaration, definitionScope, structFieldName, scope);
103 fFieldNames = ImmutableList.copyOf(fieldNames);
104 fDefinitions = definitions;
105 if (fFieldNames.isEmpty()) {
106 fDefinitionsMap = Collections.EMPTY_MAP;
107 }
108 }
109
110 // ------------------------------------------------------------------------
111 // Getters/Setters/Predicates
112 // ------------------------------------------------------------------------
113
114 @Override
115 public Definition getDefinition(String fieldName) {
116 if (fDefinitionsMap == null) {
117 /* Build the definitions map */
118 Builder<String, Definition> mapBuilder = new ImmutableMap.Builder<>();
119 for (int i = 0; i < fFieldNames.size(); i++) {
120 if (fDefinitions[i] != null) {
121 mapBuilder.put(fFieldNames.get(i), fDefinitions[i]);
122 }
123 }
124 fDefinitionsMap = mapBuilder.build();
125 }
126 return fDefinitionsMap.get(fieldName);
127 }
128
129 @Override
130 public List<String> getFieldNames() {
131 return fFieldNames;
132 }
133
134 @Override
135 public StructDeclaration getDeclaration() {
136 return (StructDeclaration) super.getDeclaration();
137 }
138
139 // ------------------------------------------------------------------------
140 // Operations
141 // ------------------------------------------------------------------------
142
143 @Override
144 public Definition lookupDefinition(String lookupPath) {
145 /*
146 * The fields are created in order of appearance, so if a variant or
147 * sequence refers to a field that is after it, the field's definition
148 * will not be there yet in the hashmap.
149 */
150 int val = fFieldNames.indexOf(lookupPath);
151 if (val != -1) {
152 return fDefinitions[val];
153 }
154 String lookupUnderscored = "_" + lookupPath; //$NON-NLS-1$
155 val = fFieldNames.indexOf(lookupUnderscored);
156 if (val != -1) {
157 return fDefinitions[val];
158 }
159 return null;
160 }
161
162 @Override
163 public String toString() {
164 StringBuilder builder = new StringBuilder();
165
166 builder.append("{ "); //$NON-NLS-1$
167
168 if (fFieldNames != null) {
169 List<String> fields = new LinkedList<>();
170 for (String field : fFieldNames) {
171 String appendee = field + " = " + lookupDefinition(field).toString(); //$NON-NLS-1$
172 fields.add(appendee);
173 }
174 Joiner joiner = Joiner.on(", ").skipNulls(); //$NON-NLS-1$
175 builder.append(joiner.join(fields));
176 }
177
178 builder.append(" }"); //$NON-NLS-1$
179
180 return builder.toString();
181 }
182
183 }
This page took 0.039912 seconds and 5 git commands to generate.