Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8b9262516f909bc61ee509c9bfc795e5666eae8d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*******************************************************************************
 * Copyright (c) 2009, 2010 Ericsson
 * 
 * All rights reserved. This program and the accompanying materials are
 * made available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *   Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.lttng.core.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.linuxtools.lttng.core.LttngConstants;
import org.eclipse.linuxtools.lttng.core.TraceDebug;
import org.eclipse.linuxtools.tmf.core.event.TmfEvent;

public abstract class LTTngTreeNodeGeneric<E extends LTTngTreeNodeGeneric<E>>
		extends TmfEvent implements ILTTngTreeNode<E> {

	// ========================================================================
	// Data
	// ========================================================================
	private final Long fid;
	private final Object ftype;
	private final Object fvalue;
	protected final Map<Long, E> fchildren = new HashMap<Long, E>();
	protected final Map<String, E> fchildrenByName = new HashMap<String, E>();
	protected final Map<String, Object> fattributesByName = new HashMap<String, Object>();
	private E fparent = null;
	private final String fname;
	private Long idCount = 0L;

	// ========================================================================
	// Constructors
	// ========================================================================
	/**
	 * @param id
	 * @param parent
	 * @param name
	 * @param value
	 */
	public LTTngTreeNodeGeneric(Long id, E parent, String name,
			Object value) {
		fid = id;
		fparent = parent;
		fname = name;

		if (value != null) {
			fvalue = value;
			ftype = fvalue.getClass();
		} else {
			fvalue = this;
			ftype = this.getClass();
		}
	}

	/**
	 * @param id
	 * @param parent
	 * @param name
	 */
	public LTTngTreeNodeGeneric(Long id, E parent, String name) {
		this(id, parent, name, null);
	}

	/**
	 * When parent is not know just yet
	 * 
	 * @param id
	 * @param type
	 * @param name
	 */
	public LTTngTreeNodeGeneric(Long id, String name, Object value) {
		this(id, null, name, value);
	}

	// ========================================================================
	// Methods
	// ========================================================================

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getId()
	 */
	@Override
	public Long getId() {
		return fid;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getType()
	 */
	@Override
    public Object getNodeType() {
		return ftype;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildByName
	 * ()
	 */
	@Override
	public E getChildByName(String name) {
		E child = null;
		if (name != null) {
			child = fchildrenByName.get(name);
		}
		return child;
	}

	/**
	 * @param child
	 */
	public void addChild(E child) {
		if (child != null) {
			Long id = child.getId();
			if (id != null) {
				if (fchildren.containsKey(id) && fchildren.get(id) != child) {
					TraceDebug.debug("Replaced child " + id + " for: " + child);  //$NON-NLS-1$//$NON-NLS-2$
				}
				fchildren.put(id, child);
				fchildrenByName.put(child.getName(), child);
			}
		}

		return;
	}

	/**
	 * @param child
	 */
	public void removeChild(E child) {
		if (child != null) {
			Long id = child.getId();
			if (id != null) {
				E childToRemove = fchildren.remove(id);
				if (childToRemove != null) {
					fchildrenByName.remove(childToRemove.getName());
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getChildren()
	 */
	@Override
	public abstract E[] getChildren();
	// {
	// return (T[]) childrenToArray(fchildren.values(), this.getClass());
	// }

	/**
	 * Convert from generic collection to generic array. An empty array is
	 * provided when no children nodes are defined
	 * 
	 * @param collection
	 * @param k
	 * @return
	 */
	@SuppressWarnings("unchecked")
	protected E[] childrenToArray(Collection<E> collection, Class<? extends E> k) {
		// check entries
		if (collection == null || k == null) {
			return null;
		}

		int size = collection.size();

		// unchecked cast
		E[] childrenArray = (E[]) java.lang.reflect.Array.newInstance(k, size);
		int i = 0;
		for (E item : collection) {
			childrenArray[i++] = item;
		}

		return childrenArray;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getChildById
	 * (java.lang.Long)
	 */
	@Override
	public E getChildById(Long id) {
		if (id == null)
			return null;
		return fchildren.get(id);
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getParent()
	 */
	@Override
	public E getParent() {
		return fparent;
	}

	/**
	 * @param parent
	 */
	public void setParent(E parent) {
		fparent = parent;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#hasChildren()
	 */
	@Override
	public boolean hasChildren() {
		if (fchildren.size() > 0) {
			return true;
		}
		return false;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getName()
	 */
	@Override
	public String getName() {
		return fname;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.control.ILTTngAnalysisResource#getPath()
	 */
	@Override
	public String getPath() {
		return getPath(this, ""); //$NON-NLS-1$
	}

	/**
	 * Obtaining the path recursively up to the related parents until no parent
	 * is found
	 * 
	 * @param child
	 * @param ipath
	 * @return
	 */
	private String getPath(LTTngTreeNodeGeneric<E> child,
			String ipath) {
		String path = ipath;
		if (ipath != null) {
			if (child == null) {
				return ipath;
			} else {
				E parent = child.getParent();
				path = getPath(parent, "/" + child.getName() + ipath); //$NON-NLS-1$
			}
		}

		return path;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.runtime.IAdaptable#getAdapter(java.lang.Class)
	 */
	@Override
	@SuppressWarnings("rawtypes")
	public Object getAdapter(Class clazz) {
		if (clazz == ftype) {
			return fvalue;
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getValue()
	 */
	@Override
	public Object getValue() {
		return fvalue;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getNextUniqueId()
	 */
	@Override
	public synchronized Long getNextUniqueId() {
	    ++idCount;
	    if (idCount > LttngConstants.MAX_NUMBER_OF_TRACES_ID) {
	        idCount = 0L;
	    }
		return idCount | LttngConstants.STATS_TRACE_NAME_ID;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#getAttribute(java.lang
	 * .String)
	 */
	@Override
	@SuppressWarnings("unchecked")
	public <T> T getAttribute(String key, Class<T> type) {
		if (key != null) {
			Object value = fattributesByName.get(key);
			if (value.getClass().isAssignableFrom(type)) {
				return (T) value;
			}
		}

		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.linuxtools.lttng.model.ILTTngTreeNode#addAttribute(java.lang
	 * .String, java.lang.Object)
	 */
	@Override
	public boolean addAttribute(String key, Object value) {
		// validate
		if (key == null || value == null) {
			return false;
		}

		fattributesByName.put(key, value);
		return true;
	}

}

Back to the top