1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson, EfficiOS Inc.
3 * Copyright (c) 2010, 2011 École Polytechnique de Montréal
4 * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com>
6 * All rights reserved. This program and the accompanying materials are
7 * made available under the terms of the Eclipse Public License v1.0 which
8 * accompanies this distribution, and is available at
9 * http://www.eclipse.org/legal/epl-v10.html
12 * Alexandre Montplaisir - Initial API and implementation
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.internal
.statesystem
.core
;
17 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import java
.io
.PrintWriter
;
20 import java
.util
.Collections
;
21 import java
.util
.LinkedHashMap
;
22 import java
.util
.LinkedList
;
25 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import com
.google
.common
.collect
.ImmutableList
;
30 * An Attribute is a "node" in the Attribute Tree. It represents a smallest
31 * unit of the model which can be in a particular state at a given time.
33 * It is abstract, as different implementations can provide different ways to
34 * access sub-attributes
36 * @author Alexandre Montplaisir
39 public final class Attribute
{
41 private final Attribute parent
;
42 private final @NonNull String name
;
43 private final int quark
;
45 /** The sub-attributes (<basename, attribute>) of this attribute */
46 private final Map
<String
, Attribute
> subAttributes
;
52 * The parent attribute of this one. Can be 'null' to represent
53 * this attribute is the root node of the tree.
55 * Base name of this attribute
57 * The integer representation of this attribute
59 public Attribute(Attribute parent
, @NonNull String name
, int quark
) {
63 this.subAttributes
= Collections
.synchronizedMap(new LinkedHashMap
<String
, Attribute
>());
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 * Get the quark (integer representation) of this attribute.
73 * @return The quark of this attribute
75 public int getQuark() {
80 * Get the name of this attribute.
82 * @return The name of this attribute
84 public @NonNull String
getName() {
89 * Get the list of child attributes below this one.
91 * @return The child attributes.
93 public Iterable
<Attribute
> getSubAttributes() {
94 return ImmutableList
.copyOf(subAttributes
.values());
98 * Get the matching quark for a given path-of-strings
101 * The path we are looking for, *relative to this node*.
102 * @return The matching quark, or -1 if that attribute does not exist.
104 public int getSubAttributeQuark(String
... path
) {
105 return this.getSubAttributeQuark(path
, 0);
109 * Other method to search through the attribute tree, but instead of
110 * returning the matching quark we return the AttributeTreeNode object
111 * itself. It can then be used as new "root node" for faster queries on the
115 * The target path, *relative to this node*
116 * @return The Node object matching the last element in the path, or "null"
117 * if that attribute does not exist.
119 public Attribute
getSubAttributeNode(String
... path
) {
120 return this.getSubAttributeNode(path
, 0);
124 * "Inner" part of the previous public method, which is used recursively. To
125 * avoid having to copy sub-arrays to pass down, we just track where we are
126 * at with the index parameter. It uses getSubAttributeNode(), whose
127 * implementation is left to the derived classes.
129 private int getSubAttributeQuark(String
[] path
, int index
) {
130 Attribute targetNode
= this.getSubAttributeNode(path
, index
);
131 if (targetNode
== null) {
134 return targetNode
.getQuark();
138 * Get the parent attribute of this attribute
140 * @return The parent attribute
142 public Attribute
getParentAttribute() {
147 * Get the parent quark of this attribute
149 * @return The quark of the parent attribute
151 public int getParentAttributeQuark() {
152 return this.parent
.getQuark();
155 /* The methods how to access children are left to derived classes */
158 * Add a sub-attribute to this attribute
160 * @param newSubAttribute The new attribute to add
162 public void addSubAttribute(Attribute newSubAttribute
) {
163 if (newSubAttribute
== null) {
164 throw new IllegalArgumentException();
166 subAttributes
.put(newSubAttribute
.getName(), newSubAttribute
);
170 * Get a sub-attribute from this node's sub-attributes
173 * The *full* path to the attribute
175 * The index in 'path' where this attribute is located
176 * (indicating where to start searching).
177 * @return The requested attribute
179 private Attribute
getSubAttributeNode(String
[] path
, int index
) {
180 final Attribute nextNode
= subAttributes
.get(path
[index
]);
182 if (nextNode
== null) {
183 /* We don't have the expected child => the attribute does not exist */
186 if (index
== path
.length
- 1) {
187 /* It's our job to process this request */
191 /* Pass on the rest of the path to the relevant child */
192 return nextNode
.getSubAttributeNode(path
, index
+ 1);
196 * Return a String array composed of the full (absolute) path representing
199 * @return The full attribute path elements
201 public String
@NonNull [] getFullAttribute() {
202 LinkedList
<String
> list
= new LinkedList
<>();
203 Attribute curNode
= this;
205 /* Add recursive parents to the list, but stop at the root node */
206 while (curNode
.parent
!= null) {
207 list
.addFirst(curNode
.getName());
208 curNode
= curNode
.parent
;
211 return list
.toArray(new String
[0]);
215 * Return the absolute path of this attribute, as a single slash-separated
218 * @return The full name of this attribute
220 public @NonNull String
getFullAttributeName() {
221 String
[] array
= this.getFullAttribute();
222 StringBuffer buf
= new StringBuffer();
224 for (int i
= 0; i
< array
.length
- 1; i
++) {
225 buf
.append(array
[i
]);
228 buf
.append(array
[array
.length
- 1]);
229 return checkNotNull(buf
.toString());
233 public String
toString() {
234 return getFullAttributeName() + " (" + quark
+ ')'; //$NON-NLS-1$
237 private int curDepth
;
239 private void attributeNodeToString(PrintWriter writer
, Attribute currentNode
) {
240 writer
.println(currentNode
.getName() + " (" + currentNode
.quark
+ ')'); //$NON-NLS-1$
243 for (Attribute nextNode
: currentNode
.getSubAttributes()) {
244 /* Skip printing 'null' entries */
245 if (nextNode
== null) {
248 for (int j
= 0; j
< curDepth
- 1; j
++) {
249 writer
.print(" "); //$NON-NLS-1$
251 writer
.print(" "); //$NON-NLS-1$
252 attributeNodeToString(writer
, nextNode
);
259 * Debugging method to print the contents of this attribute
262 * PrintWriter where to write the information
264 public void debugPrint(PrintWriter writer
) {
265 /* Only used for debugging, shouldn't be externalized */
266 writer
.println("------------------------------"); //$NON-NLS-1$
267 writer
.println("Attribute tree: (quark)\n"); //$NON-NLS-1$
269 attributeNodeToString(writer
, this);