Commit | Line | Data |
---|---|---|
a52fde77 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2012, 2014 Ericsson |
a52fde77 AM |
3 | * Copyright (c) 2010, 2011 École Polytechnique de Montréal |
4 | * Copyright (c) 2010, 2011 Alexandre Montplaisir <alexandre.montplaisir@gmail.com> | |
cb42195c | 5 | * |
a52fde77 AM |
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 | |
cb42195c | 10 | * |
a52fde77 AM |
11 | *******************************************************************************/ |
12 | ||
18ab1d18 | 13 | package org.eclipse.linuxtools.internal.tmf.core.statesystem; |
a52fde77 AM |
14 | |
15 | import java.io.PrintWriter; | |
cb42195c | 16 | import java.util.ArrayList; |
a52fde77 AM |
17 | import java.util.Collections; |
18 | import java.util.HashMap; | |
19 | import java.util.LinkedList; | |
20 | import java.util.List; | |
cb42195c | 21 | import java.util.Map; |
a52fde77 AM |
22 | |
23 | /** | |
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. | |
cb42195c | 26 | * |
a52fde77 AM |
27 | * It is abstract, as different implementations can provide different ways to |
28 | * access sub-attributes | |
cb42195c | 29 | * |
a52fde77 | 30 | * @author alexmont |
cb42195c | 31 | * |
a52fde77 | 32 | */ |
a6917276 | 33 | public abstract class Attribute { |
a52fde77 AM |
34 | |
35 | private final Attribute parent; | |
36 | private final String name; | |
37 | private final int quark; | |
a6917276 AM |
38 | |
39 | /** The list of sub-attributes */ | |
cb42195c | 40 | protected final List<Attribute> subAttributes; |
a52fde77 AM |
41 | |
42 | /** | |
43 | * Constructor | |
a6917276 AM |
44 | * |
45 | * @param parent | |
46 | * The parent attribute of this one. Can be 'null' to represent | |
47 | * this attribute is the root node of the tree. | |
48 | * @param name | |
49 | * Base name of this attribute | |
50 | * @param quark | |
51 | * The integer representation of this attribute | |
a52fde77 | 52 | */ |
a6917276 | 53 | public Attribute(Attribute parent, String name, int quark) { |
a52fde77 AM |
54 | this.parent = parent; |
55 | this.quark = quark; | |
56 | this.name = name; | |
a4524c1b | 57 | this.subAttributes = new ArrayList<>(); |
a52fde77 AM |
58 | } |
59 | ||
a6917276 AM |
60 | // ------------------------------------------------------------------------ |
61 | // Accessors | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
a52fde77 | 64 | /** |
a6917276 AM |
65 | * Get the quark (integer representation) of this attribute. |
66 | * | |
67 | * @return The quark of this attribute | |
a52fde77 | 68 | */ |
a6917276 | 69 | public int getQuark() { |
a52fde77 AM |
70 | return quark; |
71 | } | |
72 | ||
a6917276 AM |
73 | /** |
74 | * Get the name of this attribute. | |
75 | * | |
76 | * @return The name of this attribute | |
77 | */ | |
78 | public String getName() { | |
79 | return name; | |
a52fde77 AM |
80 | } |
81 | ||
a6917276 AM |
82 | /** |
83 | * Get the list of child attributes below this one. This is a read-only | |
84 | * view. | |
85 | * | |
86 | * @return The list of child attributes. | |
87 | */ | |
88 | public List<Attribute> getSubAttributes() { | |
cb42195c | 89 | return Collections.unmodifiableList(subAttributes); |
a52fde77 AM |
90 | } |
91 | ||
a52fde77 AM |
92 | /** |
93 | * Get the matching quark for a given path-of-strings | |
cb42195c | 94 | * |
a52fde77 AM |
95 | * @param path |
96 | * The path we are looking for, *relative to this node*. | |
97 | * @return The matching quark, or -1 if that attribute does not exist. | |
98 | */ | |
a6917276 | 99 | public int getSubAttributeQuark(String... path) { |
a52fde77 AM |
100 | return this.getSubAttributeQuark(path, 0); |
101 | } | |
102 | ||
103 | /** | |
104 | * Other method to search through the attribute tree, but instead of | |
105 | * returning the matching quark we return the AttributeTreeNode object | |
106 | * itself. It can then be used as new "root node" for faster queries on the | |
107 | * tree. | |
cb42195c | 108 | * |
a52fde77 AM |
109 | * @param path |
110 | * The target path, *relative to this node* | |
111 | * @return The Node object matching the last element in the path, or "null" | |
112 | * if that attribute does not exist. | |
113 | */ | |
a6917276 | 114 | public Attribute getSubAttributeNode(String... path) { |
a52fde77 AM |
115 | return this.getSubAttributeNode(path, 0); |
116 | } | |
117 | ||
118 | /** | |
119 | * "Inner" part of the previous public method, which is used recursively. To | |
120 | * avoid having to copy sub-arrays to pass down, we just track where we are | |
121 | * at with the index parameter. It uses getSubAttributeNode(), whose | |
122 | * implementation is left to the derived classes. | |
123 | */ | |
124 | private int getSubAttributeQuark(String[] path, int index) { | |
125 | Attribute targetNode = this.getSubAttributeNode(path, index); | |
126 | if (targetNode == null) { | |
127 | return -1; | |
128 | } | |
129 | return targetNode.getQuark(); | |
130 | } | |
131 | ||
132 | /* The methods how to access children are left to derived classes */ | |
a6917276 AM |
133 | |
134 | /** | |
135 | * Add a sub-attribute to this attribute | |
136 | * | |
137 | * @param newSubAttribute The new attribute to add | |
138 | */ | |
139 | protected abstract void addSubAttribute(Attribute newSubAttribute); | |
140 | ||
141 | /** | |
142 | * Get a sub-attribute from this node's sub-attributes | |
143 | * | |
144 | * @param path | |
145 | * The *full* path to the attribute | |
146 | * @param index | |
147 | * The index in 'path' where this attribute is located | |
148 | * (indicating where to start searching). | |
149 | * @return The requested attribute | |
150 | */ | |
151 | protected abstract Attribute getSubAttributeNode(String[] path, int index); | |
a52fde77 AM |
152 | |
153 | /** | |
154 | * Return a String array composed of the full (absolute) path representing | |
155 | * this attribute | |
cb42195c | 156 | * |
a52fde77 AM |
157 | * @return |
158 | */ | |
a6917276 | 159 | private String[] getFullAttribute() { |
a4524c1b | 160 | LinkedList<String> list = new LinkedList<>(); |
a52fde77 AM |
161 | Attribute curNode = this; |
162 | ||
163 | /* Add recursive parents to the list, but stop at the root node */ | |
a6917276 | 164 | while (curNode.parent != null) { |
e2af45f9 | 165 | list.addFirst(curNode.getName()); |
a6917276 | 166 | curNode = curNode.parent; |
a52fde77 AM |
167 | } |
168 | ||
a52fde77 AM |
169 | return list.toArray(new String[0]); |
170 | } | |
171 | ||
172 | /** | |
173 | * Return the absolute path of this attribute, as a single slash-separated | |
174 | * String. | |
cb42195c | 175 | * |
a6917276 | 176 | * @return The full name of this attribute |
a52fde77 | 177 | */ |
a6917276 | 178 | public String getFullAttributeName() { |
ab604305 AM |
179 | String[] array = this.getFullAttribute(); |
180 | StringBuffer buf = new StringBuffer(); | |
a52fde77 | 181 | |
a52fde77 | 182 | for (int i = 0; i < array.length - 1; i++) { |
ab604305 AM |
183 | buf.append(array[i]); |
184 | buf.append('/'); | |
a52fde77 | 185 | } |
ab604305 AM |
186 | buf.append(array[array.length - 1]); |
187 | return buf.toString(); | |
a52fde77 AM |
188 | } |
189 | ||
190 | @Override | |
191 | public String toString() { | |
192 | return getFullAttributeName() + " (" + quark + ')'; //$NON-NLS-1$ | |
193 | } | |
194 | ||
195 | private int curDepth; | |
196 | ||
197 | private void attributeNodeToString(PrintWriter writer, Attribute currentNode) { | |
198 | int j; | |
199 | ||
200 | writer.println(currentNode.getName() + " (" + currentNode.quark + ')'); //$NON-NLS-1$ | |
201 | curDepth++; | |
202 | ||
cb42195c | 203 | for (Attribute nextNode : currentNode.getSubAttributes()) { |
a52fde77 AM |
204 | /* Skip printing 'null' entries */ |
205 | if (nextNode == null) { | |
206 | continue; | |
207 | } | |
208 | for (j = 0; j < curDepth - 1; j++) { | |
209 | writer.print(" "); //$NON-NLS-1$ | |
210 | } | |
211 | writer.print(" "); //$NON-NLS-1$ | |
212 | attributeNodeToString(writer, nextNode); | |
213 | } | |
214 | curDepth--; | |
215 | return; | |
216 | } | |
217 | ||
a6917276 AM |
218 | /** |
219 | * Debugging method to print the contents of this attribute | |
220 | * | |
221 | * @param writer | |
222 | * PrintWriter where to write the information | |
223 | */ | |
224 | public void debugPrint(PrintWriter writer) { | |
a52fde77 AM |
225 | /* Only used for debugging, shouldn't be externalized */ |
226 | writer.println("------------------------------"); //$NON-NLS-1$ | |
227 | writer.println("Attribute tree: (quark)\n"); //$NON-NLS-1$ | |
228 | curDepth = 0; | |
229 | attributeNodeToString(writer, this); | |
230 | writer.print('\n'); | |
231 | } | |
232 | } | |
233 | ||
234 | /** | |
235 | * This is the basic implementation, where sub-attributes names can be composed | |
236 | * of any alphanumeric characters, and are stored as Strings. A HashMap is used | |
237 | * to access them. | |
cb42195c | 238 | * |
a52fde77 | 239 | * @author alexmont |
cb42195c | 240 | * |
a52fde77 AM |
241 | */ |
242 | final class AlphaNumAttribute extends Attribute { | |
243 | ||
a6917276 | 244 | private final Map<String, Integer> subAttributesMap; |
a52fde77 | 245 | |
a6917276 | 246 | public AlphaNumAttribute(Attribute parent, String name, int quark) { |
a52fde77 | 247 | super(parent, name, quark); |
a4524c1b | 248 | this.subAttributesMap = new HashMap<>(); |
a52fde77 AM |
249 | } |
250 | ||
251 | @Override | |
a6917276 | 252 | protected synchronized void addSubAttribute(Attribute newSubAttribute) { |
a52fde77 AM |
253 | assert (newSubAttribute != null); |
254 | assert (newSubAttribute.getName() != null); | |
255 | /* This should catch buggy state changing statements */ | |
256 | assert (!newSubAttribute.getName().equals(this.getName())); | |
257 | ||
258 | subAttributesMap.put(newSubAttribute.getName(), subAttributes.size()); | |
259 | subAttributes.add(newSubAttribute); | |
260 | } | |
261 | ||
262 | @Override | |
263 | protected synchronized Attribute getSubAttributeNode(String[] path, | |
264 | int index) { | |
265 | Integer indexOfNextNode = subAttributesMap.get(path[index]); | |
266 | Attribute nextNode; | |
267 | ||
268 | if (indexOfNextNode == null) { | |
269 | /* We don't have the expected child => the attribute does not exist */ | |
270 | return null; | |
271 | } | |
272 | if (index == path.length - 1) { | |
273 | /* It's our job to process this request */ | |
274 | return subAttributes.get(indexOfNextNode); | |
275 | } | |
276 | ||
277 | nextNode = subAttributes.get(indexOfNextNode); | |
278 | return nextNode.getSubAttributeNode(path, index + 1); | |
279 | } | |
280 | } |