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