1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.ctf
.core
.event
.types
;
15 import java
.util
.Collections
;
16 import java
.util
.LinkedList
;
17 import java
.util
.List
;
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
;
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
;
30 * A CTF structure definition (similar to a C structure).
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.
36 * TODO: move me to internal
39 * @author Matthew Khouzam
40 * @author Simon Marchi
42 public final class StructDefinition
extends ScopedDefinition
implements ICompositeDefinition
{
44 // ------------------------------------------------------------------------
46 // ------------------------------------------------------------------------
48 private final ImmutableList
<String
> fFieldNames
;
49 private final Definition
[] fDefinitions
;
50 private Map
<String
, Definition
> fDefinitionsMap
= null;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
60 * the parent declaration
61 * @param definitionScope
63 * @param structFieldName
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
;
82 * Constructor This one takes the scope and thus speeds up definition
86 * the parent declaration
87 * @param definitionScope
90 * the scope of this variable
91 * @param structFieldName
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
;
110 // ------------------------------------------------------------------------
111 // Getters/Setters/Predicates
112 // ------------------------------------------------------------------------
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
]);
124 fDefinitionsMap
= mapBuilder
.build();
126 return fDefinitionsMap
.get(fieldName
);
130 public List
<String
> getFieldNames() {
135 public StructDeclaration
getDeclaration() {
136 return (StructDeclaration
) super.getDeclaration();
139 // ------------------------------------------------------------------------
141 // ------------------------------------------------------------------------
144 public Definition
lookupDefinition(String lookupPath
) {
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.
150 int val
= fFieldNames
.indexOf(lookupPath
);
152 return fDefinitions
[val
];
154 String lookupUnderscored
= "_" + lookupPath
; //$NON-NLS-1$
155 val
= fFieldNames
.indexOf(lookupUnderscored
);
157 return fDefinitions
[val
];
159 return (Definition
) getDefinitionScope().lookupDefinition(lookupPath
);
163 public String
toString() {
164 StringBuilder builder
= new StringBuilder();
166 builder
.append("{ "); //$NON-NLS-1$
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
);
174 Joiner joiner
= Joiner
.on(", ").skipNulls(); //$NON-NLS-1$
175 builder
.append(joiner
.join(fields
));
178 builder
.append(" }"); //$NON-NLS-1$
180 return builder
.toString();