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.yangtools.binding.generator.util.generated.type.builder;
10 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
11 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
12 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
13 import org.opendaylight.yangtools.sal.binding.model.api.Type;
14 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.*;
16 import java.util.ArrayList;
17 import java.util.List;
19 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType implements
20 GeneratedTypeBuilderBase<T> {
22 private String comment = "";
24 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<>();
25 private final List<Type> implementsTypes = new ArrayList<>();
26 private final List<EnumBuilder> enumDefinitions = new ArrayList<>();
27 private final List<Constant> constants = new ArrayList<>();
28 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<>();
29 private final List<GeneratedTypeBuilder> enclosedTypes = new ArrayList<>();
30 private final List<GeneratedTOBuilder> enclosedTransferObjects = new ArrayList<>();
31 private final List<GeneratedPropertyBuilder> properties = new ArrayList<>();
32 private boolean isAbstract;
34 public AbstractGeneratedTypeBuilder(final String packageName, final String name) {
35 super(packageName, name);
38 protected String getComment() {
42 protected List<AnnotationTypeBuilder> getAnnotations() {
43 return annotationBuilders;
46 protected boolean isAbstract() {
50 protected List<Type> getImplementsTypes() {
51 return implementsTypes;
54 protected List<EnumBuilder> getEnumerations() {
55 return enumDefinitions;
58 protected List<Constant> getConstants() {
62 protected List<MethodSignatureBuilder> getMethodDefinitions() {
63 return methodDefinitions;
66 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
70 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
71 return enclosedTransferObjects;
74 protected abstract T thisInstance();
77 public GeneratedTOBuilder addEnclosingTransferObject(String name) {
79 throw new IllegalArgumentException("Name for Enclosing Generated Transfer Object cannot be null!");
81 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
82 enclosedTransferObjects.add(builder);
87 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
88 if (genTOBuilder == null) {
89 throw new IllegalArgumentException("Parameter genTOBuilder cannot be null!");
91 enclosedTransferObjects.add(genTOBuilder);
92 return thisInstance();
96 public T addComment(String comment) {
97 this.comment = comment;
98 return thisInstance();
102 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
103 if (packageName == null) {
104 throw new IllegalArgumentException("Package Name for Annotation Type cannot be null!");
107 throw new IllegalArgumentException("Name of Annotation Type cannot be null!");
110 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
111 annotationBuilders.add(builder);
116 public T setAbstract(boolean isAbstract) {
117 this.isAbstract = isAbstract;
118 return thisInstance();
122 public T addImplementsType(Type genType) {
123 if (genType == null) {
124 throw new IllegalArgumentException("Type cannot be null");
126 implementsTypes.add(genType);
127 return thisInstance();
131 public Constant addConstant(Type type, String name, Object value) {
133 throw new IllegalArgumentException("Returning Type for Constant cannot be null!");
136 throw new IllegalArgumentException("Name of constant cannot be null!");
139 final Constant constant = new ConstantImpl(this, type, name, value);
140 constants.add(constant);
145 public EnumBuilder addEnumeration(String name) {
147 throw new IllegalArgumentException("Name of enumeration cannot be null!");
149 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
150 enumDefinitions.add(builder);
155 public MethodSignatureBuilder addMethod(String name) {
157 throw new IllegalArgumentException("Name of method cannot be null!");
159 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
160 builder.setAccessModifier(AccessModifier.PUBLIC);
161 builder.setAbstract(true);
162 methodDefinitions.add(builder);
167 public boolean containsMethod(String name) {
169 throw new IllegalArgumentException("Parameter name can't be null");
171 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
172 if (name.equals(methodDefinition.getName())) {
180 public GeneratedPropertyBuilder addProperty(String name) {
181 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
182 builder.setAccessModifier(AccessModifier.PUBLIC);
183 properties.add(builder);
188 public boolean containsProperty(String name) {
190 throw new IllegalArgumentException("Parameter name can't be null");
192 for (GeneratedPropertyBuilder property : properties) {
193 if (name.equals(property.getName())) {
201 public int hashCode() {
202 final int prime = 31;
204 result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
205 result = prime * result + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
210 public boolean equals(Object obj) {
217 if (getClass() != obj.getClass()) {
220 AbstractGeneratedTypeBuilder<T> other = (AbstractGeneratedTypeBuilder<T>) obj;
221 if (getName() == null) {
222 if (other.getName() != null) {
225 } else if (!getName().equals(other.getName())) {
228 if (getPackageName() == null) {
229 if (other.getPackageName() != null) {
232 } else if (!getPackageName().equals(other.getPackageName())) {
238 public Type getParent() {
242 protected List<GeneratedPropertyBuilder> getProperties() {