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 | ||
37 | private @Nullable LamiGraphRange fInternalRange = null; | |
38 | private @Nullable LamiGraphRange fExternalRange = null; | |
39 | ||
40 | /** | |
41 | * Default constructor | |
42 | */ | |
43 | public LamiDecimalUnitFormat() { | |
44 | super(); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Constructor with internal and external LamiRange for scale transformation | |
49 | * | |
50 | * @param internalRange | |
51 | * The internal range used for graph representation | |
52 | * | |
53 | * @param externalRange | |
54 | * The external (real value) range shown to the user | |
55 | */ | |
56 | public LamiDecimalUnitFormat(LamiGraphRange internalRange, LamiGraphRange externalRange) { | |
57 | super(); | |
58 | fInternalRange = internalRange; | |
59 | fExternalRange = externalRange; | |
60 | } | |
61 | ||
62 | /** | |
63 | * Constructor with multiplication factor. | |
64 | * | |
65 | * @param factor | |
66 | * Multiplication factor to apply to the value | |
67 | */ | |
68 | public LamiDecimalUnitFormat(double factor) { | |
69 | super(factor); | |
70 | } | |
71 | ||
72 | /** | |
73 | * Constructor with multiplication factor and internal and external | |
74 | * LamiRange for scale transformation. | |
75 | * | |
76 | * @param factor | |
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 | |
82 | */ | |
83 | public LamiDecimalUnitFormat(double factor, LamiGraphRange internalRange, LamiGraphRange externalRange) { | |
84 | super(factor); | |
85 | fInternalRange = internalRange; | |
86 | fExternalRange = externalRange; | |
87 | } | |
88 | ||
89 | /** | |
90 | * @return the internal range definition | |
91 | */ | |
92 | public @Nullable LamiGraphRange getInternalRange() { | |
93 | return fInternalRange; | |
94 | } | |
95 | ||
96 | /** | |
97 | * @param internalRange | |
98 | * The internal range definition to be used by the formatter | |
99 | */ | |
100 | public void setInternalRange(@Nullable LamiGraphRange internalRange) { | |
101 | fInternalRange = internalRange; | |
102 | } | |
103 | ||
104 | /** | |
105 | * @return the external range definition | |
106 | */ | |
107 | public @Nullable LamiGraphRange getExternalRange() { | |
108 | return fExternalRange; | |
109 | } | |
110 | ||
111 | /** | |
112 | * @param externalRange | |
113 | * The external range definition to be used by the formatter | |
114 | */ | |
115 | public void setExternalRange(@Nullable LamiGraphRange externalRange) { | |
116 | fExternalRange = externalRange; | |
117 | } | |
118 | ||
119 | @Override | |
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$ | |
123 | } | |
124 | ||
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); | |
130 | } | |
131 | ||
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); | |
135 | } | |
136 | ||
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); | |
140 | } | |
141 | ||
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()); | |
148 | ||
149 | Double value = externalValue.doubleValue(); | |
150 | StringBuffer buffer = super.format(value, toAppendTo, pos); | |
151 | return (buffer == null ? new StringBuffer() : buffer); | |
152 | } | |
153 | } |