All Downloads are FREE. Search and download functionalities are using the official Maven repository.

fi.jubic.easymapper.jooq.RecordMapper Maven / Gradle / Ivy

There is a newer version: 0.5.8
Show newest version
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())

        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy