AbbreviationLabel { static boolean parse(String label, List<String> tokens) { int i = 0; int len = label.length(); while (i < len) { int st = i; int last; char c = label.charAt(i); if (c == '(' || c == ')') { tokens.add(Character.toString(c)); i++; if (c == ')') { st = i; while (i < len && isDigit(c = label.charAt(i))) { i++; } if (i > st) tokens.add(label.substring(st, i)); } continue; } if (c == '/' || c == '·' || c == '.' || c == '•' || c == '=') { tokens.add(Character.toString(c)); i++; int beg = i; while (i < label.length() && isDigit(label.charAt(i))) { i++; } if (i > beg) tokens.add(label.substring(beg, i)); continue; } if ((last = findPrefix(PREFIX_TRIE, label, i, -1)) > 0) { i += (last - i); } final int symSt = i; if ((last = findPrefix(SYMBOL_TRIE, label, i, -1)) > 0) { i += (last - i); while (i < len && isDigit(label.charAt(i))) { i++; } } else if (i == st && st > 0) { c = norm(label.charAt(i)); if (c == '-' || c == '+') { i++; while (i < len && isDigit(label.charAt(i))) { i++; } if (i < len) { return failParse(label, tokens); } } } if (i == st || i == symSt) { return failParse(label, tokens); } tokens.add(label.substring(st, i)); } return true; }    }
@Test public void carboxylicacid() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("COOH", tokens)); assertThat(tokens.size(), is(4)); assertThat(tokens, hasItems("C", "O", "O", "H")); }
@Test public void carboxylate() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("COO-", tokens)); assertThat(tokens.size(), is(4)); assertThat(tokens, hasItems("C", "O", "O", "-")); }
@Test public void trifluromethyl() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("CF3", tokens)); assertThat(tokens.size(), is(2)); assertThat(tokens, hasItems("C", "F3")); }
@Test public void triphenylmethyl() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("CPh3", tokens)); assertThat(tokens.size(), is(2)); assertThat(tokens, hasItems("C", "Ph3")); }
@Test public void tertbutyls() { List<String> tokens = new ArrayList<>(1); for (String str : Arrays.asList("tBu", "tertBu", "t-Bu", "t-Butyl", "tertButyl")) { tokens.clear(); assertTrue(str, AbbreviationLabel.parse(str, tokens)); assertThat(tokens.size(), is(1)); } }
@Test public void peglinker() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("CH2CH2OCH2CH2O", tokens)); assertThat(tokens.size(), is(10)); assertThat(tokens, hasItems("C", "H2", "C", "H2", "O", "C", "H2", "C", "H2", "O")); }
@Test public void parseFeacac3() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("Fe(acac)3", tokens)); assertThat(tokens.size(), is(5)); assertThat(tokens, hasItems("Fe", "(", "acac", ")", "3")); }
@Test public void CO2Et() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("CO2Et", tokens)); assertThat(tokens.size(), is(3)); assertThat(tokens, hasItems("C", "O2", "Et")); }
@Test public void parseBrackets() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("N(CH2CH2O)CH2", tokens)); assertThat(tokens.size(), is(10)); assertThat(tokens, hasItems("N", "(", "C", "H2", "C", "H2", "O", ")", "C", "H2")); }
@Test public void hydrateFormatting() { List<String> tokens = new ArrayList<>(); assertTrue(AbbreviationLabel.parse("SnCl4.2H2O", tokens)); assertThat(tokens, is(Arrays.asList("Sn", "Cl4", ".", "2", "H2", "O"))); }
@Test public void nonAbbreviationLabel() { List<String> tokens = new ArrayList<>(); assertFalse(AbbreviationLabel.parse("A Random Label - Don't Reverse", tokens)); assertThat(tokens.size(), is(1)); }
@Test public void hydrate() { List<String> tokens = new ArrayList<>(); AbbreviationLabel.parse("•H2O", tokens); assertThat(tokens, is(Arrays.asList("•", "H2", "O"))); }
@Test public void het() { assertFalse(AbbreviationLabel.parse("Het", new ArrayList<String>())); }
@Test public void parseChargeOnly() { assertFalse(AbbreviationLabel.parse("+", new ArrayList<String>())); }
@Test public void parseNumberOnly() { assertFalse(AbbreviationLabel.parse("1", new ArrayList<String>())); }
@Test public void nonAsciiLabel() { assertFalse(AbbreviationLabel.parse("苯基", new ArrayList<String>())); }