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;
11 import java.util.Collections;
12 import java.util.List;
13 import java.util.Objects;
14 import org.opendaylight.yangtools.binding.generator.util.AbstractBaseType;
15 import org.opendaylight.yangtools.sal.binding.model.api.AccessModifier;
16 import org.opendaylight.yangtools.sal.binding.model.api.Constant;
17 import org.opendaylight.yangtools.sal.binding.model.api.Type;
18 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.AnnotationTypeBuilder;
19 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.EnumBuilder;
20 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedPropertyBuilder;
21 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTOBuilder;
22 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilder;
23 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.GeneratedTypeBuilderBase;
24 import org.opendaylight.yangtools.sal.binding.model.api.type.builder.MethodSignatureBuilder;
25 import org.opendaylight.yangtools.util.LazyCollections;
28 * @deprecated Use {org.opendaylight.mdsal.binding.generator.util.generated.type.builder.AbstractGeneratedTypeBuilder} instead.
31 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType 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 String comment = "";
42 private boolean isAbstract;
44 protected AbstractGeneratedTypeBuilder(final String packageName, final String name) {
45 super(packageName, name);
48 protected String getComment() {
52 protected List<AnnotationTypeBuilder> getAnnotations() {
53 return annotationBuilders;
57 public boolean isAbstract() {
62 public List<Type> getImplementsTypes() {
63 return implementsTypes;
66 protected List<EnumBuilder> getEnumerations() {
67 return enumDefinitions;
70 protected List<Constant> getConstants() {
75 public List<MethodSignatureBuilder> getMethodDefinitions() {
76 return methodDefinitions;
79 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
83 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
84 return enclosedTransferObjects;
87 protected abstract T thisInstance();
90 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
91 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
92 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
94 Preconditions.checkArgument(!enclosedTransferObjects.contains(builder), "This generated type already contains equal enclosing transfer object.");
95 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder);
100 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
101 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
102 Preconditions.checkArgument(!enclosedTransferObjects.contains(genTOBuilder), "This generated type already contains equal enclosing transfer object.");
103 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder);
104 return thisInstance();
108 public T addComment(final String comment) {
109 this.comment = comment;
110 return thisInstance();
114 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
115 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
116 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
118 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
120 Preconditions.checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
121 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
126 public T setAbstract(final boolean isAbstract) {
127 this.isAbstract = isAbstract;
128 return thisInstance();
132 public T addImplementsType(final Type genType) {
133 Preconditions.checkArgument(genType != null, "Type cannot be null");
134 Preconditions.checkArgument(!implementsTypes.contains(genType), "This generated type already contains equal implements type.");
135 implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType);
136 return thisInstance();
140 public Constant addConstant(final Type type, final String name, final Object value) {
141 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
142 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
143 Preconditions.checkArgument(!containsConstant(name), "This generated type already contains constant with the same name.");
145 final Constant constant = new ConstantImpl(this, type, name, value);
146 constants = LazyCollections.lazyAdd(constants, constant);
150 public boolean containsConstant(final String name) {
151 Preconditions.checkArgument(name != null, "Parameter name can't be null");
152 for (Constant constant : constants) {
153 if (name.equals(constant.getName())) {
161 public EnumBuilder addEnumeration(final String name) {
162 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
163 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name);
165 Preconditions.checkArgument(!enumDefinitions.contains(builder), "This generated type already contains equal enumeration.");
166 enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder);
171 public MethodSignatureBuilder addMethod(final String name) {
172 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
173 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
174 builder.setAccessModifier(AccessModifier.PUBLIC);
175 builder.setAbstract(true);
176 methodDefinitions = LazyCollections.lazyAdd(methodDefinitions, builder);
181 public boolean containsMethod(final String name) {
182 Preconditions.checkArgument(name != null, "Parameter name can't be null");
183 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
184 if (name.equals(methodDefinition.getName())) {
192 public GeneratedPropertyBuilder addProperty(final String name) {
193 Preconditions.checkArgument(name != null, "Parameter name can't be null");
194 Preconditions.checkArgument(!containsProperty(name), "This generated type already contains property with the same name.");
196 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
197 builder.setAccessModifier(AccessModifier.PUBLIC);
198 properties = LazyCollections.lazyAdd(properties, builder);
203 public boolean containsProperty(final String name) {
204 Preconditions.checkArgument(name != null, "Parameter name can't be null");
205 for (GeneratedPropertyBuilder property : properties) {
206 if (name.equals(property.getName())) {
214 public int hashCode() {
215 final int prime = 31;
217 result = prime * result + Objects.hashCode(getName());
218 result = prime * result + Objects.hashCode(getPackageName());
223 public boolean equals(final Object obj) {
230 if (getClass() != obj.getClass()) {
233 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
234 return Objects.equals(getName(), other.getName()) && Objects.equals(getPackageName(), other.getPackageName());
237 public Type getParent() {
242 public List<GeneratedPropertyBuilder> getProperties() {