ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / types / VariantDefinition.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.linuxtools.ctf.core.event.types;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
17
18 /**
19 * A CTF variant definition (similar to a C union).
20 *
21 * A variant is similar to a C union, only taking the minimum size of the types,
22 * it is a compound data type that contains other datatypes in fields. they are
23 * stored in an hashmap and indexed by names which are strings.
24 *
25 * @version 1.0
26 * @author Matthew Khouzam
27 * @author Simon Marchi
28 */
29 public final class VariantDefinition extends Definition implements IDefinitionScope {
30
31 // ------------------------------------------------------------------------
32 // Attributes
33 // ------------------------------------------------------------------------
34
35 private final Definition fDefinition;
36 private final String fCurrentField;
37 private final String fFieldName;
38
39 // ------------------------------------------------------------------------
40 // Constructors
41 // ------------------------------------------------------------------------
42
43 /**
44 * Constructor
45 *
46 * @param declaration
47 * the parent declaration
48 * @param definitionScope
49 * the parent scope
50 * @param selectedField
51 * the selected field
52 * @param fieldName
53 * the field name
54 * @param fieldValue
55 * the field value
56 * @since 3.0
57 */
58 public VariantDefinition(@NonNull VariantDeclaration declaration,
59 IDefinitionScope definitionScope, String selectedField, @NonNull String fieldName, Definition fieldValue) {
60 super(declaration, definitionScope, fieldName);
61
62 fFieldName = fieldName;
63 fCurrentField = selectedField;
64 fDefinition = fieldValue;
65
66 }
67
68 // ------------------------------------------------------------------------
69 // Getters/Setters/Predicates
70 // ------------------------------------------------------------------------
71
72 @Override
73 public VariantDeclaration getDeclaration() {
74 return (VariantDeclaration) super.getDeclaration();
75 }
76
77 /**
78 * Get the current field name
79 *
80 * @return the current field name
81 */
82 public String getCurrentFieldName() {
83 return fCurrentField;
84 }
85
86 /**
87 * Get the current field
88 *
89 * @return the current field
90 */
91 public Definition getCurrentField() {
92 return fDefinition;
93 }
94
95 // ------------------------------------------------------------------------
96 // Operations
97 // ------------------------------------------------------------------------
98
99 @Override
100 public Definition lookupDefinition(String lookupPath) {
101 if (lookupPath == null) {
102 return null;
103 }
104 if (lookupPath.equals(fFieldName)) {
105 return fDefinition;
106 }
107 return getDefinitionScope().lookupDefinition(lookupPath);
108 }
109
110 /**
111 * Lookup an array in a struct. if the name returns a non-array (like an
112 * int) than the method returns null
113 *
114 * @param name
115 * the name of the array
116 * @return the array or null.
117 */
118 public ArrayDefinition lookupArray(String name) {
119 Definition def = lookupDefinition(name);
120 return (ArrayDefinition) ((def instanceof ArrayDefinition) ? def : null);
121 }
122
123 /**
124 * Lookup an enum in a struct. if the name returns a non-enum (like an int)
125 * than the method returns null
126 *
127 * @param name
128 * the name of the enum
129 * @return the enum or null.
130 */
131 public EnumDefinition lookupEnum(String name) {
132 Definition def = lookupDefinition(name);
133 return (EnumDefinition) ((def instanceof EnumDefinition) ? def : null);
134 }
135
136 /**
137 * Lookup an integer in a struct. if the name returns a non-integer (like an
138 * float) than the method returns null
139 *
140 * @param name
141 * the name of the integer
142 * @return the integer or null.
143 */
144 public IntegerDefinition lookupInteger(String name) {
145 Definition def = lookupDefinition(name);
146 return (IntegerDefinition) ((def instanceof IntegerDefinition) ? def
147 : null);
148 }
149
150 /**
151 * Lookup a sequence in a struct. if the name returns a non-sequence (like
152 * an int) than the method returns null
153 *
154 * @param name
155 * the name of the sequence
156 * @return the sequence or null.
157 */
158 public SequenceDefinition lookupSequence(String name) {
159 Definition def = lookupDefinition(name);
160 return (SequenceDefinition) ((def instanceof SequenceDefinition) ? def
161 : null);
162 }
163
164 /**
165 * Lookup a string in a struct. if the name returns a non-string (like an
166 * int) than the method returns null
167 *
168 * @param name
169 * the name of the string
170 * @return the string or null.
171 */
172 public StringDefinition lookupString(String name) {
173 Definition def = lookupDefinition(name);
174 return (StringDefinition) ((def instanceof StringDefinition) ? def
175 : null);
176 }
177
178 /**
179 * Lookup a struct in a struct. if the name returns a non-struct (like an
180 * int) than the method returns null
181 *
182 * @param name
183 * the name of the struct
184 * @return the struct or null.
185 */
186 public StructDefinition lookupStruct(String name) {
187 Definition def = lookupDefinition(name);
188 return (StructDefinition) ((def instanceof StructDefinition) ? def
189 : null);
190 }
191
192 /**
193 * Lookup a variant in a struct. if the name returns a non-variant (like an
194 * int) than the method returns null
195 *
196 * @param name
197 * the name of the variant
198 * @return the variant or null.
199 */
200 public VariantDefinition lookupVariant(String name) {
201 Definition def = lookupDefinition(name);
202 return (VariantDefinition) ((def instanceof VariantDefinition) ? def
203 : null);
204 }
205
206 @Override
207 public String toString() {
208 return "{ " + getCurrentFieldName() + //$NON-NLS-1$
209 " = " + getCurrentField() + //$NON-NLS-1$
210 " }"; //$NON-NLS-1$
211 }
212 }
This page took 0.061567 seconds and 5 git commands to generate.