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 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;
31 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType
32 implements GeneratedTypeBuilderBase<T> {
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;
45 protected AbstractGeneratedTypeBuilder(final JavaTypeName identifier) {
49 protected TypeComment getComment() {
53 protected List<AnnotationTypeBuilder> getAnnotations() {
54 return this.annotationBuilders;
58 public boolean isAbstract() {
59 return this.isAbstract;
63 public List<Type> getImplementsTypes() {
64 return this.implementsTypes;
67 protected List<Enumeration> getEnumerations() {
68 return this.enumDefinitions;
71 protected List<Constant> getConstants() {
72 return this.constants;
76 public List<MethodSignatureBuilder> getMethodDefinitions() {
77 return this.methodDefinitions;
80 protected List<GeneratedTransferObject> getEnclosedTransferObjects() {
81 return this.enclosedTransferObjects;
84 protected abstract T thisInstance();
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();
96 public T addComment(final TypeComment newComment) {
97 this.comment = requireNonNull(newComment);
98 return thisInstance();
102 public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
103 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
105 checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
106 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
111 public T setAbstract(final boolean newIsAbstract) {
112 this.isAbstract = newIsAbstract;
113 return thisInstance();
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();
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);
132 final Constant constant = new ConstantImpl(type, name, value);
133 this.constants = LazyCollections.lazyAdd(this.constants, constant);
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())) {
148 public void addEnumeration(final Enumeration enumeration) {
149 checkArgument(enumeration != null, "Enumeration cannot be null!");
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);
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);
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())) {
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.");
186 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
187 builder.setAccessModifier(AccessModifier.PUBLIC);
188 this.properties = LazyCollections.lazyAdd(this.properties, builder);
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())) {
203 public Type getParent() {
208 public List<GeneratedPropertyBuilder> getProperties() {
209 return this.properties;
213 public Optional<YangSourceDefinition> getYangSourceDefinition() {
214 return Optional.ofNullable(yangSourceDefinition);
219 public void setYangSourceDefinition(final YangSourceDefinition definition) {
220 yangSourceDefinition = requireNonNull(definition);