Copyright header update, 2015 edition
[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 * @since 3.0
25 */
26 public final class TmfStatisticsFormatter {
27
28 /**
29 * Formatter for the column data
30 */
31 private static final NumberFormat FORMATTER = NumberFormat.getNumberInstance(Locale.getDefault());
32
33 TmfStatisticsFormatter() {
34 // Nothing to construct.
35 }
36
37 /**
38 * Generate the string for display in a cell.
39 *
40 * @param node
41 * Current node.
42 * @param config
43 * Configuration between total and partial.
44 * @return The formatted string ready for display.
45 */
46 public static String toColumnData(TmfStatisticsTreeNode node, StatsColumn config) {
47
48 long eventValue = 0;
49
50 switch (config) {
51
52 case TOTAL:
53 eventValue = node.getValues().getTotal();
54 break;
55
56 case PARTIAL:
57 eventValue = node.getValues().getPartial();
58 break;
59
60 // Other values are illegal.
61 // $CASES-OMITTED$
62 default:
63 throw new IllegalArgumentException();
64 }
65
66 return FORMATTER.format(eventValue);
67 }
68
69 /**
70 * Format the percentage according to user settings.
71 *
72 * @param percentage
73 * the percentage to format
74 * @return The formated percentage as a string.
75 */
76 public static String toPercentageText(double percentage) {
77
78 // The cast to long is needed because the formatter cannot truncate the number.
79 double truncPercentage = ((long) (1000.0 * percentage)) / 10.0;
80
81 String percentageString = String.format("%s%s%s", " ", FORMATTER.format(truncPercentage), " % "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
82 return percentageString;
83 }
84 }
This page took 0.050944 seconds and 5 git commands to generate.