View Javadoc
1   /*
2    *    Copyright 2006-2023 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.api.dom.kotlin;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Optional;
21  
22  public class KotlinFunction extends KotlinNamedItem {
23      private final List<KotlinArg> arguments = new ArrayList<>();
24      private final List<String> codeLines = new ArrayList<>();
25      private final String explicitReturnType;
26      private final boolean isOneLineFunction;
27  
28      private KotlinFunction(Builder builder) {
29          super(builder);
30          arguments.addAll(builder.arguments);
31          codeLines.addAll(builder.codeLines);
32          explicitReturnType = builder.explicitReturnType;
33          isOneLineFunction = builder.isOneLineFunction;
34      }
35  
36      public void addArgument(KotlinArg argument) {
37          arguments.add(argument);
38      }
39  
40      public List<KotlinArg> getArguments() {
41          return arguments;
42      }
43  
44      public void addCodeLine(String codeLine) {
45          this.codeLines.add(codeLine);
46      }
47  
48      public void addCodeLines(List<String> codeLines) {
49          this.codeLines.addAll(codeLines);
50      }
51  
52      public List<String> getCodeLines() {
53          return codeLines;
54      }
55  
56      public Optional<String> getExplicitReturnType() {
57          return Optional.ofNullable(explicitReturnType);
58      }
59  
60      public boolean isOneLineFunction() {
61          return isOneLineFunction;
62      }
63  
64      @Override
65      public <R> R accept(KotlinNamedItemVisitor<R> visitor) {
66          return visitor.visit(this);
67      }
68  
69      public static Builder newOneLineFunction(String name) {
70          return new Builder(name, true);
71      }
72  
73      public static Builder newMultiLineFunction(String name) {
74          return new Builder(name, false);
75      }
76  
77      public static class Builder extends AbstractBuilder<Builder> {
78          private final boolean isOneLineFunction;
79          private final List<KotlinArg> arguments = new ArrayList<>();
80          private final List<String> codeLines = new ArrayList<>();
81          private String explicitReturnType;
82  
83          private Builder(String name, boolean isOneLineFunction) {
84              super(name);
85              this.isOneLineFunction = isOneLineFunction;
86          }
87  
88          public Builder withArgument(KotlinArg argument) {
89              arguments.add(argument);
90              return this;
91          }
92  
93          public Builder withCodeLine(String codeLine) {
94              codeLines.add(codeLine);
95              return this;
96          }
97  
98          public Builder withExplicitReturnType(String explicitReturnType) {
99              this.explicitReturnType = explicitReturnType;
100             return this;
101         }
102 
103         public Builder getThis() {
104             return this;
105         }
106 
107         public KotlinFunction build() {
108             return new KotlinFunction(this);
109         }
110     }
111 }