2 * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.binding2.generator.util.generated.type.builder;
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ImmutableList;
14 import java.util.List;
15 import java.util.Objects;
16 import org.opendaylight.mdsal.binding2.generator.util.AbstractBaseType;
17 import org.opendaylight.mdsal.binding2.model.api.AccessModifier;
18 import org.opendaylight.mdsal.binding2.model.api.Constant;
19 import org.opendaylight.mdsal.binding2.model.api.Type;
20 import org.opendaylight.mdsal.binding2.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.mdsal.binding2.model.api.type.builder.EnumBuilder;
22 import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedPropertyBuilder;
23 import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTOBuilder;
24 import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilder;
25 import org.opendaylight.mdsal.binding2.model.api.type.builder.GeneratedTypeBuilderBase;
26 import org.opendaylight.mdsal.binding2.model.api.type.builder.MethodSignatureBuilder;
27 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 = ImmutableList.of();
34 private List<Type> implementsTypes = ImmutableList.of();
35 private List<EnumBuilder> enumDefinitions = ImmutableList.of();
36 private List<Constant> constants = ImmutableList.of();
37 private List<MethodSignatureBuilder> methodDefinitions = ImmutableList.of();
38 private final List<GeneratedTypeBuilder> enclosedTypes = ImmutableList.of();
39 private List<GeneratedTOBuilder> enclosedTransferObjects = ImmutableList.of();
40 private List<GeneratedPropertyBuilder> properties = ImmutableList.of();
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 return Objects.hash(getName(), getPackageName());
219 public boolean equals(final Object obj) {
226 if (getClass() != obj.getClass()) {
230 if (!(obj instanceof AbstractGeneratedTypeBuilder)) {
234 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
235 return Objects.equals(this, other);
238 public Type getParent() {
243 public List<GeneratedPropertyBuilder> getProperties() {