Updating versions from 0.10.0 to 0.11.0 in pom files.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / StructDefinition.java
CommitLineData
866e5b51
FC
1/*******************************************************************************
2 * Copyright (c) 2011-2012 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
13package org.eclipse.linuxtools.ctf.core.event.types;
14
15import java.util.HashMap;
16import java.util.ListIterator;
17
18import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
19
20/**
21 * <b><u>StructDefinition</u></b>
22 */
23public class StructDefinition extends Definition implements IDefinitionScope {
24
25 // ------------------------------------------------------------------------
26 // Attributes
27 // ------------------------------------------------------------------------
28
29 private final StructDeclaration declaration;
30 private final HashMap<String, Definition> definitions = new HashMap<String, Definition>();
31
32 // ------------------------------------------------------------------------
33 // Constructors
34 // ------------------------------------------------------------------------
35
36 public StructDefinition(StructDeclaration declaration,
37 IDefinitionScope definitionScope, String structFieldName) {
38 super(definitionScope, structFieldName);
39
40 this.declaration = declaration;
41
42 for (String fName : declaration.getFieldsList()) {
43 IDeclaration fieldDecl = declaration.getFields().get(fName);
44 assert (fieldDecl != null);
45
46 Definition def = fieldDecl.createDefinition(this, fName);
47 definitions.put(fName, def);
48 }
49 }
50
51 // ------------------------------------------------------------------------
52 // Getters/Setters/Predicates
53 // ------------------------------------------------------------------------
54
55 @Override
56 public String getPath() {
57 return path;
58 }
59
60 public HashMap<String, Definition> getDefinitions() {
61 return definitions;
62 }
63
64 public StructDeclaration getDeclaration() {
65 return declaration;
66 }
67
68 // ------------------------------------------------------------------------
69 // Operations
70 // ------------------------------------------------------------------------
71
72 @Override
73 public void read(BitBuffer input) {
74 for (String fName : declaration.getFieldsList()) {
75 Definition def = definitions.get(fName);
76 assert (def != null);
77
78 def.read(input);
79 }
80 }
81
82 @Override
83 public Definition lookupDefinition(String lookupPath) {
84 /*
85 * The fields are created in order of appearance, so if a variant or
86 * sequence refers to a field that is after it, the field's definition
87 * will not be there yet in the hashmap.
88 */
89 return definitions.get(lookupPath);
90 }
91
92 public ArrayDefinition lookupArray(String name) {
93 Definition def = definitions.get(name);
94 return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
95 }
96
97 public EnumDefinition lookupEnum(String name) {
98 Definition def = definitions.get(name);
99 return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
100 }
101
102 public IntegerDefinition lookupInteger(String name) {
103 Definition def = definitions.get(name);
104 return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
105 : null);
106 }
107
108 public SequenceDefinition lookupSequence(String name) {
109 Definition def = definitions.get(name);
110 return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
111 : null);
112 }
113
114 public StringDefinition lookupString(String name) {
115 Definition def = definitions.get(name);
116 return (StringDefinition) ((def instanceof StringDefinition) ? def
117 : null);
118 }
119
120 public StructDefinition lookupStruct(String name) {
121 Definition def = definitions.get(name);
122 return (StructDefinition) ((def instanceof StructDefinition) ? def
123 : null);
124 }
125
126 public VariantDefinition lookupVariant(String name) {
127 Definition def = definitions.get(name);
128 return (VariantDefinition) ((def instanceof VariantDefinition) ? def
129 : null);
130 }
131
132 @Override
133 public String toString() {
134 StringBuilder builder = new StringBuilder();
135
136 int size = this.declaration.getFieldsList().size();
137 int n = 0;
138
139 if (size > 1) {
140 builder.append("{ "); //$NON-NLS-1$
141 }
142
143 ListIterator<String> listIterator = this.declaration.getFieldsList().listIterator();
144
145 while (listIterator.hasNext()) {
146 String field = listIterator.next();
147 builder.append(definitions.get(field).toString());
148 n++;
149 if (n != size) {
150 builder.append(", "); //$NON-NLS-1$
151 }
152 }
153
154 if (size > 1) {
155 builder.append(" }"); //$NON-NLS-1$
156 }
157
158 return builder.toString();
159 }
160}
This page took 0.030895 seconds and 5 git commands to generate.