X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fjava%2Fapi%2Fgenerator%2FEnumTemplate.xtend;h=82b36fa69bccae0699a7d2b6b98517edeec7eb54;hb=refs%2Fchanges%2F15%2F100815%2F4;hp=f2e7533a10763f5b9004bef2eb8193f12c567a79;hpb=ae34daa2a74d6b952f3567341c163a77ca3a5720;p=mdsal.git
diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/EnumTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/EnumTemplate.xtend
index f2e7533a10..82b36fa69b 100644
--- a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/EnumTemplate.xtend
+++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/EnumTemplate.xtend
@@ -7,13 +7,11 @@
*/
package org.opendaylight.mdsal.binding.java.api.generator
-import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
+import static extension org.opendaylight.mdsal.binding.generator.BindingGeneratorUtil.encodeAngleBrackets
+import static org.opendaylight.mdsal.binding.model.ri.Types.STRING;
import com.google.common.collect.ImmutableMap
import com.google.common.collect.ImmutableMap.Builder
-import java.util.Map
-import java.util.Objects
-import java.util.Optional
import org.opendaylight.mdsal.binding.model.api.Enumeration
import org.opendaylight.mdsal.binding.model.api.GeneratedType
@@ -26,6 +24,16 @@ class EnumTemplate extends BaseTemplate {
*/
val Enumeration enums
+ /**
+ * Constructs instance of this class with concrete enums
.
+ *
+ * @param enums enumeration which will be transformed to JAVA source code
+ */
+ new(AbstractJavaGeneratedType javaType, Enumeration enums) {
+ super(javaType, enums as GeneratedType)
+ this.enums = enums
+ }
+
/**
* Constructs instance of this class with concrete enums
.
*
@@ -46,7 +54,9 @@ class EnumTemplate extends BaseTemplate {
}
def writeEnumItem(String name, String mappedName, int value, String description) '''
- «asJavadoc(encodeAngleBrackets(description))»
+ «IF description !== null»
+ «description.trim.encodeAngleBrackets.encodeJavadocSymbols.wrapToDocumentation»
+ «ENDIF»
«mappedName»(«value», "«name»")
'''
@@ -56,18 +66,17 @@ class EnumTemplate extends BaseTemplate {
* @return string with the enumeration body
*/
override body() '''
- «wrapToDocumentation(formatDataForJavaDoc(enums))»
+ «enums.formatDataForJavaDoc.wrapToDocumentation»
+ «generatedAnnotation»
public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
«writeEnumeration(enums)»
- private static final «Map.importedName»<«String.importedName», «enums.name»> NAME_MAP;
- private static final «Map.importedName»<«Integer.importedName», «enums.name»> VALUE_MAP;
+ private static final «JU_MAP.importedName»<«STRING.importedName», «enums.name»> NAME_MAP;
+ private static final «JU_MAP.importedName»<«Integer.importedName», «enums.name»> VALUE_MAP;
static {
- final «Builder.importedName»<«String.importedName», «enums.name»> nb =
- «ImmutableMap.importedName».builder();
- final «Builder.importedName»<«Integer.importedName», «enums.name»> vb =
- «ImmutableMap.importedName».builder();
+ final «Builder.importedName»<«STRING.importedName», «enums.name»> nb = «ImmutableMap.importedName».builder();
+ final «Builder.importedName»<«Integer.importedName», «enums.name»> vb = «ImmutableMap.importedName».builder();
for («enums.name» enumItem : «enums.name».values()) {
vb.put(enumItem.value, enumItem);
nb.put(enumItem.name, enumItem);
@@ -77,44 +86,67 @@ class EnumTemplate extends BaseTemplate {
VALUE_MAP = vb.build();
}
- private final «String.importedName» name;
+ private final «STRING.importedNonNull» name;
private final int value;
- private «enums.name»(int value, «String.importedName» name) {
+ private «enums.name»(int value, «STRING.importedNonNull» name) {
this.value = value;
this.name = name;
}
- @Override
- public «String.importedName» getName() {
+ @«OVERRIDE.importedName»
+ public «STRING.importedNonNull» getName() {
return name;
}
- @Override
+ @«OVERRIDE.importedName»
public int getIntValue() {
return value;
}
/**
- * Return the enumeration member whose {@link #getName()} matches specified value.
+ * Return the enumeration member whose {@link #getName()} matches specified assigned name.
*
* @param name YANG assigned name
- * @return corresponding «enums.name» item, if present
- * @throws NullPointerException if name is null
+ * @return corresponding «enums.name» item, or {@code null} if no such item exists
+ * @throws NullPointerException if {@code name} is null
*/
- public static «Optional.importedName»<«enums.name»> forName(«String.importedName» name) {
- return «Optional.importedName».ofNullable(NAME_MAP.get(«Objects.importedName».requireNonNull(name)));
+ public static «enums.importedNullable» forName(«STRING.importedName» name) {
+ return NAME_MAP.get(«JU_OBJECTS.importedName».requireNonNull(name));
}
/**
* Return the enumeration member whose {@link #getIntValue()} matches specified value.
*
* @param intValue integer value
- * @return corresponding «enums.name» item, or null if no such item exists
+ * @return corresponding «enums.name» item, or {@code null} if no such item exists
*/
- public static «enums.name» forValue(int intValue) {
+ public static «enums.importedNullable» forValue(int intValue) {
return VALUE_MAP.get(intValue);
}
+
+ /**
+ * Return the enumeration member whose {@link #getName()} matches specified assigned name.
+ *
+ * @param name YANG assigned name
+ * @return corresponding «enums.name» item
+ * @throws NullPointerException if {@code name} is null
+ * @throws IllegalArgumentException if {@code name} does not match any item
+ */
+ public static «enums.importedNonNull» ofName(«STRING.importedName» name) {
+ return «CODEHELPERS.importedName».checkEnum(forName(name), name);
+ }
+
+ /**
+ * Return the enumeration member whose {@link #getIntValue()} matches specified value.
+ *
+ * @param intValue integer value
+ * @return corresponding «enums.name» item
+ * @throws IllegalArgumentException if {@code intValue} does not match any item
+ */
+ public static «enums.importedNonNull» ofValue(int intValue) {
+ return «CODEHELPERS.importedName».checkEnum(forValue(intValue), intValue);
+ }
}
'''