1 /*******************************************************************************
2 * Copyright (c) 2016 EfficiOS Inc., Jonathan Rajotte-Julien
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 java
.text
.FieldPosition
;
13 import java
.text
.Format
;
14 import java
.text
.ParsePosition
;
15 import java
.util
.Map
.Entry
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import com
.google
.common
.collect
.BiMap
;
23 * Format label based on a given Map<String, Integer>
25 * @author Jonathan Rajotte-Julien
27 public class LamiLabelFormat
extends Format
{
29 private static final long serialVersionUID
= 4939553034329681316L;
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
;
39 * Map of indices to labels
41 public LamiLabelFormat(BiMap
<@Nullable String
, Integer
> map
) {
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
);
52 Double doubleObj
= (Double
) obj
;
55 * Return a string buffer with a space in it since SWT does not like to
58 if ((doubleObj
% 1 != 0) || !fMap
.containsValue((doubleObj
.intValue()))) {
59 return new StringBuffer(SWTCHART_EMPTY_LABEL
);
62 for (Entry
<@Nullable String
, Integer
> entry
: fMap
.entrySet()) {
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.
68 if (Double
.compare(entry
.getValue().doubleValue(), doubleObj
.doubleValue()) == 0) {
69 if (entry
.getKey() == null) {
70 return new StringBuffer(UNKNOWN_REPRESENTATION
);
72 return toAppendTo
.append(entry
.getKey());
75 return new StringBuffer(SWTCHART_EMPTY_LABEL
);
79 public @Nullable Object
parseObject(@Nullable String source
, @Nullable ParsePosition pos
) {
80 return fMap
.get(source
);