Improve javadoc for generated keys
[mdsal.git] / binding / mdsal-binding-java-api-generator / src / main / java / org / opendaylight / mdsal / binding / java / api / generator / ListKeyTemplate.xtend
1 /*
2  * Copyright (c) 2020 Pantheon Technologies, s.r.o. 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 package org.opendaylight.mdsal.binding.java.api.generator
9
10 import org.opendaylight.mdsal.binding.model.api.GeneratedProperty
11 import org.opendaylight.mdsal.binding.model.api.GeneratedType
12 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
13 import org.opendaylight.mdsal.binding.model.api.ParameterizedType;
14 import org.opendaylight.mdsal.binding.model.api.Type
15 import org.opendaylight.mdsal.binding.model.ri.BindingTypes
16
17 /**
18  * Template for generating JAVA class.
19  */
20 final class ListKeyTemplate extends ClassTemplate {
21     /**
22      * Creates instance of this class with concrete <code>genType</code>.
23      *
24      * @param genType generated transfer object which will be transformed to JAVA class source code
25      */
26     new(GeneratedTransferObject genType) {
27         super(genType)
28     }
29
30     override allValuesConstructor() '''
31         /**
32          * Constructs an instance.
33          *
34          «FOR p : allProperties»
35             * @param «p.fieldName» the entity «p.getName»
36          «ENDFOR»
37          * @throws NullPointerException if any of the arguments are null
38          */
39         public «type.name»(«allProperties.asNonNullArgumentsDeclaration») {
40             «FOR p : allProperties»
41                 «val fieldName = p.fieldName»
42                 this.«fieldName» = «CODEHELPERS.importedName».requireKeyProp(«fieldName», "«p.name»")«p.cloneCall»;
43             «ENDFOR»
44             «FOR p : properties»
45                 «generateRestrictions(type, p.fieldName, p.returnType)»
46             «ENDFOR»
47         }
48     '''
49
50     override getterMethod(GeneratedProperty field) '''
51         /**
52          * Return «field.getName», guaranteed to be non-null.
53          *
54          * @return {@code «field.returnType.importedName»} «field.getName», guaranteed to be non-null.
55          */
56         public «field.returnType.importedNonNull» «field.getterMethodName»() {
57             return «field.fieldName»«field.cloneCall»;
58         }
59     '''
60
61     override protected String formatDataForJavaDoc(GeneratedType type) {
62         val listType = findListType(type)
63         if (listType === null) {
64             return ""
65         }
66
67         val importedName = listType.importedName
68         return '''
69             This class represents the key of {@link «importedName»} class.
70
71             @see «importedName»
72         '''
73     }
74
75     private static def Type findListType(GeneratedType type) {
76         for (Type implType : type.getImplements()) {
77             if (implType instanceof ParameterizedType) {
78                 val identifiable = BindingTypes.extractIdentifiable(implType)
79                 if (identifiable !== null) {
80                     return identifiable
81                 }
82             }
83         }
84         return null
85     }
86 }