5450d6e79ffecf1ffdb97d15f64012933cf59e15
[mdsal.git] / binding2 / mdsal-binding2-generator-util / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / util / generated / type / builder / AbstractGeneratedType.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder;
10
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.Objects;
17 import java.util.Optional;
18 import java.util.stream.Collectors;
19 import org.opendaylight.mdsal.binding.javav2.generator.util.AbstractBaseType;
20 import org.opendaylight.mdsal.binding.javav2.model.api.AnnotationType;
21 import org.opendaylight.mdsal.binding.javav2.model.api.Constant;
22 import org.opendaylight.mdsal.binding.javav2.model.api.Enumeration;
23 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedProperty;
24 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedType;
25 import org.opendaylight.mdsal.binding.javav2.model.api.MethodSignature;
26 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
27 import org.opendaylight.mdsal.binding.javav2.model.api.TypeComment;
28 import org.opendaylight.mdsal.binding.javav2.model.api.YangSourceDefinition;
29 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.AnnotationTypeBuilder;
30 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.EnumBuilder;
31 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedPropertyBuilder;
32 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
33 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
34 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
35
36 @Beta
37 abstract class AbstractGeneratedType extends AbstractBaseType implements GeneratedType {
38
39     private final Type parent;
40     private final Type parentTypeForBuilder;
41     private final TypeComment comment;
42     private final List<AnnotationType> annotations;
43     private final List<Type> implementsTypes;
44     private final List<Enumeration> enumerations;
45     private final List<Constant> constants;
46     private final List<MethodSignature> methodSignatures;
47     private final List<GeneratedType> enclosedTypes;
48     private final List<GeneratedProperty> properties;
49     private final boolean isAbstract;
50     private final YangSourceDefinition definition;
51
52     public AbstractGeneratedType(final AbstractGeneratedTypeBuilder<?> builder) {
53         super(builder.getPackageName(), builder.getName(), true, null);
54         this.parent = builder.getParent();
55         this.parentTypeForBuilder = builder.getParentTypeForBuilder();
56         this.comment = builder.getComment();
57         this.annotations = toUnmodifiableAnnotations(builder.getAnnotations());
58         this.implementsTypes = makeUnmodifiable(builder.getImplementsTypes());
59         this.constants = makeUnmodifiable(builder.getConstants());
60         this.enumerations = toUnmodifiableEnumerations(builder.getEnumerations());
61         this.methodSignatures = toUnmodifiableMethods(builder.getMethodDefinitions());
62         this.enclosedTypes = toUnmodifiableEnclosedTypes(builder.getEnclosedTypes(),
63                 builder.getEnclosedTransferObjects());
64         this.properties = toUnmodifiableProperties(builder.getProperties());
65         this.isAbstract = builder.isAbstract();
66         this.definition = builder.getYangSourceDefinition().orElse(null);
67     }
68
69     public AbstractGeneratedType(final Type parent, final String packageName, final String name, final TypeComment comment,
70                                  final List<AnnotationTypeBuilder> annotationBuilders, final boolean isAbstract,
71                                  final List<Type> implementsTypes, final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
72                                  final List<GeneratedTOBuilder> enclosedGenTOBuilders, final List<EnumBuilder> enumBuilders,
73                                  final List<Constant> constants, final List<MethodSignatureBuilder> methodBuilders,
74                                  final List<GeneratedPropertyBuilder> propertyBuilders, final Type parentTypeForBuilder) {
75         //TODO: not called by actual codebase, fix this up (provide context) if needed - 07/20/2017
76         super(packageName, name, null);
77         this.parent = parent;
78         this.parentTypeForBuilder = parentTypeForBuilder;
79         this.comment = comment;
80         this.annotations = toUnmodifiableAnnotations(annotationBuilders);
81         this.implementsTypes = makeUnmodifiable(implementsTypes);
82         this.constants = makeUnmodifiable(constants);
83         this.enumerations = toUnmodifiableEnumerations(enumBuilders);
84         this.methodSignatures = toUnmodifiableMethods(methodBuilders);
85         this.enclosedTypes = toUnmodifiableEnclosedTypes(enclosedGenTypeBuilders, enclosedGenTOBuilders);
86         this.properties = toUnmodifiableProperties(propertyBuilders);
87         this.isAbstract = isAbstract;
88         this.definition = null;
89     }
90
91     protected static final <T> List<T> makeUnmodifiable(final List<T> list) {
92         switch (list.size()) {
93             case 0:
94                 return ImmutableList.of();
95             case 1:
96                 return Collections.singletonList(list.get(0));
97             default:
98                 return Collections.unmodifiableList(list);
99         }
100     }
101
102     private static List<GeneratedType> toUnmodifiableEnclosedTypes(final List<GeneratedTypeBuilder> enclosedGenTypeBuilders,
103                                                                    final List<GeneratedTOBuilder> enclosedGenTOBuilders) {
104         final ArrayList<GeneratedType> enclosedTypesList = new ArrayList<>(enclosedGenTypeBuilders.size() + enclosedGenTOBuilders.size());
105         enclosedTypesList.addAll(enclosedGenTypeBuilders.stream().filter(Objects::nonNull).map(GeneratedTypeBuilder::toInstance).collect(Collectors.toList()));
106
107         enclosedTypesList.addAll(enclosedGenTOBuilders.stream().filter(Objects::nonNull).map(GeneratedTOBuilder::toInstance).collect(Collectors.toList()));
108
109         return makeUnmodifiable(enclosedTypesList);
110     }
111
112     protected static final List<AnnotationType> toUnmodifiableAnnotations(final List<AnnotationTypeBuilder> annotationBuilders) {
113         final List<AnnotationType> annotationList = new ArrayList<>(annotationBuilders.size());
114         annotationList.addAll(annotationBuilders.stream().map(AnnotationTypeBuilder::toInstance).collect(Collectors.toList()));
115         return makeUnmodifiable(annotationList);
116     }
117
118     protected final List<MethodSignature> toUnmodifiableMethods(final List<MethodSignatureBuilder> methodBuilders) {
119         final List<MethodSignature> methods = new ArrayList<>(methodBuilders.size());
120         methods.addAll(methodBuilders.stream().map(methodBuilder -> methodBuilder.toInstance(this)).collect(Collectors.toList()));
121         return makeUnmodifiable(methods);
122     }
123
124     protected final List<Enumeration> toUnmodifiableEnumerations(final List<EnumBuilder> enumBuilders) {
125         final List<Enumeration> enums = new ArrayList<>(enumBuilders.size());
126         enums.addAll(enumBuilders.stream().map(enumBuilder -> enumBuilder.toInstance(this)).collect(Collectors.toList()));
127         return makeUnmodifiable(enums);
128     }
129
130     protected final List<GeneratedProperty> toUnmodifiableProperties(final List<GeneratedPropertyBuilder> methodBuilders) {
131         final List<GeneratedProperty> methods = new ArrayList<>(methodBuilders.size());
132         methods.addAll(methodBuilders.stream().map(methodBuilder -> methodBuilder.toInstance(this)).collect(Collectors.toList()));
133         return makeUnmodifiable(methods);
134     }
135
136     @Override
137     public Type getParentType() {
138         return this.parent;
139     }
140
141     @Override
142     public Type getParentTypeForBuilder() {
143         return this.parentTypeForBuilder;
144     }
145
146     @Override
147     public final TypeComment getComment() {
148         return this.comment;
149     }
150
151     @Override
152     public List<AnnotationType> getAnnotations() {
153         return this.annotations;
154     }
155
156     @Override
157     public boolean isAbstract() {
158         return this.isAbstract;
159     }
160
161     @Override
162     public List<Type> getImplements() {
163         return this.implementsTypes;
164     }
165
166     @Override
167     public List<GeneratedType> getEnclosedTypes() {
168         return this.enclosedTypes;
169     }
170
171     @Override
172     public List<Enumeration> getEnumerations() {
173         return this.enumerations;
174     }
175
176     @Override
177     public List<Constant> getConstantDefinitions() {
178         return this.constants;
179     }
180
181     @Override
182     public List<MethodSignature> getMethodDefinitions() {
183         return this.methodSignatures;
184     }
185
186     @Override
187     public List<GeneratedProperty> getProperties() {
188         return this.properties;
189     }
190
191     @Override
192     public final Optional<YangSourceDefinition> getYangSourceDefinition() {
193         return Optional.ofNullable(definition);
194     }
195
196     @Override
197     public String toString() {
198         final StringBuilder builder = new StringBuilder();
199         builder.append("GeneratedType [packageName=");
200         builder.append(getPackageName());
201         builder.append(", name=");
202         builder.append(getName());
203         if (this.parent != null) {
204             builder.append(", parent=");
205             builder.append(this.parent.getFullyQualifiedName());
206         } else {
207             builder.append(", parent=null");
208         }
209         final TypeComment comment = getComment();
210         if (comment != null) {
211             builder.append(", comment=");
212             builder.append(comment.getJavadoc());
213         }
214         builder.append(", annotations=");
215         builder.append(this.annotations);
216         builder.append(", enclosedTypes=");
217         builder.append(this.enclosedTypes);
218         builder.append(", enumerations=");
219         builder.append(this.enumerations);
220         builder.append(", constants=");
221         builder.append(this.constants);
222         builder.append(", methodSignatures=");
223         builder.append(this.methodSignatures);
224         builder.append("]");
225         return builder.toString();
226     }
227 }