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 com.google.common.base.Preconditions;
12 import java.util.Collections;
13 import java.util.List;
15 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
16 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
17 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
18 import org.opendaylight.yangtools.sal.binding.model.api.Type;
19 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
22 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
23 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
24 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
25 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
26 import org.opendaylight.yangtools.util.LazyCollections;
28 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType implements GeneratedTypeBuilderBase<T> {
30 private List<AnnotationTypeBuilder> annotationBuilders = Collections.emptyList();
31 private List<Type> implementsTypes = Collections.emptyList();
32 private List<EnumBuilder> enumDefinitions = Collections.emptyList();
33 private List<Constant> constants = Collections.emptyList();
34 private List<MethodSignatureBuilder> methodDefinitions = Collections.emptyList();
35 private final List<GeneratedTypeBuilder> enclosedTypes = Collections.emptyList();
36 private List<GeneratedTOBuilder> enclosedTransferObjects = Collections.emptyList();
37 private List<GeneratedPropertyBuilder> properties = Collections.emptyList();
38 private String comment = "";
39 private boolean isAbstract;
41 protected AbstractGeneratedTypeBuilder(final String packageName, final String name) {
42 super(packageName, name);
45 protected String getComment() {
49 protected List<AnnotationTypeBuilder> getAnnotations() {
50 return annotationBuilders;
54 public boolean isAbstract() {
59 public List<Type> getImplementsTypes() {
60 return implementsTypes;
63 protected List<EnumBuilder> getEnumerations() {
64 return enumDefinitions;
67 protected List<Constant> getConstants() {
72 public List<MethodSignatureBuilder> getMethodDefinitions() {
73 return methodDefinitions;
76 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
80 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
81 return enclosedTransferObjects;
84 protected abstract T thisInstance();
87 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
88 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
89 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
91 Preconditions.checkArgument(!enclosedTransferObjects.contains(builder), "This generated type already contains equal enclosing transfer object.");
92 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder);
97 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
98 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
99 Preconditions.checkArgument(!enclosedTransferObjects.contains(genTOBuilder), "This generated type already contains equal enclosing transfer object.");
100 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder);
101 return thisInstance();
105 public T addComment(final String comment) {
106 this.comment = comment;
107 return thisInstance();
111 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
112 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
113 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
115 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
117 Preconditions.checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
118 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
123 public T setAbstract(final boolean isAbstract) {
124 this.isAbstract = isAbstract;
125 return thisInstance();
129 public T addImplementsType(final Type genType) {
130 Preconditions.checkArgument(genType != null, "Type cannot be null");
131 Preconditions.checkArgument(!implementsTypes.contains(genType), "This generated type already contains equal implements type.");
132 implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType);
133 return thisInstance();
137 public Constant addConstant(final Type type, final String name, final Object value) {
138 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
139 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
140 Preconditions.checkArgument(!containsConstant(name), "This generated type already contains constant with the same name.");
142 final Constant constant = new ConstantImpl(this, type, name, value);
143 constants = LazyCollections.lazyAdd(constants, constant);
147 public boolean containsConstant(final String name) {
148 Preconditions.checkArgument(name != null, "Parameter name can't be null");
149 for (Constant constant : constants) {
150 if (name.equals(constant.getName())) {
158 public EnumBuilder addEnumeration(final String name) {
159 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
160 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
162 Preconditions.checkArgument(!enumDefinitions.contains(builder), "This generated type already contains equal enumeration.");
163 enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder);
168 public MethodSignatureBuilder addMethod(final String name) {
169 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
170 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
171 builder.setAccessModifier(AccessModifier.PUBLIC);
172 builder.setAbstract(true);
173 methodDefinitions = LazyCollections.lazyAdd(methodDefinitions, builder);
178 public boolean containsMethod(final String name) {
179 Preconditions.checkArgument(name != null, "Parameter name can't be null");
180 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
181 if (name.equals(methodDefinition.getName())) {
189 public GeneratedPropertyBuilder addProperty(final String name) {
190 Preconditions.checkArgument(name != null, "Parameter name can't be null");
191 Preconditions.checkArgument(!containsProperty(name), "This generated type already contains property with the same name.");
193 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
194 builder.setAccessModifier(AccessModifier.PUBLIC);
195 properties = LazyCollections.lazyAdd(properties, builder);
200 public boolean containsProperty(final String name) {
201 Preconditions.checkArgument(name != null, "Parameter name can't be null");
202 for (GeneratedPropertyBuilder property : properties) {
203 if (name.equals(property.getName())) {
211 public int hashCode() {
212 final int prime = 31;
214 result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
215 result = prime * result + ((getPackageName() == null) ? 0 : getPackageName().hashCode());
220 public boolean equals(final Object obj) {
227 if (getClass() != obj.getClass()) {
230 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
231 if (getName() == null) {
232 if (other.getName() != null) {
235 } else if (!getName().equals(other.getName())) {
238 if (getPackageName() == null) {
239 if (other.getPackageName() != null) {
242 } else if (!getPackageName().equals(other.getPackageName())) {
248 public Type getParent() {
253 public List<GeneratedPropertyBuilder> getProperties() {