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