charts: Add unit tests for the .ui plugin
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.chart.ui / src / org / eclipse / tracecompass / internal / tmf / chart / ui / format / LabelFormat.java
1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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
10 package org.eclipse.tracecompass.internal.tmf.chart.ui.format;
11
12 import java.text.FieldPosition;
13 import java.text.Format;
14 import java.text.ParsePosition;
15
16 import org.eclipse.jdt.annotation.Nullable;
17
18 import com.google.common.collect.BiMap;
19
20 /**
21 * Format label based on a given Map<String, Integer>.
22 *
23 * TODO: See if this formatter is specific to the swtchart charts that are
24 * implemented in this plugin or if they can be re-used in another other
25 * charting scheme. We'll probably know when we actually have another
26 * implementation. If swtchart specific, the name of the class and package
27 * should make it clear.
28 *
29 * @author Jonathan Rajotte-Julien
30 * @author Gabriel-Andrew Pollo-Guilbert
31 */
32 public class LabelFormat extends Format {
33
34 // ------------------------------------------------------------------------
35 // Constants
36 // ------------------------------------------------------------------------
37
38 private static final long serialVersionUID = -7046922375212377647L;
39 private static final String CHART_EMPTY_LABEL = " "; //$NON-NLS-1$
40
41 // ------------------------------------------------------------------------
42 // Members
43 // ------------------------------------------------------------------------
44
45 private final BiMap<String, Integer> fMap;
46
47 // ------------------------------------------------------------------------
48 // Constructors
49 // ------------------------------------------------------------------------
50
51 /**
52 * Constructor.
53 *
54 * @param map
55 * Map of labels and indices
56 */
57 public LabelFormat(BiMap<String, Integer> map) {
58 super();
59
60 fMap = map;
61 }
62
63 // ------------------------------------------------------------------------
64 // Operations
65 // ------------------------------------------------------------------------
66
67 @Override
68 public @Nullable StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
69 if (obj == null || toAppendTo == null) {
70 return new StringBuffer(CHART_EMPTY_LABEL);
71 }
72
73 Double doubleObj = (Double) obj;
74
75 /* If the key is not contained in the map, format a empty label */
76 if ((doubleObj % 1 != 0) || !fMap.containsValue((doubleObj.intValue()))) {
77 return new StringBuffer(CHART_EMPTY_LABEL);
78 }
79
80 /* If the value is null in the map, format an unknown label */
81 String key = fMap.inverse().get(doubleObj.intValue());
82
83 return toAppendTo.append(key);
84 }
85
86 @Override
87 public @Nullable Object parseObject(@Nullable String source, @Nullable ParsePosition pos) {
88 return fMap.get(source);
89 }
90
91 }
This page took 0.033878 seconds and 5 git commands to generate.