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

wtf.metio.yosql.codegen.dao.DefaultCodeGenerator Maven / Gradle / Ivy

/*
 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
 * in the LICENSE file.
 */

package wtf.metio.yosql.codegen.dao;

import wtf.metio.yosql.models.immutables.PackagedTypeSpec;
import wtf.metio.yosql.models.immutables.RepositoriesConfiguration;
import wtf.metio.yosql.models.immutables.SqlStatement;

import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public final class DefaultCodeGenerator implements CodeGenerator {

    private final RepositoryGenerator repositoryGenerator;
    private final ConverterGenerator converterGenerator;
    private final RepositoriesConfiguration repositories;

    public DefaultCodeGenerator(
            final RepositoryGenerator repositoryGenerator,
            final ConverterGenerator converterGenerator,
            final RepositoriesConfiguration repositories) {
        this.repositoryGenerator = repositoryGenerator;
        this.converterGenerator = converterGenerator;
        this.repositories = repositories;
    }

    @Override
    public Stream generateCode(final List statements) {
        return Stream.concat(generateRepositories(statements), converterGenerator.generateConverterClasses(statements));
    }

    private Stream generateRepositories(final List statements) {
        final var classes = generateRepositoryClasses(statements);
        final var interfaces = repositories.generateInterfaces()
                ? generateRepositoryInterfaces(statements)
                : Stream.empty();
        return Stream.concat(classes, interfaces);
    }

    private Stream generateRepositoryClasses(final List statements) {
        return generate(statements, Collectors.groupingBy(SqlStatement::getRepositoryClass), repositoryGenerator::generateRepositoryClass);
    }

    private Stream generateRepositoryInterfaces(final List statements) {
        return generate(statements, Collectors.groupingBy(SqlStatement::getRepositoryInterface), repositoryGenerator::generateRepositoryInterface);
    }

    private static Stream generate(
            final List statements,
            final Collector>> collector,
            final BiFunction, PackagedTypeSpec> generator) {
        return statements.parallelStream()
                .collect(collector)
                .entrySet()
                .parallelStream()
                .map(repository -> generator.apply(repository.getKey(), repository.getValue()));
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy