View Javadoc
1   /*
2    *    Copyright 2006-2026 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
15   */
16  package org.mybatis.generator.codegen;
17  
18  import java.util.List;
19  import java.util.Objects;
20  
21  import org.jspecify.annotations.Nullable;
22  import org.mybatis.generator.api.CommentGenerator;
23  import org.mybatis.generator.api.IntrospectedTable;
24  import org.mybatis.generator.api.ProgressCallback;
25  import org.mybatis.generator.config.Context;
26  import org.mybatis.generator.internal.PluginAggregator;
27  
28  public abstract class AbstractGenerator {
29      protected final Context context;
30      protected final IntrospectedTable introspectedTable;
31      protected final List<String> warnings;
32      protected final ProgressCallback progressCallback;
33      protected final CommentGenerator commentGenerator;
34      protected final PluginAggregator pluginAggregator;
35  
36      protected AbstractGenerator(AbstractGeneratorBuilder<?> builder) {
37          this.context = Objects.requireNonNull(builder.context);
38          this.introspectedTable = Objects.requireNonNull(builder.introspectedTable);
39          this.warnings = Objects.requireNonNull(builder.warnings);
40          this.progressCallback = Objects.requireNonNull(builder.progressCallback);
41          this.commentGenerator = Objects.requireNonNull(builder.commentGenerator);
42          this.pluginAggregator = Objects.requireNonNull(builder.pluginAggregator);
43      }
44  
45      protected <T extends AbstractGeneratorBuilder<T>> T initializeSubBuilder(T builder) {
46          return builder.withContext(context)
47                  .withIntrospectedTable(introspectedTable)
48                  .withWarnings(warnings)
49                  .withProgressCallback(progressCallback)
50                  .withCommentGenerator(commentGenerator)
51                  .withPluginAggregator(pluginAggregator);
52      }
53  
54      public abstract static class AbstractGeneratorBuilder<T extends AbstractGeneratorBuilder<T>> {
55          private @Nullable Context context;
56          private @Nullable IntrospectedTable introspectedTable;
57          private @Nullable List<String> warnings;
58          private @Nullable ProgressCallback progressCallback;
59          private @Nullable CommentGenerator commentGenerator;
60          private @Nullable PluginAggregator pluginAggregator;
61  
62          public T withContext(Context context) {
63              this.context = context;
64              return getThis();
65          }
66  
67          public T withIntrospectedTable(IntrospectedTable introspectedTable) {
68              this.introspectedTable = introspectedTable;
69              return getThis();
70          }
71  
72          public T withWarnings(List<String> warnings) {
73              this.warnings = warnings;
74              return getThis();
75          }
76  
77          public T withProgressCallback(ProgressCallback progressCallback) {
78              this.progressCallback = progressCallback;
79              return getThis();
80          }
81  
82          public T withCommentGenerator(CommentGenerator commentGenerator) {
83              this.commentGenerator = commentGenerator;
84              return getThis();
85          }
86  
87          public T withPluginAggregator(PluginAggregator pluginAggregator) {
88              this.pluginAggregator = pluginAggregator;
89              return getThis();
90          }
91  
92          protected abstract T getThis();
93      }
94  }