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