charts: Add unit tests for the .ui plugin
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.chart.ui / src / org / eclipse / tracecompass / internal / tmf / chart / ui / format / ChartTimeStampFormat.java
CommitLineData
0a772436
GAPG
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
10package org.eclipse.tracecompass.internal.tmf.chart.ui.format;
11
12import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14import java.math.BigDecimal;
15import java.text.FieldPosition;
16import java.text.Format;
17import java.text.ParsePosition;
18
19import org.eclipse.jdt.annotation.Nullable;
0a772436
GAPG
20import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRangeMap;
21import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
22
23/**
24 * Formatter for timestamps.
25 *
0c927289
GB
26 * TODO: See if this formatter is specific to the swtchart charts that are
27 * implemented in this plugin or if they can be re-used in another other
28 * charting scheme. We'll probably know when we actually have another
29 * implementation. If swtchart specific, the name of the class and package
30 * should make it clear.
31 *
0a772436
GAPG
32 * @author Michael Jeanson
33 * @author Gabriel-Andrew Pollo-Guilbert
34 */
35public class ChartTimeStampFormat extends Format {
36
37 // ------------------------------------------------------------------------
38 // Constants
39 // ------------------------------------------------------------------------
40
41 private static final long serialVersionUID = 8102026791684954897L;
42
43 // ------------------------------------------------------------------------
44 // Members
45 // ------------------------------------------------------------------------
46
47 private final TmfTimestampFormat fFormat;
48 private @Nullable ChartRangeMap fRangeMap;
49
50 // ------------------------------------------------------------------------
51 // Constructors
52 // ------------------------------------------------------------------------
53
54 /**
55 * Constructor with a range map supplied.
56 *
57 * @param map
58 * A internal and external ranges map
59 */
60 public ChartTimeStampFormat(@Nullable ChartRangeMap map) {
61 fFormat = checkNotNull(TmfTimestampFormat.getDefaulTimeFormat());
62 fRangeMap = map;
63 }
64
65 /**
66 * Constructor with a pattern and a range map supplied.
67 *
68 * @param pattern
69 * The format pattern
70 * @param map
71 * A chart range map for mapping values
72 */
73 public ChartTimeStampFormat(String pattern, @Nullable ChartRangeMap map) {
74 fFormat = new TmfTimestampFormat(pattern);
75 fRangeMap = map;
76 }
77
78 // ------------------------------------------------------------------------
79 // Mutators
80 // ------------------------------------------------------------------------
81
82 /**
83 * Mutators that sets the chart range map of this formatter.
84 *
85 * @param map
86 * The new chart range map
87 */
88 public void setRangeMap(ChartRangeMap map) {
89 fRangeMap = map;
90 }
91
4f5813eb
GB
92 /**
93 * Get the pattern string of the format.
94 *
95 * @return the pattern string.
96 */
97 public String getPattern() {
197ef97d 98 return fFormat.toPattern();
4f5813eb
GB
99 }
100
0a772436
GAPG
101 // ------------------------------------------------------------------------
102 // Operations
103 // ------------------------------------------------------------------------
104
105 @Override
106 public StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
107 if (!(obj instanceof Number) || toAppendTo == null) {
108 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj); //$NON-NLS-1$
109 }
110
111 Number number = (Number) obj;
112
113 /* If no range was provided, format with the number unchanged */
114 ChartRangeMap rangeMap = fRangeMap;
115 if (rangeMap == null) {
116 long time = ((Number) obj).longValue();
117 return checkNotNull(toAppendTo.append(fFormat.format(time)));
118 }
119
0a772436
GAPG
120 /* Find external value before formatting */
121 BigDecimal externalValue = checkNotNull(fRangeMap).getExternalValue(number);
122 return checkNotNull(toAppendTo.append(fFormat.format(externalValue.longValue())));
123 }
124
125 @Override
126 public @Nullable Object parseObject(@Nullable String source, @Nullable ParsePosition pos) {
127 return null;
128 }
129
130}
This page took 0.032709 seconds and 5 git commands to generate.