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
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
;
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
;
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
;
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
));
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
));
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()
83 .collect(Collectors
.groupingBy(SqlStatement
::getName
))
86 .map(statementsInRepository
-> generator
.apply(
87 SqlConfiguration
.fromStatements(statementsInRepository
), statementsInRepository
))
88 .filter(Objects
::nonNull
)