cdc.applic.dictionaries.edit.checks.utils.AmbiguityHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cdc-applic-dictionaries-edit Show documentation
Show all versions of cdc-applic-dictionaries-edit Show documentation
Applicabilities Dictionaries Edition.
The newest version!
package cdc.applic.dictionaries.edit.checks.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public final class AmbiguityHelper {
private final Function> keysExtractor;
private final Predicate keyValidator;
private AmbiguityHelper(Function> keysExtractor,
Predicate keyValidator) {
this.keysExtractor = keysExtractor;
this.keyValidator = keyValidator;
}
public static AmbiguityHelper ofN(Function> keysExtractor,
Predicate keyValidator) {
return new AmbiguityHelper<>(keysExtractor,
keyValidator);
}
public static AmbiguityHelper of1(Function keyExtractor,
Predicate keyValidator) {
final Function> keysExtractor =
e -> {
final K k = keyExtractor.apply(e);
return keyValidator.test(k) ? Set.of(k) : Set.of();
};
return new AmbiguityHelper<>(keysExtractor,
keyValidator);
}
public List getAmbiguities(List extends E> elements,
List extends E> with) {
final List list = elements.stream()
.flatMap(e -> keysExtractor.apply(e).stream())
.filter(keyValidator)
.toList();
final Set set = with.stream()
.flatMap(e -> keysExtractor.apply(e).stream())
.filter(keyValidator)
.collect(Collectors.toSet());
final List ambiguities = new ArrayList<>();
for (final K k : list) {
if (set.contains(k)) {
ambiguities.add(k);
}
}
return ambiguities;
}
public boolean hasAmbiguities(List extends E> elements,
List extends E> with) {
return !getAmbiguities(elements, with).isEmpty();
}
public String explain(List extends E> elements,
List extends E> with) {
final List ambiguities = getAmbiguities(elements, with);
return ambiguities.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
}
}