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
.provisional
.analysis
.lami
.ui
.format
;
12 import java
.math
.BigDecimal
;
13 import java
.text
.FieldPosition
;
15 import org
.eclipse
.jdt
.annotation
.Nullable
;
16 import org
.eclipse
.tracecompass
.common
.core
.format
.DecimalUnitFormat
;
17 import org
.eclipse
.tracecompass
.internal
.provisional
.analysis
.lami
.ui
.viewers
.LamiGraphRange
;
20 * Decimal formatter for Lami graph
22 * Since the graph use normalized internal value the initial (external)
23 * representation needs to be obtained. Subsequent formatting is done based on a
24 * Double. Loss of precision could occurs based on the size. For now, loss of
25 * precision for decimal values is not a big concern. If it ever become one the
26 * use of Long while formatting might come in handy.
28 * @author Jonathan Rajotte-Julien
30 public class LamiDecimalUnitFormat
extends DecimalUnitFormat
{
32 /** Maximum amount of digits that can be represented into a double */
33 private static final int BIG_DECIMAL_DIVISION_SCALE
= 22;
35 private static final long serialVersionUID
= 977671266270661188L;
37 private @Nullable LamiGraphRange fInternalRange
= null;
38 private @Nullable LamiGraphRange fExternalRange
= null;
43 public LamiDecimalUnitFormat() {
48 * Constructor with internal and external LamiRange for scale transformation
50 * @param internalRange
51 * The internal range used for graph representation
53 * @param externalRange
54 * The external (real value) range shown to the user
56 public LamiDecimalUnitFormat(LamiGraphRange internalRange
, LamiGraphRange externalRange
) {
58 fInternalRange
= internalRange
;
59 fExternalRange
= externalRange
;
63 * Constructor with multiplication factor.
66 * Multiplication factor to apply to the value
68 public LamiDecimalUnitFormat(double factor
) {
73 * Constructor with multiplication factor and internal and external
74 * LamiRange for scale transformation.
77 * Multiplication factor to apply to the value
78 * @param internalRange
79 * The internal range used for graph representation
80 * @param externalRange
81 * The external (real value) range shown to the user
83 public LamiDecimalUnitFormat(double factor
, LamiGraphRange internalRange
, LamiGraphRange externalRange
) {
85 fInternalRange
= internalRange
;
86 fExternalRange
= externalRange
;
90 * @return the internal range definition
92 public @Nullable LamiGraphRange
getInternalRange() {
93 return fInternalRange
;
97 * @param internalRange
98 * The internal range definition to be used by the formatter
100 public void setInternalRange(@Nullable LamiGraphRange internalRange
) {
101 fInternalRange
= internalRange
;
105 * @return the external range definition
107 public @Nullable LamiGraphRange
getExternalRange() {
108 return fExternalRange
;
112 * @param externalRange
113 * The external range definition to be used by the formatter
115 public void setExternalRange(@Nullable LamiGraphRange externalRange
) {
116 fExternalRange
= externalRange
;
120 public StringBuffer
format(@Nullable Object obj
, @Nullable StringBuffer toAppendTo
, @Nullable FieldPosition pos
) {
121 if (!(obj
instanceof Number
) || toAppendTo
== null) {
122 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj
); //$NON-NLS-1$
125 @Nullable LamiGraphRange internalRange
= fInternalRange
;
126 @Nullable LamiGraphRange externalRange
= fExternalRange
;
127 if (internalRange
== null || externalRange
== null) {
128 StringBuffer buffer
= super.format(obj
, toAppendTo
, pos
);
129 return (buffer
== null ?
new StringBuffer() : buffer
);
132 if (internalRange
.getDelta().compareTo(BigDecimal
.ZERO
) == 0) {
133 StringBuffer buffer
= super.format(externalRange
.getMinimum().doubleValue(), toAppendTo
, pos
);
134 return (buffer
== null ?
new StringBuffer() : buffer
);
137 if (externalRange
.getDelta().compareTo(BigDecimal
.ZERO
) == 0) {
138 StringBuffer buffer
= super.format(externalRange
.getMinimum().doubleValue(), toAppendTo
, pos
);
139 return (buffer
== null ?
new StringBuffer() : buffer
);
142 /* Find external value before formatting */
143 BigDecimal externalValue
= (new BigDecimal(obj
.toString()))
144 .subtract(internalRange
.getMinimum())
145 .multiply(externalRange
.getDelta())
146 .divide(internalRange
.getDelta(), BIG_DECIMAL_DIVISION_SCALE
, BigDecimal
.ROUND_DOWN
)
147 .add(externalRange
.getMinimum());
149 Double value
= externalValue
.doubleValue();
150 StringBuffer buffer
= super.format(value
, toAppendTo
, pos
);
151 return (buffer
== null ?
new StringBuffer() : buffer
);