Commit | Line | Data |
---|---|---|
5b973e7c JR |
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.provisional.analysis.lami.ui.format; | |
11 | ||
12 | import java.math.BigDecimal; | |
13 | import java.text.FieldPosition; | |
14 | ||
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; | |
18 | ||
19 | /** | |
20 | * Decimal formatter for Lami graph | |
21 | * | |
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. | |
27 | * | |
28 | * @author Jonathan Rajotte-Julien | |
29 | */ | |
30 | public class LamiDecimalUnitFormat extends DecimalUnitFormat { | |
31 | ||
32 | /** Maximum amount of digits that can be represented into a double */ | |
33 | private static final int BIG_DECIMAL_DIVISION_SCALE = 22; | |
34 | ||
35 | private static final long serialVersionUID = 977671266270661188L; | |
36 | ||
f6c5f8e1 JR |
37 | private final @Nullable LamiGraphRange fInternalRange; |
38 | private final @Nullable LamiGraphRange fExternalRange; | |
5b973e7c JR |
39 | |
40 | /** | |
41 | * Default constructor | |
42 | */ | |
43 | public LamiDecimalUnitFormat() { | |
44 | super(); | |
f6c5f8e1 JR |
45 | fInternalRange = null; |
46 | fExternalRange = null; | |
5b973e7c JR |
47 | } |
48 | ||
49 | /** | |
50 | * Constructor with internal and external LamiRange for scale transformation | |
51 | * | |
52 | * @param internalRange | |
53 | * The internal range used for graph representation | |
54 | * | |
55 | * @param externalRange | |
56 | * The external (real value) range shown to the user | |
57 | */ | |
f6c5f8e1 | 58 | public LamiDecimalUnitFormat(@Nullable LamiGraphRange internalRange, @Nullable LamiGraphRange externalRange) { |
5b973e7c JR |
59 | super(); |
60 | fInternalRange = internalRange; | |
61 | fExternalRange = externalRange; | |
62 | } | |
63 | ||
5b973e7c JR |
64 | /** |
65 | * Constructor with multiplication factor and internal and external | |
66 | * LamiRange for scale transformation. | |
67 | * | |
68 | * @param factor | |
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 | |
74 | */ | |
f6c5f8e1 | 75 | public LamiDecimalUnitFormat(double factor, @Nullable LamiGraphRange internalRange, @Nullable LamiGraphRange externalRange) { |
5b973e7c JR |
76 | super(factor); |
77 | fInternalRange = internalRange; | |
78 | fExternalRange = externalRange; | |
79 | } | |
80 | ||
81 | /** | |
82 | * @return the internal range definition | |
83 | */ | |
84 | public @Nullable LamiGraphRange getInternalRange() { | |
85 | return fInternalRange; | |
86 | } | |
87 | ||
5b973e7c JR |
88 | /** |
89 | * @return the external range definition | |
90 | */ | |
91 | public @Nullable LamiGraphRange getExternalRange() { | |
92 | return fExternalRange; | |
93 | } | |
94 | ||
5b973e7c JR |
95 | @Override |
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$ | |
99 | } | |
100 | ||
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); | |
106 | } | |
107 | ||
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); | |
111 | } | |
112 | ||
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); | |
116 | } | |
117 | ||
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()); | |
124 | ||
125 | Double value = externalValue.doubleValue(); | |
126 | StringBuffer buffer = super.format(value, toAppendTo, pos); | |
127 | return (buffer == null ? new StringBuffer() : buffer); | |
128 | } | |
129 | } |