Fixing sonar issues 2
[mdsal.git] / code-generator / binding-generator-util / src / main / java / org / opendaylight / yangtools / binding / generator / util / generated / type / builder / AbstractGeneratedTypeBuilder.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.binding.generator.util.generated.type.builder;
9
10 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
11 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
12 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
13 import org.opendaylight.yangtools.sal.binding.model.api.Type;
14 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.*;
15
16 import java.util.ArrayList;
17 import java.util.List;
18
19 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType implements
20         GeneratedTypeBuilderBase<T> {
21
22     private String comment = "";
23
24     private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<>();
25     private final List<Type> implementsTypes = new ArrayList<>();
26     private final List<EnumBuilder> enumDefinitions = new ArrayList<>();
27     private final List<Constant> constants = new ArrayList<>();
28     private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<>();
29     private final List<GeneratedTypeBuilder> enclosedTypes = new ArrayList<>();
30     private final List<GeneratedTOBuilder> enclosedTransferObjects = new ArrayList<>();
31     private final List<GeneratedPropertyBuilder> properties = new ArrayList<>();
32     private boolean isAbstract;
33
34     public AbstractGeneratedTypeBuilder(final String packageName, final String name) {
35         super(packageName, name);
36     }
37
38     protected String getComment() {
39         return comment;
40     }
41
42     protected List<AnnotationTypeBuilder> getAnnotations() {
43         return annotationBuilders;
44     }
45
46     protected boolean isAbstract() {
47         return isAbstract;
48     }
49
50     protected List<Type> getImplementsTypes() {
51         return implementsTypes;
52     }
53
54     protected List<EnumBuilder> getEnumerations() {
55         return enumDefinitions;
56     }
57
58     protected List<Constant> getConstants() {
59         return constants;
60     }
61
62     protected List<MethodSignatureBuilder> getMethodDefinitions() {
63         return methodDefinitions;
64     }
65
66     protected List<GeneratedTypeBuilder> getEnclosedTypes() {
67         return enclosedTypes;
68     }
69
70     protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
71         return enclosedTransferObjects;
72     }
73
74     protected abstract T thisInstance();
75
76     @Override
77     public GeneratedTOBuilder addEnclosingTransferObject(String name) {
78         if (name == null) {
79             throw new IllegalArgumentException("Name for Enclosing Generated Transfer Object cannot be null!");
80         }
81         GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
82         enclosedTransferObjects.add(builder);
83         return builder;
84     }
85
86     @Override
87     public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
88         if (genTOBuilder == null) {
89             throw new IllegalArgumentException("Parameter genTOBuilder cannot be null!");
90         }
91         enclosedTransferObjects.add(genTOBuilder);
92         return thisInstance();
93     }
94
95     @Override
96     public T addComment(String comment) {
97         this.comment = comment;
98         return thisInstance();
99     }
100
101     @Override
102     public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
103         if (packageName == null) {
104             throw new IllegalArgumentException("Package Name for Annotation Type cannot be null!");
105         }
106         if (name == null) {
107             throw new IllegalArgumentException("Name of Annotation Type cannot be null!");
108         }
109
110         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
111         annotationBuilders.add(builder);
112         return builder;
113     }
114
115     @Override
116     public T setAbstract(boolean isAbstract) {
117         this.isAbstract = isAbstract;
118         return thisInstance();
119     }
120
121     @Override
122     public T addImplementsType(Type genType) {
123         if (genType == null) {
124             throw new IllegalArgumentException("Type cannot be null");
125         }
126         implementsTypes.add(genType);
127         return thisInstance();
128     }
129
130     @Override
131     public Constant addConstant(Type type, String name, Object value) {
132         if (type == null) {
133             throw new IllegalArgumentException("Returning Type for Constant cannot be null!");
134         }
135         if (name == null) {
136             throw new IllegalArgumentException("Name of constant cannot be null!");
137         }
138
139         final Constant constant = new ConstantImpl(this, type, name, value);
140         constants.add(constant);
141         return constant;
142     }
143
144     @Override
145     public EnumBuilder addEnumeration(String name) {
146         if (name == null) {
147             throw new IllegalArgumentException("Name of enumeration cannot be null!");
148         }
149         final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
150         enumDefinitions.add(builder);
151         return builder;
152     }
153
154     @Override
155     public MethodSignatureBuilder addMethod(String name) {
156         if (name == null) {
157             throw new IllegalArgumentException("Name of method cannot be null!");
158         }
159         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
160         builder.setAccessModifier(AccessModifier.PUBLIC);
161         builder.setAbstract(true);
162         methodDefinitions.add(builder);
163         return builder;
164     }
165
166     @Override
167     public boolean containsMethod(String name) {
168         if (name == null) {
169             throw new IllegalArgumentException("Parameter name can't be null");
170         }
171         for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
172             if (name.equals(methodDefinition.getName())) {
173                 return true;
174             }
175         }
176         return false;
177     }
178
179     @Override
180     public GeneratedPropertyBuilder addProperty(String name) {
181         final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
182         builder.setAccessModifier(AccessModifier.PUBLIC);
183         properties.add(builder);
184         return builder;
185     }
186
187     @Override
188     public boolean containsProperty(String name) {
189         if (name == null) {
190             throw new IllegalArgumentException("Parameter name can't be null");
191         }
192         for (GeneratedPropertyBuilder property : properties) {
193             if (name.equals(property.getName())) {
194                 return true;
195             }
196         }
197         return false;
198     }
199
200     @Override
201     public int hashCode() {
202         final int prime = 31;
203         int result = 1;
204         result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
205         result = prime * result + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
206         return result;
207     }
208
209     @Override
210     public boolean equals(Object obj) {
211         if (this == obj) {
212             return true;
213         }
214         if (obj == null) {
215             return false;
216         }
217         if (getClass() != obj.getClass()) {
218             return false;
219         }
220         AbstractGeneratedTypeBuilder<T> other = (AbstractGeneratedTypeBuilder<T>) obj;
221         if (getName() == null) {
222             if (other.getName() != null) {
223                 return false;
224             }
225         } else if (!getName().equals(other.getName())) {
226             return false;
227         }
228         if (getPackageName() == null) {
229             if (other.getPackageName() != null) {
230                 return false;
231             }
232         } else if (!getPackageName().equals(other.getPackageName())) {
233             return false;
234         }
235         return true;
236     }
237
238     public Type getParent() {
239         return null;
240     }
241
242     protected List<GeneratedPropertyBuilder> getProperties() {
243         return properties;
244     }
245 }