1 /*******************************************************************************
2 * Copyright (c) 2012 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
.ArrayList
;
17 import java
.util
.Collections
;
18 import java
.util
.HashMap
;
19 import java
.util
.LinkedList
;
20 import java
.util
.List
;
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 abstract class Attribute
{
35 private final Attribute parent
;
36 private final String name
;
37 private final int quark
;
38 protected final List
<Attribute
> subAttributes
;
43 Attribute(Attribute parent
, String name
, int quark
) {
47 this.subAttributes
= new ArrayList
<>();
58 Attribute
getParent() {
62 List
<Attribute
> getSubAttributes() {
63 return Collections
.unmodifiableList(subAttributes
);
71 * Get the matching quark for a given path-of-strings
74 * The path we are looking for, *relative to this node*.
75 * @return The matching quark, or -1 if that attribute does not exist.
77 int getSubAttributeQuark(String
... path
) {
78 return this.getSubAttributeQuark(path
, 0);
82 * Other method to search through the attribute tree, but instead of
83 * returning the matching quark we return the AttributeTreeNode object
84 * itself. It can then be used as new "root node" for faster queries on the
88 * The target path, *relative to this node*
89 * @return The Node object matching the last element in the path, or "null"
90 * if that attribute does not exist.
92 Attribute
getSubAttributeNode(String
... path
) {
93 return this.getSubAttributeNode(path
, 0);
97 * "Inner" part of the previous public method, which is used recursively. To
98 * avoid having to copy sub-arrays to pass down, we just track where we are
99 * at with the index parameter. It uses getSubAttributeNode(), whose
100 * implementation is left to the derived classes.
102 private int getSubAttributeQuark(String
[] path
, int index
) {
103 Attribute targetNode
= this.getSubAttributeNode(path
, index
);
104 if (targetNode
== null) {
107 return targetNode
.getQuark();
110 /* The methods how to access children are left to derived classes */
111 abstract void addSubAttribute(Attribute newSubAttribute
);
112 abstract Attribute
getSubAttributeNode(String
[] path
, int index
);
115 * Return a String array composed of the full (absolute) path representing
120 String
[] getFullAttribute() {
121 LinkedList
<String
> list
= new LinkedList
<>();
122 Attribute curNode
= this;
124 /* Add recursive parents to the list, but stop at the root node */
125 while (curNode
.getParent() != null) {
126 list
.addFirst(curNode
.getName());
127 curNode
= curNode
.getParent();
130 return list
.toArray(new String
[0]);
134 * Return the absolute path of this attribute, as a single slash-separated
139 String
getFullAttributeName() {
140 String
[] array
= this.getFullAttribute();
141 StringBuffer buf
= new StringBuffer();
143 for (int i
= 0; i
< array
.length
- 1; i
++) {
144 buf
.append(array
[i
]);
147 buf
.append(array
[array
.length
- 1]);
148 return buf
.toString();
152 public String
toString() {
153 return getFullAttributeName() + " (" + quark
+ ')'; //$NON-NLS-1$
156 private int curDepth
;
158 private void attributeNodeToString(PrintWriter writer
, Attribute currentNode
) {
161 writer
.println(currentNode
.getName() + " (" + currentNode
.quark
+ ')'); //$NON-NLS-1$
164 for (Attribute nextNode
: currentNode
.getSubAttributes()) {
165 /* Skip printing 'null' entries */
166 if (nextNode
== null) {
169 for (j
= 0; j
< curDepth
- 1; j
++) {
170 writer
.print(" "); //$NON-NLS-1$
172 writer
.print(" "); //$NON-NLS-1$
173 attributeNodeToString(writer
, nextNode
);
179 void debugPrint(PrintWriter writer
) {
180 /* Only used for debugging, shouldn't be externalized */
181 writer
.println("------------------------------"); //$NON-NLS-1$
182 writer
.println("Attribute tree: (quark)\n"); //$NON-NLS-1$
184 attributeNodeToString(writer
, this);
190 * This is the basic implementation, where sub-attributes names can be composed
191 * of any alphanumeric characters, and are stored as Strings. A HashMap is used
197 final class AlphaNumAttribute
extends Attribute
{
199 private Map
<String
, Integer
> subAttributesMap
;
201 AlphaNumAttribute(Attribute parent
, String name
, int quark
) {
202 super(parent
, name
, quark
);
203 this.subAttributesMap
= new HashMap
<>();
207 synchronized void addSubAttribute(Attribute newSubAttribute
) {
208 assert (newSubAttribute
!= null);
209 assert (newSubAttribute
.getName() != null);
210 /* This should catch buggy state changing statements */
211 assert (!newSubAttribute
.getName().equals(this.getName()));
213 subAttributesMap
.put(newSubAttribute
.getName(), subAttributes
.size());
214 subAttributes
.add(newSubAttribute
);
218 protected synchronized Attribute
getSubAttributeNode(String
[] path
,
220 Integer indexOfNextNode
= subAttributesMap
.get(path
[index
]);
223 if (indexOfNextNode
== null) {
224 /* We don't have the expected child => the attribute does not exist */
227 if (index
== path
.length
- 1) {
228 /* It's our job to process this request */
229 return subAttributes
.get(indexOfNextNode
);
232 nextNode
= subAttributes
.get(indexOfNextNode
);
233 return nextNode
.getSubAttributeNode(path
, index
+ 1);