io.legaldocml.xpath.cerebro.CerebroListLink Maven / Gradle / Ivy
package io.legaldocml.xpath.cerebro;
import io.legaldocml.akn.AknObject;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
final class CerebroListLink implements CerebroLink {
private final Class aknClass;
private final Function> link;
public CerebroListLink(Class aknClass, Function> link) {
this.aknClass = aknClass;
this.link = link;
}
public Class getAknClass() {
return aknClass;
}
@Override
public Object apply(T t) {
List list;
if (t instanceof List) {
list = (List) ((List)t).stream().map(link).collect(Collectors.toList());
} else {
list = this.link.apply(t).collect(Collectors.toList());
}
if (list != null && list.size() == 1) {
return list.get(0);
}
return list;
}
}