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 final @Nullable LamiGraphRange fInternalRange
;
38 private final @Nullable LamiGraphRange fExternalRange
;
43 public LamiDecimalUnitFormat() {
45 fInternalRange
= null;
46 fExternalRange
= null;
50 * Constructor with internal and external LamiRange for scale transformation
52 * @param internalRange
53 * The internal range used for graph representation
55 * @param externalRange
56 * The external (real value) range shown to the user
58 public LamiDecimalUnitFormat(@Nullable LamiGraphRange internalRange
, @Nullable LamiGraphRange externalRange
) {
60 fInternalRange
= internalRange
;
61 fExternalRange
= externalRange
;
65 * Constructor with multiplication factor and internal and external
66 * LamiRange for scale transformation.
69 * Multiplication factor to apply to the value
70 * @param internalRange
71 * The internal range used for graph representation
72 * @param externalRange
73 * The external (real value) range shown to the user
75 public LamiDecimalUnitFormat(double factor
, @Nullable LamiGraphRange internalRange
, @Nullable LamiGraphRange externalRange
) {
77 fInternalRange
= internalRange
;
78 fExternalRange
= externalRange
;
82 * @return the internal range definition
84 public @Nullable LamiGraphRange
getInternalRange() {
85 return fInternalRange
;
89 * @return the external range definition
91 public @Nullable LamiGraphRange
getExternalRange() {
92 return fExternalRange
;
96 public StringBuffer
format(@Nullable Object obj
, @Nullable StringBuffer toAppendTo
, @Nullable FieldPosition pos
) {
97 if (!(obj
instanceof Number
) || toAppendTo
== null) {
98 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj
); //$NON-NLS-1$
101 @Nullable LamiGraphRange internalRange
= fInternalRange
;
102 @Nullable LamiGraphRange externalRange
= fExternalRange
;
103 if (internalRange
== null || externalRange
== null) {
104 StringBuffer buffer
= super.format(obj
, toAppendTo
, pos
);
105 return (buffer
== null ?
new StringBuffer() : buffer
);
108 if (internalRange
.getDelta().compareTo(BigDecimal
.ZERO
) == 0) {
109 StringBuffer buffer
= super.format(externalRange
.getMinimum().doubleValue(), toAppendTo
, pos
);
110 return (buffer
== null ?
new StringBuffer() : buffer
);
113 if (externalRange
.getDelta().compareTo(BigDecimal
.ZERO
) == 0) {
114 StringBuffer buffer
= super.format(externalRange
.getMinimum().doubleValue(), toAppendTo
, pos
);
115 return (buffer
== null ?
new StringBuffer() : buffer
);
118 /* Find external value before formatting */
119 BigDecimal externalValue
= (new BigDecimal(obj
.toString()))
120 .subtract(internalRange
.getMinimum())
121 .multiply(externalRange
.getDelta())
122 .divide(internalRange
.getDelta(), BIG_DECIMAL_DIVISION_SCALE
, BigDecimal
.ROUND_DOWN
)
123 .add(externalRange
.getMinimum());
125 Double value
= externalValue
.doubleValue();
126 StringBuffer buffer
= super.format(value
, toAppendTo
, pos
);
127 return (buffer
== null ?
new StringBuffer() : buffer
);