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