fi.jubic.easymapper.jooq.RecordMapper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of easymapper-jooq Show documentation
Show all versions of easymapper-jooq Show documentation
Map JOOQ records to value objects.
package fi.jubic.easymapper.jooq;
import fi.jubic.easymapper.Mangler;
import fi.jubic.easymapper.Mapper;
import fi.jubic.easymapper.MappingException;
import org.jooq.Record;
import org.jooq.Table;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public interface RecordMapper
extends Mapper, Mangler {
RecordMapper alias(Table alias);
Table table();
default Collector> partitionAndFlatten(
JooqFieldAccessor partitionKeyAccessor,
Table table,
Collector> collector
) {
return Collectors.collectingAndThen(
Collectors.groupingBy(
record -> {
try {
return Optional.ofNullable(
partitionKeyAccessor.extract(record.into(table))
);
}
catch (MappingException e) {
throw new RuntimeException(e);
}
},
collector
),
map -> map.entrySet()
.stream()
.filter(entry -> entry.getKey().isPresent())
.map(Map.Entry::getValue)
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList())
);
}
}