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