charts: Better protect ChartRange to avoid null ranges
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.chart.ui / src / org / eclipse / tracecompass / internal / tmf / chart / ui / format / ChartDecimalUnitFormat.java
1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Jonathan Rajotte-Julien
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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.text.FieldPosition;
15
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.eclipse.tracecompass.common.core.format.DecimalUnitFormat;
18 import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRangeMap;
19
20 /**
21 * Decimal formatter for graph
22 *
23 * Since the graph use normalized internal value the initial (external)
24 * representation needs to be obtained. Subsequent formatting is done based on a
25 * Double. Loss of precision could occurs based on the size. For now, loss of
26 * precision for decimal values is not a big concern. If it ever become one the
27 * use of Long while formatting might come in handy.
28 *
29 * @author Jonathan Rajotte-Julien
30 * @author Gabriel-Andrew Pollo-Guilbert
31 */
32 public class ChartDecimalUnitFormat extends DecimalUnitFormat {
33
34 // ------------------------------------------------------------------------
35 // Constants
36 // ------------------------------------------------------------------------
37
38 private static final long serialVersionUID = -4288059349658845257L;
39
40 // ------------------------------------------------------------------------
41 // Members
42 // ------------------------------------------------------------------------
43
44 private @Nullable ChartRangeMap fRangeMap;
45
46 // ------------------------------------------------------------------------
47 // Constructors
48 // ------------------------------------------------------------------------
49
50 /**
51 * Constructor with a range map supplied.
52 *
53 * @param map
54 * A chart range map for mapping values
55 */
56 public ChartDecimalUnitFormat(@Nullable ChartRangeMap map) {
57 super();
58 fRangeMap = map;
59 }
60
61 /**
62 * Constructor with a multiplication factor and range map.
63 *
64 * @param factor
65 * A multiplication factor to apply to the value
66 * @param map
67 * A chart range map for mapping values
68 */
69 public ChartDecimalUnitFormat(double factor, @Nullable ChartRangeMap map) {
70 super(factor);
71 fRangeMap = map;
72 }
73
74 // ------------------------------------------------------------------------
75 // Mutators
76 // ------------------------------------------------------------------------
77
78 /**
79 * Mutators that sets the chart range map of this formatter.
80 *
81 * @param map
82 * The new chart range map
83 */
84 public void setRangeMap(ChartRangeMap map) {
85 fRangeMap = map;
86 }
87
88 // ------------------------------------------------------------------------
89 // Operations
90 // ------------------------------------------------------------------------
91
92 @Override
93 public StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
94 if (!(obj instanceof Number) || toAppendTo == null) {
95 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj); //$NON-NLS-1$
96 }
97
98 Number number = (Number) obj;
99
100 /* If no map was provided, format with the number unchanged */
101 ChartRangeMap rangeMap = fRangeMap;
102 if (rangeMap == null) {
103 StringBuffer buffer = super.format(number, toAppendTo, pos);
104 return (buffer == null ? new StringBuffer() : buffer);
105 }
106
107 /* Find external value before formatting */
108 Double externalValue = checkNotNull(fRangeMap).getExternalValue(number).doubleValue();
109 StringBuffer buffer = super.format(externalValue, toAppendTo, pos);
110 return (buffer == null ? new StringBuffer() : buffer);
111 }
112
113 }
This page took 0.032267 seconds and 5 git commands to generate.