1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
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  * A copy of the License is located at
7  *
8  *  http://aws.amazon.com/apache2.0
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 package software.amazon.awssdk.codegen.model.intermediate;
17 
18 import java.util.Collection;
19 import java.util.List;
20 
21 public class VariableModel extends DocumentationModel {
22 
23     private String variableName;
24 
25     private String variableType;
26 
27     /**
28      * Variable declaration type, which can be different from the
29      * {@link #variableType}, for example, for auto construct list or map.
30      * Otherwise, it's the same as the {@link #variableType}.
31      */
32     private String variableDeclarationType;
33 
VariableModel()34     public VariableModel() {
35     }
36 
VariableModel(String variableName, String variableType)37     public VariableModel(String variableName, String variableType) {
38         this(variableName, variableType, variableType);
39     }
40 
VariableModel(String variableName, String variableType, String variableDeclarationType)41     public VariableModel(String variableName,
42                          String variableType,
43                          String variableDeclarationType) {
44         setVariableName(variableName);
45         setVariableType(variableType);
46         setVariableDeclarationType(variableDeclarationType);
47     }
48 
getVariableName()49     public String getVariableName() {
50         return variableName;
51     }
52 
setVariableName(String variableName)53     public void setVariableName(String variableName) {
54         this.variableName = variableName;
55     }
56 
getVariableType()57     public String getVariableType() {
58         return variableType;
59     }
60 
setVariableType(String variableType)61     public void setVariableType(String variableType) {
62         this.variableType = variableType;
63     }
64 
getSimpleType()65     public String getSimpleType() {
66         if (variableType.contains(".")) {
67             return variableType.substring(variableType.lastIndexOf('.') + 1);
68         }
69         return variableType;
70     }
71 
withDocumentation(String documentation)72     public VariableModel withDocumentation(String documentation) {
73         setDocumentation(documentation);
74         return this;
75     }
76 
getVariableDeclarationType()77     public String getVariableDeclarationType() {
78         return variableDeclarationType;
79     }
80 
setVariableDeclarationType(String variableDeclarationType)81     public void setVariableDeclarationType(String variableDeclarationType) {
82         this.variableDeclarationType = variableDeclarationType;
83     }
84 
85     /**
86      * Returns the Java type used for the input parameter of a setter method.
87      */
getVariableSetterType()88     public String getVariableSetterType() {
89         String prefix = List.class.getName();
90         if (variableType.startsWith(prefix)) {
91             return Collection.class.getName() + variableType.substring(prefix.length());
92         } else {
93             return variableType;
94         }
95     }
96 
97     @Override
toString()98     public String toString() {
99         return variableName;
100     }
101 }
102