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