2 * Copyright (c) 2017 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.binding.javav2.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.binding.javav2.generator.util.AbstractBaseType;
17 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
18 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
19 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
20 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.AnnotationTypeBuilder;
21 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
22 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
23 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
24 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
25 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilderBase;
26 import org.opendaylight.mdsal.binding.javav2.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;
43 private Type parentTypeForBuilder;
45 protected AbstractGeneratedTypeBuilder(final String packageName, final String name) {
46 super(packageName, name);
49 protected AbstractGeneratedTypeBuilder(final String packageName, final String name, final boolean isNormalized) {
50 super(packageName, name, true);
53 protected AbstractGeneratedTypeBuilder(final String packageName, final String name,
54 final boolean isPkNameNormalized,
55 final boolean isTypeNormalized) {
56 super(packageName, name, isPkNameNormalized, isTypeNormalized);
59 protected String getComment() {
63 protected List<AnnotationTypeBuilder> getAnnotations() {
64 return annotationBuilders;
68 public boolean isAbstract() {
73 public List<Type> getImplementsTypes() {
74 return implementsTypes;
77 protected List<EnumBuilder> getEnumerations() {
78 return enumDefinitions;
81 protected List<Constant> getConstants() {
86 public List<MethodSignatureBuilder> getMethodDefinitions() {
87 return methodDefinitions;
90 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
94 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
95 return enclosedTransferObjects;
98 protected abstract T thisInstance();
101 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
102 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
103 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
105 Preconditions.checkArgument(!enclosedTransferObjects.contains(builder), "This generated type already contains equal enclosing transfer object.");
106 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder);
111 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
112 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
113 Preconditions.checkArgument(!enclosedTransferObjects.contains(genTOBuilder), "This generated type already contains equal enclosing transfer object.");
114 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder);
115 return thisInstance();
119 public T addComment(final String comment) {
120 this.comment = comment;
121 return thisInstance();
125 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
126 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
127 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
129 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
131 Preconditions.checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
132 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
137 public T setAbstract(final boolean isAbstract) {
138 this.isAbstract = isAbstract;
139 return thisInstance();
143 public T addImplementsType(final Type genType) {
144 Preconditions.checkArgument(genType != null, "Type cannot be null");
145 Preconditions.checkArgument(!implementsTypes.contains(genType), "This generated type already contains equal implements type.");
146 implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType);
147 return thisInstance();
151 public Constant addConstant(final Type type, final String name, final Object value) {
152 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
153 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
154 Preconditions.checkArgument(!containsConstant(name), "This generated type already contains constant with the same name.");
156 final Constant constant = new ConstantImpl(this, type, name, value);
157 constants = LazyCollections.lazyAdd(constants, constant);
162 public Type setParentTypeForBuilder(Type type) {
163 return this.parentTypeForBuilder = type;
166 public boolean containsConstant(final String name) {
167 Preconditions.checkArgument(name != null, "Parameter name can't be null");
168 for (Constant constant : constants) {
169 if (name.equals(constant.getName())) {
177 public EnumBuilder addEnumeration(final String name) {
178 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
179 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name, true, false);
181 Preconditions.checkArgument(!enumDefinitions.contains(builder), "This generated type already contains equal enumeration.");
182 enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder);
187 public MethodSignatureBuilder addMethod(final String name) {
188 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
189 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
190 builder.setAccessModifier(AccessModifier.PUBLIC);
191 builder.setAbstract(true);
192 methodDefinitions = LazyCollections.lazyAdd(methodDefinitions, builder);
197 public boolean containsMethod(final String name) {
198 Preconditions.checkArgument(name != null, "Parameter name can't be null");
199 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
200 if (name.equals(methodDefinition.getName())) {
208 public GeneratedPropertyBuilder addProperty(final String name) {
209 Preconditions.checkArgument(name != null, "Parameter name can't be null");
210 Preconditions.checkArgument(!containsProperty(name), "This generated type already contains property with the same name.");
212 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
213 builder.setAccessModifier(AccessModifier.PUBLIC);
214 properties = LazyCollections.lazyAdd(properties, builder);
219 public boolean containsProperty(final String name) {
220 Preconditions.checkArgument(name != null, "Parameter name can't be null");
221 for (GeneratedPropertyBuilder property : properties) {
222 if (name.equals(property.getName())) {
230 public int hashCode() {
231 return Objects.hash(getName(), getPackageName());
235 public boolean equals(final Object obj) {
242 if (getClass() != obj.getClass()) {
246 if (!(obj instanceof AbstractGeneratedTypeBuilder)) {
250 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
251 return Objects.equals(getFullyQualifiedName(), other.getFullyQualifiedName());
254 public Type getParent() {
258 public Type getParentTypeForBuilder() {
259 return parentTypeForBuilder;
263 public List<GeneratedPropertyBuilder> getProperties() {