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%2FBuilderTemplate.xtend;h=d322833ad81ab9c69e8309462779a1f24c1a37fc;hb=a86eb24054ac65d491af28f25ef33cfa59e6f103;hp=94f63e996f444a5b4ec86dfbd8121e8e770a83f9;hpb=58ce9cc8c9ee0086c63e4eeac53e4ecccbc92047;p=mdsal.git diff --git a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend index 94f63e996f..d322833ad8 100644 --- a/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend +++ b/code-generator/binding-java-api-generator/src/main/java/org/opendaylight/yangtools/sal/java/api/generator/BuilderTemplate.xtend @@ -7,7 +7,14 @@ */ package org.opendaylight.yangtools.sal.java.api.generator -import java.util.Arrays; +import com.google.common.collect.ImmutableSortedSet +import com.google.common.collect.Range +import java.util.ArrayList +import java.util.Arrays +import java.util.Collection +import java.util.Collections +import java.util.HashMap +import java.util.HashSet import java.util.LinkedHashSet import java.util.List import java.util.Map @@ -15,22 +22,15 @@ import java.util.Set import org.opendaylight.yangtools.binding.generator.util.ReferencedTypeImpl import org.opendaylight.yangtools.binding.generator.util.Types import org.opendaylight.yangtools.binding.generator.util.generated.type.builder.GeneratedTOBuilderImpl +import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType 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 org.opendaylight.yangtools.sal.binding.model.api.MethodSignature import org.opendaylight.yangtools.sal.binding.model.api.Type import org.opendaylight.yangtools.yang.binding.Augmentable -import static org.opendaylight.yangtools.binding.generator.util.Types.* -import java.util.HashMap -import java.util.Collections import org.opendaylight.yangtools.yang.binding.DataObject -import java.util.ArrayList -import java.util.HashSet -import java.util.Collection import org.opendaylight.yangtools.yang.binding.Identifiable -import com.google.common.collect.Range -import org.opendaylight.yangtools.sal.binding.model.api.ConcreteType /** * Template for generating JAVA builder classes. @@ -63,6 +63,8 @@ class BuilderTemplate extends BaseTemplate { */ val Set properties + private static val METHOD_COMPARATOR = new AlphabeticallyTypeMemberComparator(); + /** * Constructs new instance of this class. * @throws IllegalArgumentException if genType equals null @@ -79,15 +81,17 @@ class BuilderTemplate extends BaseTemplate { * @returns set of method signature instances */ def private Set createMethods() { - val Set methods = new LinkedHashSet + val Set methods = new LinkedHashSet(); methods.addAll(type.methodDefinitions) collectImplementedMethods(methods, type.implements) - return methods + val Set sortedMethods = ImmutableSortedSet.orderedBy(METHOD_COMPARATOR).addAll(methods).build() + + return sortedMethods } /** * Adds to the methods set all the methods of the implementedIfcs - * and recursivelly their implemented interfaces. + * and recursively their implemented interfaces. * * @param methods set of method signatures * @param implementedIfcs list of implemented interfaces @@ -187,7 +191,7 @@ class BuilderTemplate extends BaseTemplate { throw new IllegalArgumentException("Method, method name, method return type reference cannot be NULL or empty!") } var prefix = "get"; - if(BOOLEAN.equals(method.returnType)) { + if(Types.BOOLEAN.equals(method.returnType)) { prefix = "is"; } if (method.name.startsWith(prefix)) { @@ -209,7 +213,7 @@ class BuilderTemplate extends BaseTemplate { «generateFields(false)» - «generateAugmentField(true)» + «generateAugmentField(false)» «generateConstructorsFromIfcs(type)» @@ -231,7 +235,7 @@ class BuilderTemplate extends BaseTemplate { «generateFields(true)» - «generateAugmentField(false)» + «generateAugmentField(true)» «generateCopyConstructor(true)» @@ -417,9 +421,9 @@ class BuilderTemplate extends BaseTemplate { «ENDIF» ''' - def private generateAugmentField(boolean init) ''' + def private generateAugmentField(boolean isPrivate) ''' «IF augmentField != null» - private «Map.importedName»<«Class.importedName», «augmentField.returnType.importedName»> «augmentField.name» = new «HashMap.importedName»<>(); + «IF isPrivate»private «ENDIF»«Map.importedName»<«Class.importedName», «augmentField.returnType.importedName»> «augmentField.name» = new «HashMap.importedName»<>(); «ENDIF» ''' @@ -542,17 +546,21 @@ class BuilderTemplate extends BaseTemplate { «IF !impl»if (base instanceof «type.name»«IMPL») {«ENDIF» «IF !impl»«type.name»«IMPL» _impl = («type.name»«IMPL») base;«ENDIF» «val prop = if (impl) "base" else "_impl"» - switch («prop».«augmentField.name».size()) { - case 0: - this.«augmentField.name» = «Collections.importedName».emptyMap(); - break; - case 1: - final «Map.importedName».Entry<«Class.importedName», «augmentField.returnType.importedName»> e = «prop».«augmentField.name».entrySet().iterator().next(); - this.«augmentField.name» = «Collections.importedName».<«Class.importedName», «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue()); - break; - default : + «IF impl» + switch («prop».«augmentField.name».size()) { + case 0: + this.«augmentField.name» = «Collections.importedName».emptyMap(); + break; + case 1: + final «Map.importedName».Entry<«Class.importedName», «augmentField.returnType.importedName»> e = «prop».«augmentField.name».entrySet().iterator().next(); + this.«augmentField.name» = «Collections.importedName».<«Class.importedName», «augmentField.returnType.importedName»>singletonMap(e.getKey(), e.getValue()); + break; + default : + this.«augmentField.name» = new «HashMap.importedName»<>(«prop».«augmentField.name»); + } + «ELSE» this.«augmentField.name» = new «HashMap.importedName»<>(«prop».«augmentField.name»); - } + «ENDIF» «IF !impl»}«ENDIF» «ENDIF» } @@ -651,35 +659,50 @@ class BuilderTemplate extends BaseTemplate { if (this == obj) { return true; } - if (obj == null) { + if (!(obj instanceof «DataObject.importedName»)) { return false; } - if (getClass() != obj.getClass()) { + if (!«type.importedName».class.equals(((«DataObject.importedName»)obj).getImplementedInterface())) { return false; } - «type.name»«IMPL» other = («type.name»«IMPL») obj; + «type.importedName» other = («type.importedName»)obj; «FOR property : properties» «val fieldName = property.fieldName» if («fieldName» == null) { - if (other.«fieldName» != null) { + if (other.«property.getterMethodName»() != null) { return false; } «IF property.returnType.name.contains("[")» - } else if(!«Arrays.importedName».equals(«fieldName», other.«fieldName»)) { + } else if(!«Arrays.importedName».equals(«fieldName», other.«property.getterMethodName»())) { «ELSE» - } else if(!«fieldName».equals(other.«fieldName»)) { + } else if(!«fieldName».equals(other.«property.getterMethodName»())) { «ENDIF» return false; } «ENDFOR» «IF augmentField != null» - «val fieldName = augmentField.name» - if («fieldName» == null) { - if (other.«fieldName» != null) { + if (getClass() == obj.getClass()) { + // Simple case: we are comparing against self + «type.name»«IMPL» otherImpl = («type.name»«IMPL») obj; + «val fieldName = augmentField.name» + if («fieldName» == null) { + if (otherImpl.«fieldName» != null) { + return false; + } + } else if(!«fieldName».equals(otherImpl.«fieldName»)) { + return false; + } + } else { + // Hard case: compare our augments with presence there... + for («Map.importedName».Entry<«Class.importedName», «augmentField.returnType.importedName»> e : «augmentField.name».entrySet()) { + if (!e.getValue().equals(other.getAugmentation(e.getKey()))) { + return false; + } + } + // .. and give the other one the chance to do the same + if (!obj.equals(this)) { return false; } - } else if(!«fieldName».equals(other.«fieldName»)) { - return false; } «ENDIF» return true; @@ -732,6 +755,23 @@ class BuilderTemplate extends BaseTemplate { return «type.importedName».class; } ''' + + private def createDescription(GeneratedType type) { + return ''' + Class that builds {@link «type.importedName»} instances. + + @see «type.importedName» + ''' + } + + override def protected String formatDataForJavaDoc(GeneratedType type) { + val typeDescription = createDescription(type) + return ''' + «IF !typeDescription.nullOrEmpty» + «typeDescription» + «ENDIF» + '''.toString + } }