fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / main / java / wtf / metio / yosql / codegen / dao / DefaultCodeGenerator.java
blob753e4ec88368b47108e50b0d0a93a6d721cb4009
1 /*
2 * This file is part of yosql. It is subject to the license terms in the LICENSE file found in the top-level
3 * directory of this distribution and at https://creativecommons.org/publicdomain/zero/1.0/. No part of yosql,
4 * including this file, may be copied, modified, propagated, or distributed except according to the terms contained
5 * in the LICENSE file.
6 */
8 package wtf.metio.yosql.codegen.dao;
10 import wtf.metio.yosql.models.immutables.PackagedTypeSpec;
11 import wtf.metio.yosql.models.immutables.RepositoriesConfiguration;
12 import wtf.metio.yosql.models.immutables.SqlStatement;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.function.BiFunction;
17 import java.util.stream.Collector;
18 import java.util.stream.Collectors;
19 import java.util.stream.Stream;
21 public final class DefaultCodeGenerator implements CodeGenerator {
23 private final RepositoryGenerator repositoryGenerator;
24 private final ConverterGenerator converterGenerator;
25 private final RepositoriesConfiguration repositories;
27 public DefaultCodeGenerator(
28 final RepositoryGenerator repositoryGenerator,
29 final ConverterGenerator converterGenerator,
30 final RepositoriesConfiguration repositories) {
31 this.repositoryGenerator = repositoryGenerator;
32 this.converterGenerator = converterGenerator;
33 this.repositories = repositories;
36 @Override
37 public Stream<PackagedTypeSpec> generateCode(final List<SqlStatement> statements) {
38 return Stream.concat(generateRepositories(statements), converterGenerator.generateConverterClasses(statements));
41 private Stream<PackagedTypeSpec> generateRepositories(final List<SqlStatement> statements) {
42 final var classes = generateRepositoryClasses(statements);
43 final var interfaces = repositories.generateInterfaces()
44 ? generateRepositoryInterfaces(statements)
45 : Stream.<PackagedTypeSpec>empty();
46 return Stream.concat(classes, interfaces);
49 private Stream<PackagedTypeSpec> generateRepositoryClasses(final List<SqlStatement> statements) {
50 return generate(statements, Collectors.groupingBy(SqlStatement::getRepositoryClass), repositoryGenerator::generateRepositoryClass);
53 private Stream<PackagedTypeSpec> generateRepositoryInterfaces(final List<SqlStatement> statements) {
54 return generate(statements, Collectors.groupingBy(SqlStatement::getRepositoryInterface), repositoryGenerator::generateRepositoryInterface);
57 private static Stream<PackagedTypeSpec> generate(
58 final List<SqlStatement> statements,
59 final Collector<SqlStatement, ?, Map<String, List<SqlStatement>>> collector,
60 final BiFunction<String, List<SqlStatement>, PackagedTypeSpec> generator) {
61 return statements.parallelStream()
62 .collect(collector)
63 .entrySet()
64 .parallelStream()
65 .map(repository -> generator.apply(repository.getKey(), repository.getValue()));