1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.lttng
.core
.model
;
14 import java
.util
.Collection
;
15 import java
.util
.HashMap
;
18 import org
.eclipse
.linuxtools
.lttng
.core
.LttngConstants
;
19 import org
.eclipse
.linuxtools
.lttng
.core
.TraceDebug
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfData
;
22 public abstract class LTTngTreeNodeGeneric
<E
extends LTTngTreeNodeGeneric
<E
>>
23 extends TmfData
implements ILTTngTreeNode
<E
> {
25 // ========================================================================
27 // ========================================================================
28 private final Long fid
;
29 private final Object ftype
;
30 private final Object fvalue
;
31 protected final Map
<Long
, E
> fchildren
= new HashMap
<Long
, E
>();
32 protected final Map
<String
, E
> fchildrenByName
= new HashMap
<String
, E
>();
33 protected final Map
<String
, Object
> fattributesByName
= new HashMap
<String
, Object
>();
34 private E fparent
= null;
35 private final String fname
;
36 private Long idCount
= 0L;
38 // ========================================================================
40 // ========================================================================
47 public LTTngTreeNodeGeneric(Long id
, E parent
, String name
,
55 ftype
= fvalue
.getClass();
58 ftype
= this.getClass();
67 public LTTngTreeNodeGeneric(Long id
, E parent
, String name
) {
68 this(id
, parent
, name
, null);
72 * When parent is not know just yet
78 public LTTngTreeNodeGeneric(Long id
, String name
, Object value
) {
79 this(id
, null, name
, value
);
82 // ========================================================================
84 // ========================================================================
89 * @see org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getId()
100 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getType()
103 public Object
getType() {
111 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildByName
115 public E
getChildByName(String name
) {
118 child
= fchildrenByName
.get(name
);
126 public void addChild(E child
) {
128 Long id
= child
.getId();
130 if (fchildren
.containsKey(id
) && fchildren
.get(id
) != child
) {
131 TraceDebug
.debug("Replaced child " + id
+ " for: " + child
); //$NON-NLS-1$//$NON-NLS-2$
133 fchildren
.put(id
, child
);
134 fchildrenByName
.put(child
.getName(), child
);
144 public void removeChild(E child
) {
146 Long id
= child
.getId();
148 E childToRemove
= fchildren
.remove(id
);
149 if (childToRemove
!= null) {
150 fchildrenByName
.remove(childToRemove
.getName());
159 * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getChildren()
162 public abstract E
[] getChildren();
164 // return (T[]) childrenToArray(fchildren.values(), this.getClass());
168 * Convert from generic collection to generic array. An empty array is
169 * provided when no children nodes are defined
175 @SuppressWarnings("unchecked")
176 protected E
[] childrenToArray(Collection
<E
> collection
, Class
<?
extends E
> k
) {
178 if (collection
== null || k
== null) {
182 int size
= collection
.size();
185 E
[] childrenArray
= (E
[]) java
.lang
.reflect
.Array
.newInstance(k
, size
);
187 for (E item
: collection
) {
188 childrenArray
[i
++] = item
;
191 return childrenArray
;
198 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildById
202 public E
getChildById(Long id
) {
205 return fchildren
.get(id
);
213 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getParent()
216 public E
getParent() {
223 public void setParent(E parent
) {
231 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#hasChildren()
234 public boolean hasChildren() {
235 if (fchildren
.size() > 0) {
245 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getName()
248 public String
getName() {
256 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getPath()
259 public String
getPath() {
260 return getPath(this, ""); //$NON-NLS-1$
264 * Obtaining the path recursively up to the related parents until no parent
271 private String
getPath(LTTngTreeNodeGeneric
<E
> child
,
278 E parent
= child
.getParent();
279 path
= getPath(parent
, "/" + child
.getName() + ipath
); //$NON-NLS-1$
289 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
292 @SuppressWarnings("rawtypes")
293 public Object
getAdapter(Class clazz
) {
294 if (clazz
== ftype
) {
303 * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getValue()
306 public Object
getValue() {
313 * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getNextUniqueId()
316 public synchronized Long
getNextUniqueId() {
318 if (idCount
> LttngConstants
.MAX_NUMBER_OF_TRACES_ID
) {
321 return idCount
| LttngConstants
.STATS_TRACE_NAME_ID
;
328 * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getAttribute(java.lang
332 @SuppressWarnings("unchecked")
333 public <T
> T
getAttribute(String key
, Class
<T
> type
) {
335 Object value
= fattributesByName
.get(key
);
336 if (value
.getClass().isAssignableFrom(type
)) {
348 * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#addAttribute(java.lang
349 * .String, java.lang.Object)
352 public boolean addAttribute(String key
, Object value
) {
354 if (key
== null || value
== null) {
358 fattributesByName
.put(key
, value
);
365 * @see org.eclipse.linuxtools.tmf.event.TmfData#isNullRef()
368 public boolean isNullRef() {
This page took 0.039212 seconds and 5 git commands to generate.