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.mdsal.binding.javav2.spec.structural.Augmentable;
28 import org.opendaylight.mdsal.binding.javav2.spec.structural.Augmentation;
29 import org.opendaylight.yangtools.util.LazyCollections;
32 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType
33 implements GeneratedTypeBuilderBase<T> {
35 private List<AnnotationTypeBuilder> annotationBuilders = ImmutableList.of();
36 private List<Type> implementsTypes = ImmutableList.of();
37 private List<EnumBuilder> enumDefinitions = ImmutableList.of();
38 private List<Constant> constants = ImmutableList.of();
39 private List<MethodSignatureBuilder> methodDefinitions = ImmutableList.of();
40 private final List<GeneratedTypeBuilder> enclosedTypes = ImmutableList.of();
41 private List<GeneratedTOBuilder> enclosedTransferObjects = ImmutableList.of();
42 private List<GeneratedPropertyBuilder> properties = ImmutableList.of();
43 private String comment = "";
44 private boolean isAbstract;
45 private Type parentTypeForBuilder;
46 private boolean isDataObjectType = false;
47 private String basePackageName = null;
49 protected AbstractGeneratedTypeBuilder(final String packageName, final String name) {
50 super(packageName, name);
53 protected AbstractGeneratedTypeBuilder(final String packageName, final String name, final boolean isNormalized) {
54 super(packageName, name, true);
57 protected AbstractGeneratedTypeBuilder(final String packageName, final String name,
58 final boolean isPkNameNormalized,
59 final boolean isTypeNormalized) {
60 super(packageName, name, isPkNameNormalized, isTypeNormalized);
63 protected String getComment() {
67 protected List<AnnotationTypeBuilder> getAnnotations() {
68 return annotationBuilders;
72 public boolean isAbstract() {
77 public List<Type> getImplementsTypes() {
78 return implementsTypes;
81 protected List<EnumBuilder> getEnumerations() {
82 return enumDefinitions;
85 protected List<Constant> getConstants() {
90 public List<MethodSignatureBuilder> getMethodDefinitions() {
91 return methodDefinitions;
94 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
98 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
99 return enclosedTransferObjects;
102 protected boolean isDataObjectType() {
103 return isDataObjectType;
106 protected String getBasePackageName() {
107 return basePackageName;
110 protected abstract T thisInstance();
113 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
114 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
115 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name);
117 Preconditions.checkArgument(!enclosedTransferObjects.contains(builder), "This generated type already contains equal enclosing transfer object.");
118 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder);
123 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
124 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
125 Preconditions.checkArgument(!enclosedTransferObjects.contains(genTOBuilder), "This generated type already contains equal enclosing transfer object.");
126 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder);
127 return thisInstance();
131 public T addComment(final String comment) {
132 this.comment = comment;
133 return thisInstance();
137 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
138 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
139 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
141 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
143 Preconditions.checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
144 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
149 public T setAbstract(final boolean isAbstract) {
150 this.isAbstract = isAbstract;
151 return thisInstance();
155 public void setBasePackageName(String basePackageName) {
156 this.basePackageName = basePackageName;
160 public T addImplementsType(final Type genType) {
161 Preconditions.checkArgument(genType != null, "Type cannot be null");
162 Preconditions.checkArgument(!implementsTypes.contains(genType), "This generated type already contains equal implements type.");
163 implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType);
165 if (genType.getFullyQualifiedName().equals(Augmentable.class.getName())
166 || genType.getFullyQualifiedName().equals(Augmentation.class.getName())) {
167 this.isDataObjectType = true;
169 return thisInstance();
173 public Constant addConstant(final Type type, final String name, final Object value) {
174 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
175 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
176 Preconditions.checkArgument(!containsConstant(name), "This generated type already contains constant with the same name.");
178 final Constant constant = new ConstantImpl(this, type, name, value);
179 constants = LazyCollections.lazyAdd(constants, constant);
184 public Type setParentTypeForBuilder(Type type) {
185 return this.parentTypeForBuilder = type;
188 public boolean containsConstant(final String name) {
189 Preconditions.checkArgument(name != null, "Parameter name can't be null");
190 for (Constant constant : constants) {
191 if (name.equals(constant.getName())) {
199 public EnumBuilder addEnumeration(final String name) {
200 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
201 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name, true, false);
203 Preconditions.checkArgument(!enumDefinitions.contains(builder), "This generated type already contains equal enumeration.");
204 enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder);
209 public MethodSignatureBuilder addMethod(final String name) {
210 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
211 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
212 builder.setAccessModifier(AccessModifier.PUBLIC);
213 builder.setAbstract(true);
214 methodDefinitions = LazyCollections.lazyAdd(methodDefinitions, builder);
219 public boolean containsMethod(final String name) {
220 Preconditions.checkArgument(name != null, "Parameter name can't be null");
221 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
222 if (name.equals(methodDefinition.getName())) {
230 public GeneratedPropertyBuilder addProperty(final String name) {
231 Preconditions.checkArgument(name != null, "Parameter name can't be null");
232 Preconditions.checkArgument(!containsProperty(name), "This generated type already contains property with the same name.");
234 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
235 builder.setAccessModifier(AccessModifier.PUBLIC);
236 properties = LazyCollections.lazyAdd(properties, builder);
241 public boolean containsProperty(final String name) {
242 Preconditions.checkArgument(name != null, "Parameter name can't be null");
243 for (GeneratedPropertyBuilder property : properties) {
244 if (name.equals(property.getName())) {
252 public int hashCode() {
253 return Objects.hash(getName(), getPackageName());
257 public boolean equals(final Object obj) {
264 if (getClass() != obj.getClass()) {
268 if (!(obj instanceof AbstractGeneratedTypeBuilder)) {
272 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
273 return Objects.equals(getFullyQualifiedName(), other.getFullyQualifiedName());
276 public Type getParent() {
280 public Type getParentTypeForBuilder() {
281 return parentTypeForBuilder;
285 public List<GeneratedPropertyBuilder> getProperties() {