X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fjava%2Fapi%2Fgenerator%2FEnumTemplate.xtend;fp=binding%2Fmdsal-binding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fjava%2Fapi%2Fgenerator%2FEnumTemplate.xtend;h=92466b2bd2b2d7ad8e27360ddca0c982dc860cc6;hb=470e20edf85dbf3731d1fea7fde5eab7bd3afe95;hp=0000000000000000000000000000000000000000;hpb=0835b2cf948156252e2363a53f3dd48853bd27ab;p=mdsal.git diff --git a/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/EnumTemplate.xtend b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/EnumTemplate.xtend new file mode 100644 index 0000000000..92466b2bd2 --- /dev/null +++ b/binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/EnumTemplate.xtend @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.yangtools.sal.java.api.generator + +import org.opendaylight.yangtools.sal.binding.model.api.Enumeration +import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType + +/** + * Template for generating JAVA enumeration type. + */ +class EnumTemplate extends BaseTemplate { + + + /** + * Enumeration which will be transformed to JAVA source code for enumeration + */ + val Enumeration enums + + /** + * Constructs instance of this class with concrete enums. + * + * @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. + * + * @return string with JAVA enumeration source code + */ + def generateAsInnerClass() { + return body + } + + def writeEnumItem(String name, int value, String description) ''' + «asJavadoc(formatToParagraph(description))» + «name»(«value») + ''' + + /** + * Template method which generates enumeration body (declaration + enumeration items). + * + * @return string with the enumeration body + */ + override body() ''' + «wrapToDocumentation(formatDataForJavaDoc(enums))» + public enum «enums.name» { + «writeEnumeration(enums)» + + + int value; + private static final java.util.Map VALUE_MAP; + + static { + final com.google.common.collect.ImmutableMap.Builder b = com.google.common.collect.ImmutableMap.builder(); + for («enums.name» enumItem : «enums.name».values()) + { + b.put(enumItem.value, enumItem); + } + + VALUE_MAP = b.build(); + } + + private «enums.name»(int value) { + this.value = value; + } + + /** + * @return integer value + */ + public int getIntValue() { + return value; + } + + /** + * @param valueArg + * @return corresponding «enums.name» item + */ + public static «enums.name» forValue(int valueArg) { + return VALUE_MAP.get(valueArg); + } + } + ''' + + def writeEnumeration(Enumeration enumeration) + ''' + «FOR v : enumeration.values SEPARATOR ",\n" AFTER ";"» + «writeEnumItem(v.name, v.value, v.description)»« + ENDFOR» + ''' +}