ctf: Move plugins to the Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / scope / LexicalScope.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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 *******************************************************************************/
11
12 package org.eclipse.linuxtools.ctf.core.event.scope;
13
14 import java.util.Collections;
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 import com.google.common.base.Joiner;
23
24 /**
25 * A node of a lexical scope
26 *
27 * @author Matthew Khouzam
28 * @since 3.0
29 */
30 @NonNullByDefault
31 public class LexicalScope implements Comparable<LexicalScope> {
32 /**
33 * Empty string
34 *
35 * @since 3.0
36 */
37 public static final LexicalScope ROOT = new RootScope();
38
39 /**
40 * Trace string
41 *
42 * @since 3.0
43 */
44 public static final LexicalScope TRACE = new LexicalScope(ROOT, "trace"); //$NON-NLS-1$
45
46 /**
47 * Env string
48 *
49 * @since 3.0
50 */
51 public static final LexicalScope ENV = new LexicalScope(ROOT, "env"); //$NON-NLS-1$
52
53 /**
54 * Stream string
55 *
56 * @since 3.0
57 */
58 public static final LexicalScope STREAM = new LexicalScope(ROOT, "stream"); //$NON-NLS-1$
59
60 /**
61 * Event string
62 *
63 * @since 3.0
64 */
65 public static final LexicalScope EVENT = new LexicalScope(ROOT, "event"); //$NON-NLS-1$
66
67 /**
68 * Variant string
69 *
70 * @since 3.0
71 */
72 public static final LexicalScope VARIANT = new LexicalScope(ROOT, "variant"); //$NON-NLS-1$
73
74 /**
75 * packet string
76 *
77 * @since 3.0
78 */
79 public static final LexicalScope PACKET = new LexicalScope(ROOT, "packet"); //$NON-NLS-1$
80
81 /**
82 * Packet header string
83 *
84 * @since 3.0
85 *
86 */
87 public static final LexicalScope PACKET_HEADER = new PacketHeaderScope();
88
89 /**
90 * Stream packet scope
91 *
92 * @since 3.0
93 */
94 public static final LexicalScope STREAM_PACKET = new LexicalScope(STREAM, "packet"); //$NON-NLS-1$
95
96 /**
97 * Stream Packet header string
98 *
99 * @since 3.0
100 */
101 public static final LexicalScope STREAM_PACKET_CONTEXT = new LexicalScope(STREAM_PACKET, "context"); //$NON-NLS-1$
102
103 /**
104 * Trace packet scope
105 *
106 * @since 3.0
107 */
108 public static final LexicalScope TRACE_PACKET = new LexicalScope(TRACE, "packet"); //$NON-NLS-1$
109
110 /**
111 * Stream event scope
112 *
113 * @since 3.0
114 */
115 public static final LexicalScope STREAM_EVENT = new LexicalScope(STREAM, "event"); //$NON-NLS-1$
116
117 /**
118 * Trace packet header string
119 *
120 * @since 3.0
121 */
122 public static final LexicalScope TRACE_PACKET_HEADER = new LexicalScope(TRACE_PACKET, "header"); //$NON-NLS-1$
123
124 /**
125 * Stream event context
126 *
127 * @since 3.0
128 */
129 public static final LexicalScope STREAM_EVENT_CONTEXT = new LexicalScope(STREAM_EVENT, "context"); //$NON-NLS-1$
130
131 /**
132 * Stream event header
133 *
134 * @since 3.0
135 */
136 public static final LexicalScope STREAM_EVENT_HEADER = new LexicalScope(STREAM_EVENT, "header"); //$NON-NLS-1$
137
138 /**
139 * Event header
140 *
141 * @since 3.1
142 */
143 public static final LexicalScope EVENT_HEADER = new EventHeaderScope(EVENT, "header"); //$NON-NLS-1$
144
145 /**
146 * Fields in an event
147 *
148 * @since 3.0
149 */
150 public static final LexicalScope FIELDS = new FieldsScope(ROOT, "fields"); //$NON-NLS-1$
151
152 /**
153 * Context of an event
154 *
155 * @since 3.0
156 */
157 public static final LexicalScope CONTEXT = new LexicalScope(ROOT, "context"); //$NON-NLS-1$
158
159 /**
160 * Sorted list of parent paths
161 *
162 * @since 3.0
163 */
164 public static final LexicalScope[] PARENT_PATHS = {
165 ROOT,
166 CONTEXT,
167 FIELDS,
168 PACKET_HEADER,
169 STREAM_EVENT_CONTEXT,
170 STREAM_EVENT_HEADER,
171 STREAM_PACKET_CONTEXT,
172 TRACE_PACKET_HEADER
173 };
174
175 private int hash = 0;
176 private final String fName;
177 private final String fPath;
178 private final Map<String, LexicalScope> fChildren;
179
180 /**
181 * The scope constructor
182 *
183 * @param parent
184 * The parent node, can be null, but shouldn't
185 * @param name
186 * the name of the field
187 */
188 @SuppressWarnings("null")
189 public LexicalScope(@Nullable LexicalScope parent, String name) {
190 fName = name;
191 if (parent != null) {
192 String pathString = Joiner.on('.').skipNulls().join(parent.fPath, parent.getName());
193 /*
194 * if joiner return null, we get an NPE... so we won't assign fPath
195 * to null
196 */
197 if (pathString.startsWith(".")) { //$NON-NLS-1$
198 /*
199 * substring throws an exception or returns a string, it won't
200 * return null
201 */
202 pathString = pathString.substring(1);
203 }
204 fPath = pathString;
205 parent.addChild(fName, this);
206 } else {
207 fPath = ""; //$NON-NLS-1$
208 }
209 @NonNull
210 Map<String, LexicalScope> children =
211 Collections.synchronizedMap(new HashMap<String, LexicalScope>());
212 fChildren = children;
213 }
214
215 /**
216 * Adds a child lexical scope
217 *
218 * @param name
219 * the name of the child
220 * @param child
221 * the child
222 */
223 private void addChild(String name, LexicalScope child) {
224 fChildren.put(name, child);
225 }
226
227 /**
228 * Get the name
229 *
230 * @return the name
231 */
232 public String getName() {
233 return fName;
234 }
235
236 /**
237 * Gets a child of a given name
238 *
239 * @param name
240 * the child
241 * @return the scope, can be null
242 */
243 @Nullable
244 public LexicalScope getChild(String name) {
245 return fChildren.get(name);
246 }
247
248 @Override
249 public String toString() {
250 return (fPath.isEmpty() ? fName : fPath + '.' + fName);
251 }
252
253 @Override
254 public int compareTo(@Nullable LexicalScope other) {
255 if (other == null) {
256 throw new IllegalArgumentException();
257 }
258 int comp = fPath.compareTo(other.fPath);
259 if (comp == 0) {
260 return fName.compareTo(other.fName);
261 }
262 return comp;
263 }
264
265 @Override
266 public synchronized int hashCode() {
267 if (hash == 0) {
268 final int prime = 31;
269 hash = prime * (prime + fName.hashCode()) + fPath.hashCode();
270 }
271 return hash;
272 }
273
274 @Override
275 public boolean equals(@Nullable Object obj) {
276 if (this == obj) {
277 return true;
278 }
279 if (obj == null) {
280 return false;
281 }
282 if (getClass() != obj.getClass()) {
283 return false;
284 }
285 LexicalScope other = (LexicalScope) obj;
286 if (!fName.equals(other.fName)) {
287 return false;
288 }
289 return fPath.equals(other.fPath);
290 }
291 }
This page took 0.051342 seconds and 5 git commands to generate.