package org.keyboardplaying.roman;

import org.keyboardplaying.util.BiHashMap;
import org.keyboardplaying.util.BiMap;

/**
 * Converts an integer from and to roman numerals.
 *
 * @author Cyrille Chopelet (http://keyboardplaying.org)
 */
public class RomanConverter {

    private BiMap<Character, Integer> correspondance;

    {
        correspondance = new BiHashMap<>();
        correspondance.put(Character.valueOf('I'), Integer.valueOf(1));
        correspondance.put(Character.valueOf('V'), Integer.valueOf(5));
        correspondance.put(Character.valueOf('X'), Integer.valueOf(10));
        correspondance.put(Character.valueOf('L'), Integer.valueOf(50));
        correspondance.put(Character.valueOf('C'), Integer.valueOf(100));
        correspondance.put(Character.valueOf('D'), Integer.valueOf(500));
        correspondance.put(Character.valueOf('M'), Integer.valueOf(1000));
    }

    /**
     * Parses a Roman numeral to an integer.
     * <p/>
     * Due to the numerous variants of Roman numerals writing, no constraint is checked. At the
     * current time, this algorithm is a dumb application of conversion rules.
     *
     * @param roman
     *            the Roman numeral
     * @return the integer version of the number
     */
    public int from(String roman) {
        int result = 0;

        if (roman != null) {
            String roman2 = roman.trim().toUpperCase();
            int length = roman2.length();
            if (length != 0) {
                int i = 0;
                while (i < length) {
                    char current = roman2.charAt(i);
                    int value = getIntValue(current);
                    int group = 0;
                    do {
                        group += value;
                        i++;
                    } while (i < length && roman2.charAt(i) == current);
                    if (i == length || getIntValue(roman2.charAt(i)) < value) {
                        result += group;
                    } else {
                        result -= group;
                    }
                }
            }
        }

        return result;
    }

    private int getIntValue(char roman) {
        return correspondance.get(Character.valueOf(roman)).intValue();
    }
}

@@ -1439,43 +1439,8 @@
 ();%0A
-            if (length != 0) %7B%0A    
     
@@ -1454,28 +1454,24 @@
  int i = 0;%0A
-    
             
@@ -1507,20 +1507,16 @@
         
- 
-   
 char cur
@@ -1540,20 +1540,16 @@
 rAt(i);%0A
-    
         
@@ -1602,28 +1602,24 @@
             
-    
 int group = 
@@ -1641,21 +1641,13 @@
     
-  
-  
 do %7B%0A
-    
     
@@ -1702,21 +1702,13 @@
     
-    
 i++;%0A
-    
     
@@ -1764,36 +1764,33 @@
 i) == current);%0A
-    
+%0A
                 
@@ -1865,28 +1865,24 @@
             
-    
 result += gr
@@ -1878,36 +1878,32 @@
 esult += group;%0A
-    
                 
@@ -1927,28 +1927,24 @@
             
-    
 result -= gr
@@ -1952,30 +1952,8 @@
 up;%0A
-                    %7D%0A
     

Remove redundant test