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 RootScope();
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 PacketHeaderScope();
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(TRACE_PACKET
, "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(STREAM_EVENT
, "header"); //$NON-NLS-1$
143 public static final LexicalScope EVENT_HEADER
= new EventHeaderScope(EVENT
, "header"); //$NON-NLS-1$
150 public static final LexicalScope FIELDS
= new FieldsScope(ROOT
, "fields"); //$NON-NLS-1$
153 * Context of an event
157 public static final LexicalScope CONTEXT
= new LexicalScope(ROOT
, "context"); //$NON-NLS-1$
160 * Sorted list of parent paths
164 public static final LexicalScope
[] PARENT_PATHS
= {
169 STREAM_EVENT_CONTEXT
,
171 STREAM_PACKET_CONTEXT
,
175 private int hash
= 0;
176 private final String fName
;
177 private final String fPath
;
178 private final Map
<String
, LexicalScope
> fChildren
;
181 * The scope constructor
184 * The parent node, can be null, but shouldn't
186 * the name of the field
188 @SuppressWarnings("null")
189 public LexicalScope(@Nullable LexicalScope parent
, String name
) {
191 if (parent
!= null) {
192 String pathString
= Joiner
.on('.').skipNulls().join(parent
.fPath
, parent
.getName());
194 * if joiner return null, we get an NPE... so we won't assign fPath
197 if (pathString
.startsWith(".")) { //$NON-NLS-1$
199 * substring throws an exception or returns a string, it won't
202 pathString
= pathString
.substring(1);
205 parent
.addChild(fName
, this);
207 fPath
= ""; //$NON-NLS-1$
210 Map
<String
, LexicalScope
> children
=
211 Collections
.synchronizedMap(new HashMap
<String
, LexicalScope
>());
212 fChildren
= children
;
216 * Adds a child lexical scope
219 * the name of the child
223 private void addChild(String name
, LexicalScope child
) {
224 fChildren
.put(name
, child
);
232 public String
getName() {
237 * Gets a child of a given name
241 * @return the scope, can be null
244 public LexicalScope
getChild(String name
) {
245 return fChildren
.get(name
);
249 public String
toString() {
250 return (fPath
.isEmpty() ? fName
: fPath
+ '.' + fName
);
254 public int compareTo(@Nullable LexicalScope other
) {
256 throw new IllegalArgumentException();
258 int comp
= fPath
.compareTo(other
.fPath
);
260 return fName
.compareTo(other
.fName
);
266 public synchronized int hashCode() {
268 final int prime
= 31;
269 hash
= prime
* (prime
+ fName
.hashCode()) + fPath
.hashCode();
275 public boolean equals(@Nullable Object obj
) {
282 if (getClass() != obj
.getClass()) {
285 LexicalScope other
= (LexicalScope
) obj
;
286 if (!fName
.equals(other
.fName
)) {
289 return fPath
.equals(other
.fPath
);