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();
91 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
92 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
93 final GeneratedTOBuilder builder = new CodegenGeneratedTOBuilder(getFullyQualifiedName(), name);
95 Preconditions.checkArgument(!this.enclosedTransferObjects.contains(builder),
96 "This generated type already contains equal enclosing transfer object.");
97 this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, builder);
102 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
103 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
104 Preconditions.checkArgument(!this.enclosedTransferObjects.contains(genTOBuilder),
105 "This generated type already contains equal enclosing transfer object.");
106 this.enclosedTransferObjects = LazyCollections.lazyAdd(this.enclosedTransferObjects, genTOBuilder);
107 return thisInstance();
111 public T addComment(final TypeComment comment) {
112 this.comment = Preconditions.checkNotNull(comment);
113 return thisInstance();
117 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
118 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
119 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
121 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
123 Preconditions.checkArgument(!this.annotationBuilders.contains(builder),
124 "This generated type already contains equal annotation.");
125 this.annotationBuilders = LazyCollections.lazyAdd(this.annotationBuilders, builder);
130 public T setAbstract(final boolean isAbstract) {
131 this.isAbstract = isAbstract;
132 return thisInstance();
136 public T addImplementsType(final Type genType) {
137 Preconditions.checkArgument(genType != null, "Type cannot be null");
138 Preconditions.checkArgument(!this.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 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
147 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
148 Preconditions.checkArgument(!containsConstant(name),
149 "This generated type already contains constant with the same name.");
151 final Constant constant = new ConstantImpl(this, type, name, value);
152 this.constants = LazyCollections.lazyAdd(this.constants, constant);
156 public boolean containsConstant(final String name) {
157 Preconditions.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 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
169 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
171 Preconditions.checkArgument(!this.enumDefinitions.contains(builder),
172 "This generated type already contains equal enumeration.");
173 this.enumDefinitions = LazyCollections.lazyAdd(this.enumDefinitions, builder);
178 public MethodSignatureBuilder addMethod(final String name) {
179 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
180 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
181 builder.setAccessModifier(AccessModifier.PUBLIC);
182 builder.setAbstract(true);
183 this.methodDefinitions = LazyCollections.lazyAdd(this.methodDefinitions, builder);
188 public boolean containsMethod(final String name) {
189 Preconditions.checkArgument(name != null, "Parameter name can't be null");
190 for (final MethodSignatureBuilder methodDefinition : this.methodDefinitions) {
191 if (name.equals(methodDefinition.getName())) {
199 public GeneratedPropertyBuilder addProperty(final String name) {
200 Preconditions.checkArgument(name != null, "Parameter name can't be null");
201 Preconditions.checkArgument(!containsProperty(name),
202 "This generated type already contains property with the same name.");
204 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
205 builder.setAccessModifier(AccessModifier.PUBLIC);
206 this.properties = LazyCollections.lazyAdd(this.properties, builder);
211 public boolean containsProperty(final String name) {
212 Preconditions.checkArgument(name != null, "Parameter name can't be null");
213 for (final GeneratedPropertyBuilder property : this.properties) {
214 if (name.equals(property.getName())) {
221 public Type getParent() {
226 public List<GeneratedPropertyBuilder> getProperties() {
227 return this.properties;
231 public Optional<YangSourceDefinition> getYangSourceDefinition() {
232 return Optional.ofNullable(yangSourceDefinition);
237 public void setYangSourceDefinition(final YangSourceDefinition definition) {
238 yangSourceDefinition = Preconditions.checkNotNull(definition);
242 public int hashCode() {
243 final int prime = 31;
245 result = prime * result + Objects.hashCode(getName());
246 result = prime * result + Objects.hashCode(getPackageName());
251 public boolean equals(final Object obj) {
258 if (getClass() != obj.getClass()) {
261 final AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
262 return Objects.equals(getName(), other.getName()) && Objects.equals(getPackageName(), other.getPackageName());