1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson
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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.core
.statesystem
;
15 import java
.io
.PrintWriter
;
16 import java
.util
.Collections
;
17 import java
.util
.HashMap
;
18 import java
.util
.LinkedList
;
21 import com
.google
.common
.collect
.ImmutableList
;
24 * An Attribute is a "node" in the Attribute Tree. It represents a smallest
25 * unit of the model which can be in a particular state at a given time.
27 * It is abstract, as different implementations can provide different ways to
28 * access sub-attributes
33 public final class Attribute
{
35 private final Attribute parent
;
36 private final String name
;
37 private final int quark
;
39 /** The sub-attributes (<basename, attribute>) of this attribute */
40 private final Map
<String
, Attribute
> subAttributes
;
46 * The parent attribute of this one. Can be 'null' to represent
47 * this attribute is the root node of the tree.
49 * Base name of this attribute
51 * The integer representation of this attribute
53 public Attribute(Attribute parent
, String name
, int quark
) {
57 this.subAttributes
= Collections
.synchronizedMap(new HashMap
<String
, Attribute
>());
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
65 * Get the quark (integer representation) of this attribute.
67 * @return The quark of this attribute
69 public int getQuark() {
74 * Get the name of this attribute.
76 * @return The name of this attribute
78 public String
getName() {
83 * Get the list of child attributes below this one.
85 * @return The child attributes.
87 public Iterable
<Attribute
> getSubAttributes() {
88 return ImmutableList
.copyOf(subAttributes
.values());
92 * Get the matching quark for a given path-of-strings
95 * The path we are looking for, *relative to this node*.
96 * @return The matching quark, or -1 if that attribute does not exist.
98 public int getSubAttributeQuark(String
... path
) {
99 return this.getSubAttributeQuark(path
, 0);
103 * Other method to search through the attribute tree, but instead of
104 * returning the matching quark we return the AttributeTreeNode object
105 * itself. It can then be used as new "root node" for faster queries on the
109 * The target path, *relative to this node*
110 * @return The Node object matching the last element in the path, or "null"
111 * if that attribute does not exist.
113 public Attribute
getSubAttributeNode(String
... path
) {
114 return this.getSubAttributeNode(path
, 0);
118 * "Inner" part of the previous public method, which is used recursively. To
119 * avoid having to copy sub-arrays to pass down, we just track where we are
120 * at with the index parameter. It uses getSubAttributeNode(), whose
121 * implementation is left to the derived classes.
123 private int getSubAttributeQuark(String
[] path
, int index
) {
124 Attribute targetNode
= this.getSubAttributeNode(path
, index
);
125 if (targetNode
== null) {
128 return targetNode
.getQuark();
132 * Get the parent attribute of this attribute
134 * @return The parent attribute
136 public Attribute
getParentAttribute() {
141 * Get the parent quark of this attribute
143 * @return The quark of the parent attribute
145 public int getParentAttributeQuark() {
146 return this.parent
.getQuark();
149 /* The methods how to access children are left to derived classes */
152 * Add a sub-attribute to this attribute
154 * @param newSubAttribute The new attribute to add
156 public void addSubAttribute(Attribute newSubAttribute
) {
157 if (newSubAttribute
== null || newSubAttribute
.getName() == null) {
158 throw new IllegalArgumentException();
160 subAttributes
.put(newSubAttribute
.getName(), newSubAttribute
);
164 * Get a sub-attribute from this node's sub-attributes
167 * The *full* path to the attribute
169 * The index in 'path' where this attribute is located
170 * (indicating where to start searching).
171 * @return The requested attribute
173 private Attribute
getSubAttributeNode(String
[] path
, int index
) {
174 final Attribute nextNode
= subAttributes
.get(path
[index
]);
176 if (nextNode
== null) {
177 /* We don't have the expected child => the attribute does not exist */
180 if (index
== path
.length
- 1) {
181 /* It's our job to process this request */
185 /* Pass on the rest of the path to the relevant child */
186 return nextNode
.getSubAttributeNode(path
, index
+ 1);
190 * Return a String array composed of the full (absolute) path representing
195 private String
[] getFullAttribute() {
196 LinkedList
<String
> list
= new LinkedList
<>();
197 Attribute curNode
= this;
199 /* Add recursive parents to the list, but stop at the root node */
200 while (curNode
.parent
!= null) {
201 list
.addFirst(curNode
.getName());
202 curNode
= curNode
.parent
;
205 return list
.toArray(new String
[0]);
209 * Return the absolute path of this attribute, as a single slash-separated
212 * @return The full name of this attribute
214 public String
getFullAttributeName() {
215 String
[] array
= this.getFullAttribute();
216 StringBuffer buf
= new StringBuffer();
218 for (int i
= 0; i
< array
.length
- 1; i
++) {
219 buf
.append(array
[i
]);
222 buf
.append(array
[array
.length
- 1]);
223 return buf
.toString();
227 public String
toString() {
228 return getFullAttributeName() + " (" + quark
+ ')'; //$NON-NLS-1$
231 private int curDepth
;
233 private void attributeNodeToString(PrintWriter writer
, Attribute currentNode
) {
234 writer
.println(currentNode
.getName() + " (" + currentNode
.quark
+ ')'); //$NON-NLS-1$
237 for (Attribute nextNode
: currentNode
.getSubAttributes()) {
238 /* Skip printing 'null' entries */
239 if (nextNode
== null) {
242 for (int j
= 0; j
< curDepth
- 1; j
++) {
243 writer
.print(" "); //$NON-NLS-1$
245 writer
.print(" "); //$NON-NLS-1$
246 attributeNodeToString(writer
, nextNode
);
253 * Debugging method to print the contents of this attribute
256 * PrintWriter where to write the information
258 public void debugPrint(PrintWriter writer
) {
259 /* Only used for debugging, shouldn't be externalized */
260 writer
.println("------------------------------"); //$NON-NLS-1$
261 writer
.println("Attribute tree: (quark)\n"); //$NON-NLS-1$
263 attributeNodeToString(writer
, this);