1 /*******************************************************************************
2 * Copyright (c) 2015, 2016 EfficiOS Inc. and others
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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.math
.BigDecimal
;
15 import java
.text
.FieldPosition
;
16 import java
.text
.Format
;
17 import java
.text
.ParsePosition
;
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
;
24 * Formatter for time stamps
26 public class LamiTimeStampFormat
extends Format
{
28 private static final int BIG_DECIMAL_DIVISION_SCALE
= 22;
30 private static final long serialVersionUID
= 4285447886537779762L;
32 private final TmfTimestampFormat fFormat
;
34 private @Nullable LamiGraphRange fInternalRange
= null;
35 private @Nullable LamiGraphRange fExternalRange
= null;
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
42 * The default constructor
44 public LamiTimeStampFormat() {
45 fFormat
= checkNotNull(TmfTimestampFormat
.getDefaulTimeFormat());
49 * The base constructor
51 * @param internalRange
52 * The internal range used for graph representation
53 * @param externalRange
54 * The external (real value) range shown to the user
56 public LamiTimeStampFormat(LamiGraphRange internalRange
, LamiGraphRange externalRange
) {
57 fFormat
= checkNotNull(TmfTimestampFormat
.getDefaulTimeFormat());
58 fInternalRange
= internalRange
;
59 fExternalRange
= externalRange
;
63 * The normal constructor
68 public LamiTimeStampFormat(String pattern
) {
69 fFormat
= new TmfTimestampFormat(pattern
);
73 * The normal constructor
77 * @param internalRange
78 * The internal range used for graph representation
79 * @param externalRange
80 * The external (real value) range shown to the user
82 public LamiTimeStampFormat(String pattern
, LamiGraphRange internalRange
, LamiGraphRange externalRange
) {
83 fFormat
= new TmfTimestampFormat(pattern
);
84 fInternalRange
= internalRange
;
85 fExternalRange
= externalRange
;
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
93 * @return the internal range definition
95 public @Nullable LamiGraphRange
getInternalRange() {
96 return fInternalRange
;
100 * @param internalRange
101 * The internal range definition to be used by the formatter
103 public void setInternalRange(@Nullable LamiGraphRange internalRange
) {
104 fInternalRange
= internalRange
;
108 * @return the external range definition
110 public @Nullable LamiGraphRange
getExternalRange() {
111 return fExternalRange
;
115 * @param externalRange
116 * The external range definition to be used by the formatter
118 public void setExternalRange(@Nullable LamiGraphRange externalRange
) {
119 fExternalRange
= externalRange
;
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
)));
133 if (internalRange
.getDelta().compareTo(BigDecimal
.ZERO
) == 0) {
134 return checkNotNull(toAppendTo
.append(fFormat
.format(externalRange
.getMinimum().doubleValue())));
137 if (externalRange
.getDelta().compareTo(BigDecimal
.ZERO
) == 0) {
138 return checkNotNull(toAppendTo
.append(fFormat
.format(externalRange
.getMinimum().doubleValue())));
141 /* Find external value before formatting */
142 BigDecimal externalValue
= (new BigDecimal(obj
.toString()))
143 .subtract(internalRange
.getMinimum())
144 .multiply(externalRange
.getDelta())
145 .divide(internalRange
.getDelta(), BIG_DECIMAL_DIVISION_SCALE
, BigDecimal
.ROUND_DOWN
)
146 .add(externalRange
.getMinimum());
148 return checkNotNull(toAppendTo
.append(fFormat
.format(externalValue
.longValue())));
150 return new StringBuffer();
154 public @Nullable Object
parseObject(@Nullable String source
, @Nullable ParsePosition pos
) {