Question from the Java and Craftsmanship test

A Java class that converts Arabic numbers to Roman numerals.

Expert

This code

public class Numerals {

  private static final NavigableMap<Integer, String> CONVERSIONS = buildConversions();

  private Numerals() {}

  public static String toRoman(int arabic) {
    return highestKnownConversion(arabic).map(toRomanRepresentation(arabic)).orElse("");
  }

  private static Optional<Entry<Integer, String>> highestKnownConversion(int arabic) {
    return Optional.ofNullable(CONVERSIONS.floorEntry(arabic));
  }

  private static Function<Entry<Integer, String>, String> toRomanRepresentation(int arabic) {
    return conversion -> conversion.getValue() + toRoman(arabic - conversion.getKey());
  }

  private static NavigableMap<Integer, String> buildConversions() {
    NavigableMap<Integer, String> conversions = new TreeMap<>();

    conversions.put(1, "I");
    conversions.put(4, "IV");
    conversions.put(5, "V");
    conversions.put(9, "IX");
    conversions.put(10, "X");

    return conversions;
  }
}
Author: Clément DevosStatus: PublishedQuestion passed 286 times
Edit
1
Community EvaluationsNo one has reviewed this question yet, be the first!