Remove parent type references
[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 static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
12
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import org.opendaylight.mdsal.binding.model.api.AbstractBaseType;
17 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
18 import org.opendaylight.mdsal.binding.model.api.Constant;
19 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
20 import org.opendaylight.mdsal.binding.model.api.Type;
21 import org.opendaylight.mdsal.binding.model.api.TypeComment;
22 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition;
23 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
25 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
26 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
27 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
28 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
29 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
30 import org.opendaylight.yangtools.util.LazyCollections;
31
32 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType
33         implements GeneratedTypeBuilderBase<T> {
34
35     private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
36     private List<Type> implementsTypes = Collections.emptyList();
37     private List<EnumBuilder> enumDefinitions = Collections.emptyList();
38     private List<Constant> constants = Collections.emptyList();
39     private List<MethodSignatureBuilder> methodDefinitions = Collections.emptyList();
40     private final List<GeneratedTypeBuilder> enclosedTypes = Collections.emptyList();
41     private List<GeneratedTOBuilder> enclosedTransferObjects = Collections.emptyList();
42     private List<GeneratedPropertyBuilder> properties = Collections.emptyList();
43     private TypeComment comment;
44     private boolean isAbstract;
45     private YangSourceDefinition yangSourceDefinition;
46
47     protected AbstractGeneratedTypeBuilder(final JavaTypeName identifier) {
48         super(identifier);
49     }
50
51     protected TypeComment getComment() {
52         return this.comment;
53     }
54
55     protected List<AnnotationTypeBuilder> getAnnotations() {
56         return this.annotationBuilders;
57     }
58
59     @Override
60     public boolean isAbstract() {
61         return this.isAbstract;
62     }
63
64     @Override
65     public List<Type> getImplementsTypes() {
66         return this.implementsTypes;
67     }
68
69     protected List<EnumBuilder> getEnumerations() {
70         return this.enumDefinitions;
71     }
72
73     protected List<Constant> getConstants() {
74         return this.constants;
75     }
76
77     @Override
78     public List<MethodSignatureBuilder> getMethodDefinitions() {
79         return this.methodDefinitions;
80     }
81
82     protected List<GeneratedTypeBuilder> getEnclosedTypes() {
83         return this.enclosedTypes;
84     }
85
86     protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
87         return this.enclosedTransferObjects;
88     }
89
90     protected abstract T thisInstance();
91
92     abstract AbstractEnumerationBuilder newEnumerationBuilder(JavaTypeName identifier);
93
94     @Override
95     public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
96         checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
97         final GeneratedTOBuilder builder = new CodegenGeneratedTOBuilder(getIdentifier().createEnclosed(name));
98
99         checkArgument(!enclosedTransferObjects.contains(builder),
100             "This generated type already contains equal enclosing transfer object.");
101         this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, builder);
102         return builder;
103     }
104
105     @Override
106     public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
107         checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
108         checkArgument(!enclosedTransferObjects.contains(genTOBuilder),
109             "This generated type already contains equal enclosing transfer object.");
110         this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, genTOBuilder);
111         return thisInstance();
112     }
113
114     @Override
115     public T addComment(final TypeComment newComment) {
116         this.comment = requireNonNull(newComment);
117         return thisInstance();
118     }
119
120     @Override
121     public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
122         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
123
124         checkArgument(!annotationBuilders.contains(builder), "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 newIsAbstract) {
131         this.isAbstract = newIsAbstract;
132         return thisInstance();
133     }
134
135     @Override
136     public T addImplementsType(final Type genType) {
137         checkArgument(genType != null, "Type cannot be null");
138         checkArgument(!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         checkArgument(type != null, "Returning Type for Constant cannot be null!");
147         checkArgument(name != null, "Name of constant cannot be null!");
148         checkArgument(!containsConstant(name),
149             "This generated type already contains a \"%s\" constant", name);
150
151         final Constant constant = new ConstantImpl(type, name, value);
152         this.constants = LazyCollections.lazyAdd(this.constants, constant);
153         return constant;
154     }
155
156     public boolean containsConstant(final String name) {
157         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         checkArgument(name != null, "Name of enumeration cannot be null!");
169
170         // This enumeration may be generated from a leaf, which may end up colliding with its enclosing type
171         // hierarchy. If that is the case, we use a single '$' suffix to disambiguate -- that cannot come from the user
172         // and hence is marking our namespace
173         final EnumBuilder builder = newEnumerationBuilder(getIdentifier().createEnclosed(name, "$"));
174         checkArgument(!enumDefinitions.contains(builder),
175             "Generated type %s already contains an enumeration for %s", this, builder);
176         this.enumDefinitions = LazyCollections.lazyAdd(this.enumDefinitions, builder);
177         return builder;
178     }
179
180     @Override
181     public MethodSignatureBuilder addMethod(final String name) {
182         checkArgument(name != null, "Name of method cannot be null!");
183         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
184         builder.setAccessModifier(AccessModifier.PUBLIC);
185         builder.setAbstract(true);
186         this.methodDefinitions = LazyCollections.lazyAdd(this.methodDefinitions, builder);
187         return builder;
188     }
189
190     @Override
191     public boolean containsMethod(final String name) {
192         checkArgument(name != null, "Parameter name can't be null");
193         for (final MethodSignatureBuilder methodDefinition : this.methodDefinitions) {
194             if (name.equals(methodDefinition.getName())) {
195                 return true;
196             }
197         }
198         return false;
199     }
200
201     @Override
202     public GeneratedPropertyBuilder addProperty(final String name) {
203         checkArgument(name != null, "Parameter name can't be null");
204         checkArgument(!containsProperty(name),
205             "This generated type already contains property with the same name.");
206
207         final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
208         builder.setAccessModifier(AccessModifier.PUBLIC);
209         this.properties = LazyCollections.lazyAdd(this.properties, builder);
210         return builder;
211     }
212
213     @Override
214     public boolean containsProperty(final String name) {
215         checkArgument(name != null, "Parameter name can't be null");
216         for (final GeneratedPropertyBuilder property : this.properties) {
217             if (name.equals(property.getName())) {
218                 return true;
219             }
220         }
221         return false;
222     }
223
224     public Type getParent() {
225         return null;
226     }
227
228     @Override
229     public List<GeneratedPropertyBuilder> getProperties() {
230         return this.properties;
231     }
232
233     @Override
234     public Optional<YangSourceDefinition> getYangSourceDefinition() {
235         return Optional.ofNullable(yangSourceDefinition);
236     }
237
238
239     @Override
240     public void setYangSourceDefinition(final YangSourceDefinition definition) {
241         yangSourceDefinition = requireNonNull(definition);
242     }
243 }