View Javadoc
1   /**
2    *    Copyright 2006-2016 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    *       http://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.java;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  /**
22   * @author Jeff Butler
23   */
24  public class Parameter {
25      private String name;
26      private FullyQualifiedJavaType type;
27      private boolean isVarargs;
28  
29      private List<String> annotations;
30  
31      public Parameter(FullyQualifiedJavaType type, String name, boolean isVarargs) {
32          super();
33          this.name = name;
34          this.type = type;
35          this.isVarargs = isVarargs;
36          annotations = new ArrayList<String>();
37      }
38  
39      public Parameter(FullyQualifiedJavaType type, String name) {
40          this(type, name, false);
41      }
42  
43      public Parameter(FullyQualifiedJavaType type, String name, String annotation) {
44          this(type, name, false);
45          addAnnotation(annotation);
46      }
47  
48      public Parameter(FullyQualifiedJavaType type, String name, String annotation, boolean isVarargs) {
49          this(type, name, isVarargs);
50          addAnnotation(annotation);
51      }
52  
53      /**
54       * @return Returns the name.
55       */
56      public String getName() {
57          return name;
58      }
59  
60      /**
61       * @return Returns the type.
62       */
63      public FullyQualifiedJavaType getType() {
64          return type;
65      }
66  
67      public List<String> getAnnotations() {
68          return annotations;
69      }
70  
71      public void addAnnotation(String annotation) {
72          annotations.add(annotation);
73      }
74  
75      public String getFormattedContent(CompilationUnit compilationUnit) {
76          StringBuilder sb = new StringBuilder();
77  
78          for (String annotation : annotations) {
79              sb.append(annotation);
80              sb.append(' ');
81          }
82  
83          sb.append(JavaDomUtils.calculateTypeName(compilationUnit, type));
84          
85          sb.append(' ');
86          if (isVarargs) {
87              sb.append("... "); //$NON-NLS-1$
88          }
89          sb.append(name);
90  
91          return sb.toString();
92      }
93  
94      @Override
95      public String toString() {
96          return getFormattedContent(null);
97      }
98  
99      public boolean isVarargs() {
100         return isVarargs;
101     }
102 }