View Javadoc
1   // =================== DO NOT EDIT THIS FILE ====================
2   //  Generated by Modello Velocity from transformer.vm
3   //  template, any modifications will be overwritten.
4   // ==============================================================
5   package org.apache.maven.toolchain.v4;
6   
7   import java.io.ObjectStreamException;
8   import java.util.AbstractList;
9   import java.util.ArrayList;
10  import java.util.Collection;
11  import java.util.HashMap;
12  import java.util.Iterator;
13  import java.util.LinkedHashMap;
14  import java.util.List;
15  import java.util.Map;
16  import java.util.Properties;
17  import java.util.Objects;
18  import java.util.function.BinaryOperator;
19  import java.util.function.Function;
20  import java.util.function.Supplier;
21  import java.util.stream.Collectors;
22  
23  import org.apache.maven.api.annotations.Generated;
24  import org.apache.maven.api.xml.XmlNode;
25  import org.apache.maven.api.toolchain.TrackableBase;
26  import org.apache.maven.api.toolchain.PersistedToolchains;
27  import org.apache.maven.api.toolchain.ToolchainModel;
28  import org.apache.maven.api.toolchain.InputLocation;
29  import org.apache.maven.api.toolchain.InputSource;
30  import org.apache.maven.internal.xml.XmlNodeImpl;
31  
32  @Generated
33  public class MavenToolchainsTransformer {
34  
35      private final Function<String, String> transformer;
36  
37      public MavenToolchainsTransformer(Function<String, String> transformer) {
38          this.transformer = transformer;
39      }
40  
41      /**
42       * Transforms the given model
43       */
44      public PersistedToolchains visit(PersistedToolchains target) {
45          Objects.requireNonNull(target, "target cannot be null");
46          return transformPersistedToolchains(target);
47      }
48  
49      /**
50       * The transformation function.
51       */
52      protected String transform(String value) {
53          return transformer.apply(value);
54      }
55  
56      protected TrackableBase transformTrackableBase(TrackableBase target) {
57          if (target == null) {
58              return null;
59          }
60          Supplier<TrackableBase.Builder> creator = () -> TrackableBase.newBuilder(target);
61          TrackableBase.Builder builder = null;
62          return builder != null ? builder.build() : target;
63      }
64  
65  
66      protected PersistedToolchains transformPersistedToolchains(PersistedToolchains target) {
67          if (target == null) {
68              return null;
69          }
70          Supplier<PersistedToolchains.Builder> creator = () -> PersistedToolchains.newBuilder(target);
71          PersistedToolchains.Builder builder = null;
72          builder = (PersistedToolchains.Builder) transformPersistedToolchains_Toolchains(creator, builder, target);
73          return builder != null ? builder.build() : target;
74      }
75  
76      protected PersistedToolchains.Builder transformPersistedToolchains_Toolchains(Supplier<? extends PersistedToolchains.Builder> creator, PersistedToolchains.Builder builder, PersistedToolchains target) {
77          List<ToolchainModel> oldVal = target.getToolchains();
78          List<ToolchainModel> newVal = transform(oldVal, this::transformToolchainModel);
79          return newVal != oldVal ? (builder != null ? builder : creator.get()).toolchains(newVal) : builder;
80      }
81  
82      protected ToolchainModel transformToolchainModel(ToolchainModel target) {
83          if (target == null) {
84              return null;
85          }
86          Supplier<ToolchainModel.Builder> creator = () -> ToolchainModel.newBuilder(target);
87          ToolchainModel.Builder builder = null;
88          builder = (ToolchainModel.Builder) transformToolchainModel_Type(creator, builder, target);
89          builder = (ToolchainModel.Builder) transformToolchainModel_Provides(creator, builder, target);
90          builder = (ToolchainModel.Builder) transformToolchainModel_Configuration(creator, builder, target);
91          return builder != null ? builder.build() : target;
92      }
93  
94      protected ToolchainModel.Builder transformToolchainModel_Type(Supplier<? extends ToolchainModel.Builder> creator, ToolchainModel.Builder builder, ToolchainModel target) {
95          String oldVal = target.getType();
96          String newVal = transform(oldVal);
97          return newVal != oldVal ? (builder != null ? builder : creator.get()).type(newVal) : builder;
98      }
99      protected ToolchainModel.Builder transformToolchainModel_Provides(Supplier<? extends ToolchainModel.Builder> creator, ToolchainModel.Builder builder, ToolchainModel target) {
100         Map<String, String> props = target.getProvides();
101         Map<String, String> newProps = null;
102         for (Map.Entry<String, String> entry : props.entrySet()) {
103             String newVal = transform(entry.getValue());
104             if (newVal != null && newVal != entry.getValue()) {
105                 if (newProps == null) {
106                     newProps = new HashMap<>();
107                     newProps.putAll(props);
108                     builder = builder != null ? builder : creator.get();
109                     builder.provides(newProps);
110                 }
111                 newProps.put(entry.getKey(), newVal);
112             }
113         }
114         return builder;
115     }
116     protected ToolchainModel.Builder transformToolchainModel_Configuration(Supplier<? extends ToolchainModel.Builder> creator, ToolchainModel.Builder builder, ToolchainModel target) {
117         XmlNode oldVal = target.getConfiguration();
118         XmlNode newVal = transform(oldVal);
119         return newVal != oldVal ? (builder != null ? builder : creator.get()).configuration(newVal) : builder;
120     }
121 
122     protected <T> List<T> transform(List<T> list, Function<T, T> transformer) {
123         List<T> newList = list;
124         for (int i = 0; i < list.size(); i++) {
125             T oldVal = list.get(i);
126             T newVal = transformer.apply(oldVal);
127             if (newVal != oldVal) {
128                 if (newList == list) {
129                     newList = new ArrayList<>(list);
130                 }
131                 newList.set(i, newVal);
132             }
133         }
134         return newList;
135     }
136 
137     protected <T> Map<String, T> transform(Map<String, T> map, Function<T, T> transformer) {
138         Map<String, T> newMap = map;
139         for (String key : map.keySet()) {
140             T oldVal = map.get(key);
141             T newVal = transformer.apply(oldVal);
142             if (newVal != oldVal) {
143                 if (newMap == map) {
144                     newMap = new HashMap<>(map);
145                 }
146                 newMap.put(key, newVal);
147             }
148         }
149         return newMap;
150     }
151 
152     protected XmlNode transform(XmlNode node) {
153         if (node != null) {
154             String oldValue = node.getValue();
155             String newValue = transform(oldValue);
156             Map<String, String> oldAttrs = node.getAttributes();
157             Map<String, String> newAttrs = transform(oldAttrs, this::transform);
158             List<XmlNode> oldChildren = node.getChildren();
159             List<XmlNode> newChildren = transform(oldChildren, this::transform);
160             if (oldValue != newValue || oldAttrs != newAttrs || oldChildren != newChildren) {
161                 return new XmlNodeImpl(node.getPrefix(), node.getNamespaceUri(), node.getName(),
162                         newValue, newAttrs, newChildren, node.getInputLocation());
163             }
164         }
165         return node;
166     }
167 }