Roman Numerals, Groovy Style
Your project is already late when you discover in the small print of the contract that the system in development "SHALL manage Roman digits in chapter numbers". You frantically search the 'Net and, yes there are solutions. Some of them seem complicated on first sight. Do you wish for less convoluted code? You've got it.
Here is an attempt at a Groovy solution, i.e. compact but not obscure code. The techniques are, of course, borrowed from other people on the 'Net. The parse method relies heavily on the Groovy switch statement. The print method uses floorKey, a method that GDK adds to the Java API. Both are recursive.
The main contribution may be that the parsing method does a fairly complete validation of its input.
-
span style="color: #a1a100;">import java.util.regex.Matcher
-
-
/**
-
* Class for processing Roman numerals.
-
*//**
-
* Convert a Roman numeral to an Integer value with decent validation.
-
* @param numeral must be a string of Roman digits,
-
* @throws NumberFormatException if the numeral is invalid.
-
*/// Cases not detected by other regex.
-
"Invalid Roman numeral: ${numeral}"'IX''IV''III''II''I'"Invalid Roman numeral: ${numeral}")
-
}
-
}
-
-
/**
-
* The recursive part of parsing.
-
* @param value must be the integer value collected so far.
-
* The method picks up the tail of the last regex match.
-
*//**
-
* Format an integer as a Roman numeral.
-
* @param value must be the integer to format,
-
* @throws IllegalArgumentException if the integer is out of range (1..3999).
-
*/"Integer out of range: ${value}"'M', 900: 'CM', 500: 'D', 400: 'CD',
-
100: 'C', 90: 'XC', 50: 'L', 40: 'XL',
-
10: 'X', 9: 'IX', 5: 'V', 4: 'IV', 1: 'I'])
-
}
You may download a zipped archive here containing the above code (with better indenting) and also a Spock unit test.