class RootTermDistanceCalculator {

    public static String nearestRootTerm(Set<RootTermDistanceCalculator> distanceCalculators, String term, int threshold) {
        if(threshold < 0) {
            throw new IllegalArgumentException("Threshold must be greater than or equal to 0");
        }

        var calculatorMindDistanceEntries = distanceCalculators.parallelStream()
                .collect(Collectors.toMap(
                        Function.identity(),
                        distanceCalculator -> distanceCalculator.calculateMinimalDistance(term)
                ))
                .entrySet();

        var minDistanceEntry = Collections.min(calculatorMindDistanceEntries, Comparator.comparingInt(Map.Entry::getValue));

        if(minDistanceEntry.getValue() <= threshold) {
            return minDistanceEntry.getKey().getOriginalRootTerm();
        } else {
            return term;
        }
    }

    public  RootTermDistanceCalculator(String rootTerm, Set<String> derivedTerms);

}