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.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;
32 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType
33 implements GeneratedTypeBuilderBase<T> {
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;
47 protected AbstractGeneratedTypeBuilder(final JavaTypeName identifier) {
51 protected TypeComment getComment() {
55 protected List<AnnotationTypeBuilder> getAnnotations() {
56 return this.annotationBuilders;
60 public boolean isAbstract() {
61 return this.isAbstract;
65 public List<Type> getImplementsTypes() {
66 return this.implementsTypes;
69 protected List<EnumBuilder> getEnumerations() {
70 return this.enumDefinitions;
73 protected List<Constant> getConstants() {
74 return this.constants;
78 public List<MethodSignatureBuilder> getMethodDefinitions() {
79 return this.methodDefinitions;
82 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
83 return this.enclosedTypes;
86 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
87 return this.enclosedTransferObjects;
90 protected abstract T thisInstance();
92 abstract AbstractEnumerationBuilder newEnumerationBuilder(JavaTypeName identifier);
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));
99 checkArgument(!enclosedTransferObjects.contains(builder),
100 "This generated type already contains equal enclosing transfer object.");
101 this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, builder);
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();
115 public T addComment(final TypeComment newComment) {
116 this.comment = requireNonNull(newComment);
117 return thisInstance();
121 public AnnotationTypeBuilder addAnnotation(final JavaTypeName identifier) {
122 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(identifier);
124 checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
125 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
130 public T setAbstract(final boolean newIsAbstract) {
131 this.isAbstract = newIsAbstract;
132 return thisInstance();
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();
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);
151 final Constant constant = new ConstantImpl(type, name, value);
152 this.constants = LazyCollections.lazyAdd(this.constants, constant);
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())) {
167 public EnumBuilder addEnumeration(final String name) {
168 checkArgument(name != null, "Name of enumeration cannot be null!");
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);
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);
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())) {
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.");
207 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
208 builder.setAccessModifier(AccessModifier.PUBLIC);
209 this.properties = LazyCollections.lazyAdd(this.properties, builder);
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())) {
224 public Type getParent() {
229 public List<GeneratedPropertyBuilder> getProperties() {
230 return this.properties;
234 public Optional<YangSourceDefinition> getYangSourceDefinition() {
235 return Optional.ofNullable(yangSourceDefinition);
240 public void setYangSourceDefinition(final YangSourceDefinition definition) {
241 yangSourceDefinition = requireNonNull(definition);