ctf: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / ctf / 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 java.util.Map;
15 import java.util.concurrent.ConcurrentHashMap;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19
20 /**
21 * A node of a lexical scope
22 *
23 * @author Matthew Khouzam
24 */
25 public class LexicalScope implements ILexicalScope {
26 private int hash = 0;
27 private final @NonNull String fName;
28 private final @NonNull String fPath;
29 private final Map<String, ILexicalScope> fChildren = new ConcurrentHashMap<>();
30
31 /**
32 * Hidden constructor for the root node only
33 *
34 * @since 1.0
35 */
36 protected LexicalScope() {
37 fPath = ""; //$NON-NLS-1$
38 fName = ""; //$NON-NLS-1$
39 }
40
41 /**
42 * The scope constructor
43 *
44 * @param parent
45 * The parent node, can be null, but shouldn't
46 * @param name
47 * the name of the field
48 * @since 1.0
49 */
50 public LexicalScope(ILexicalScope parent, @NonNull String name) {
51 fName = name;
52 fPath = parent.getPath().isEmpty() ? fName : parent.getPath() + '.' + fName;
53 parent.addChild(name, this);
54 }
55
56 /**
57 * @since 1.0
58 */
59 @Override
60 public void addChild(String name, ILexicalScope child) {
61 fChildren.put(name, child);
62 }
63
64 @Override
65 public @NonNull String getName() {
66 return fName;
67 }
68
69 /**
70 * @since 1.0
71 */
72 @Override
73 public @Nullable ILexicalScope getChild(String name) {
74 return fChildren.get(name);
75 }
76
77 /**
78 * @since 1.0
79 */
80 @Override
81 public @NonNull String getPath() {
82 return fPath;
83 }
84
85 // for debugging purposes
86 @Override
87 public String toString() {
88 return getPath();
89 }
90
91 @Override
92 public synchronized int hashCode() {
93 if (hash == 0) {
94 final int prime = 31;
95 hash = prime * (prime + fName.hashCode()) + fPath.hashCode();
96 }
97 return hash;
98 }
99
100 @Override
101 public boolean equals(@Nullable Object obj) {
102 if (this == obj) {
103 return true;
104 }
105 if (obj == null) {
106 return false;
107 }
108 if (getClass() != obj.getClass()) {
109 return false;
110 }
111 LexicalScope other = (LexicalScope) obj;
112 if (!fName.equals(other.fName)) {
113 return false;
114 }
115 return fPath.equals(other.fPath);
116 }
117 }
This page took 0.047903 seconds and 5 git commands to generate.