Integrate JavaTypeName as Identifier
[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.JavaTypeName;
18 import org.opendaylight.mdsal.binding.model.api.Type;
19 import org.opendaylight.mdsal.binding.model.api.TypeComment;
20 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition;
21 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
22 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
23 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
25 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
26 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
27 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
28 import org.opendaylight.mdsal.binding.model.util.AbstractBaseType;
29 import org.opendaylight.yangtools.util.LazyCollections;
30
31 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType
32         implements GeneratedTypeBuilderBase<T> {
33
34     private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
35     private List<Type> implementsTypes = Collections.emptyList();
36     private List<EnumBuilder> enumDefinitions = Collections.emptyList();
37     private List<Constant> constants = Collections.emptyList();
38     private List<MethodSignatureBuilder> methodDefinitions = Collections.emptyList();
39     private final List<GeneratedTypeBuilder> enclosedTypes = Collections.emptyList();
40     private List<GeneratedTOBuilder> enclosedTransferObjects = Collections.emptyList();
41     private List<GeneratedPropertyBuilder> properties = Collections.emptyList();
42     private TypeComment comment;
43     private boolean isAbstract;
44     private YangSourceDefinition yangSourceDefinition;
45
46     protected AbstractGeneratedTypeBuilder(final JavaTypeName identifier) {
47         super(identifier);
48     }
49
50     protected TypeComment getComment() {
51         return this.comment;
52     }
53
54     protected List<AnnotationTypeBuilder> getAnnotations() {
55         return this.annotationBuilders;
56     }
57
58     @Override
59     public boolean isAbstract() {
60         return this.isAbstract;
61     }
62
63     @Override
64     public List<Type> getImplementsTypes() {
65         return this.implementsTypes;
66     }
67
68     protected List<EnumBuilder> getEnumerations() {
69         return this.enumDefinitions;
70     }
71
72     protected List<Constant> getConstants() {
73         return this.constants;
74     }
75
76     @Override
77     public List<MethodSignatureBuilder> getMethodDefinitions() {
78         return this.methodDefinitions;
79     }
80
81     protected List<GeneratedTypeBuilder> getEnclosedTypes() {
82         return this.enclosedTypes;
83     }
84
85     protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
86         return this.enclosedTransferObjects;
87     }
88
89     protected abstract T thisInstance();
90
91     abstract AbstractEnumerationBuilder newEnumerationBuilder(JavaTypeName identifier);
92
93     @Override
94     public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
95         Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
96         final GeneratedTOBuilder builder = new CodegenGeneratedTOBuilder(getIdentifier().createEnclosed(name));
97
98         Preconditions.checkArgument(!this.enclosedTransferObjects.contains(builder),
99             "This generated type already contains equal enclosing transfer object.");
100         this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, builder);
101         return builder;
102     }
103
104     @Override
105     public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
106         Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
107         Preconditions.checkArgument(!this.enclosedTransferObjects.contains(genTOBuilder),
108             "This generated type already contains equal enclosing transfer object.");
109         this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, genTOBuilder);
110         return thisInstance();
111     }
112
113     @Override
114     public T addComment(final TypeComment comment) {
115         this.comment = Preconditions.checkNotNull(comment);
116         return thisInstance();
117     }
118
119     @Override
120     public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
121         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
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 = newEnumerationBuilder(getIdentifier().createEnclosed(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 getIdentifier().equals(other.getIdentifier());
263     }
264 }