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
.ChartRange
;
19 import org
.eclipse
.tracecompass
.internal
.tmf
.chart
.ui
.data
.ChartRangeMap
;
22 * Decimal formatter for graph
24 * Since the graph use normalized internal value the initial (external)
25 * representation needs to be obtained. Subsequent formatting is done based on a
26 * Double. Loss of precision could occurs based on the size. For now, loss of
27 * precision for decimal values is not a big concern. If it ever become one the
28 * use of Long while formatting might come in handy.
30 * @author Jonathan Rajotte-Julien
31 * @author Gabriel-Andrew Pollo-Guilbert
33 public class ChartDecimalUnitFormat
extends DecimalUnitFormat
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private static final long serialVersionUID
= -4288059349658845257L;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 private @Nullable ChartRangeMap fRangeMap
;
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
52 * Constructor with a range map supplied.
55 * A chart range map for mapping values
57 public ChartDecimalUnitFormat(@Nullable ChartRangeMap map
) {
63 * Constructor with a multiplication factor and range map.
66 * A multiplication factor to apply to the value
68 * A chart range map for mapping values
70 public ChartDecimalUnitFormat(double factor
, @Nullable ChartRangeMap map
) {
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
80 * Mutators that sets the chart range map of this formatter.
83 * The new chart range map
85 public void setRangeMap(ChartRangeMap map
) {
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
94 public StringBuffer
format(@Nullable Object obj
, @Nullable StringBuffer toAppendTo
, @Nullable FieldPosition pos
) {
95 if (!(obj
instanceof Number
) || toAppendTo
== null) {
96 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj
); //$NON-NLS-1$
99 Number number
= (Number
) obj
;
101 /* If no map was provided, format with the number unchanged */
102 ChartRangeMap rangeMap
= fRangeMap
;
103 if (rangeMap
== null) {
104 StringBuffer buffer
= super.format(number
, toAppendTo
, pos
);
105 return (buffer
== null ?
new StringBuffer() : buffer
);
108 ChartRange internalRange
= rangeMap
.getPlottedRange();
109 ChartRange externalRange
= rangeMap
.getInputDataRange();
111 /* If any range's delta is null, format with the external bounds */
112 if (internalRange
.isDeltaNull() || externalRange
.isDeltaNull()) {
113 StringBuffer buffer
= super.format(externalRange
.getMinimum().doubleValue(), toAppendTo
, pos
);
114 return (buffer
== null ?
new StringBuffer() : buffer
);
117 /* Find external value before formatting */
118 Double externalValue
= checkNotNull(fRangeMap
).getExternalValue(number
).doubleValue();
119 StringBuffer buffer
= super.format(externalValue
, toAppendTo
, pos
);
120 return (buffer
== null ?
new StringBuffer() : buffer
);
This page took 0.037442 seconds and 5 git commands to generate.