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 | */ | |
90 | public Long getId() { | |
91 | return fid; | |
92 | } | |
93 | ||
94 | /* | |
95 | * (non-Javadoc) | |
96 | * | |
97 | * @see | |
98 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getType() | |
99 | */ | |
100 | public Object getType() { | |
101 | return ftype; | |
102 | } | |
103 | ||
104 | /* | |
105 | * (non-Javadoc) | |
106 | * | |
107 | * @see | |
108 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildByName | |
109 | * () | |
110 | */ | |
111 | public E getChildByName(String name) { | |
112 | E child = null; | |
113 | if (name != null) { | |
114 | child = fchildrenByName.get(name); | |
115 | } | |
116 | return child; | |
117 | } | |
118 | ||
119 | /** | |
120 | * @param child | |
121 | */ | |
122 | public void addChild(E child) { | |
123 | if (child != null) { | |
124 | Long id = child.getId(); | |
125 | if (id != null) { | |
126 | if (fchildren.containsKey(id) && fchildren.get(id) != child) { | |
127 | TraceDebug.debug("Replaced child " + id + " for: " + child); | |
128 | } | |
129 | fchildren.put(id, child); | |
130 | fchildrenByName.put(child.getName(), child); | |
131 | } | |
132 | } | |
133 | ||
134 | return; | |
135 | } | |
136 | ||
137 | /** | |
138 | * @param child | |
139 | */ | |
140 | public void removeChild(E child) { | |
141 | if (child != null) { | |
142 | Long id = child.getId(); | |
143 | if (id != null) { | |
144 | E childToRemove = fchildren.remove(id); | |
145 | if (childToRemove != null) { | |
146 | fchildrenByName.remove(childToRemove.getName()); | |
147 | } | |
148 | } | |
149 | } | |
150 | } | |
151 | ||
152 | /* | |
153 | * (non-Javadoc) | |
154 | * | |
155 | * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getChildren() | |
156 | */ | |
157 | public abstract E[] getChildren(); | |
158 | // { | |
159 | // return (T[]) childrenToArray(fchildren.values(), this.getClass()); | |
160 | // } | |
161 | ||
162 | /** | |
163 | * Convert from generic collection to generic array. An empty array is | |
164 | * provided when no children nodes are defined | |
165 | * | |
166 | * @param collection | |
167 | * @param k | |
168 | * @return | |
169 | */ | |
170 | @SuppressWarnings("unchecked") | |
171 | protected E[] childrenToArray(Collection<E> collection, Class<? extends E> k) { | |
172 | // check entries | |
173 | if (collection == null || k == null) { | |
174 | return null; | |
175 | } | |
176 | ||
177 | int size = collection.size(); | |
178 | ||
179 | // unchecked cast | |
180 | E[] childrenArray = (E[]) java.lang.reflect.Array.newInstance(k, size); | |
181 | int i = 0; | |
182 | for (E item : collection) { | |
183 | childrenArray[i++] = item; | |
184 | } | |
185 | ||
186 | return childrenArray; | |
187 | } | |
188 | ||
189 | /* | |
190 | * (non-Javadoc) | |
191 | * | |
192 | * @see | |
193 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildById | |
194 | * (java.lang.Long) | |
195 | */ | |
196 | public E getChildById(Long id) { | |
197 | if (id == null) | |
198 | return null; | |
199 | return fchildren.get(id); | |
200 | } | |
201 | ||
202 | ||
203 | /* | |
204 | * (non-Javadoc) | |
205 | * | |
206 | * @see | |
207 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getParent() | |
208 | */ | |
209 | public E getParent() { | |
210 | return fparent; | |
211 | } | |
212 | ||
213 | /** | |
214 | * @param parent | |
215 | */ | |
216 | public void setParent(E parent) { | |
217 | fparent = parent; | |
218 | } | |
219 | ||
220 | /* | |
221 | * (non-Javadoc) | |
222 | * | |
223 | * @see | |
224 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#hasChildren() | |
225 | */ | |
226 | public boolean hasChildren() { | |
227 | if (fchildren.size() > 0) { | |
228 | return true; | |
229 | } | |
230 | return false; | |
231 | } | |
232 | ||
233 | /* | |
234 | * (non-Javadoc) | |
235 | * | |
236 | * @see | |
237 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getName() | |
238 | */ | |
239 | public String getName() { | |
240 | return fname; | |
241 | } | |
242 | ||
243 | /* | |
244 | * (non-Javadoc) | |
245 | * | |
246 | * @see | |
247 | * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getPath() | |
248 | */ | |
249 | public String getPath() { | |
250 | return getPath(this, ""); | |
251 | } | |
252 | ||
253 | /** | |
254 | * Obtaining the path recursively up to the related parents until no parent | |
255 | * is found | |
256 | * | |
257 | * @param child | |
258 | * @param ipath | |
259 | * @return | |
260 | */ | |
261 | private String getPath(LTTngTreeNodeGeneric<E> child, | |
262 | String ipath) { | |
263 | String path = ipath; | |
264 | if (ipath != null) { | |
265 | if (child == null) { | |
266 | return ipath; | |
267 | } else { | |
268 | E parent = child.getParent(); | |
269 | path = getPath(parent, "/" + child.getName() + ipath); | |
270 | } | |
271 | } | |
272 | ||
273 | return path; | |
274 | } | |
275 | ||
276 | /* | |
277 | * (non-Javadoc) | |
278 | * | |
279 | * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class) | |
280 | */ | |
281 | @SuppressWarnings("rawtypes") | |
282 | public Object getAdapter(Class clazz) { | |
283 | if (clazz == ftype) { | |
284 | return fvalue; | |
285 | } | |
286 | return null; | |
287 | } | |
288 | ||
289 | /* | |
290 | * (non-Javadoc) | |
291 | * | |
292 | * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getValue() | |
293 | */ | |
294 | public Object getValue() { | |
295 | return fvalue; | |
296 | } | |
297 | ||
298 | /* | |
299 | * (non-Javadoc) | |
300 | * | |
301 | * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getNextUniqueId() | |
302 | */ | |
303 | public synchronized Long getNextUniqueId() { | |
304 | return idCount++; | |
305 | } | |
306 | ||
307 | /* | |
308 | * (non-Javadoc) | |
309 | * | |
310 | * @see | |
311 | * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getAttribute(java.lang | |
312 | * .String) | |
313 | */ | |
314 | @SuppressWarnings("unchecked") | |
315 | public <T> T getAttribute(String key, Class<T> type) { | |
316 | if (key != null) { | |
317 | Object value = fattributesByName.get(key); | |
318 | if (value.getClass().isAssignableFrom(type)) { | |
319 | return (T) value; | |
320 | } | |
321 | } | |
322 | ||
323 | return null; | |
324 | } | |
325 | ||
326 | /* | |
327 | * (non-Javadoc) | |
328 | * | |
329 | * @see | |
330 | * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#addAttribute(java.lang | |
331 | * .String, java.lang.Object) | |
332 | */ | |
333 | public boolean addAttribute(String key, Object value) { | |
334 | // validate | |
335 | if (key == null || value == null) { | |
336 | return false; | |
337 | } | |
338 | ||
339 | fattributesByName.put(key, value); | |
340 | return true; | |
341 | } | |
342 | ||
343 | /* | |
344 | * (non-Javadoc) | |
345 | * | |
346 | * @see org.eclipse.linuxtools.tmf.event.TmfData#isNullRef() | |
347 | */ | |
550d787e | 348 | @Override |
5d10d135 ASL |
349 | public boolean isNullRef() { |
350 | return false; | |
351 | } | |
352 | } |