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 java.util.Optional;
17 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
18 import org.opendaylight.mdsal.binding.javav2.generator.util.AbstractBaseType;
19 import org.opendaylight.mdsal.binding.javav2.model.api.AccessModifier;
20 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
21 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
22 import org.opendaylight.mdsal.binding.javav2.model.api.TypeComment;
23 import org.opendaylight.mdsal.binding.javav2.model.api.YangSourceDefinition;
24 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.AnnotationTypeBuilder;
25 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
26 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
27 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
28 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
29 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilderBase;
30 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
31 import org.opendaylight.yangtools.util.LazyCollections;
34 abstract class AbstractGeneratedTypeBuilder<T extends GeneratedTypeBuilderBase<T>> extends AbstractBaseType
35 implements GeneratedTypeBuilderBase<T> {
37 private List<AnnotationTypeBuilder> annotationBuilders = ImmutableList.of();
38 private List<Type> implementsTypes = ImmutableList.of();
39 private List<EnumBuilder> enumDefinitions = ImmutableList.of();
40 private List<Constant> constants = ImmutableList.of();
41 private List<MethodSignatureBuilder> methodDefinitions = ImmutableList.of();
42 private final List<GeneratedTypeBuilder> enclosedTypes = ImmutableList.of();
43 private List<GeneratedTOBuilder> enclosedTransferObjects = ImmutableList.of();
44 private List<GeneratedPropertyBuilder> properties = ImmutableList.of();
45 private TypeComment comment;
46 private boolean isAbstract;
47 private Type parentTypeForBuilder;
48 private YangSourceDefinition yangSourceDefinition;
50 protected AbstractGeneratedTypeBuilder(final String packageName, final String name, ModuleContext context) {
51 super(packageName, name, context);
54 protected AbstractGeneratedTypeBuilder(final String packageName, final String name, final boolean isNormalized,
55 ModuleContext context) {
56 super(packageName, name, true, null);
59 protected AbstractGeneratedTypeBuilder(final String packageName, final String name, final boolean isPkNameNormalized,
60 final boolean isTypeNormalized, ModuleContext context) {
61 super(packageName, name, isPkNameNormalized, isTypeNormalized, context);
64 protected TypeComment getComment() {
68 protected List<AnnotationTypeBuilder> getAnnotations() {
69 return annotationBuilders;
73 public boolean isAbstract() {
78 public List<Type> getImplementsTypes() {
79 return implementsTypes;
82 protected List<EnumBuilder> getEnumerations() {
83 return enumDefinitions;
86 protected List<Constant> getConstants() {
91 public List<MethodSignatureBuilder> getMethodDefinitions() {
92 return methodDefinitions;
95 protected List<GeneratedTypeBuilder> getEnclosedTypes() {
99 protected List<GeneratedTOBuilder> getEnclosedTransferObjects() {
100 return enclosedTransferObjects;
103 protected abstract T thisInstance();
106 public GeneratedTOBuilder addEnclosingTransferObject(final String name) {
107 Preconditions.checkArgument(name != null, "Name for Enclosing Generated Transfer Object cannot be null!");
108 GeneratedTOBuilder builder = new GeneratedTOBuilderImpl(getFullyQualifiedName(), name, true);
110 Preconditions.checkArgument(!enclosedTransferObjects.contains(builder), "This generated type already contains equal enclosing transfer object.");
111 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, builder);
116 public T addEnclosingTransferObject(final GeneratedTOBuilder genTOBuilder) {
117 Preconditions.checkArgument(genTOBuilder != null, "Parameter genTOBuilder cannot be null!");
118 Preconditions.checkArgument(!enclosedTransferObjects.contains(genTOBuilder), "This generated type already contains equal enclosing transfer object.");
119 enclosedTransferObjects = LazyCollections.lazyAdd(enclosedTransferObjects, genTOBuilder);
120 return thisInstance();
124 public T addComment(final TypeComment comment) {
125 this.comment = Preconditions.checkNotNull(comment);
126 return thisInstance();
130 public AnnotationTypeBuilder addAnnotation(final String packageName, final String name) {
131 Preconditions.checkArgument(packageName != null, "Package Name for Annotation Type cannot be null!");
132 Preconditions.checkArgument(name != null, "Name of Annotation Type cannot be null!");
134 final AnnotationTypeBuilder builder = new AnnotationTypeBuilderImpl(packageName, name);
136 Preconditions.checkArgument(!annotationBuilders.contains(builder), "This generated type already contains equal annotation.");
137 annotationBuilders = LazyCollections.lazyAdd(annotationBuilders, builder);
142 public T setAbstract(final boolean isAbstract) {
143 this.isAbstract = isAbstract;
144 return thisInstance();
148 public T addImplementsType(final Type genType) {
149 Preconditions.checkArgument(genType != null, "Type cannot be null");
150 Preconditions.checkArgument(!implementsTypes.contains(genType), "This generated type already contains equal implements type.");
151 implementsTypes = LazyCollections.lazyAdd(implementsTypes, genType);
152 return thisInstance();
156 public Constant addConstant(final Type type, final String name, final Object value) {
157 Preconditions.checkArgument(type != null, "Returning Type for Constant cannot be null!");
158 Preconditions.checkArgument(name != null, "Name of constant cannot be null!");
159 Preconditions.checkArgument(!containsConstant(name), "This generated type already contains constant with the same name.");
161 final Constant constant = new ConstantImpl(this, type, name, value);
162 constants = LazyCollections.lazyAdd(constants, constant);
169 public Type setParentTypeForBuilder(Type type) {
170 return this.parentTypeForBuilder = type;
173 public boolean containsConstant(final String name) {
174 Preconditions.checkArgument(name != null, "Parameter name can't be null");
175 for (Constant constant : constants) {
176 if (name.equals(constant.getName())) {
184 public EnumBuilder addEnumeration(final String name, ModuleContext context) {
185 Preconditions.checkArgument(name != null, "Name of enumeration cannot be null!");
186 final EnumBuilder builder = new EnumerationBuilderImpl(getFullyQualifiedName(), name, true, false,
189 Preconditions.checkArgument(!enumDefinitions.contains(builder), "This generated type already contains equal enumeration.");
190 enumDefinitions = LazyCollections.lazyAdd(enumDefinitions, builder);
195 public MethodSignatureBuilder addMethod(final String name) {
196 Preconditions.checkArgument(name != null, "Name of method cannot be null!");
197 final MethodSignatureBuilder builder = new MethodSignatureBuilderImpl(name);
198 builder.setAccessModifier(AccessModifier.PUBLIC);
199 builder.setAbstract(true);
200 methodDefinitions = LazyCollections.lazyAdd(methodDefinitions, builder);
205 public boolean containsMethod(final String name) {
206 Preconditions.checkArgument(name != null, "Parameter name can't be null");
207 for (MethodSignatureBuilder methodDefinition : methodDefinitions) {
208 if (name.equals(methodDefinition.getName())) {
216 public GeneratedPropertyBuilder addProperty(final String name) {
217 Preconditions.checkArgument(name != null, "Parameter name can't be null");
218 Preconditions.checkArgument(!containsProperty(name), "This generated type already contains property with the same name.");
220 final GeneratedPropertyBuilder builder = new GeneratedPropertyBuilderImpl(name);
221 builder.setAccessModifier(AccessModifier.PUBLIC);
222 properties = LazyCollections.lazyAdd(properties, builder);
227 public boolean containsProperty(final String name) {
228 Preconditions.checkArgument(name != null, "Parameter name can't be null");
229 for (GeneratedPropertyBuilder property : properties) {
230 if (name.equals(property.getName())) {
238 public Optional<YangSourceDefinition> getYangSourceDefinition() {
239 return Optional.ofNullable(yangSourceDefinition);
244 public void setYangSourceDefinition(final YangSourceDefinition definition) {
245 yangSourceDefinition = Preconditions.checkNotNull(definition);
249 public int hashCode() {
250 return Objects.hash(getName(), getPackageName());
254 public boolean equals(final Object obj) {
261 if (getClass() != obj.getClass()) {
265 if (!(obj instanceof AbstractGeneratedTypeBuilder)) {
269 AbstractGeneratedTypeBuilder<?> other = (AbstractGeneratedTypeBuilder<?>) obj;
270 return Objects.equals(getFullyQualifiedName(), other.getFullyQualifiedName());
273 public Type getParent() {
278 public Type getParentTypeForBuilder() {
279 return parentTypeForBuilder;
283 public List<GeneratedPropertyBuilder> getProperties() {