import static org.opendaylight.mdsal.binding.spec.naming.BindingMapping.DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME
import com.google.common.collect.ImmutableList
+import com.google.common.collect.Sets
import java.util.ArrayList
import java.util.Collection
import java.util.HashSet
import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject
import org.opendaylight.mdsal.binding.model.api.GeneratedType
import org.opendaylight.mdsal.binding.model.api.JavaTypeName
+import org.opendaylight.mdsal.binding.model.api.MethodSignature;
import org.opendaylight.mdsal.binding.model.api.ParameterizedType
import org.opendaylight.mdsal.binding.model.api.Type
import org.opendaylight.mdsal.binding.model.util.TypeConstants
import org.opendaylight.mdsal.binding.model.util.Types
import org.opendaylight.mdsal.binding.spec.naming.BindingMapping
import org.opendaylight.yangtools.concepts.Builder
-import org.opendaylight.yangtools.yang.binding.AugmentationHolder
+import com.google.common.collect.ImmutableSet
/**
* Template for generating JAVA builder classes.
*/
package static val BUILDER_STR = "Builder";
- static val AUGMENTATION_FIELD_UPPER = AUGMENTATION_FIELD.toFirstUpper
static val BUILDER = JavaTypeName.create(Builder)
+ val BuilderImplTemplate implTemplate
+
/**
* Constructs new instance of this class.
* @throws IllegalArgumentException if <code>genType</code> equals <code>null</code>
*/
- new(GeneratedType genType, GeneratedType targetType, Set<GeneratedProperty> properties, Type augmentType,
- Type keyType) {
- super(genType, targetType, properties, augmentType, keyType)
+ new(GeneratedType genType, GeneratedType targetType, Type keyType) {
+ super(genType, targetType, keyType)
+ implTemplate = new BuilderImplTemplate(this, type.enclosedTypes.get(0))
}
override isLocalInnerClass(JavaTypeName name) {
return new «type.enclosedTypes.get(0).importedName»(this);
}
- «new BuilderImplTemplate(this, type.enclosedTypes.get(0)).body»
+ «implTemplate.body»
}
'''
def private generateConstructorsFromIfcs() '''
public «type.name»() {
}
+
«IF (!(targetType instanceof GeneratedTransferObject))»
- «FOR impl : targetType.implements»
+ «FOR impl : targetType.implements SEPARATOR "\n"»
«generateConstructorFromIfc(impl)»
«ENDFOR»
«ENDIF»
«val ifc = implementedIfc as GeneratedType»
«FOR getter : ifc.nonDefaultMethods»
«IF BindingMapping.isGetterMethodName(getter.name)»
- this._«getter.propertyNameFromGetter» = arg.«getter.name»();
+ «val propertyName = getter.propertyNameFromGetter»
+ «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»;
«ENDIF»
«ENDFOR»
«FOR impl : ifc.implements»
- «printConstructorPropertySetter(impl)»
+ «printConstructorPropertySetter(impl, getSpecifiedGetters(ifc))»
+ «ENDFOR»
+ «ENDIF»
+ '''
+
+ def private Object printConstructorPropertySetter(Type implementedIfc, Set<MethodSignature> alreadySetProperties) '''
+ «IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
+ «val ifc = implementedIfc as GeneratedType»
+ «FOR getter : ifc.nonDefaultMethods»
+ «IF BindingMapping.isGetterMethodName(getter.name) && getterByName(alreadySetProperties, getter.name).isEmpty»
+ «val propertyName = getter.propertyNameFromGetter»
+ «printPropertySetter(getter, '''arg.«getter.name»()''', propertyName)»;
+ «ENDIF»
+ «ENDFOR»
+ «FOR descendant : ifc.implements»
+ «printConstructorPropertySetter(descendant, Sets.union(alreadySetProperties, getSpecifiedGetters(ifc)))»
«ENDFOR»
«ENDIF»
'''
+ def static Set<MethodSignature> getSpecifiedGetters(GeneratedType type) {
+ val ImmutableSet.Builder<MethodSignature> setBuilder = new ImmutableSet.Builder
+ for (MethodSignature method : type.getMethodDefinitions()) {
+ if (method.hasOverrideAnnotation) {
+ setBuilder.add(method)
+ }
+ }
+ return setBuilder.build()
+ }
+
/**
* Generate 'fieldsFrom' method to set builder properties based on type of given argument.
*/
* </ul>
*
* @param arg grouping object
- * @throws IllegalArgumentException if given argument is none of valid types
+ * @throws IllegalArgumentException if given argument is none of valid types or has property with incompatible value
*/
'''
«IF (implementedIfc instanceof GeneratedType && !(implementedIfc instanceof GeneratedTransferObject))»
«val ifc = implementedIfc as GeneratedType»
«FOR getter : ifc.nonDefaultMethods»
- «IF BindingMapping.isGetterMethodName(getter.name)»
- this._«getter.propertyNameFromGetter» = ((«implementedIfc.fullyQualifiedName»)arg).«getter.name»();
+ «IF BindingMapping.isGetterMethodName(getter.name) && !hasOverrideAnnotation(getter)»
+ «printPropertySetter(getter, '''((«ifc.fullyQualifiedName»)arg).«getter.name»()''', getter.propertyNameFromGetter)»;
«ENDIF»
«ENDFOR»
«ENDIF»
'''
+ def private printPropertySetter(MethodSignature getter, String retrieveProperty, String propertyName) {
+ val ownGetter = implTemplate.findGetter(getter.name)
+ val ownGetterType = ownGetter.returnType
+ if (Types.strictTypeEquals(getter.returnType, ownGetterType)) {
+ return "this._" + propertyName + " = " + retrieveProperty
+ }
+ if (Types.isListType(ownGetterType)) {
+ val itemType = (ownGetterType as ParameterizedType).actualTypeArguments.get(0)
+ return '''
+ this._«propertyName» = «CODEHELPERS.importedName».checkListFieldCast(«itemType.fullyQualifiedName».class, "«propertyName»", «retrieveProperty»)'''
+ }
+ return '''
+ this._«propertyName» = «CODEHELPERS.importedName».checkFieldCast(«ownGetter.returnType.fullyQualifiedName».class, "«propertyName»", «retrieveProperty»)'''
+ }
+
private def List<Type> getBaseIfcs(GeneratedType type) {
val List<Type> baseIfcs = new ArrayList();
for (ifc : type.implements) {
val returnType = field.returnType
if (returnType instanceof ParameterizedType) {
if (Types.isListType(returnType)) {
- return generateListSetter(field, returnType.actualTypeArguments.get(0), "")
+ return generateListSetter(field, returnType.actualTypeArguments.get(0))
} else if (Types.isMapType(returnType)) {
- return generateListSetter(field, returnType.actualTypeArguments.get(1), ".values()")
+ return generateMapSetter(field, returnType.actualTypeArguments.get(1))
}
}
return generateSimpleSetter(field, returnType)
}
- def private generateListSetter(GeneratedProperty field, Type actualType, String extractor) '''
+ def private generateListSetter(GeneratedProperty field, Type actualType) '''
«val restrictions = restrictionsForSetter(actualType)»
«IF restrictions !== null»
«generateCheckers(field, restrictions, actualType)»
public «type.getName» set«field.getName.toFirstUpper»(final «field.returnType.importedName» values) {
«IF restrictions !== null»
if (values != null) {
- for («actualType.importedName» value : values«extractor») {
+ for («actualType.importedName» value : values) {
«checkArgument(field, restrictions, actualType, "value")»
}
}
'''
+ // FIXME: MDSAL-540: remove the migration setter
+ def private generateMapSetter(GeneratedProperty field, Type actualType) '''
+ «val restrictions = restrictionsForSetter(actualType)»
+ «val actualTypeRef = actualType.importedName»
+ «val setterName = "set" + field.name.toFirstUpper»
+ «IF restrictions !== null»
+ «generateCheckers(field, restrictions, actualType)»
+ «ENDIF»
+ public «type.getName» «setterName»(final «field.returnType.importedName» values) {
+ «IF restrictions !== null»
+ if (values != null) {
+ for («actualTypeRef» value : values.values()) {
+ «checkArgument(field, restrictions, actualType, "value")»
+ }
+ }
+ «ENDIF»
+ this.«field.fieldName» = values;
+ return this;
+ }
+
+ /**
+ * Utility migration setter.
+ *
+ * <b>IMPORTANT NOTE</b>: This method does not completely match previous mechanics, as the list is processed as
+ * during this method's execution. Any future modifications of the list are <b>NOT</b>
+ * reflected in this builder nor its products.
+ *
+ * @param values Legacy List of values
+ * @return this builder
+ * @throws IllegalArgumentException if the list contains entries with the same key
+ * @throws NullPointerException if the list contains a null entry
+ * @deprecated Use {#link #«setterName»(«JU_MAP.importedName»)} instead.
+ */
+ @«DEPRECATED.importedName»(forRemoval = true)
+ public «type.getName» «setterName»(final «JU_LIST.importedName»<«actualTypeRef»> values) {
+ return «setterName»(«CODEHELPERS.importedName».compatMap(values));
+ }
+ '''
+
def private generateSimpleSetter(GeneratedProperty field, Type actualType) '''
«val restrictions = restrictionsForSetter(actualType)»
«IF restrictions !== null»
this.«field.fieldName» = value;
return this;
}
+ «val uintType = UINT_TYPES.get(field.returnType)»
+ «IF uintType !== null»
+
+ /**
+ * Utility migration setter.
+ *
+ * @param value field value in legacy type
+ * @return this builder
+ * @deprecated Use {#link «setterName»(«field.returnType.importedJavadocName»)} instead.
+ */
+ @Deprecated(forRemoval = true)
+ public «type.getName» «setterName»(final «uintType.importedName» value) {
+ return «setterName»(«CODEHELPERS.importedName».compatUint(value));
+ }
+ «ENDIF»
'''
/**
«val augmentTypeRef = augmentType.importedName»
«val jlClassRef = CLASS.importedName»
«val hashMapRef = JU_HASHMAP.importedName»
- public «type.name» add«AUGMENTATION_FIELD_UPPER»(«augmentTypeRef» augmentation) {
- return add«AUGMENTATION_FIELD_UPPER»(augmentation.«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»(), augmentation);
- }
-
- public «type.name» add«AUGMENTATION_FIELD_UPPER»(«jlClassRef»<? extends «augmentTypeRef»> augmentationType, «augmentTypeRef» augmentationValue) {
- if (augmentationValue == null) {
- return remove«AUGMENTATION_FIELD_UPPER»(augmentationType);
- }
-
+ /**
+ * Add an augmentation to this builder's product.
+ *
+ * @param augmentation augmentation to be added
+ * @return this builder
+ * @throws NullPointerException if {@code augmentation} is null
+ */
+ public «type.name» addAugmentation(«augmentTypeRef» augmentation) {
+ «jlClassRef»<? extends «augmentTypeRef»> augmentationType = augmentation.«DATA_CONTAINER_IMPLEMENTED_INTERFACE_NAME»();
if (!(this.«AUGMENTATION_FIELD» instanceof «hashMapRef»)) {
this.«AUGMENTATION_FIELD» = new «hashMapRef»<>();
}
- this.«AUGMENTATION_FIELD».put(augmentationType, augmentationValue);
+ this.«AUGMENTATION_FIELD».put(augmentationType, augmentation);
return this;
}
- public «type.name» remove«AUGMENTATION_FIELD_UPPER»(«jlClassRef»<? extends «augmentTypeRef»> augmentationType) {
+ /**
+ * Remove an augmentation from this builder's product. If this builder does not track such an augmentation
+ * type, this method does nothing.
+ *
+ * @param augmentationType augmentation type to be removed
+ * @return this builder
+ */
+ public «type.name» removeAugmentation(«jlClassRef»<? extends «augmentTypeRef»> augmentationType) {
if (this.«AUGMENTATION_FIELD» instanceof «hashMapRef») {
this.«AUGMENTATION_FIELD».remove(augmentationType);
}
override protected generateCopyKeys(List<GeneratedProperty> keyProps) '''
this.key = base.«BindingMapping.IDENTIFIABLE_KEY_NAME»();
- «generateCopyNonKeys(keyProps)»
+ «FOR field : keyProps»
+ this.«field.fieldName» = base.«field.getterMethodName»();
+ «ENDFOR»
'''
-
- override protected CharSequence generateCopyNonKeys(Collection<GeneratedProperty> props) '''
+ override protected CharSequence generateCopyNonKeys(Collection<BuilderGeneratedProperty> props) '''
«FOR field : props»
- this.«field.fieldName» = base.«field.getterMethodName»();
+ this.«field.fieldName» = base.«field.getterName»();
«ENDFOR»
'''
override protected generateCopyAugmentation(Type implType) {
- val augmentationHolderRef = AugmentationHolder.importedName
- val typeRef = targetType.importedName
val hashMapRef = JU_HASHMAP.importedName
val augmentTypeRef = augmentType.importedName
return '''
- if (base instanceof «augmentationHolderRef») {
- @SuppressWarnings("unchecked")
- «JU_MAP.importedName»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> aug =((«augmentationHolderRef»<«typeRef»>) base).augmentations();
- if (!aug.isEmpty()) {
- this.«AUGMENTATION_FIELD» = new «hashMapRef»<>(aug);
- }
+ «JU_MAP.importedName»<«CLASS.importedName»<? extends «augmentTypeRef»>, «augmentTypeRef»> aug = base.augmentations();
+ if (!aug.isEmpty()) {
+ this.«AUGMENTATION_FIELD» = new «hashMapRef»<>(aug);
}
'''
}
-
- private static def hasNonDefaultMethods(GeneratedType type) {
- !type.methodDefinitions.isEmpty && type.methodDefinitions.exists([def | !def.isDefault])
- }
-
- private static def nonDefaultMethods(GeneratedType type) {
- type.methodDefinitions.filter([def | !def.isDefault])
- }
}