| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 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> - Implementation and Initial API |
| 11 | * Alexandre Montplaisir - Merge TmfBaseStatisticsTree and AbsStatisticsTree |
| 12 | * Move the tree structure logic into the nodes |
| 13 | *******************************************************************************/ |
| 14 | |
| 15 | package org.eclipse.linuxtools.tmf.ui.viewers.statistics.model; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * Base class for the statistics storage. It allow to implement a tree structure |
| 20 | * while avoiding the need to run through the tree each time you need to add a |
| 21 | * node at a given place. |
| 22 | * |
| 23 | * @author Mathieu Denis |
| 24 | * @version 2.0 |
| 25 | * @since 2.0 |
| 26 | */ |
| 27 | public class TmfStatisticsTree { |
| 28 | |
| 29 | /** Header for the event type categories. */ |
| 30 | public static final String HEADER_EVENT_TYPES = Messages.TmfStatisticsData_EventTypes; |
| 31 | |
| 32 | /** Root node of this tree */ |
| 33 | private final TmfStatisticsTreeNode rootNode; |
| 34 | |
| 35 | /** |
| 36 | * Default constructor. Creates base statistics tree for counting total |
| 37 | * number of events and number of events per event type. |
| 38 | */ |
| 39 | public TmfStatisticsTree() { |
| 40 | rootNode = new TmfStatisticsTreeNode(this, null, new String[0]); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Retrieve the root node of this tree. |
| 45 | * |
| 46 | * @return The root node |
| 47 | */ |
| 48 | public TmfStatisticsTreeNode getRootNode() { |
| 49 | return rootNode; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get a node. |
| 54 | * |
| 55 | * @param path |
| 56 | * Path to the node. |
| 57 | * @return The node, or null if it doesn't current exist in the tree. |
| 58 | */ |
| 59 | public TmfStatisticsTreeNode getNode(String... path) { |
| 60 | TmfStatisticsTreeNode curNode = rootNode; |
| 61 | for (String pathElem : path) { |
| 62 | curNode = curNode.getChild(pathElem); |
| 63 | if (curNode == null) { |
| 64 | /* The requested path doesn't exist, return null */ |
| 65 | break; |
| 66 | } |
| 67 | } |
| 68 | return curNode; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get or create a node. |
| 73 | * |
| 74 | * @param path |
| 75 | * Path to the node. |
| 76 | * @return The requested node. Will be created if it didn't exist. |
| 77 | */ |
| 78 | public TmfStatisticsTreeNode getOrCreateNode(String... path) { |
| 79 | TmfStatisticsTreeNode curNode = rootNode; |
| 80 | TmfStatisticsTreeNode nextNode; |
| 81 | for (String pathElem : path) { |
| 82 | nextNode = curNode.getChild(pathElem); |
| 83 | if (nextNode == null) { |
| 84 | nextNode = curNode.addChild(pathElem); |
| 85 | } |
| 86 | curNode = nextNode; |
| 87 | } |
| 88 | return curNode; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Set the value to display in the "total" cells. This means the row |
| 93 | * indicating the total count of events for a trace. |
| 94 | * |
| 95 | * @param traceName |
| 96 | * The name of the trace (will be used as a sub-tree in the view) |
| 97 | * @param isGlobal |
| 98 | * Is this a for a global or a time range request? Determines if |
| 99 | * this goes in the Global column or the Selected Time Range one. |
| 100 | * @param qty |
| 101 | * The value to display |
| 102 | */ |
| 103 | public void setTotal(String traceName, boolean isGlobal, long qty) { |
| 104 | String[][] paths = getNormalPaths(traceName); |
| 105 | for (String path[] : paths) { |
| 106 | getOrCreateNode(path).getValues().setValue(isGlobal, qty); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Set the value to display in the "Type count" cells. These are the counts |
| 112 | * for each event types. |
| 113 | * |
| 114 | * @param traceName |
| 115 | * The name of the trace (will be used as a sub-tree in the view) |
| 116 | * @param type |
| 117 | * The event type |
| 118 | * @param isGlobal |
| 119 | * Is this a for a global or a time range request? Determines if |
| 120 | * this goes in the Global column or the Selected Time Range one. |
| 121 | * @param qty |
| 122 | * The value to display |
| 123 | */ |
| 124 | public void setTypeCount(String traceName, String type, boolean isGlobal, long qty) { |
| 125 | String[][] paths = getTypePaths(traceName, type); |
| 126 | for (String[] path : paths) { |
| 127 | getOrCreateNode(path).getValues().setValue(isGlobal, qty); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Get the event types paths. |
| 133 | * |
| 134 | * @param traceName |
| 135 | * The name of the trace (will be used as a sub-tree in the view) |
| 136 | * @param type |
| 137 | * The event type |
| 138 | * @return Array of arrays representing the paths |
| 139 | */ |
| 140 | protected String[][] getTypePaths(String traceName, String type) { |
| 141 | String[][] paths = { new String[] {traceName, HEADER_EVENT_TYPES, type } }; |
| 142 | return paths; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Get the standard paths for an event. |
| 147 | * |
| 148 | * @param traceName |
| 149 | * The name of the trace (will be used as a sub-tree in the view) |
| 150 | * @return Array of arrays representing the paths |
| 151 | */ |
| 152 | protected String[][] getNormalPaths(String traceName) { |
| 153 | String[][] paths = { new String[] { traceName } }; |
| 154 | return paths; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Function to merge many string more efficiently. |
| 159 | * |
| 160 | * @param strings |
| 161 | * Strings to merge. |
| 162 | * @return A new string containing all the strings. |
| 163 | */ |
| 164 | protected static String mergeString(String... strings) { |
| 165 | StringBuilder builder = new StringBuilder(); |
| 166 | for (String s : strings) { |
| 167 | builder.append(s); |
| 168 | } |
| 169 | return builder.toString(); |
| 170 | } |
| 171 | } |