Skip to content

Commit

Permalink
Reverse abbreviation labels with numbered brackets correctly, B(OH)2 …
Browse files Browse the repository at this point in the history
…becomes (HO)2B not 2(HO)B. Add recent reduce method needed for tests to pass.
  • Loading branch information
johnmay committed Sep 16, 2016
1 parent a9062d7 commit a3ddccb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Expand Up @@ -25,8 +25,10 @@

import org.openscience.cdk.config.Elements;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;

/**
Expand Down Expand Up @@ -247,6 +249,13 @@ private static boolean failParse(String label, List<String> tokens) {
return false;
}

private static boolean isNumber(String str) {
for (int i = 0; i < str.length(); i++)
if (!isDigit(str.charAt(i)))
return false;
return true;
}

/**
* Reverse a list of tokens for display, flipping
* brackets as needed.
Expand All @@ -255,11 +264,24 @@ private static boolean failParse(String label, List<String> tokens) {
*/
static void reverse(List<String> tokens) {
Collections.reverse(tokens);
// now flip brackets
// now flip brackets and move numbers
Deque<String> numbers = new ArrayDeque<>();
for (int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i);
if (token.equals("(")) tokens.set(i, ")");
else if (token.equals(")")) tokens.set(i, "(");
if (token.equals("(")) {
tokens.set(i, ")");
tokens.add(i+1, numbers.pop());
i++;
}
else if (token.equals(")")) {
tokens.set(i, "(");
if (i>0 && isNumber(tokens.get(i - 1))) {
numbers.push(tokens.remove(i - 1));
i--;
} else {
numbers.push("");
}
}
}
}

Expand Down
Expand Up @@ -101,6 +101,7 @@ public void formatFeacac3() {
List<String> tokens = new ArrayList<>();
assertTrue(AbbreviationLabel.parse("Fe(acac)3", tokens));
List<AbbreviationLabel.FormattedText> formatted = AbbreviationLabel.format(tokens);
AbbreviationLabel.reduce(formatted, 0, formatted.size());
assertThat(formatted.get(0).text, is("Fe(acac)"));
assertThat(formatted.get(0).style, is(0));
assertThat(formatted.get(1).text, is("3"));
Expand Down Expand Up @@ -132,6 +133,14 @@ public void reversingBrackets() {
assertThat(Joiner.on("").join(tokens), is("H2C(OH2CH2C)N"));
}

@Test
public void reversingBracketsWithNumbers() {
List<String> tokens = new ArrayList<>();
assertTrue(AbbreviationLabel.parse("B(OH)2", tokens));
AbbreviationLabel.reverse(tokens);
assertThat(Joiner.on("").join(tokens), is("(HO)2B"));
}

@Test
public void nonAbbreviationLabel() {
List<String> tokens = new ArrayList<>();
Expand All @@ -143,6 +152,7 @@ public void nonAbbreviationLabel() {
public void formatOPO3() {
List<String> tokens = Arrays.asList("O", "P", "O3", "-2");
List<AbbreviationLabel.FormattedText> texts = AbbreviationLabel.format(tokens);
AbbreviationLabel.reduce(texts, 0, texts.size());
assertThat(texts.size(), is(3));
assertThat(texts.get(0).text, is("OPO"));
assertThat(texts.get(0).style, is(AbbreviationLabel.STYLE_NORMAL));
Expand All @@ -167,6 +177,7 @@ public void formatTBu() {
public void formatOPO3H2() {
List<String> tokens = Arrays.asList("O", "P", "O3", "H2");
List<AbbreviationLabel.FormattedText> texts = AbbreviationLabel.format(tokens);
AbbreviationLabel.reduce(texts, 0, texts.size());
assertThat(texts.size(), is(4));
assertThat(texts.get(0).text, is("OPO"));
assertThat(texts.get(0).style, is(0));
Expand Down

0 comments on commit a3ddccb

Please sign in to comment.