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 java.util.ArrayList;
11 import java.util.List;
13 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
14 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
15 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
16 import org.opendaylight.yangtools.sal.binding.model.api.Type;
17 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
18 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
19 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
22 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
23 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
25 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType implements
26 GeneratedTypeBuilderBase<T> {
28 private String comment = "";
30 private final List<AnnotationTypeBuilder> annotationBuilders = new ArrayList<>();
31 private final List<Type> implementsTypes = new ArrayList<>();
32 private final List<EnumBuilder> enumDefinitions = new ArrayList<>();
33 private final List<Constant> constants = new ArrayList<>();
34 private final List<MethodSignatureBuilder> methodDefinitions = new ArrayList<>();
35 private final List<GeneratedTypeBuilder> enclosedTypes = new ArrayList<>();
36 private final List<GeneratedTOBuilder> enclosedTransferObjects = new ArrayList<>();
37 private final List<GeneratedPropertyBuilder> properties = new ArrayList<>();
38 private boolean isAbstract;
40 public AbstractGeneratedTypeBuilder(final String packageName, final String name) {
41 super(packageName, name);
44 protected String getComment() {
48 protected List<AnnotationTypeBuilder> getAnnotations() {
49 return annotationBuilders;
52 public boolean isAbstract() {
56 public List<Type> getImplementsTypes() {
57 return implementsTypes;
60 protected List<EnumBuilder> getEnumerations() {
61 return enumDefinitions;
64 protected List<Constant> getConstants() {
68 public List<MethodSignatureBuilder> getMethodDefinitions() {
69 return methodDefinitions;
72 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
76 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
77 return enclosedTransferObjects;
80 protected abstract T thisInstance();
83 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
85 throw new IllegalArgumentException("Name for Enclosing Generated Transfer Object cannot be null!");
87 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
88 enclosedTransferObjects.add(builder);
93 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
94 if (genTOBuilder == null) {
95 throw new IllegalArgumentException("Parameter genTOBuilder cannot be null!");
97 enclosedTransferObjects.add(genTOBuilder);
98 return thisInstance();
102 public T addComment(final String comment) {
103 this.comment = comment;
104 return thisInstance();
108 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
109 if (packageName == null) {
110 throw new IllegalArgumentException("Package Name for Annotation Type cannot be null!");
113 throw new IllegalArgumentException("Name of Annotation Type cannot be null!");
116 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
117 annotationBuilders.add(builder);
122 public T setAbstract(final boolean isAbstract) {
123 this.isAbstract = isAbstract;
124 return thisInstance();
128 public T addImplementsType(final Type genType) {
129 if (genType == null) {
130 throw new IllegalArgumentException("Type cannot be null");
132 implementsTypes.add(genType);
133 return thisInstance();
137 public Constant addConstant(final Type type, final String name, final Object value) {
139 throw new IllegalArgumentException("Returning Type for Constant cannot be null!");
142 throw new IllegalArgumentException("Name of constant cannot be null!");
145 final Constant constant = new ConstantImpl(this, type, name, value);
146 constants.add(constant);
151 public EnumBuilder addEnumeration(final String name) {
153 throw new IllegalArgumentException("Name of enumeration cannot be null!");
155 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
156 enumDefinitions.add(builder);
161 public MethodSignatureBuilder addMethod(final String name) {
163 throw new IllegalArgumentException("Name of method cannot be null!");
165 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
166 builder.setAccessModifier(AccessModifier.PUBLIC);
167 builder.setAbstract(true);
168 methodDefinitions.add(builder);
173 public boolean containsMethod(final String name) {
175 throw new IllegalArgumentException("Parameter name can't be null");
177 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
178 if (name.equals(methodDefinition.getName())) {
186 public GeneratedPropertyBuilder addProperty(final String name) {
187 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
188 builder.setAccessModifier(AccessModifier.PUBLIC);
189 properties.add(builder);
194 public boolean containsProperty(final String name) {
196 throw new IllegalArgumentException("Parameter name can't be null");
198 for (GeneratedPropertyBuilder property : properties) {
199 if (name.equals(property.getName())) {
207 public int hashCode() {
208 final int prime = 31;
210 result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
211 result = prime * result + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
216 public boolean equals(final Object obj) {
223 if (getClass() != obj.getClass()) {
226 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
227 if (getName() == null) {
228 if (other.getName() != null) {
231 } else if (!getName().equals(other.getName())) {
234 if (getPackageName() == null) {
235 if (other.getPackageName() != null) {
238 } else if (!getPackageName().equals(other.getPackageName())) {
244 public Type getParent() {
248 public List<GeneratedPropertyBuilder> getProperties() {