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 | ||
bcec0116 | 13 | package org.eclipse.linuxtools.internal.statesystem.core; |
a52fde77 AM |
14 | |
15 | import java.io.PrintWriter; | |
16 | import java.util.Collections; | |
fe0b6837 | 17 | import java.util.LinkedHashMap; |
a52fde77 | 18 | import java.util.LinkedList; |
cb42195c | 19 | import java.util.Map; |
a52fde77 | 20 | |
c3f21a07 AM |
21 | import com.google.common.collect.ImmutableList; |
22 | ||
a52fde77 AM |
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 | */ |
c3f21a07 | 33 | public final class Attribute { |
a52fde77 AM |
34 | |
35 | private final Attribute parent; | |
36 | private final String name; | |
37 | private final int quark; | |
a6917276 | 38 | |
c3f21a07 AM |
39 | /** The sub-attributes (<basename, attribute>) of this attribute */ |
40 | private final Map<String, 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; | |
fe0b6837 | 57 | this.subAttributes = Collections.synchronizedMap(new LinkedHashMap<String, Attribute>()); |
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 | 82 | /** |
c3f21a07 | 83 | * Get the list of child attributes below this one. |
a6917276 | 84 | * |
c3f21a07 | 85 | * @return The child attributes. |
a6917276 | 86 | */ |
c3f21a07 AM |
87 | public Iterable<Attribute> getSubAttributes() { |
88 | return ImmutableList.copyOf(subAttributes.values()); | |
a52fde77 AM |
89 | } |
90 | ||
a52fde77 AM |
91 | /** |
92 | * Get the matching quark for a given path-of-strings | |
cb42195c | 93 | * |
a52fde77 AM |
94 | * @param path |
95 | * The path we are looking for, *relative to this node*. | |
96 | * @return The matching quark, or -1 if that attribute does not exist. | |
97 | */ | |
a6917276 | 98 | public int getSubAttributeQuark(String... path) { |
a52fde77 AM |
99 | return this.getSubAttributeQuark(path, 0); |
100 | } | |
101 | ||
102 | /** | |
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 | |
106 | * tree. | |
cb42195c | 107 | * |
a52fde77 AM |
108 | * @param path |
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. | |
112 | */ | |
a6917276 | 113 | public Attribute getSubAttributeNode(String... path) { |
a52fde77 AM |
114 | return this.getSubAttributeNode(path, 0); |
115 | } | |
116 | ||
117 | /** | |
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. | |
122 | */ | |
123 | private int getSubAttributeQuark(String[] path, int index) { | |
124 | Attribute targetNode = this.getSubAttributeNode(path, index); | |
125 | if (targetNode == null) { | |
126 | return -1; | |
127 | } | |
128 | return targetNode.getQuark(); | |
129 | } | |
130 | ||
0fdd2c45 FG |
131 | /** |
132 | * Get the parent attribute of this attribute | |
133 | * | |
134 | * @return The parent attribute | |
135 | */ | |
136 | public Attribute getParentAttribute() { | |
137 | return this.parent; | |
138 | } | |
139 | ||
140 | /** | |
141 | * Get the parent quark of this attribute | |
142 | * | |
143 | * @return The quark of the parent attribute | |
144 | */ | |
145 | public int getParentAttributeQuark() { | |
146 | return this.parent.getQuark(); | |
147 | } | |
148 | ||
a52fde77 | 149 | /* The methods how to access children are left to derived classes */ |
a6917276 AM |
150 | |
151 | /** | |
152 | * Add a sub-attribute to this attribute | |
153 | * | |
154 | * @param newSubAttribute The new attribute to add | |
155 | */ | |
c3f21a07 AM |
156 | public void addSubAttribute(Attribute newSubAttribute) { |
157 | if (newSubAttribute == null || newSubAttribute.getName() == null) { | |
158 | throw new IllegalArgumentException(); | |
159 | } | |
160 | subAttributes.put(newSubAttribute.getName(), newSubAttribute); | |
161 | } | |
a6917276 AM |
162 | |
163 | /** | |
164 | * Get a sub-attribute from this node's sub-attributes | |
165 | * | |
166 | * @param path | |
167 | * The *full* path to the attribute | |
168 | * @param index | |
169 | * The index in 'path' where this attribute is located | |
170 | * (indicating where to start searching). | |
171 | * @return The requested attribute | |
172 | */ | |
c3f21a07 AM |
173 | private Attribute getSubAttributeNode(String[] path, int index) { |
174 | final Attribute nextNode = subAttributes.get(path[index]); | |
175 | ||
176 | if (nextNode == null) { | |
177 | /* We don't have the expected child => the attribute does not exist */ | |
178 | return null; | |
179 | } | |
180 | if (index == path.length - 1) { | |
181 | /* It's our job to process this request */ | |
182 | return nextNode; | |
183 | } | |
184 | ||
185 | /* Pass on the rest of the path to the relevant child */ | |
186 | return nextNode.getSubAttributeNode(path, index + 1); | |
187 | } | |
a52fde77 AM |
188 | |
189 | /** | |
190 | * Return a String array composed of the full (absolute) path representing | |
191 | * this attribute | |
cb42195c | 192 | * |
a52fde77 AM |
193 | * @return |
194 | */ | |
a6917276 | 195 | private String[] getFullAttribute() { |
a4524c1b | 196 | LinkedList<String> list = new LinkedList<>(); |
a52fde77 AM |
197 | Attribute curNode = this; |
198 | ||
199 | /* Add recursive parents to the list, but stop at the root node */ | |
a6917276 | 200 | while (curNode.parent != null) { |
e2af45f9 | 201 | list.addFirst(curNode.getName()); |
a6917276 | 202 | curNode = curNode.parent; |
a52fde77 AM |
203 | } |
204 | ||
a52fde77 AM |
205 | return list.toArray(new String[0]); |
206 | } | |
207 | ||
208 | /** | |
209 | * Return the absolute path of this attribute, as a single slash-separated | |
210 | * String. | |
cb42195c | 211 | * |
a6917276 | 212 | * @return The full name of this attribute |
a52fde77 | 213 | */ |
a6917276 | 214 | public String getFullAttributeName() { |
ab604305 AM |
215 | String[] array = this.getFullAttribute(); |
216 | StringBuffer buf = new StringBuffer(); | |
a52fde77 | 217 | |
a52fde77 | 218 | for (int i = 0; i < array.length - 1; i++) { |
ab604305 AM |
219 | buf.append(array[i]); |
220 | buf.append('/'); | |
a52fde77 | 221 | } |
ab604305 AM |
222 | buf.append(array[array.length - 1]); |
223 | return buf.toString(); | |
a52fde77 AM |
224 | } |
225 | ||
226 | @Override | |
227 | public String toString() { | |
228 | return getFullAttributeName() + " (" + quark + ')'; //$NON-NLS-1$ | |
229 | } | |
230 | ||
231 | private int curDepth; | |
232 | ||
233 | private void attributeNodeToString(PrintWriter writer, Attribute currentNode) { | |
a52fde77 AM |
234 | writer.println(currentNode.getName() + " (" + currentNode.quark + ')'); //$NON-NLS-1$ |
235 | curDepth++; | |
236 | ||
cb42195c | 237 | for (Attribute nextNode : currentNode.getSubAttributes()) { |
a52fde77 AM |
238 | /* Skip printing 'null' entries */ |
239 | if (nextNode == null) { | |
240 | continue; | |
241 | } | |
c3f21a07 | 242 | for (int j = 0; j < curDepth - 1; j++) { |
a52fde77 AM |
243 | writer.print(" "); //$NON-NLS-1$ |
244 | } | |
245 | writer.print(" "); //$NON-NLS-1$ | |
246 | attributeNodeToString(writer, nextNode); | |
247 | } | |
248 | curDepth--; | |
249 | return; | |
250 | } | |
251 | ||
a6917276 AM |
252 | /** |
253 | * Debugging method to print the contents of this attribute | |
254 | * | |
255 | * @param writer | |
256 | * PrintWriter where to write the information | |
257 | */ | |
258 | public void debugPrint(PrintWriter writer) { | |
a52fde77 AM |
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$ | |
262 | curDepth = 0; | |
263 | attributeNodeToString(writer, this); | |
264 | writer.print('\n'); | |
265 | } | |
266 | } |