Commit | Line | Data |
---|---|---|
5b973e7c JR |
1 | /******************************************************************************* |
2 | * Copyright (c) 2015, 2016 EfficiOS Inc. and others | |
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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; | |
13 | ||
14 | import java.math.BigDecimal; | |
15 | import java.text.FieldPosition; | |
16 | import java.text.Format; | |
17 | import java.text.ParsePosition; | |
18 | ||
19 | import org.eclipse.jdt.annotation.Nullable; | |
20 | import org.eclipse.tracecompass.internal.provisional.analysis.lami.ui.viewers.LamiGraphRange; | |
21 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat; | |
22 | ||
23 | /** | |
24 | * Formatter for time stamps | |
25 | */ | |
26 | public class LamiTimeStampFormat extends Format { | |
27 | ||
28 | private static final int BIG_DECIMAL_DIVISION_SCALE = 22; | |
29 | ||
30 | private static final long serialVersionUID = 4285447886537779762L; | |
31 | ||
32 | private final TmfTimestampFormat fFormat; | |
33 | ||
34 | private @Nullable LamiGraphRange fInternalRange = null; | |
35 | private @Nullable LamiGraphRange fExternalRange = null; | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Constructors | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | /** | |
42 | * The default constructor | |
43 | */ | |
44 | public LamiTimeStampFormat() { | |
45 | fFormat = checkNotNull(TmfTimestampFormat.getDefaulTimeFormat()); | |
46 | } | |
47 | ||
48 | /** | |
49 | * The base constructor | |
50 | * | |
51 | * @param internalRange | |
52 | * The internal range used for graph representation | |
53 | * @param externalRange | |
54 | * The external (real value) range shown to the user | |
55 | */ | |
56 | public LamiTimeStampFormat(LamiGraphRange internalRange, LamiGraphRange externalRange) { | |
57 | fFormat = checkNotNull(TmfTimestampFormat.getDefaulTimeFormat()); | |
58 | fInternalRange = internalRange; | |
59 | fExternalRange = externalRange; | |
60 | } | |
61 | ||
62 | /** | |
63 | * The normal constructor | |
64 | * | |
65 | * @param pattern | |
66 | * The format pattern | |
67 | */ | |
68 | public LamiTimeStampFormat(String pattern) { | |
69 | fFormat = new TmfTimestampFormat(pattern); | |
70 | } | |
71 | ||
72 | /** | |
73 | * The normal constructor | |
74 | * | |
75 | * @param pattern | |
76 | * the format pattern | |
77 | * @param internalRange | |
78 | * The internal range used for graph representation | |
79 | * @param externalRange | |
80 | * The external (real value) range shown to the user | |
81 | */ | |
82 | public LamiTimeStampFormat(String pattern, LamiGraphRange internalRange, LamiGraphRange externalRange) { | |
83 | fFormat = new TmfTimestampFormat(pattern); | |
84 | fInternalRange = internalRange; | |
85 | fExternalRange = externalRange; | |
86 | } | |
87 | ||
88 | // ------------------------------------------------------------------------ | |
89 | // Operations | |
90 | // ------------------------------------------------------------------------ | |
91 | ||
92 | /** | |
93 | * @return the internal range definition | |
94 | */ | |
95 | public @Nullable LamiGraphRange getInternalRange() { | |
96 | return fInternalRange; | |
97 | } | |
98 | ||
99 | /** | |
100 | * @param internalRange | |
101 | * The internal range definition to be used by the formatter | |
102 | */ | |
103 | public void setInternalRange(@Nullable LamiGraphRange internalRange) { | |
104 | fInternalRange = internalRange; | |
105 | } | |
106 | ||
107 | /** | |
108 | * @return the external range definition | |
109 | */ | |
110 | public @Nullable LamiGraphRange getExternalRange() { | |
111 | return fExternalRange; | |
112 | } | |
113 | ||
114 | /** | |
115 | * @param externalRange | |
116 | * The external range definition to be used by the formatter | |
117 | */ | |
118 | public void setExternalRange(@Nullable LamiGraphRange externalRange) { | |
119 | fExternalRange = externalRange; | |
120 | } | |
121 | ||
122 | ||
123 | @Override | |
124 | public StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) { | |
125 | if (obj != null && obj instanceof Number && toAppendTo != null) { | |
126 | @Nullable LamiGraphRange internalRange = fInternalRange; | |
127 | @Nullable LamiGraphRange externalRange = fExternalRange; | |
128 | if (internalRange == null || externalRange == null) { | |
129 | long time = ((Number)obj).longValue(); | |
130 | return checkNotNull(toAppendTo.append(fFormat.format(time))); | |
131 | } | |
132 | ||
c0794ee7 JR |
133 | if (internalRange.getDelta().compareTo(BigDecimal.ZERO) == 0 || |
134 | externalRange.getDelta().compareTo(BigDecimal.ZERO) == 0) { | |
5b973e7c JR |
135 | return checkNotNull(toAppendTo.append(fFormat.format(externalRange.getMinimum().doubleValue()))); |
136 | } | |
137 | ||
138 | /* Find external value before formatting */ | |
139 | BigDecimal externalValue = (new BigDecimal(obj.toString())) | |
140 | .subtract(internalRange.getMinimum()) | |
141 | .multiply(externalRange.getDelta()) | |
142 | .divide(internalRange.getDelta(), BIG_DECIMAL_DIVISION_SCALE, BigDecimal.ROUND_DOWN) | |
143 | .add(externalRange.getMinimum()); | |
144 | ||
145 | return checkNotNull(toAppendTo.append(fFormat.format(externalValue.longValue()))); | |
146 | } | |
147 | return new StringBuffer(); | |
148 | } | |
149 | ||
150 | @Override | |
151 | public @Nullable Object parseObject(@Nullable String source, @Nullable ParsePosition pos) { | |
152 | return null; | |
153 | } | |
154 | ||
155 | } |