1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.project;
20
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Objects;
28 import java.util.Set;
29 import org.apache.maven.model.Model;
30
31
32
33
34
35
36
37
38 class ReactorModelPool {
39 private final Map<GAKey, Set<Model>> modelsByGa = new HashMap<>();
40
41 private final Map<Path, Model> modelsByPath = new HashMap<>();
42
43
44
45
46
47
48
49
50
51
52 public Model get(String groupId, String artifactId, String version) {
53 return modelsByGa.getOrDefault(new GAKey(groupId, artifactId), Collections.emptySet()).stream()
54 .filter(m -> version == null || version.equals(getVersion(m)))
55 .reduce((a, b) -> {
56 throw new IllegalStateException(
57 "Multiple modules with key " + a.getGroupId() + ':' + a.getArtifactId());
58 })
59 .orElse(null);
60 }
61
62
63
64
65
66
67
68
69 public Model get(Path path) {
70 final Path pomFile;
71 if (Files.isDirectory(path)) {
72 pomFile = path.resolve("pom.xml");
73 } else {
74 pomFile = path;
75 }
76 return modelsByPath.get(pomFile);
77 }
78
79 private String getVersion(Model model) {
80 String version = model.getVersion();
81 if (version == null && model.getParent() != null) {
82 version = model.getParent().getVersion();
83 }
84 return version;
85 }
86
87 static class Builder {
88 private ReactorModelPool pool = new ReactorModelPool();
89
90 Builder put(Path pomFile, Model model) {
91 pool.modelsByPath.put(pomFile, model);
92 pool.modelsByGa
93 .computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<Model>())
94 .add(model);
95 return this;
96 }
97
98 ReactorModelPool build() {
99 return pool;
100 }
101
102 private static String getGroupId(Model model) {
103 String groupId = model.getGroupId();
104 if (groupId == null && model.getParent() != null) {
105 groupId = model.getParent().getGroupId();
106 }
107 return groupId;
108 }
109 }
110
111 private static final class GAKey {
112
113 private final String groupId;
114
115 private final String artifactId;
116
117 private final int hashCode;
118
119 GAKey(String groupId, String artifactId) {
120 this.groupId = (groupId != null) ? groupId : "";
121 this.artifactId = (artifactId != null) ? artifactId : "";
122
123 hashCode = Objects.hash(this.groupId, this.artifactId);
124 }
125
126 @Override
127 public boolean equals(Object obj) {
128 if (this == obj) {
129 return true;
130 }
131
132 GAKey that = (GAKey) obj;
133
134 return artifactId.equals(that.artifactId) && groupId.equals(that.groupId);
135 }
136
137 @Override
138 public int hashCode() {
139 return hashCode;
140 }
141
142 @Override
143 public String toString() {
144 StringBuilder buffer = new StringBuilder(128);
145 buffer.append(groupId).append(':').append(artifactId);
146 return buffer.toString();
147 }
148 }
149 }