1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
package tim.prune.data;
import tim.prune.I18nManager;
import java.util.HashMap;
public class FieldRecogniser
{
static final HashMap<String, String> _lookupMap = new HashMap<>();
/** Called only once to populate the static lookup map */
private static synchronized void fillMap()
{
if (!_lookupMap.isEmpty()) {
return; // already done
}
_lookupMap.put("hr", "heartrate");
_lookupMap.put("heart", "heartrate");
_lookupMap.put("cad", "cadence");
String[] knowns = {"heartrate", "cadence", "power", "speed", "course"};
for (String known : knowns) {
_lookupMap.put(known, known);
}
}
/** @return a displayable label for the given xml tag */
public static String getLabel(String inTagName)
{
fillMap();
int colonPos = inTagName.lastIndexOf(':');
if (colonPos < 0) {
return translate(inTagName);
}
String suffix = inTagName.substring(colonPos + 1);
return translate(suffix);
}
/** @return the name of the field if a mapping is known, otherwise the tag name itself */
private static String translate(String inTagName)
{
String token = _lookupMap.getOrDefault(inTagName.toLowerCase(), null);
if (token != null) {
return I18nManager.getText("fieldname." + token);
}
return inTagName;
}
/** Just for testing! */
protected static int getMappingSize() {
return _lookupMap == null ? 0 : _lookupMap.size();
}
/** Just for testing! */
protected static void clearMap() {
_lookupMap.clear();
}
}
|