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.Optional;
16 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
17 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedProperty;
18 import org.opendaylight.mdsal.binding.javav2.model.api.GeneratedTransferObject;
19 import org.opendaylight.mdsal.binding.javav2.model.api.ParameterizedType;
20 import org.opendaylight.mdsal.binding.javav2.model.api.Restrictions;
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.type.builder.GeneratedPropertyBuilder;
24 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTOBuilder;
25 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.MethodSignatureBuilder;
26 import org.opendaylight.yangtools.util.LazyCollections;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
31 public final class GeneratedTOBuilderImpl extends AbstractGeneratedTypeBuilder<GeneratedTOBuilder> implements
34 private GeneratedTransferObject extendsType;
35 private List<GeneratedPropertyBuilder> equalsProperties = ImmutableList.of();
36 private List<GeneratedPropertyBuilder> hashProperties = ImmutableList.of();
37 private List<GeneratedPropertyBuilder> toStringProperties = ImmutableList.of();
38 private boolean isTypedef = false;
39 private boolean isUnionType = false;
40 private boolean isUnionTypeBuilder = false;
41 private TypeDefinition<?> baseType = null;
42 private Restrictions restrictions;
43 private GeneratedPropertyBuilder suid;
44 private String reference;
45 private String description;
46 private String moduleName;
47 private List<QName> schemaPath;
49 public GeneratedTOBuilderImpl(final String packageName, final String name, ModuleContext context) {
50 super(packageName, name, context);
54 public GeneratedTOBuilderImpl(final String packageName, final String name, final boolean isNormalized) {
55 super(packageName, name, true, null);
58 public GeneratedTOBuilderImpl(final String packageName, final String name, final boolean isPkNameNormalized,
59 final boolean isTypeNormalized, ModuleContext context) {
60 super(packageName, name, isPkNameNormalized, isTypeNormalized, context);
65 public GeneratedTOBuilder setExtendsType(GeneratedTransferObject genTransObj) {
66 Preconditions.checkArgument(genTransObj != null, "Generated Transfer Object cannot be null!");
67 extendsType = genTransObj;
72 * Add new MethodSignature definition for GeneratedTypeBuilder and
73 * returns MethodSignatureBuilder for specifying all Method parameters. <br>
74 * Name of Method cannot be <code>null</code>, if it is <code>null</code>
75 * the method SHOULD throw {@link IllegalArgumentException} <br>
76 * By <i>Default</i> the MethodSignatureBuilder SHOULD be pre-set as
77 * {@link MethodSignatureBuilder#setAbstract(boolean)},
78 * {TypeMemberBuilder#setFinal(boolean)} and
79 * {TypeMemberBuilder#setAccessModifier(boolean)}
83 * @return <code>new</code> instance of Method Signature Builder.
86 public MethodSignatureBuilder addMethod(final String name) {
87 final MethodSignatureBuilder builder = super.addMethod(name);
88 builder.setAbstract(false);
93 public GeneratedTOBuilder addEqualsIdentity(GeneratedPropertyBuilder property) {
94 equalsProperties = LazyCollections.lazyAdd(equalsProperties, property);
99 public GeneratedTOBuilder addHashIdentity(GeneratedPropertyBuilder property) {
100 hashProperties = LazyCollections.lazyAdd(hashProperties, property);
105 public GeneratedTOBuilder addToStringProperty(GeneratedPropertyBuilder property) {
106 toStringProperties = LazyCollections.lazyAdd(toStringProperties, property);
111 public void setRestrictions(Restrictions restrictions) {
112 this.restrictions = restrictions;
116 public GeneratedTransferObject toInstance() {
117 return new GeneratedTransferObjectImpl(this);
121 public void setTypedef(boolean isTypedef) {
122 this.isTypedef = isTypedef;
126 public void setBaseType(TypeDefinition<?> typeDef) {
127 this.baseType = typeDef;
131 public void setIsUnion(boolean isUnion) {
132 this.isUnionType = isUnion;
136 public void setIsUnionBuilder(boolean isUnionTypeBuilder) {
137 this.isUnionTypeBuilder = isUnionTypeBuilder;
141 public void setSUID(GeneratedPropertyBuilder suid) {
146 public void setDescription(String description) {
147 this.description = description;
151 public void setModuleName(String moduleName) {
152 this.moduleName = moduleName;
156 public void setSchemaPath(List<QName> schemaPath) {
157 this.schemaPath = schemaPath;
161 public void setReference(String reference) {
162 this.reference = reference;
166 protected GeneratedTOBuilderImpl thisInstance() {
171 public String toString() {
172 StringBuilder builder = new StringBuilder();
173 builder.append("GeneratedTransferObject [packageName=");
174 builder.append(getPackageName());
175 builder.append(", name=");
176 builder.append(getName());
177 builder.append(", comment=");
178 builder.append(getComment());
179 builder.append(", constants=");
180 builder.append(getConstants());
181 builder.append(", enumerations=");
182 builder.append(getEnumerations());
183 builder.append(", equalsProperties=");
184 builder.append(equalsProperties);
185 builder.append(", hashCodeProperties=");
186 builder.append(hashProperties);
187 builder.append(", stringProperties=");
188 builder.append(toStringProperties);
189 builder.append(", annotations=");
190 builder.append(getAnnotations());
191 builder.append(", methods=");
192 builder.append(getMethodDefinitions());
194 return builder.toString();
197 private static final class GeneratedTransferObjectImpl extends AbstractGeneratedType implements GeneratedTransferObject {
199 private final List<GeneratedProperty> equalsProperties;
200 private final List<GeneratedProperty> hashCodeProperties;
201 private final List<GeneratedProperty> stringProperties;
202 private final GeneratedTransferObject extendsType;
203 private final boolean isTypedef;
204 private final TypeDefinition<?> baseType;
205 private final boolean isUnionType;
206 private final boolean isUnionTypeBuilder;
207 private final Restrictions restrictions;
208 private final GeneratedProperty innerSuid;
209 private final String reference;
210 private final String description;
211 private final String moduleName;
212 private final List<QName> schemaPath;
214 public GeneratedTransferObjectImpl(final GeneratedTOBuilderImpl builder) {
217 this.extendsType = builder.extendsType;
218 this.equalsProperties = toUnmodifiableProperties(builder.equalsProperties);
219 this.hashCodeProperties = toUnmodifiableProperties(builder.hashProperties);
220 this.stringProperties = toUnmodifiableProperties(builder.toStringProperties);
222 this.isTypedef = builder.isTypedef;
223 this.baseType = builder.baseType;
224 this.isUnionType = builder.isUnionType;
225 this.isUnionTypeBuilder = builder.isUnionTypeBuilder;
226 this.restrictions = builder.restrictions;
227 this.reference = builder.reference;
228 this.description = builder.description;
229 this.moduleName = builder.moduleName;
230 this.schemaPath = builder.schemaPath;
232 if (builder.suid == null) {
233 this.innerSuid = null;
235 this.innerSuid = builder.suid.toInstance(GeneratedTransferObjectImpl.this);
240 public GeneratedProperty getSUID() {
245 public GeneratedTransferObject getSuperType() {
250 public List<GeneratedProperty> getEqualsIdentifiers() {
251 return equalsProperties;
255 public List<GeneratedProperty> getHashCodeIdentifiers() {
256 return hashCodeProperties;
260 public List<GeneratedProperty> getToStringIdentifiers() {
261 return stringProperties;
265 public boolean isTypedef() {
270 public TypeDefinition<?> getBaseType() {
275 public boolean isUnionType() {
280 public boolean isUnionTypeBuilder() {
281 return isUnionTypeBuilder;
285 public Restrictions getRestrictions() {
290 public Optional<String> getDescription() {
291 return Optional.ofNullable(this.description);
295 public Optional<String> getReference() {
296 return Optional.ofNullable(this.reference);
300 public List<QName> getSchemaPath() {
305 public String getModuleName() {
310 public String toString() {
312 return serializeTypedef(this);
314 StringBuilder builder = new StringBuilder();
315 builder.append("GeneratedTransferObject [packageName=");
316 builder.append(getPackageName());
317 builder.append(", name=");
318 builder.append(getName());
319 builder.append(", annotations=");
320 builder.append(getAnnotations());
321 final TypeComment comment = getComment();
322 if (comment != null) {
323 builder.append(", comment=");
324 builder.append(comment.getJavadoc());
326 builder.append(", extends=");
327 builder.append(getSuperType());
328 builder.append(", implements=");
329 builder.append(getImplements());
330 builder.append(", enclosedTypes=");
331 builder.append(getEnclosedTypes());
332 builder.append(", constants=");
333 builder.append(getConstantDefinitions());
334 builder.append(", enumerations=");
335 builder.append(getEnumerations());
336 builder.append(", properties=");
337 builder.append(getProperties());
338 builder.append(", equalsProperties=");
339 builder.append(equalsProperties);
340 builder.append(", hashCodeProperties=");
341 builder.append(hashCodeProperties);
342 builder.append(", stringProperties=");
343 builder.append(stringProperties);
344 builder.append(", methods=");
345 builder.append(getMethodDefinitions());
347 return builder.toString();
350 private String serializeTypedef(final Type type) {
351 if (type instanceof ParameterizedType) {
352 ParameterizedType parameterizedType = (ParameterizedType) type;
353 StringBuilder sb = new StringBuilder();
354 sb.append(parameterizedType.getRawType().getFullyQualifiedName());
356 boolean first = true;
357 for (Type parameter : parameterizedType.getActualTypeArguments()) {
363 sb.append(serializeTypedef(parameter));
366 return sb.toString();
368 return type.getFullyQualifiedName();