2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.model.util.generated.type.builder;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static java.util.Objects.requireNonNull;
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;
32 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractType
33 implements GeneratedTypeBuilderBase<T> {
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;
46 protected AbstractGeneratedTypeBuilder(final JavaTypeName identifier) {
50 protected TypeComment getComment() {
54 protected List<AnnotationTypeBuilder> getAnnotations() {
55 return this.annotationBuilders;
59 public boolean isAbstract() {
60 return this.isAbstract;
64 public List<Type> getImplementsTypes() {
65 return this.implementsTypes;
68 protected List<Enumeration> getEnumerations() {
69 return this.enumDefinitions;
72 protected List<Constant> getConstants() {
73 return this.constants;
77 public List<MethodSignatureBuilder> getMethodDefinitions() {
78 return this.methodDefinitions;
81 protected List<GeneratedTransferObject> getEnclosedTransferObjects() {
82 return this.enclosedTransferObjects;
85 protected abstract T thisInstance();
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();
97 public T addComment(final TypeComment newComment) {
98 this.comment = requireNonNull(newComment);
99 return thisInstance();
103 public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
104 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
106 checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
107 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
112 public T setAbstract(final boolean newIsAbstract) {
113 this.isAbstract = newIsAbstract;
114 return thisInstance();
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();
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);
133 final Constant constant = new ConstantImpl(type, name, value);
134 this.constants = LazyCollections.lazyAdd(this.constants, constant);
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())) {
149 public void addEnumeration(final Enumeration enumeration) {
150 checkArgument(enumeration != null, "Enumeration cannot be null!");
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);
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);
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())) {
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.");
187 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
188 builder.setAccessModifier(AccessModifier.PUBLIC);
189 this.properties = LazyCollections.lazyAdd(this.properties, builder);
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())) {
204 public Type getParent() {
209 public List<GeneratedPropertyBuilder> getProperties() {
210 return this.properties;
214 public Optional<YangSourceDefinition> getYangSourceDefinition() {
215 return Optional.ofNullable(yangSourceDefinition);
219 public void setYangSourceDefinition(final YangSourceDefinition definition) {
220 yangSourceDefinition = requireNonNull(definition);
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);