| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2012 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 | * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial API |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model; |
| 14 | |
| 15 | import java.util.HashMap; |
| 16 | import java.util.Map; |
| 17 | |
| 18 | /** |
| 19 | * Factory class to create and store TMF statistic trees. |
| 20 | * |
| 21 | * Based on a given tree node ID a TMF statistic tree is stored internally. A |
| 22 | * root node is created for each tree. Using the tree node ID the statistics |
| 23 | * tree can be retrieved. |
| 24 | * |
| 25 | * @author Mathieu Denis |
| 26 | * @version 2.0 |
| 27 | * @since 2.0 |
| 28 | */ |
| 29 | public class TmfStatisticsTreeManager { |
| 30 | |
| 31 | /** |
| 32 | * Contains the experiment name as the key and the traces data |
| 33 | */ |
| 34 | private static final Map<String, TmfStatisticsTree> fTreeInstances = new HashMap<String, TmfStatisticsTree>(); |
| 35 | |
| 36 | /** |
| 37 | * Provide a statisticsTree instance per trace |
| 38 | * |
| 39 | * @param traceUniqueId |
| 40 | * Unique ID for the trace |
| 41 | * @return The root node of the corresponding trace statistics tree |
| 42 | */ |
| 43 | public static TmfStatisticsTreeNode getStatTreeRoot(String traceUniqueId) { |
| 44 | |
| 45 | TmfStatisticsTree tree = getStatTree(traceUniqueId); |
| 46 | if (tree == null) { |
| 47 | return null; |
| 48 | } |
| 49 | return tree.getRootNode(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get the tree that's being used for statistics |
| 54 | * |
| 55 | * @param traceUniqueId |
| 56 | * Unique ID for the trace |
| 57 | * @return the corresponding trace statistics tree |
| 58 | */ |
| 59 | public static TmfStatisticsTree getStatTree(String traceUniqueId) { |
| 60 | if (traceUniqueId == null) { |
| 61 | return null; |
| 62 | } |
| 63 | |
| 64 | TmfStatisticsTree tree = fTreeInstances.get(traceUniqueId); |
| 65 | return tree; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Add the new trace statistics data in the tree. Can be used later on if |
| 70 | * the same traces is selected back. |
| 71 | * |
| 72 | * @param traceUniqueId |
| 73 | * The name of the trace which will be used as a key to store the |
| 74 | * data. Must be different for each traces, otherwise the traces |
| 75 | * might be overwritten which would trigger a reload of the same |
| 76 | * trace. |
| 77 | * @param statsData |
| 78 | * The information about the trace |
| 79 | * @return The newly created root node of the trace statistics tree, or null if something went wrong |
| 80 | */ |
| 81 | public static TmfStatisticsTreeNode addStatsTreeRoot(String traceUniqueId, TmfStatisticsTree statsData) { |
| 82 | if (traceUniqueId == null || statsData == null) { |
| 83 | return null; |
| 84 | } |
| 85 | fTreeInstances.put(traceUniqueId, statsData); |
| 86 | return statsData.getRootNode(); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Return if the given trace is currently known by the statistics manager. |
| 91 | * |
| 92 | * @param traceUniqueId |
| 93 | * The unique ID of the trace |
| 94 | * @return true if the trace id is known |
| 95 | */ |
| 96 | public static boolean containsTreeRoot(String traceUniqueId) { |
| 97 | return fTreeInstances.containsKey(traceUniqueId); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Remove previously registered statistics tree. |
| 102 | * |
| 103 | * @param traceUniqueId |
| 104 | * The unique ID of the trace |
| 105 | */ |
| 106 | public static void removeStatTreeRoot(String traceUniqueId) { |
| 107 | if (traceUniqueId != null && fTreeInstances.containsKey(traceUniqueId)) { |
| 108 | fTreeInstances.remove(traceUniqueId); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Remove all tree and root instances |
| 114 | */ |
| 115 | public static void removeAll() { |
| 116 | fTreeInstances.clear(); |
| 117 | } |
| 118 | } |