X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=code-generator%2Fbinding-java-api-generator%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fjava%2Fapi%2Fgenerator%2FClassTemplate.xtend;h=53f7d4c5cd045a352176b71ce5f16dacf70131ca;hb=refs%2Fchanges%2F76%2F2876%2F1;hp=4869c3f847ba7dba2ade9eea1b89bf770bd49408;hpb=7d7503ed440129ac9736bfd1e5ee99ebb3ba18b8;p=yangtools.git diff --git a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend index 4869c3f847..53f7d4c5cd 100644 --- a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend +++ b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/ClassTemplate.xtend @@ -7,8 +7,12 @@ import org.opendaylight.yangtools.sal.binding.model.api.Enumeration import org.opendaylight.yangtools.sal.binding.model.api.GeneratedProperty import org.opendaylight.yangtools.sal.binding.model.api.GeneratedTransferObject import org.opendaylight.yangtools.sal.binding.model.api.GeneratedType - - +import java.util.ArrayList +import java.util.Collections import java.util.Arrays +import org.opendaylight.yangtools.sal.binding.model.api.Restrictions +import com.google.common.collect.Range +import java.util.regex.Pattern + /** * Template for generating JAVA class. */ @@ -18,6 +22,7 @@ class ClassTemplate extends BaseTemplate { protected val List finalProperties protected val List parentProperties protected val Iterable allProperties; + protected val Restrictions restrictions /** * List of enumeration which are generated as JAVA enum type. @@ -36,7 +41,7 @@ class ClassTemplate extends BaseTemplate { protected val GeneratedTransferObject genTO; - + /** * Creates instance of this class with concrete genType. * @@ -48,16 +53,20 @@ class ClassTemplate extends BaseTemplate { this.properties = genType.properties this.finalProperties = GeneratorUtil.resolveReadOnlyPropertiesFromTO(genTO.properties) this.parentProperties = GeneratorUtil.getPropertiesOfAllParents(genTO) - this.allProperties = properties + parentProperties + this.restrictions = genType.restrictions + + var List sorted = new ArrayList(); + sorted.addAll(properties); + sorted.addAll(parentProperties); + Collections.sort(sorted, new PropertyComparator()); + + this.allProperties = sorted this.enums = genType.enumerations this.consts = genType.constantDefinitions this.enclosedGeneratedTypes = genType.enclosedTypes } - - - - + /** * Generates JAVA class source code (class body only). * @@ -66,9 +75,8 @@ class ClassTemplate extends BaseTemplate { def CharSequence generateAsInnerClass() { return generateBody(true) } - - + override protected body() { generateBody(false); } @@ -82,7 +90,8 @@ class ClassTemplate extends BaseTemplate { def protected generateBody(boolean isInnerClass) ''' «type.comment.asJavadoc» «generateClassDeclaration(isInnerClass)» { - «innerClassesDeclarations» + «suidDeclaration» + «innerClassesDeclarations» «enumDeclarations» «constantsDeclarations» «generateFields» @@ -93,14 +102,19 @@ class ClassTemplate extends BaseTemplate { «field.setterMethod» «ENDIF» «ENDFOR» + «generateHashCode» + «generateEquals» - «generateToString» - + + «generateToString(genTO.toStringIdentifiers)» + + «generateGetLength» + } ''' - - + + /** * Template method which generates inner classes inside this interface. * @@ -120,13 +134,17 @@ class ClassTemplate extends BaseTemplate { def protected constructors() ''' - «allValuesConstructor» - «IF !allProperties.empty» - «copyConstructor» - «ENDIF» - «IF properties.empty && !parentProperties.empty » - «parentConstructor» - «ENDIF» + «IF genTO.unionType» + «genUnionConstructor» + «ELSE» + «allValuesConstructor» + «ENDIF» + «IF !allProperties.empty» + «copyConstructor» + «ENDIF» + «IF properties.empty && !parentProperties.empty » + «parentConstructor» + «ENDIF» ''' def protected allValuesConstructor() ''' @@ -134,13 +152,37 @@ class ClassTemplate extends BaseTemplate { «IF false == parentProperties.empty» super(«parentProperties.asArguments»); «ENDIF» + «FOR p : allProperties» + «generateRestrictions(type, p.fieldName.toString, p.returnType)» + «ENDFOR» «FOR p : properties» this.«p.fieldName» = «p.fieldName»; «ENDFOR» } ''' - - + + def protected genUnionConstructor() ''' + «FOR p : allProperties» + «val List other = new ArrayList(properties)» + «val added = other.remove(p)» + «genConstructor(p, other)» + «ENDFOR» + + ''' + + def protected genConstructor(GeneratedProperty property, GeneratedProperty... other) ''' + public «type.name»(«property.returnType.importedName + " " + property.name») { + «IF false == parentProperties.empty» + super(«parentProperties.asArguments»); + «ENDIF» + «generateRestrictions(type, property.fieldName.toString, property.returnType)» + this.«property.fieldName» = «property.name»; + «FOR p : other» + this.«p.fieldName» = null; + «ENDFOR» + } + ''' + def protected copyConstructor() ''' /** * Creates a copy from Source Object. @@ -209,7 +251,13 @@ class ClassTemplate extends BaseTemplate { «ENDFOR» «ENDIF» ''' - + + def protected suidDeclaration() ''' + «IF genTO.SUID != null» + private static final long serialVersionUID = «genTO.SUID.value»L; + «ENDIF» + ''' + /** * Template method wich generates JAVA constants. * @@ -222,14 +270,14 @@ class ClassTemplate extends BaseTemplate { «val cValue = c.value» «IF cValue instanceof List» «val cValues = cValue as List» - private static final List «Constants.MEMBER_PATTERN_LIST» = new ArrayList(); - public static final List «TypeConstants.PATTERN_CONSTANT_NAME» = Arrays.asList(« + private static final «List.importedName»<«Pattern.importedName»> «Constants.MEMBER_PATTERN_LIST» = new «ArrayList.importedName»<«Pattern.importedName»>(); + public static final «List.importedName» «TypeConstants.PATTERN_CONSTANT_NAME» = «Arrays.importedName».asList(« FOR v : cValues SEPARATOR ", "»« IF v instanceof String»"« v as String»"« ENDIF»« ENDFOR»); - + «generateStaticInicializationBlock» «ENDIF» «ELSE» @@ -238,10 +286,10 @@ class ClassTemplate extends BaseTemplate { «ENDFOR» «ENDIF» ''' - + /** * Template method which generates JAVA static initialization block. - * + * * @return string with static initialization block in JAVA format */ def protected generateStaticInicializationBlock() ''' @@ -251,10 +299,10 @@ class ClassTemplate extends BaseTemplate { } } ''' - + /** * Template method which generates JAVA class attributes. - * + * * @return string with the class attributes in JAVA format */ def protected generateFields() ''' @@ -264,11 +312,11 @@ class ClassTemplate extends BaseTemplate { «ENDFOR» «ENDIF» ''' - + /** * Template method which generates the method hashCode(). - * + * * @return string with the hashCode() method definition in JAVA format */ def protected generateHashCode() ''' @@ -278,17 +326,21 @@ class ClassTemplate extends BaseTemplate { final int prime = 31; int result = 1; «FOR property : genTO.hashCodeIdentifiers» + «IF property.returnType.name.contains("[")» + result = prime * result + ((«property.fieldName» == null) ? 0 : «Arrays.importedName».hashCode(«property.fieldName»)); + «ELSE» result = prime * result + ((«property.fieldName» == null) ? 0 : «property.fieldName».hashCode()); + «ENDIF» «ENDFOR» return result; } «ENDIF» ''' - + /** * Template method which generates the method equals(). - * - * @return string with the equals() method definition in JAVA format + * + * @return string with the equals() method definition in JAVA format */ def protected generateEquals() ''' «IF !genTO.equalsIdentifiers.empty» @@ -310,7 +362,11 @@ class ClassTemplate extends BaseTemplate { if (other.«fieldName» != null) { return false; } + «IF property.returnType.name.contains("[")» + } else if(!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) { + «ELSE» } else if(!«fieldName».equals(other.«fieldName»)) { + «ENDIF» return false; } «ENDFOR» @@ -318,36 +374,18 @@ class ClassTemplate extends BaseTemplate { } «ENDIF» ''' - - /** - * Template method which generates the method toString(). - * - * @return string with the toString() method definition in JAVA format - */ - def protected generateToString() ''' - «IF !genTO.toStringIdentifiers.empty» - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - «val properties = genTO.toStringIdentifiers» - builder.append("«type.name» [«properties.get(0).fieldName»="); - «IF properties.get(0).returnType.name.contains("[")» - builder.append(java.util.Arrays.toString(«properties.get(0).fieldName»)); - «ELSE» - builder.append(«properties.get(0).fieldName»); - «ENDIF» - «FOR i : 1..> getLength() { + final «List.importedName»<«Range.importedName»> result = new «ArrayList.importedName»<>(); + «List.importedName»<«Range.importedName»<«Integer.importedName»>> lengthConstraints = new «ArrayList.importedName»<>(); + «FOR r : restrictions.lengthConstraints» + result.add(«Range.importedName».closed(«r.min», «r.max»)); «ENDFOR» - builder.append("]"); - return builder.toString(); + return result; } «ENDIF» ''' - + }