Commit | Line | Data |
---|---|---|
5d10d135 ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | package org.eclipse.linuxtools.lttng.model; | |
13 | ||
14 | import java.util.Collection; | |
15 | import java.util.HashMap; | |
16 | import java.util.Map; | |
17 | ||
18 | import org.eclipse.linuxtools.lttng.TraceDebug; | |
19 | import org.eclipse.linuxtools.tmf.event.TmfData; | |
20 | ||
21 | public abstract class LTTngTreeNodeGeneric<E extends LTTngTreeNodeGeneric<E>> | |
22 | extends TmfData implements ILTTngTreeNode<E> { | |
23 | ||
24 | // ======================================================================== | |
25 | // Data | |
26 | // ======================================================================== | |
27 | private final Long fid; | |
28 | private final Object ftype; | |
29 | private final Object fvalue; | |
30 | protected final Map<Long, E> fchildren = new HashMap<Long, E>(); | |
31 | protected final Map<String, E> fchildrenByName = new HashMap<String, E>(); | |
32 | protected final Map<String, Object> fattributesByName = new HashMap<String, Object>(); | |
33 | private E fparent = null; | |
34 | private final String fname; | |
35 | private Long idCount = 0L; | |
36 | ||
37 | // ======================================================================== | |
38 | // Constructors | |
39 | // ======================================================================== | |
40 | /** | |
41 | * @param id | |
42 | * @param parent | |
43 | * @param name | |
44 | * @param value | |
45 | */ | |
46 | public LTTngTreeNodeGeneric(Long id, E parent, String name, | |
47 | Object value) { | |
48 | fid = id; | |
49 | fparent = parent; | |
50 | fname = name; | |
51 | ||
52 | if (value != null) { | |
53 | fvalue = value; | |
54 | ftype = fvalue.getClass(); | |
55 | } else { | |
56 | fvalue = this; | |
57 | ftype = this.getClass(); | |
58 | } | |
59 | } | |
60 | ||
61 | /** | |
62 | * @param id | |
63 | * @param parent | |
64 | * @param name | |
65 | */ | |
66 | public LTTngTreeNodeGeneric(Long id, E parent, String name) { | |
67 | this(id, parent, name, null); | |
68 | } | |
69 | ||
70 | /** | |
71 | * When parent is not know just yet | |
72 | * | |
73 | * @param id | |
74 | * @param type | |
75 | * @param name | |
76 | */ | |
77 | public LTTngTreeNodeGeneric(Long id, String name, Object value) { | |
78 | this(id, null, name, value); | |
79 | } | |
80 | ||
81 | // ======================================================================== | |
82 | // Methods | |
83 | // ======================================================================== | |
84 | ||
85 | /* | |
86 | * (non-Javadoc) | |
87 | * | |
88 | * @see org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getId() | |
89 | */ | |
d4011df2 | 90 | @Override |
5d10d135 ASL |
91 | public Long getId() { |
92 | return fid; | |
93 | } | |
94 | ||
95 | /* | |
96 | * (non-Javadoc) | |
97 | * | |
98 | * @see | |
99 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getType() | |
100 | */ | |
d4011df2 | 101 | @Override |
5d10d135 ASL |
102 | public Object getType() { |
103 | return ftype; | |
104 | } | |
105 | ||
106 | /* | |
107 | * (non-Javadoc) | |
108 | * | |
109 | * @see | |
110 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildByName | |
111 | * () | |
112 | */ | |
d4011df2 | 113 | @Override |
5d10d135 ASL |
114 | public E getChildByName(String name) { |
115 | E child = null; | |
116 | if (name != null) { | |
117 | child = fchildrenByName.get(name); | |
118 | } | |
119 | return child; | |
120 | } | |
121 | ||
122 | /** | |
123 | * @param child | |
124 | */ | |
125 | public void addChild(E child) { | |
126 | if (child != null) { | |
127 | Long id = child.getId(); | |
128 | if (id != null) { | |
129 | if (fchildren.containsKey(id) && fchildren.get(id) != child) { | |
130 | TraceDebug.debug("Replaced child " + id + " for: " + child); | |
131 | } | |
132 | fchildren.put(id, child); | |
133 | fchildrenByName.put(child.getName(), child); | |
134 | } | |
135 | } | |
136 | ||
137 | return; | |
138 | } | |
139 | ||
140 | /** | |
141 | * @param child | |
142 | */ | |
143 | public void removeChild(E child) { | |
144 | if (child != null) { | |
145 | Long id = child.getId(); | |
146 | if (id != null) { | |
147 | E childToRemove = fchildren.remove(id); | |
148 | if (childToRemove != null) { | |
149 | fchildrenByName.remove(childToRemove.getName()); | |
150 | } | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | /* | |
156 | * (non-Javadoc) | |
157 | * | |
158 | * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getChildren() | |
159 | */ | |
d4011df2 | 160 | @Override |
5d10d135 ASL |
161 | public abstract E[] getChildren(); |
162 | // { | |
163 | // return (T[]) childrenToArray(fchildren.values(), this.getClass()); | |
164 | // } | |
165 | ||
166 | /** | |
167 | * Convert from generic collection to generic array. An empty array is | |
168 | * provided when no children nodes are defined | |
169 | * | |
170 | * @param collection | |
171 | * @param k | |
172 | * @return | |
173 | */ | |
174 | @SuppressWarnings("unchecked") | |
175 | protected E[] childrenToArray(Collection<E> collection, Class<? extends E> k) { | |
176 | // check entries | |
177 | if (collection == null || k == null) { | |
178 | return null; | |
179 | } | |
180 | ||
181 | int size = collection.size(); | |
182 | ||
183 | // unchecked cast | |
184 | E[] childrenArray = (E[]) java.lang.reflect.Array.newInstance(k, size); | |
185 | int i = 0; | |
186 | for (E item : collection) { | |
187 | childrenArray[i++] = item; | |
188 | } | |
189 | ||
190 | return childrenArray; | |
191 | } | |
192 | ||
193 | /* | |
194 | * (non-Javadoc) | |
195 | * | |
196 | * @see | |
197 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildById | |
198 | * (java.lang.Long) | |
199 | */ | |
d4011df2 | 200 | @Override |
5d10d135 ASL |
201 | public E getChildById(Long id) { |
202 | if (id == null) | |
203 | return null; | |
204 | return fchildren.get(id); | |
205 | } | |
206 | ||
207 | ||
208 | /* | |
209 | * (non-Javadoc) | |
210 | * | |
211 | * @see | |
212 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getParent() | |
213 | */ | |
d4011df2 | 214 | @Override |
5d10d135 ASL |
215 | public E getParent() { |
216 | return fparent; | |
217 | } | |
218 | ||
219 | /** | |
220 | * @param parent | |
221 | */ | |
222 | public void setParent(E parent) { | |
223 | fparent = parent; | |
224 | } | |
225 | ||
226 | /* | |
227 | * (non-Javadoc) | |
228 | * | |
229 | * @see | |
230 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#hasChildren() | |
231 | */ | |
d4011df2 | 232 | @Override |
5d10d135 ASL |
233 | public boolean hasChildren() { |
234 | if (fchildren.size() > 0) { | |
235 | return true; | |
236 | } | |
237 | return false; | |
238 | } | |
239 | ||
240 | /* | |
241 | * (non-Javadoc) | |
242 | * | |
243 | * @see | |
244 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getName() | |
245 | */ | |
d4011df2 | 246 | @Override |
5d10d135 ASL |
247 | public String getName() { |
248 | return fname; | |
249 | } | |
250 | ||
251 | /* | |
252 | * (non-Javadoc) | |
253 | * | |
254 | * @see | |
255 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getPath() | |
256 | */ | |
d4011df2 | 257 | @Override |
5d10d135 ASL |
258 | public String getPath() { |
259 | return getPath(this, ""); | |
260 | } | |
261 | ||
262 | /** | |
263 | * Obtaining the path recursively up to the related parents until no parent | |
264 | * is found | |
265 | * | |
266 | * @param child | |
267 | * @param ipath | |
268 | * @return | |
269 | */ | |
270 | private String getPath(LTTngTreeNodeGeneric<E> child, | |
271 | String ipath) { | |
272 | String path = ipath; | |
273 | if (ipath != null) { | |
274 | if (child == null) { | |
275 | return ipath; | |
276 | } else { | |
277 | E parent = child.getParent(); | |
278 | path = getPath(parent, "/" + child.getName() + ipath); | |
279 | } | |
280 | } | |
281 | ||
282 | return path; | |
283 | } | |
284 | ||
285 | /* | |
286 | * (non-Javadoc) | |
287 | * | |
288 | * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) | |
289 | */ | |
d4011df2 | 290 | @Override |
5d10d135 ASL |
291 | @SuppressWarnings("rawtypes") |
292 | public Object getAdapter(Class clazz) { | |
293 | if (clazz == ftype) { | |
294 | return fvalue; | |
295 | } | |
296 | return null; | |
297 | } | |
298 | ||
299 | /* | |
300 | * (non-Javadoc) | |
301 | * | |
302 | * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getValue() | |
303 | */ | |
d4011df2 | 304 | @Override |
5d10d135 ASL |
305 | public Object getValue() { |
306 | return fvalue; | |
307 | } | |
308 | ||
309 | /* | |
310 | * (non-Javadoc) | |
311 | * | |
312 | * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getNextUniqueId() | |
313 | */ | |
d4011df2 | 314 | @Override |
5d10d135 ASL |
315 | public synchronized Long getNextUniqueId() { |
316 | return idCount++; | |
317 | } | |
318 | ||
319 | /* | |
320 | * (non-Javadoc) | |
321 | * | |
322 | * @see | |
323 | * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getAttribute(java.lang | |
324 | * .String) | |
325 | */ | |
d4011df2 | 326 | @Override |
5d10d135 ASL |
327 | @SuppressWarnings("unchecked") |
328 | public <T> T getAttribute(String key, Class<T> type) { | |
329 | if (key != null) { | |
330 | Object value = fattributesByName.get(key); | |
331 | if (value.getClass().isAssignableFrom(type)) { | |
332 | return (T) value; | |
333 | } | |
334 | } | |
335 | ||
336 | return null; | |
337 | } | |
338 | ||
339 | /* | |
340 | * (non-Javadoc) | |
341 | * | |
342 | * @see | |
343 | * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#addAttribute(java.lang | |
344 | * .String, java.lang.Object) | |
345 | */ | |
d4011df2 | 346 | @Override |
5d10d135 ASL |
347 | public boolean addAttribute(String key, Object value) { |
348 | // validate | |
349 | if (key == null || value == null) { | |
350 | return false; | |
351 | } | |
352 | ||
353 | fattributesByName.put(key, value); | |
354 | return true; | |
355 | } | |
356 | ||
357 | /* | |
358 | * (non-Javadoc) | |
359 | * | |
360 | * @see org.eclipse.linuxtools.tmf.event.TmfData#isNullRef() | |
361 | */ | |
550d787e | 362 | @Override |
5d10d135 ASL |
363 | public boolean isNullRef() { |
364 | return false; | |
365 | } | |
366 | } |