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 com.google.common.base.Preconditions;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Objects;
14 import java.util.Optional;
15 import org.opendaylight.mdsal.binding.model.api.AccessModifier;
16 import org.opendaylight.mdsal.binding.model.api.Constant;
17 import org.opendaylight.mdsal.binding.model.api.Type;
18 import org.opendaylight.mdsal.binding.model.api.TypeComment;
19 import org.opendaylight.mdsal.binding.model.api.YangSourceDefinition;
20 import org.opendaylight.mdsal.binding.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.mdsal.binding.model.api.type.builder.EnumBuilder;
22 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedPropertyBuilder;
23 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTOBuilder;
24 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilder;
25 import org.opendaylight.mdsal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
26 import org.opendaylight.mdsal.binding.model.api.type.builder.MethodSignatureBuilder;
27 import org.opendaylight.mdsal.binding.model.util.AbstractBaseType;
28 import org.opendaylight.yangtools.util.LazyCollections;
30 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType
31 implements GeneratedTypeBuilderBase<T> {
33 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
34 private List<Type> implementsTypes = Collections.emptyList();
35 private List<EnumBuilder> enumDefinitions = Collections.emptyList();
36 private List<Constant> constants = Collections.emptyList();
37 private List<MethodSignatureBuilder> methodDefinitions = Collections.emptyList();
38 private final List<GeneratedTypeBuilder> enclosedTypes = Collections.emptyList();
39 private List<GeneratedTOBuilder> 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 String packageName, final String name) {
46 super(packageName, name);
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<EnumBuilder> 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<GeneratedTypeBuilder> getEnclosedTypes() {
81 return this.enclosedTypes;
84 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
85 return this.enclosedTransferObjects;
88 protected abstract T thisInstance();
90 abstract AbstractEnumerationBuilder newEnumerationBuilder(String packageName, String name);
93 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
94 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
95 final GeneratedTOBuilder builder = new CodegenGeneratedTOBuilder(getFullyQualifiedName(), name);
97 Preconditions.checkArgument(!this.enclosedTransferObjects.contains(builder),
98 "This generated type already contains equal enclosing transfer object.");
99 this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, builder);
104 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
105 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
106 Preconditions.checkArgument(!this.enclosedTransferObjects.contains(genTOBuilder),
107 "This generated type already contains equal enclosing transfer object.");
108 this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, genTOBuilder);
109 return thisInstance();
113 public T addComment(final TypeComment comment) {
114 this.comment = Preconditions.checkNotNull(comment);
115 return thisInstance();
119 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
120 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
121 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
123 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
125 Preconditions.checkArgument(!this.annotationBuilders.contains(builder),
126 "This generated type already contains equal annotation.");
127 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
132 public T setAbstract(final boolean isAbstract) {
133 this.isAbstract = isAbstract;
134 return thisInstance();
138 public T addImplementsType(final Type genType) {
139 Preconditions.checkArgument(genType != null, "Type cannot be null");
140 Preconditions.checkArgument(!this.implementsTypes.contains(genType),
141 "This generated type already contains equal implements type.");
142 this.implementsTypes = LazyCollections.lazyAdd(this.implementsTypes, genType);
143 return thisInstance();
147 public Constant addConstant(final Type type, final String name, final Object value) {
148 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
149 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
150 Preconditions.checkArgument(!containsConstant(name),
151 "This generated type already contains constant with the same name.");
153 final Constant constant = new ConstantImpl(this, type, name, value);
154 this.constants = LazyCollections.lazyAdd(this.constants, constant);
158 public boolean containsConstant(final String name) {
159 Preconditions.checkArgument(name != null, "Parameter name can't be null");
160 for (final Constant constant : this.constants) {
161 if (name.equals(constant.getName())) {
169 public EnumBuilder addEnumeration(final String name) {
170 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
171 final EnumBuilder builder = newEnumerationBuilder(getFullyQualifiedName(), name);
173 Preconditions.checkArgument(!this.enumDefinitions.contains(builder),
174 "This generated type already contains equal enumeration.");
175 this.enumDefinitions = LazyCollections.lazyAdd(this.enumDefinitions, builder);
180 public MethodSignatureBuilder addMethod(final String name) {
181 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
182 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
183 builder.setAccessModifier(AccessModifier.PUBLIC);
184 builder.setAbstract(true);
185 this.methodDefinitions = LazyCollections.lazyAdd(this.methodDefinitions, builder);
190 public boolean containsMethod(final String name) {
191 Preconditions.checkArgument(name != null, "Parameter name can't be null");
192 for (final MethodSignatureBuilder methodDefinition : this.methodDefinitions) {
193 if (name.equals(methodDefinition.getName())) {
201 public GeneratedPropertyBuilder addProperty(final String name) {
202 Preconditions.checkArgument(name != null, "Parameter name can't be null");
203 Preconditions.checkArgument(!containsProperty(name),
204 "This generated type already contains property with the same name.");
206 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
207 builder.setAccessModifier(AccessModifier.PUBLIC);
208 this.properties = LazyCollections.lazyAdd(this.properties, builder);
213 public boolean containsProperty(final String name) {
214 Preconditions.checkArgument(name != null, "Parameter name can't be null");
215 for (final GeneratedPropertyBuilder property : this.properties) {
216 if (name.equals(property.getName())) {
223 public Type getParent() {
228 public List<GeneratedPropertyBuilder> getProperties() {
229 return this.properties;
233 public Optional<YangSourceDefinition> getYangSourceDefinition() {
234 return Optional.ofNullable(yangSourceDefinition);
239 public void setYangSourceDefinition(final YangSourceDefinition definition) {
240 yangSourceDefinition = Preconditions.checkNotNull(definition);
244 public int hashCode() {
245 final int prime = 31;
247 result = prime * result + Objects.hashCode(getName());
248 result = prime * result + Objects.hashCode(getPackageName());
253 public boolean equals(final Object obj) {
260 if (getClass() != obj.getClass()) {
263 final AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
264 return Objects.equals(getName(), other.getName()) && Objects.equals(getPackageName(), other.getPackageName());