package org.springframework.data.spel;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.data.spel.spi.Function;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
class Functions {
private static final String MESSAGE_TEMPLATE = "There are multiple matching methods of name '%s' for parameter types (%s), but no "
+ "exact match. Make sure to provide only one matching overload or one with exactly those types.";
private final MultiValueMap<String, Function> functions = new LinkedMultiValueMap<>();
void addAll(Map<String, Function> newFunctions) {
newFunctions.forEach((n, f) -> {
List<Function> currentElements = get(n);
if (!contains(currentElements, f)) {
functions.add(n, f);
}
});
}
void addAll(MultiValueMap<String, Function> newFunctions) {
newFunctions.forEach((k, list) -> {
List<Function> currentElements = get(k);
list.stream()
.filter(f -> !contains(currentElements, f))
.forEach(f -> functions.add(k, f));
});
}
List<Function> get(String name) {
return functions.getOrDefault(name, Collections.emptyList());
}
Optional<Function> get(String name, List<TypeDescriptor> argumentTypes) {
Stream<Function> candidates = get(name).stream()
.filter(f -> f.supports(argumentTypes));
List<Function> collect = candidates.collect(Collectors.toList());
return bestMatch(collect, argumentTypes);
}
private static boolean contains(List<Function> elements, Function f) {
return elements.stream().anyMatch(f::isSignatureEqual);
}
private static Optional<Function> bestMatch(List<Function> candidates, List<TypeDescriptor> argumentTypes) {
if (candidates.isEmpty()) {
return Optional.empty();
}
if (candidates.size() == 1) {
return Optional.of(candidates.get(0));
}
Optional<Function> exactMatch = candidates.stream().filter(f -> f.supportsExact(argumentTypes)).findFirst();
if (!exactMatch.isPresent()) {
throw new IllegalStateException(createErrorMessage(candidates, argumentTypes));
}
return exactMatch;
}
private static String createErrorMessage(List<Function> candidates, List<TypeDescriptor> argumentTypes) {
String argumentTypeString = argumentTypes.stream()
.map(TypeDescriptor::getName)
.collect(Collectors.joining(","));
return String.format(MESSAGE_TEMPLATE, candidates.get(0).getName(), argumentTypeString);
}
}