1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
;
14 import java
.util
.Collections
;
15 import java
.util
.HashMap
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import com
.google
.common
.base
.Joiner
;
25 * A node of a lexical scope
27 * @author Matthew Khouzam
31 public class LexicalScope
implements Comparable
<LexicalScope
> {
37 public static final LexicalScope ROOT
= new LexicalScope(null, ""); //$NON-NLS-1$
44 public static final LexicalScope TRACE
= new LexicalScope(ROOT
, "trace"); //$NON-NLS-1$
51 public static final LexicalScope ENV
= new LexicalScope(ROOT
, "env"); //$NON-NLS-1$
58 public static final LexicalScope STREAM
= new LexicalScope(ROOT
, "stream"); //$NON-NLS-1$
65 public static final LexicalScope EVENT
= new LexicalScope(ROOT
, "event"); //$NON-NLS-1$
72 public static final LexicalScope VARIANT
= new LexicalScope(ROOT
, "variant"); //$NON-NLS-1$
79 public static final LexicalScope PACKET
= new LexicalScope(ROOT
, "packet"); //$NON-NLS-1$
82 * Packet header string
87 public static final LexicalScope PACKET_HEADER
= new LexicalScope(PACKET
, "header"); //$NON-NLS-1$
94 public static final LexicalScope STREAM_PACKET
= new LexicalScope(STREAM
, "packet"); //$NON-NLS-1$
97 * Stream Packet header string
101 public static final LexicalScope STREAM_PACKET_CONTEXT
= new LexicalScope(STREAM_PACKET
, "context"); //$NON-NLS-1$
108 public static final LexicalScope TRACE_PACKET
= new LexicalScope(TRACE
, "packet"); //$NON-NLS-1$
115 public static final LexicalScope STREAM_EVENT
= new LexicalScope(STREAM
, "event"); //$NON-NLS-1$
118 * Trace packet header string
122 public static final LexicalScope TRACE_PACKET_HEADER
= new LexicalScope(STREAM_EVENT
, "header"); //$NON-NLS-1$
125 * Stream event context
129 public static final LexicalScope STREAM_EVENT_CONTEXT
= new LexicalScope(STREAM_EVENT
, "context"); //$NON-NLS-1$
132 * Stream event header
136 public static final LexicalScope STREAM_EVENT_HEADER
= new LexicalScope(TRACE_PACKET
, "header"); //$NON-NLS-1$
143 public static final LexicalScope FIELDS
= new LexicalScope(ROOT
, "fields"); //$NON-NLS-1$
146 * Context of an event
150 public static final LexicalScope CONTEXT
= new LexicalScope(ROOT
, "context"); //$NON-NLS-1$
153 * Sorted list of parent paths
157 public static final LexicalScope
[] PARENT_PATHS
= {
162 STREAM_EVENT_CONTEXT
,
164 STREAM_PACKET_CONTEXT
,
168 private int hash
= 0;
169 private final String fName
;
170 private final String fPath
;
171 private final Map
<String
, LexicalScope
> fChildren
;
174 * The scope constructor
177 * The parent node, can be null, but shouldn't
179 * the name of the field
181 public LexicalScope(@Nullable LexicalScope parent
, String name
) {
183 if (parent
!= null) {
184 String pathString
= Joiner
.on('.').skipNulls().join(parent
.fPath
, parent
.getName());
185 if (pathString
.startsWith(".")) { //$NON-NLS-1$
186 pathString
= pathString
.substring(1);
188 if (pathString
== null) {
189 // we should get an NPE on pathString.startsWith before getting this
190 throw new IllegalStateException(
191 "Lexical scope constructor had null pathstring for " + //$NON-NLS-1$
192 parent
.toString() + " and " + name
); //$NON-NLS-1$
195 parent
.addChild(fName
, this);
197 fPath
= ""; //$NON-NLS-1$
200 @SuppressWarnings("null")
202 Map
<String
, LexicalScope
> children
=
203 Collections
.synchronizedMap(new HashMap
<String
, LexicalScope
>());
204 fChildren
= children
;
208 * Adds a child lexical scope
211 * the name of the child
215 private void addChild(String name
, LexicalScope child
) {
216 fChildren
.put(name
, child
);
224 public String
getName() {
229 * Gets a child of a given name
233 * @return the scope, can be null
236 public LexicalScope
getChild(String name
) {
237 return fChildren
.get(name
);
241 public String
toString() {
242 return fPath
+ '.' + fName
;
246 public int compareTo(@Nullable LexicalScope other
) {
248 throw new IllegalArgumentException();
250 int comp
= fPath
.compareTo(other
.fPath
);
252 return fName
.compareTo(other
.fName
);
258 public synchronized int hashCode() {
260 final int prime
= 31;
261 hash
= prime
* (prime
+ fName
.hashCode()) + fPath
.hashCode();
267 public boolean equals(@Nullable Object obj
) {
274 if (getClass() != obj
.getClass()) {
277 LexicalScope other
= (LexicalScope
) obj
;
278 if (!fName
.equals(other
.fName
)) {
281 return fPath
.equals(other
.fPath
);