1 package copilot.roman;
2
3 import java.util.function.ToIntFunction;
4
5
6 public final class RomanToArabicConverter implements ToIntFunction<String> {
7
8 @Override
9 public int applyAsInt(String value) {
10 int result = 0;
11 for (int i = value.length(); i-- > 0; ) {
12 char symbol = value.charAt(i);
13 result += switch (symbol) {
14 case 'I' -> result >= 5 ? -1 : 1;
15 case 'V' -> 5;
16 case 'X' -> result >= 50 ? -10 : 10;
17 case 'L' -> 50;
18 case 'C' -> result >= 500 ? -100 : 100;
19 case 'D' -> 500;
20 case 'M' -> 1000;
21 default -> throw new IllegalArgumentException("Unexpected value: " + symbol);
22 };
23 }
24 return result;
25 }
26 }