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.collect.ImmutableList;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Optional;
17 import java.util.stream.Collectors;
18 import org.opendaylight.mdsal.binding.javav2.generator.util.AbstractBaseType;
19 import org.opendaylight.mdsal.binding.javav2.model.api.AnnotationType;
20 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
21 import org.opendaylight.mdsal.binding.javav2.model.api.Enumeration;
22 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedProperty;
23 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
24 import org.opendaylight.mdsal.binding.javav2.model.api.MethodSignature;
25 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
26 import org.opendaylight.mdsal.binding.javav2.model.api.TypeComment;
27 import org.opendaylight.mdsal.binding.javav2.model.api.YangSourceDefinition;
28 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.AnnotationTypeBuilder;
29 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
30 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
31 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
32 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
33 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
36 abstract class AbstractGeneratedType extends AbstractBaseType implements GeneratedType {
38 private final Type parent;
39 private final Type parentTypeForBuilder;
40 private final TypeComment comment;
41 private final List<AnnotationType> annotations;
42 private final List<Type> implementsTypes;
43 private final List<Enumeration> enumerations;
44 private final List<Constant> constants;
45 private final List<MethodSignature> methodSignatures;
46 private final List<GeneratedType> enclosedTypes;
47 private final List<GeneratedProperty> properties;
48 private final boolean isAbstract;
49 private final YangSourceDefinition definition;
51 public AbstractGeneratedType(final AbstractGeneratedTypeBuilder<?> builder) {
52 super(builder.getPackageName(), builder.getName(), true, null);
53 this.parent = builder.getParent();
54 this.parentTypeForBuilder = builder.getParentTypeForBuilder();
55 this.comment = builder.getComment();
56 this.annotations = toUnmodifiableAnnotations(builder.getAnnotations());
57 this.implementsTypes = makeUnmodifiable(builder.getImplementsTypes());
58 this.constants = makeUnmodifiable(builder.getConstants());
59 this.enumerations = toUnmodifiableEnumerations(builder.getEnumerations());
60 this.methodSignatures = toUnmodifiableMethods(builder.getMethodDefinitions());
61 this.enclosedTypes = toUnmodifiableEnclosedTypes(builder.getEnclosedTypes(),
62 builder.getEnclosedTransferObjects());
63 this.properties = toUnmodifiableProperties(builder.getProperties());
64 this.isAbstract = builder.isAbstract();
65 this.definition = builder.getYangSourceDefinition().orElse(null);
68 public AbstractGeneratedType(final Type parent, final String packageName, final String name, final TypeComment comment,
69 final List<AnnotationTypeBuilder> annotationBuilders, final boolean isAbstract,
70 final List<Type> implementsTypes, final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
71 final List<GeneratedTOBuilder> enclosedGenTOBuilders, final List<EnumBuilder> enumBuilders,
72 final List<Constant> constants, final List<MethodSignatureBuilder> methodBuilders,
73 final List<GeneratedPropertyBuilder> propertyBuilders, final Type parentTypeForBuilder) {
74 //TODO: not called by actual codebase, fix this up (provide context) if needed - 07/20/2017
75 super(packageName, name, null);
77 this.parentTypeForBuilder = parentTypeForBuilder;
78 this.comment = comment;
79 this.annotations = toUnmodifiableAnnotations(annotationBuilders);
80 this.implementsTypes = makeUnmodifiable(implementsTypes);
81 this.constants = makeUnmodifiable(constants);
82 this.enumerations = toUnmodifiableEnumerations(enumBuilders);
83 this.methodSignatures = toUnmodifiableMethods(methodBuilders);
84 this.enclosedTypes = toUnmodifiableEnclosedTypes(enclosedGenTypeBuilders, enclosedGenTOBuilders);
85 this.properties = toUnmodifiableProperties(propertyBuilders);
86 this.isAbstract = isAbstract;
87 this.definition = null;
90 protected static final <T> List<T> makeUnmodifiable(final List<T> list) {
91 switch (list.size()) {
93 return ImmutableList.of();
95 return Collections.singletonList(list.get(0));
97 return Collections.unmodifiableList(list);
101 private static List<GeneratedType> toUnmodifiableEnclosedTypes(final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
102 final List<GeneratedTOBuilder> enclosedGenTOBuilders) {
103 final ArrayList<GeneratedType> enclosedTypesList = new ArrayList<>(enclosedGenTypeBuilders.size() + enclosedGenTOBuilders.size());
104 enclosedTypesList.addAll(enclosedGenTypeBuilders.stream().filter(builder -> builder != null).map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
106 enclosedTypesList.addAll(enclosedGenTOBuilders.stream().filter(builder -> builder != null).map(GeneratedTOBuilder::toInstance).collect(Collectors.toList()));
108 return makeUnmodifiable(enclosedTypesList);
111 protected static final List<AnnotationType> toUnmodifiableAnnotations(final List<AnnotationTypeBuilder> annotationBuilders) {
112 final List<AnnotationType> annotationList = new ArrayList<>(annotationBuilders.size());
113 annotationList.addAll(annotationBuilders.stream().map(AnnotationTypeBuilder::toInstance).collect(Collectors.toList()));
114 return makeUnmodifiable(annotationList);
117 protected final List<MethodSignature> toUnmodifiableMethods(final List<MethodSignatureBuilder> methodBuilders) {
118 final List<MethodSignature> methods = new ArrayList<>(methodBuilders.size());
119 methods.addAll(methodBuilders.stream().map(methodBuilder -> methodBuilder.toInstance(this)).collect(Collectors.toList()));
120 return makeUnmodifiable(methods);
123 protected final List<Enumeration> toUnmodifiableEnumerations(final List<EnumBuilder> enumBuilders) {
124 final List<Enumeration> enums = new ArrayList<>(enumBuilders.size());
125 enums.addAll(enumBuilders.stream().map(enumBuilder -> enumBuilder.toInstance(this)).collect(Collectors.toList()));
126 return makeUnmodifiable(enums);
129 protected final List<GeneratedProperty> toUnmodifiableProperties(final List<GeneratedPropertyBuilder> methodBuilders) {
130 final List<GeneratedProperty> methods = new ArrayList<>(methodBuilders.size());
131 methods.addAll(methodBuilders.stream().map(methodBuilder -> methodBuilder.toInstance(this)).collect(Collectors.toList()));
132 return makeUnmodifiable(methods);
136 public Type getParentType() {
141 public Type getParentTypeForBuilder() {
142 return this.parentTypeForBuilder;
146 public final TypeComment getComment() {
151 public List<AnnotationType> getAnnotations() {
152 return this.annotations;
156 public boolean isAbstract() {
157 return this.isAbstract;
161 public List<Type> getImplements() {
162 return this.implementsTypes;
166 public List<GeneratedType> getEnclosedTypes() {
167 return this.enclosedTypes;
171 public List<Enumeration> getEnumerations() {
172 return this.enumerations;
176 public List<Constant> getConstantDefinitions() {
177 return this.constants;
181 public List<MethodSignature> getMethodDefinitions() {
182 return this.methodSignatures;
186 public List<GeneratedProperty> getProperties() {
187 return this.properties;
191 public final Optional<YangSourceDefinition> getYangSourceDefinition() {
192 return Optional.ofNullable(definition);
196 public String toString() {
197 final StringBuilder builder = new StringBuilder();
198 builder.append("GeneratedType [packageName=");
199 builder.append(getPackageName());
200 builder.append(", name=");
201 builder.append(getName());
202 if (this.parent != null) {
203 builder.append(", parent=");
204 builder.append(this.parent.getFullyQualifiedName());
206 builder.append(", parent=null");
208 final TypeComment comment = getComment();
209 if (comment != null) {
210 builder.append(", comment=");
211 builder.append(comment.getJavadoc());
213 builder.append(", annotations=");
214 builder.append(this.annotations);
215 builder.append(", enclosedTypes=");
216 builder.append(this.enclosedTypes);
217 builder.append(", enumerations=");
218 builder.append(this.enumerations);
219 builder.append(", constants=");
220 builder.append(this.constants);
221 builder.append(", methodSignatures=");
222 builder.append(this.methodSignatures);
224 return builder.toString();