TMF: Add class to manage XML view information
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / event / scope / LexicalScope.java
CommitLineData
a4fa4e36
MK
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
12package org.eclipse.linuxtools.ctf.core.event.scope;
13
14import java.util.Collections;
15import java.util.HashMap;
16import java.util.Map;
17
18import org.eclipse.jdt.annotation.NonNull;
19import org.eclipse.jdt.annotation.NonNullByDefault;
20import org.eclipse.jdt.annotation.Nullable;
21
22import 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
31public class LexicalScope implements Comparable<LexicalScope> {
32 /**
33 * Empty string
34 *
35 * @since 3.0
36 */
37 public static final LexicalScope ROOT = new LexicalScope(null, ""); //$NON-NLS-1$
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 LexicalScope(PACKET, "header"); //$NON-NLS-1$
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 */
c5a0bb90 122 public static final LexicalScope TRACE_PACKET_HEADER = new LexicalScope(TRACE_PACKET, "header"); //$NON-NLS-1$
a4fa4e36
MK
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 */
c5a0bb90 136 public static final LexicalScope STREAM_EVENT_HEADER = new LexicalScope(STREAM_EVENT, "header"); //$NON-NLS-1$
a4fa4e36
MK
137
138 /**
139 * Fields in an event
140 *
141 * @since 3.0
142 */
143 public static final LexicalScope FIELDS = new LexicalScope(ROOT, "fields"); //$NON-NLS-1$
144
145 /**
146 * Context of an event
147 *
148 * @since 3.0
149 */
150 public static final LexicalScope CONTEXT = new LexicalScope(ROOT, "context"); //$NON-NLS-1$
151
152 /**
153 * Sorted list of parent paths
154 *
155 * @since 3.0
156 */
157 public static final LexicalScope[] PARENT_PATHS = {
158 ROOT,
159 CONTEXT,
160 FIELDS,
161 PACKET_HEADER,
162 STREAM_EVENT_CONTEXT,
163 STREAM_EVENT_HEADER,
164 STREAM_PACKET_CONTEXT,
165 TRACE_PACKET_HEADER
166 };
167
168 private int hash = 0;
169 private final String fName;
170 private final String fPath;
171 private final Map<String, LexicalScope> fChildren;
172
c5a0bb90 173
a4fa4e36
MK
174 /**
175 * The scope constructor
176 *
177 * @param parent
178 * The parent node, can be null, but shouldn't
179 * @param name
180 * the name of the field
181 */
182 public LexicalScope(@Nullable LexicalScope parent, String name) {
183 fName = name;
184 if (parent != null) {
185 String pathString = Joiner.on('.').skipNulls().join(parent.fPath, parent.getName());
186 if (pathString.startsWith(".")) { //$NON-NLS-1$
187 pathString = pathString.substring(1);
188 }
189 if (pathString == null) {
190 // we should get an NPE on pathString.startsWith before getting this
191 throw new IllegalStateException(
192 "Lexical scope constructor had null pathstring for " + //$NON-NLS-1$
193 parent.toString() + " and " + name); //$NON-NLS-1$
194 }
195 fPath = pathString;
196 parent.addChild(fName, this);
197 } else {
198 fPath = ""; //$NON-NLS-1$
199 }
200
201 @SuppressWarnings("null")
202 @NonNull
203 Map<String, LexicalScope> children =
204 Collections.synchronizedMap(new HashMap<String, LexicalScope>());
205 fChildren = children;
206 }
207
208 /**
209 * Adds a child lexical scope
210 *
211 * @param name
212 * the name of the child
213 * @param child
214 * the child
215 */
216 private void addChild(String name, LexicalScope child) {
217 fChildren.put(name, child);
218 }
219
220 /**
221 * Get the name
222 *
223 * @return the name
224 */
225 public String getName() {
226 return fName;
227 }
228
229 /**
230 * Gets a child of a given name
231 *
232 * @param name
233 * the child
234 * @return the scope, can be null
235 */
236 @Nullable
237 public LexicalScope getChild(String name) {
238 return fChildren.get(name);
239 }
240
241 @Override
242 public String toString() {
243 return fPath + '.' + fName;
244 }
245
246 @Override
247 public int compareTo(@Nullable LexicalScope other) {
248 if (other == null) {
249 throw new IllegalArgumentException();
250 }
251 int comp = fPath.compareTo(other.fPath);
252 if (comp == 0) {
253 return fName.compareTo(other.fName);
254 }
255 return comp;
256 }
257
258 @Override
259 public synchronized int hashCode() {
260 if (hash == 0) {
261 final int prime = 31;
262 hash = prime * (prime + fName.hashCode()) + fPath.hashCode();
263 }
264 return hash;
265 }
266
267 @Override
268 public boolean equals(@Nullable Object obj) {
269 if (this == obj) {
270 return true;
271 }
272 if (obj == null) {
273 return false;
274 }
275 if (getClass() != obj.getClass()) {
276 return false;
277 }
278 LexicalScope other = (LexicalScope) obj;
279 if (!fName.equals(other.fName)) {
280 return false;
281 }
282 return fPath.equals(other.fPath);
283 }
284}
This page took 0.037622 seconds and 5 git commands to generate.