1 /*******************************************************************************
2 * Copyright (c) 2016 École Polytechnique de Montréal
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 java
.text
.FieldPosition
;
13 import java
.text
.Format
;
14 import java
.text
.ParsePosition
;
16 import org
.eclipse
.jdt
.annotation
.Nullable
;
18 import com
.google
.common
.collect
.BiMap
;
21 * Format label based on a given Map<String, Integer>.
23 * @author Jonathan Rajotte-Julien
24 * @author Gabriel-Andrew Pollo-Guilbert
26 public class LabelFormat
extends Format
{
28 // ------------------------------------------------------------------------
30 // ------------------------------------------------------------------------
32 private static final long serialVersionUID
= -7046922375212377647L;
33 private static final String CHART_EMPTY_LABEL
= " "; //$NON-NLS-1$
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 private final BiMap
<String
, Integer
> fMap
;
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
49 * Map of labels and indices
51 public LabelFormat(BiMap
<String
, Integer
> map
) {
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 public @Nullable StringBuffer
format(@Nullable Object obj
, @Nullable StringBuffer toAppendTo
, @Nullable FieldPosition pos
) {
63 if (obj
== null || toAppendTo
== null) {
64 return new StringBuffer(CHART_EMPTY_LABEL
);
67 Double doubleObj
= (Double
) obj
;
69 /* If the key is not contained in the map, format a empty label */
70 if ((doubleObj
% 1 != 0) || !fMap
.containsValue((doubleObj
.intValue()))) {
71 return new StringBuffer(CHART_EMPTY_LABEL
);
74 /* If the value is null in the map, format an unknown label */
75 String key
= fMap
.inverse().get(doubleObj
.intValue());
77 return toAppendTo
.append(key
);
81 public @Nullable Object
parseObject(@Nullable String source
, @Nullable ParsePosition pos
) {
82 return fMap
.get(source
);
This page took 0.032662 seconds and 5 git commands to generate.