Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / statistics / model / TmfStatisticsFormatter.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Vincent Perot - Add percentages to the label provider
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.viewers.statistics.model;
14
15 import java.text.NumberFormat;
16 import java.util.Locale;
17
18 import org.eclipse.tracecompass.tmf.ui.viewers.statistics.model.TmfBaseColumnDataProvider.StatsColumn;
19
20 /**
21 * Class that format data for cells in the statistics view.
22 *
23 * @author Vincent Perot
24 */
25 public final class TmfStatisticsFormatter {
26
27 /**
28 * Formatter for the column data
29 */
30 private static final NumberFormat FORMATTER = NumberFormat.getNumberInstance(Locale.getDefault());
31
32 TmfStatisticsFormatter() {
33 // Nothing to construct.
34 }
35
36 /**
37 * Generate the string for display in a cell.
38 *
39 * @param node
40 * Current node.
41 * @param config
42 * Configuration between total and partial.
43 * @return The formatted string ready for display.
44 */
45 public static String toColumnData(TmfStatisticsTreeNode node, StatsColumn config) {
46
47 long eventValue = 0;
48
49 switch (config) {
50
51 case TOTAL:
52 eventValue = node.getValues().getTotal();
53 break;
54
55 case PARTIAL:
56 eventValue = node.getValues().getPartial();
57 break;
58
59 // Other values are illegal.
60 // $CASES-OMITTED$
61 default:
62 throw new IllegalArgumentException();
63 }
64
65 return FORMATTER.format(eventValue);
66 }
67
68 /**
69 * Format the percentage according to user settings.
70 *
71 * @param percentage
72 * the percentage to format
73 * @return The formated percentage as a string.
74 */
75 public static String toPercentageText(double percentage) {
76
77 // The cast to long is needed because the formatter cannot truncate the number.
78 double truncPercentage = ((long) (1000.0 * percentage)) / 10.0;
79
80 String percentageString = String.format("%s%s%s", " ", FORMATTER.format(truncPercentage), " % "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
81 return percentageString;
82 }
83 }
This page took 0.034805 seconds and 5 git commands to generate.