*/
package org.opendaylight.mdsal.binding.java.api.generator
+import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
+import static org.opendaylight.mdsal.binding.model.util.Types.STRING;
+
+import com.google.common.collect.ImmutableMap
+import com.google.common.collect.ImmutableMap.Builder
+import java.util.Optional
import org.opendaylight.mdsal.binding.model.api.Enumeration
import org.opendaylight.mdsal.binding.model.api.GeneratedType
-import static org.opendaylight.mdsal.binding.model.util.BindingGeneratorUtil.encodeAngleBrackets
-
/**
* Template for generating JAVA enumeration type.
*/
class EnumTemplate extends BaseTemplate {
-
-
/**
* Enumeration which will be transformed to JAVA source code for enumeration
*/
*
* @param enums enumeration which will be transformed to JAVA source code
*/
- new(Enumeration enums) {
- super(enums as GeneratedType )
+ new(AbstractJavaGeneratedType javaType, Enumeration enums) {
+ super(javaType, enums as GeneratedType)
this.enums = enums
}
+ /**
+ * Constructs instance of this class with concrete <code>enums</code>.
+ *
+ * @param enums enumeration which will be transformed to JAVA source code
+ */
+ new(Enumeration enums) {
+ super(enums as GeneratedType)
+ this.enums = enums
+ }
/**
* Generates only JAVA enumeration source code.
*/
override body() '''
«wrapToDocumentation(formatDataForJavaDoc(enums))»
- public enum «enums.name» {
+ public enum «enums.name» implements «org.opendaylight.yangtools.yang.binding.Enumeration.importedName» {
«writeEnumeration(enums)»
- private static final java.util.Map<java.lang.Integer, «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 com.google.common.collect.ImmutableMap.Builder<java.lang.Integer, «enums.name»> b = com.google.common.collect.ImmutableMap.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()) {
- b.put(enumItem.value, enumItem);
+ vb.put(enumItem.value, enumItem);
+ nb.put(enumItem.name, enumItem);
}
- VALUE_MAP = b.build();
+ NAME_MAP = nb.build();
+ VALUE_MAP = vb.build();
}
- private final «String.importedName» name;
+ private final «STRING.importedName» name;
private final int value;
- private «enums.name»(int value, «String.importedName» name) {
+ private «enums.name»(int value, «STRING.importedName» name) {
this.value = value;
this.name = name;
}
- /**
- * Returns the name of the enumeration item as it is specified in the input yang.
- *
- * @return the name of the enumeration item as it is specified in the input yang
- */
- public «String.importedName» getName() {
+ @«OVERRIDE.importedName»
+ public «STRING.importedName» getName() {
return name;
}
- /**
- * @return integer value
- */
+ @«OVERRIDE.importedName»
public int getIntValue() {
return value;
}
/**
- * @param valueArg integer value
- * @return corresponding «enums.name» item
+ * Return the enumeration member whose {@link #getName()} matches specified value.
+ *
+ * @param name YANG assigned name
+ * @return corresponding «enums.name» item, if present
+ * @throws NullPointerException if name is null
+ */
+ public static «Optional.importedName»<«enums.name»> forName(«STRING.importedName» name) {
+ return «Optional.importedName».ofNullable(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
*/
- public static «enums.name» forValue(int valueArg) {
- return VALUE_MAP.get(valueArg);
+ public static «enums.name» forValue(int intValue) {
+ return VALUE_MAP.get(intValue);
}
}
'''
def writeEnumeration(Enumeration enumeration)
'''
«FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"»
- «writeEnumItem(v.name, v.mappedName, v.value, v.description)»«
+ «writeEnumItem(v.name, v.mappedName, v.value, v.description.orElse(null))»«
ENDFOR»
'''
}