Commit | Line | Data |
---|---|---|
4208b510 AM |
1 | /******************************************************************************* |
2 | * Copyright (c) 2016 EfficiOS Inc., Jonathan Rajotte-Julien | |
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.core.module; | |
11 | ||
12 | import java.text.FieldPosition; | |
13 | import java.text.Format; | |
14 | import java.text.ParsePosition; | |
15 | import java.util.Map.Entry; | |
16 | ||
17 | ||
18 | import org.eclipse.jdt.annotation.Nullable; | |
19 | ||
20 | import com.google.common.collect.BiMap; | |
21 | ||
22 | /** | |
23 | * Format label based on a given Map<String, Integer> | |
24 | * | |
25 | * @author Jonathan Rajotte-Julien | |
26 | */ | |
27 | public class LamiLabelFormat extends Format { | |
28 | ||
29 | private static final long serialVersionUID = 4939553034329681316L; | |
30 | ||
31 | private static final String SWTCHART_EMPTY_LABEL = " "; //$NON-NLS-1$ | |
32 | private static final String UNKNOWN_REPRESENTATION = "?"; //$NON-NLS-1$ | |
33 | private final BiMap<@Nullable String, Integer> fMap; | |
34 | ||
35 | /** | |
36 | * Constructor | |
37 | * | |
38 | * @param map | |
39 | * Map of indices to labels | |
40 | */ | |
41 | public LamiLabelFormat(BiMap<@Nullable String, Integer> map) { | |
42 | super(); | |
43 | fMap = map; | |
44 | } | |
45 | ||
46 | @Override | |
47 | public @Nullable StringBuffer format(@Nullable Object obj, @Nullable StringBuffer toAppendTo, @Nullable FieldPosition pos) { | |
48 | if (obj == null || toAppendTo == null) { | |
49 | return new StringBuffer(SWTCHART_EMPTY_LABEL); | |
50 | } | |
51 | ||
52 | Double doubleObj = (Double) obj; | |
53 | ||
54 | /* | |
55 | * Return a string buffer with a space in it since SWT does not like to | |
56 | * draw empty strings. | |
57 | */ | |
58 | if ((doubleObj % 1 != 0) || !fMap.containsValue((doubleObj.intValue()))) { | |
59 | return new StringBuffer(SWTCHART_EMPTY_LABEL); | |
60 | } | |
61 | ||
62 | for (Entry<@Nullable String, Integer> entry : fMap.entrySet()) { | |
63 | /* | |
64 | * FIXME: Find if the elements are the same, based on their double | |
65 | * value, because SWTChart uses double values so we do the same | |
66 | * check. The loss of precision could lead to false positives. | |
67 | */ | |
68 | if (Double.compare(entry.getValue().doubleValue(), doubleObj.doubleValue()) == 0) { | |
69 | if (entry.getKey() == null) { | |
70 | return new StringBuffer(UNKNOWN_REPRESENTATION); | |
71 | } | |
72 | return toAppendTo.append(entry.getKey()); | |
73 | } | |
74 | } | |
75 | return new StringBuffer(SWTCHART_EMPTY_LABEL); | |
76 | } | |
77 | ||
78 | @Override | |
79 | public @Nullable Object parseObject(@Nullable String source, @Nullable ParsePosition pos) { | |
80 | return fMap.get(source); | |
81 | } | |
82 | ||
83 | } |