1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Jonathan Rajotte-Julien
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.tmf
.chart
.ui
.format
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.text
.FieldPosition
;
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
;
21 * Decimal formatter for graph
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.
29 * @author Jonathan Rajotte-Julien
30 * @author Gabriel-Andrew Pollo-Guilbert
32 public class ChartDecimalUnitFormat
extends DecimalUnitFormat
{
34 // ------------------------------------------------------------------------
36 // ------------------------------------------------------------------------
38 private static final long serialVersionUID
= -4288059349658845257L;
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
44 private @Nullable ChartRangeMap fRangeMap
;
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * Constructor with a range map supplied.
54 * A chart range map for mapping values
56 public ChartDecimalUnitFormat(@Nullable ChartRangeMap map
) {
62 * Constructor with a multiplication factor and range map.
65 * A multiplication factor to apply to the value
67 * A chart range map for mapping values
69 public ChartDecimalUnitFormat(double factor
, @Nullable ChartRangeMap map
) {
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
79 * Mutators that sets the chart range map of this formatter.
82 * The new chart range map
84 public void setRangeMap(ChartRangeMap map
) {
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
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$
98 Number number
= (Number
) obj
;
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
);
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
);
This page took 0.032267 seconds and 5 git commands to generate.