Hide binding.model.api.DefaultType
[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 com.google.common.base.MoreObjects.ToStringHelper;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import org.opendaylight.mdsal.binding.model.api.AbstractType;
18 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
19 import org.opendaylight.mdsal.binding.model.api.Constant;
20 import org.opendaylight.mdsal.binding.model.api.Enumeration;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.mdsal.binding.model.api.TypeComment;
25 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition;
26 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
27 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
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 AbstractType
33         implements GeneratedTypeBuilderBase<T> {
34
35     private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
36     private List<Type> implementsTypes = Collections.emptyList();
37     private List<Enumeration> enumDefinitions = Collections.emptyList();
38     private List<Constant> constants = Collections.emptyList();
39     private List<MethodSignatureBuilder> methodDefinitions = Collections.emptyList();
40     private List<GeneratedTransferObject> 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<Enumeration> 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<GeneratedTransferObject> getEnclosedTransferObjects() {
82         return this.enclosedTransferObjects;
83     }
84
85     protected abstract T thisInstance();
86
87     @Override
88     public T addEnclosingTransferObject(final GeneratedTransferObject genTO) {
89         checkArgument(genTO != null, "Parameter genTO cannot be null!");
90         checkArgument(!enclosedTransferObjects.contains(genTO),
91             "This generated type already contains equal enclosing transfer object.");
92         this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, genTO);
93         return thisInstance();
94     }
95
96     @Override
97     public T addComment(final TypeComment newComment) {
98         this.comment = requireNonNull(newComment);
99         return thisInstance();
100     }
101
102     @Override
103     public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
104         final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
105
106         checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
107         this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
108         return builder;
109     }
110
111     @Override
112     public T setAbstract(final boolean newIsAbstract) {
113         this.isAbstract = newIsAbstract;
114         return thisInstance();
115     }
116
117     @Override
118     public T addImplementsType(final Type genType) {
119         checkArgument(genType != null, "Type cannot be null");
120         checkArgument(!implementsTypes.contains(genType),
121             "This generated type already contains equal implements type.");
122         this.implementsTypes = LazyCollections.lazyAdd(this.implementsTypes, genType);
123         return thisInstance();
124     }
125
126     @Override
127     public Constant addConstant(final Type type, final String name, final Object value) {
128         checkArgument(type != null, "Returning Type for Constant cannot be null!");
129         checkArgument(name != null, "Name of constant cannot be null!");
130         checkArgument(!containsConstant(name),
131             "This generated type already contains a \"%s\" constant", name);
132
133         final Constant constant = new ConstantImpl(type, name, value);
134         this.constants = LazyCollections.lazyAdd(this.constants, constant);
135         return constant;
136     }
137
138     public boolean containsConstant(final String name) {
139         checkArgument(name != null, "Parameter name can't be null");
140         for (final Constant constant : this.constants) {
141             if (name.equals(constant.getName())) {
142                 return true;
143             }
144         }
145         return false;
146     }
147
148     @Override
149     public void addEnumeration(final Enumeration enumeration) {
150         checkArgument(enumeration != null, "Enumeration cannot be null!");
151
152         // This enumeration may be generated from a leaf, which may end up colliding with its enclosing type
153         // hierarchy. If that is the case, we use a single '$' suffix to disambiguate -- that cannot come from the user
154         // and hence is marking our namespace
155         checkArgument(!enumDefinitions.contains(enumeration),
156             "Generated type %s already contains an enumeration for %s", this, enumeration);
157         this.enumDefinitions = LazyCollections.lazyAdd(this.enumDefinitions, enumeration);
158     }
159
160     @Override
161     public MethodSignatureBuilder addMethod(final String name) {
162         checkArgument(name != null, "Name of method cannot be null!");
163         final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
164         builder.setAccessModifier(AccessModifier.PUBLIC);
165         builder.setAbstract(true);
166         this.methodDefinitions = LazyCollections.lazyAdd(this.methodDefinitions, builder);
167         return builder;
168     }
169
170     @Override
171     public boolean containsMethod(final String name) {
172         checkArgument(name != null, "Parameter name can't be null");
173         for (final MethodSignatureBuilder methodDefinition : this.methodDefinitions) {
174             if (name.equals(methodDefinition.getName())) {
175                 return true;
176             }
177         }
178         return false;
179     }
180
181     @Override
182     public GeneratedPropertyBuilder addProperty(final String name) {
183         checkArgument(name != null, "Parameter name can't be null");
184         checkArgument(!containsProperty(name),
185             "This generated type already contains property with the same name.");
186
187         final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
188         builder.setAccessModifier(AccessModifier.PUBLIC);
189         this.properties = LazyCollections.lazyAdd(this.properties, builder);
190         return builder;
191     }
192
193     @Override
194     public boolean containsProperty(final String name) {
195         checkArgument(name != null, "Parameter name can't be null");
196         for (final GeneratedPropertyBuilder property : this.properties) {
197             if (name.equals(property.getName())) {
198                 return true;
199             }
200         }
201         return false;
202     }
203
204     public Type getParent() {
205         return null;
206     }
207
208     @Override
209     public List<GeneratedPropertyBuilder> getProperties() {
210         return this.properties;
211     }
212
213     @Override
214     public Optional<YangSourceDefinition> getYangSourceDefinition() {
215         return Optional.ofNullable(yangSourceDefinition);
216     }
217
218     @Override
219     public void setYangSourceDefinition(final YangSourceDefinition definition) {
220         yangSourceDefinition = requireNonNull(definition);
221     }
222
223     @Override
224     protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
225         return super.addToStringAttributes(toStringHelper).omitNullValues()
226             .add("comment", comment == null ? null : comment.getJavadoc())
227             .add("constants", constants)
228             .add("enumerations", enumDefinitions)
229             .add("methods", methodDefinitions)
230             .add("annotations", annotationBuilders)
231             .add("implements", implementsTypes);
232     }
233 }