charts: Better protect ChartRange to avoid null ranges
[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;
4f5813eb 20import org.eclipse.tracecompass.common.core.NonNullUtils;
0a772436
GAPG
21import org.eclipse.tracecompass.internal.tmf.chart.ui.data.ChartRangeMap;
22import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestampFormat;
23
24/**
25 * Formatter for timestamps.
26 *
27 * @author Michael Jeanson
28 * @author Gabriel-Andrew Pollo-Guilbert
29 */
30public class ChartTimeStampFormat extends Format {
31
32 // ------------------------------------------------------------------------
33 // Constants
34 // ------------------------------------------------------------------------
35
36 private static final long serialVersionUID = 8102026791684954897L;
37
38 // ------------------------------------------------------------------------
39 // Members
40 // ------------------------------------------------------------------------
41
42 private final TmfTimestampFormat fFormat;
43 private @Nullable ChartRangeMap fRangeMap;
44
45 // ------------------------------------------------------------------------
46 // Constructors
47 // ------------------------------------------------------------------------
48
49 /**
50 * Constructor with a range map supplied.
51 *
52 * @param map
53 * A internal and external ranges map
54 */
55 public ChartTimeStampFormat(@Nullable ChartRangeMap map) {
56 fFormat = checkNotNull(TmfTimestampFormat.getDefaulTimeFormat());
57 fRangeMap = map;
58 }
59
60 /**
61 * Constructor with a pattern and a range map supplied.
62 *
63 * @param pattern
64 * The format pattern
65 * @param map
66 * A chart range map for mapping values
67 */
68 public ChartTimeStampFormat(String pattern, @Nullable ChartRangeMap map) {
69 fFormat = new TmfTimestampFormat(pattern);
70 fRangeMap = map;
71 }
72
73 // ------------------------------------------------------------------------
74 // Mutators
75 // ------------------------------------------------------------------------
76
77 /**
78 * Mutators that sets the chart range map of this formatter.
79 *
80 * @param map
81 * The new chart range map
82 */
83 public void setRangeMap(ChartRangeMap map) {
84 fRangeMap = map;
85 }
86
4f5813eb
GB
87 /**
88 * Get the pattern string of the format.
89 *
90 * @return the pattern string.
91 */
92 public String getPattern() {
93 return NonNullUtils.checkNotNull(fFormat.toPattern());
94 }
95
0a772436
GAPG
96 // ------------------------------------------------------------------------
97 // Operations
98 // ------------------------------------------------------------------------
99
100 @Override
101 public StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) {
102 if (!(obj instanceof Number) || toAppendTo == null) {
103 throw new IllegalArgumentException("Cannot format given Object as a Number: " + obj); //$NON-NLS-1$
104 }
105
106 Number number = (Number) obj;
107
108 /* If no range was provided, format with the number unchanged */
109 ChartRangeMap rangeMap = fRangeMap;
110 if (rangeMap == null) {
111 long time = ((Number) obj).longValue();
112 return checkNotNull(toAppendTo.append(fFormat.format(time)));
113 }
114
0a772436
GAPG
115 /* Find external value before formatting */
116 BigDecimal externalValue = checkNotNull(fRangeMap).getExternalValue(number);
117 return checkNotNull(toAppendTo.append(fFormat.format(externalValue.longValue())));
118 }
119
120 @Override
121 public @Nullable Object parseObject(@Nullable String source, @Nullable ParsePosition pos) {
122 return null;
123 }
124
125}
This page took 0.03967 seconds and 5 git commands to generate.