fix #123 allow to specify extra annotations on repositories/methods
[yosql.git] / yosql-codegen / src / main / java / wtf / metio / yosql / codegen / dao / DefaultMethodsGenerator.java
blob860d0ef3c2b857931b9ebb0a4c844df0b3d4687f
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 */
7 package wtf.metio.yosql.codegen.dao;
9 import com.squareup.javapoet.MethodSpec;
10 import wtf.metio.yosql.models.immutables.SqlConfiguration;
11 import wtf.metio.yosql.models.immutables.SqlStatement;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Objects;
16 import java.util.function.BiFunction;
17 import java.util.function.Predicate;
18 import java.util.stream.Collectors;
20 /**
21 * Default implementation of a {@link MethodsGenerator} that delegates most of its work to other interfaces/classes.
23 public final class DefaultMethodsGenerator implements MethodsGenerator {
25 private final ConstructorGenerator constructor;
26 private final ReadMethodGenerator readMethods;
27 private final WriteMethodGenerator writeMethods;
28 private final CallMethodGenerator callingMethods;
30 /**
31 * @param constructor The constructor generator to use.
32 * @param readMethods The read methods generator to use.
33 * @param writeMethods The write methods generator to use.
34 * @param callingMethods The call methods generator to use.
36 public DefaultMethodsGenerator(
37 final ConstructorGenerator constructor,
38 final ReadMethodGenerator readMethods,
39 final WriteMethodGenerator writeMethods,
40 final CallMethodGenerator callingMethods) {
41 this.constructor = constructor;
42 this.readMethods = readMethods;
43 this.writeMethods = writeMethods;
44 this.callingMethods = callingMethods;
47 @Override
48 public Iterable<MethodSpec> asMethodsDeclarations(final List<SqlStatement> statements) {
49 final var methods = new ArrayList<MethodSpec>(statements.size());
51 methods.addAll(asMethods(statements, SqlStatement::generateStandardReadAPI,
52 readMethods::readMethodDeclaration));
53 methods.addAll(asMethods(statements, SqlStatement::generateStandardCallAPI,
54 callingMethods::callMethodDeclaration));
55 methods.addAll(asMethods(statements, SqlStatement::generateStandardWriteAPI,
56 writeMethods::writeMethodDeclaration));
57 methods.addAll(asMethods(statements, SqlStatement::generateBatchWriteAPI,
58 writeMethods::batchWriteMethodDeclaration));
60 return methods;
63 @Override
64 public Iterable<MethodSpec> asMethods(final List<SqlStatement> statements) {
65 final var methods = new ArrayList<MethodSpec>(statements.size());
67 methods.add(constructor.repository(statements));
69 methods.addAll(asMethods(statements, SqlStatement::generateStandardReadAPI, readMethods::readMethod));
70 methods.addAll(asMethods(statements, SqlStatement::generateStandardCallAPI, callingMethods::callMethod));
71 methods.addAll(asMethods(statements, SqlStatement::generateStandardWriteAPI, writeMethods::writeMethod));
72 methods.addAll(asMethods(statements, SqlStatement::generateBatchWriteAPI, writeMethods::batchWriteMethod));
74 return methods;
77 private static List<MethodSpec> asMethods(
78 final List<SqlStatement> statements,
79 final Predicate<SqlStatement> filter,
80 final BiFunction<SqlConfiguration, List<SqlStatement>, MethodSpec> generator) {
81 return statements.stream()
82 .filter(filter)
83 .collect(Collectors.groupingBy(SqlStatement::getName))
84 .values()
85 .stream()
86 .map(statementsInRepository -> generator.apply(
87 SqlConfiguration.fromStatements(statementsInRepository), statementsInRepository))
88 .filter(Objects::nonNull)
89 .toList();