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
.tmf
.chart
.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
.tmf
.chart
.ui
.data
.ChartRange
;
21 import org
.eclipse
.tracecompass
.internal
.tmf
.chart
.ui
.data
.ChartRangeMap
;
22 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestampFormat
;
25 * Formatter for timestamps.
27 * @author Michael Jeanson
28 * @author Gabriel-Andrew Pollo-Guilbert
30 public class ChartTimeStampFormat
extends Format
{
32 // ------------------------------------------------------------------------
34 // ------------------------------------------------------------------------
36 private static final long serialVersionUID
= 8102026791684954897L;
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 private final TmfTimestampFormat fFormat
;
43 private @Nullable ChartRangeMap fRangeMap
;
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 * Constructor with a range map supplied.
53 * A internal and external ranges map
55 public ChartTimeStampFormat(@Nullable ChartRangeMap map
) {
56 fFormat
= checkNotNull(TmfTimestampFormat
.getDefaulTimeFormat());
61 * Constructor with a pattern and a range map supplied.
66 * A chart range map for mapping values
68 public ChartTimeStampFormat(String pattern
, @Nullable ChartRangeMap map
) {
69 fFormat
= new TmfTimestampFormat(pattern
);
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Mutators that sets the chart range map of this formatter.
81 * The new chart range map
83 public void setRangeMap(ChartRangeMap map
) {
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
92 public StringBuffer
format(@Nullable Object obj
, @Nullable StringBuffer toAppendTo
, @Nullable FieldPosition pos
) {
93 if (!(obj
instanceof Number
) || toAppendTo
== null) {
94 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj
); //$NON-NLS-1$
97 Number number
= (Number
) obj
;
99 /* If no range was provided, format with the number unchanged */
100 ChartRangeMap rangeMap
= fRangeMap
;
101 if (rangeMap
== null) {
102 long time
= ((Number
) obj
).longValue();
103 return checkNotNull(toAppendTo
.append(fFormat
.format(time
)));
106 ChartRange internalRange
= rangeMap
.getPlottedRange();
107 ChartRange externalRange
= rangeMap
.getInputDataRange();
109 /* If any range's delta is null, format with the external bounds */
110 if (internalRange
.isDeltaNull() || externalRange
.isDeltaNull()) {
111 return checkNotNull(toAppendTo
.append(fFormat
.format(externalRange
.getMinimum().doubleValue())));
114 /* Find external value before formatting */
115 BigDecimal externalValue
= checkNotNull(fRangeMap
).getExternalValue(number
);
116 return checkNotNull(toAppendTo
.append(fFormat
.format(externalValue
.longValue())));
120 public @Nullable Object
parseObject(@Nullable String source
, @Nullable ParsePosition pos
) {