1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.toolchain.merge;
20
21 import java.util.ArrayList;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25 import org.apache.maven.api.toolchain.PersistedToolchains;
26 import org.apache.maven.api.toolchain.ToolchainModel;
27 import org.apache.maven.api.xml.Dom;
28
29
30
31
32
33
34 public class MavenToolchainMerger {
35
36 public PersistedToolchains merge(
37 PersistedToolchains dominant, PersistedToolchains recessive, String recessiveSourceLevel) {
38 if (dominant == null || recessive == null) {
39 return dominant;
40 }
41
42 recessive.setSourceLevel(recessiveSourceLevel);
43
44 return shallowMerge(dominant.getToolchains(), recessive.getToolchains(), recessiveSourceLevel);
45 }
46
47 private PersistedToolchains shallowMerge(
48 List<ToolchainModel> dominant, List<ToolchainModel> recessive, String recessiveSourceLevel) {
49 Map<Object, ToolchainModel> merged = new LinkedHashMap<>();
50
51 for (ToolchainModel dominantModel : dominant) {
52 Object key = getToolchainModelKey(dominantModel);
53
54 merged.put(key, dominantModel);
55 }
56
57 for (ToolchainModel recessiveModel : recessive) {
58 Object key = getToolchainModelKey(recessiveModel);
59
60 ToolchainModel dominantModel = merged.get(key);
61 if (dominantModel == null) {
62 recessiveModel.setSourceLevel(recessiveSourceLevel);
63 merged.put(key, recessiveModel);
64 } else {
65 merged.put(key, mergeToolchainModelConfiguration(dominantModel, recessiveModel));
66 }
67 }
68 return PersistedToolchains.newBuilder()
69 .toolchains(new ArrayList<>(merged.values()))
70 .build();
71 }
72
73 protected ToolchainModel mergeToolchainModelConfiguration(ToolchainModel target, ToolchainModel source) {
74 Dom src = source.getConfiguration();
75 Dom tgt = target.getConfiguration();
76 Dom merged = Dom.merge(tgt, src);
77 return target.withConfiguration(merged);
78 }
79
80 protected Object getToolchainModelKey(ToolchainModel model) {
81 return model;
82 }
83 }