AbbreviationLabel { static List<FormattedText> format(List<String> tokens) { List<FormattedText> texts = new ArrayList<>(2 + tokens.size()); for (String token : tokens) { if (isChargeToken(token)) { String sign = Character.toString(norm(token.charAt(0))); String coef = token.length() > 1 ? token.substring(1) : ""; if (sign.equals("-")) sign = MINUS_STRING; texts.add(new FormattedText(coef + sign, STYLE_SUPSCRIPT)); } else if (token.length() == 1 && isDigit(token.charAt(0)) && !texts.isEmpty() && texts.get(texts.size()-1).text.equals(")")) { texts.add(new FormattedText(token, STYLE_SUBSCRIPT)); } else { int i = findPrefix(ITAL_PREFIX_TRIE, token, 0, 0); int j = token.length(); while (j > 0 && isDigit(token.charAt(j - 1))) j--; if (j > 0 && j < token.length()) { if (i > j) i = 0; if (i > 0) texts.add(new FormattedText(token.substring(0, i), STYLE_ITALIC)); texts.add(new FormattedText(token.substring(i, j), STYLE_NORMAL)); texts.add(new FormattedText(token.substring(j), STYLE_SUBSCRIPT)); } else { if (i > 0) texts.add(new FormattedText(token.substring(0, i), STYLE_ITALIC)); texts.add(new FormattedText(token.substring(i), STYLE_NORMAL)); } } } return texts; }    }
@Test public void formatTBu() { List<String> tokens = Arrays.asList("tBu"); List<AbbreviationLabel.FormattedText> texts = AbbreviationLabel.format(tokens); assertThat(texts.size(), is(2)); assertThat(texts.get(0).text, is("t")); assertThat(texts.get(0).style, is(AbbreviationLabel.STYLE_ITALIC)); assertThat(texts.get(1).text, is("Bu")); assertThat(texts.get(1).style, is(AbbreviationLabel.STYLE_NORMAL)); }