mirror of https://github.com/apache/maven.git
Lazily create builders during transformations
This commit is contained in:
parent
8418fb3960
commit
084e3f9500
|
@ -41,6 +41,7 @@ import java.util.Properties;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.function.BinaryOperator;
|
import java.util.function.BinaryOperator;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.maven.api.annotations.Generated;
|
import org.apache.maven.api.annotations.Generated;
|
||||||
|
@ -82,21 +83,25 @@ public class ${className} {
|
||||||
if (target == null) {
|
if (target == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
${class.name}.Builder builder = ${class.name}.newBuilder(target);
|
Supplier<${class.name}.Builder> creator = () -> ${class.name}.newBuilder(target);
|
||||||
|
${class.name}.Builder builder = null;
|
||||||
#foreach ( $field in $allFields )
|
#foreach ( $field in $allFields )
|
||||||
transform${field.modelClass.name}_${Helper.capitalise($field.name)}(builder, target);
|
builder = (${class.name}.Builder) transform${field.modelClass.name}_${Helper.capitalise($field.name)}(creator, builder, target);
|
||||||
#end
|
#end
|
||||||
return builder.build();
|
return builder != null ? builder.build() : target;
|
||||||
}
|
}
|
||||||
|
|
||||||
#foreach ( $field in $allFields )
|
#foreach ( $field in $allFields )
|
||||||
#set ( $capField = ${Helper.capitalise($field.name)} )
|
#set ( $capField = ${Helper.capitalise($field.name)} )
|
||||||
protected void transform${class.name}_${capField}(${class.name}.Builder builder, ${class.name} target) {
|
protected ${class.name}.Builder transform${class.name}_${capField}(Supplier<? extends ${class.name}.Builder> creator, ${class.name}.Builder builder, ${class.name} target) {
|
||||||
#if ( $field.type == "String" )
|
#if ( $field.type == "String" )
|
||||||
String newVal = transform(target.get${capField}());
|
String oldVal = target.get${capField}();
|
||||||
builder.${field.name}(newVal != target.get${capField}() ? newVal : null);
|
String newVal = transform(oldVal);
|
||||||
|
return newVal != oldVal ? (builder != null ? builder : creator.get()).${field.name}(newVal) : builder;
|
||||||
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
|
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
|
||||||
builder.${field.name}(transform(target.get${capField}(), this::transform));
|
List<String> oldVal = target.get${capField}();
|
||||||
|
List<String> newVal = transform(oldVal, this::transform);
|
||||||
|
return newVal != oldVal ? (builder != null ? builder : creator.get()).${field.name}(newVal) : builder;
|
||||||
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
|
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
|
||||||
Map<String, String> props = target.get${capField}();
|
Map<String, String> props = target.get${capField}();
|
||||||
Map<String, String> newProps = null;
|
Map<String, String> newProps = null;
|
||||||
|
@ -106,21 +111,28 @@ public class ${className} {
|
||||||
if (newProps == null) {
|
if (newProps == null) {
|
||||||
newProps = new HashMap<>();
|
newProps = new HashMap<>();
|
||||||
newProps.putAll(props);
|
newProps.putAll(props);
|
||||||
|
builder = builder != null ? builder : creator.get();
|
||||||
builder.${field.name}(newProps);
|
builder.${field.name}(newProps);
|
||||||
}
|
}
|
||||||
newProps.put(entry.getKey(), newVal);
|
newProps.put(entry.getKey(), newVal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return builder;
|
||||||
#elseif ( $field.to && $field.multiplicity == "1" )
|
#elseif ( $field.to && $field.multiplicity == "1" )
|
||||||
${field.to} newVal = transform${field.to}(target.get${capField}());
|
${field.to} oldVal = target.get${capField}();
|
||||||
builder.${field.name}(newVal != target.get${capField}() ? newVal : null);
|
${field.to} newVal = transform${field.to}(oldVal);
|
||||||
|
return newVal != oldVal ? (builder != null ? builder : creator.get()).${field.name}(newVal) : builder;
|
||||||
#elseif ( $field.to && $field.multiplicity == "*" )
|
#elseif ( $field.to && $field.multiplicity == "*" )
|
||||||
builder.${field.name}(transform(target.get${capField}(), this::transform${field.to}));
|
List<${field.to}> oldVal = target.get${capField}();
|
||||||
|
List<${field.to}> newVal = transform(oldVal, this::transform${field.to});
|
||||||
|
return newVal != oldVal ? (builder != null ? builder : creator.get()).${field.name}(newVal) : builder;
|
||||||
#elseif ( $field.type == "DOM" )
|
#elseif ( $field.type == "DOM" )
|
||||||
XmlNode newVal = transform(target.get${capField}());
|
XmlNode oldVal = target.get${capField}();
|
||||||
builder.${field.name}(newVal != target.get${capField}() ? newVal : null);
|
XmlNode newVal = transform(oldVal);
|
||||||
|
return newVal != oldVal ? (builder != null ? builder : creator.get()).${field.name}(newVal) : builder;
|
||||||
#elseif ( $field.type == "boolean" || $field.type == "int" || $field.type == "java.nio.file.Path" )
|
#elseif ( $field.type == "boolean" || $field.type == "int" || $field.type == "java.nio.file.Path" )
|
||||||
// nothing to do, the transformer only handles strings
|
// nothing to do, the transformer only handles strings
|
||||||
|
return builder;
|
||||||
#else
|
#else
|
||||||
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
|
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
|
||||||
#end
|
#end
|
||||||
|
@ -130,11 +142,12 @@ public class ${className} {
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
protected <T> List<T> transform(List<T> list, Function<T, T> transformer) {
|
protected <T> List<T> transform(List<T> list, Function<T, T> transformer) {
|
||||||
List<T> newList = null;
|
List<T> newList = list;
|
||||||
for (int i = 0; i < list.size(); i++) {
|
for (int i = 0; i < list.size(); i++) {
|
||||||
T newVal = transformer.apply(list.get(i));
|
T oldVal = list.get(i);
|
||||||
if (newVal != list.get(i)) {
|
T newVal = transformer.apply(oldVal);
|
||||||
if (newList == null) {
|
if (newVal != oldVal) {
|
||||||
|
if (newList == list) {
|
||||||
newList = new ArrayList<>(list);
|
newList = new ArrayList<>(list);
|
||||||
}
|
}
|
||||||
newList.set(i, newVal);
|
newList.set(i, newVal);
|
||||||
|
@ -143,17 +156,34 @@ public class ${className} {
|
||||||
return newList;
|
return newList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected <T> Map<String, T> transform(Map<String, T> map, Function<T, T> transformer) {
|
||||||
|
Map<String, T> newMap = map;
|
||||||
|
for (String key : map.keySet()) {
|
||||||
|
T oldVal = map.get(key);
|
||||||
|
T newVal = transformer.apply(oldVal);
|
||||||
|
if (newVal != oldVal) {
|
||||||
|
if (newMap == map) {
|
||||||
|
newMap = new HashMap<>(map);
|
||||||
|
}
|
||||||
|
newMap.put(key, newVal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newMap;
|
||||||
|
}
|
||||||
|
|
||||||
protected XmlNode transform(XmlNode node) {
|
protected XmlNode transform(XmlNode node) {
|
||||||
return node != null ? new XmlNodeImpl(
|
if (node != null) {
|
||||||
node.getPrefix(),
|
String oldValue = node.getValue();
|
||||||
node.getNamespaceUri(),
|
String newValue = transform(oldValue);
|
||||||
node.getName(),
|
Map<String, String> oldAttrs = node.getAttributes();
|
||||||
transform(node.getValue()),
|
Map<String, String> newAttrs = transform(oldAttrs, this::transform);
|
||||||
node.getAttributes().entrySet()
|
List<XmlNode> oldChildren = node.getChildren();
|
||||||
.stream().collect(Collectors.toMap(e -> e.getKey(), e -> transform(e.getValue()))),
|
List<XmlNode> newChildren = transform(oldChildren, this::transform);
|
||||||
node.getChildren().stream()
|
if (oldValue != newValue || oldAttrs != newAttrs || oldChildren != newChildren) {
|
||||||
.map(this::transform).collect(Collectors.toList()),
|
return new XmlNodeImpl(node.getPrefix(), node.getNamespaceUri(), node.getName(),
|
||||||
node.getInputLocation()
|
newValue, newAttrs, newChildren, node.getInputLocation());
|
||||||
) : null;
|
}
|
||||||
|
}
|
||||||
|
return node;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue