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.Path;
22 import java.util.Collections;
23 import java.util.HashSet;
24 import java.util.Map;
25 import java.util.Objects;
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentHashMap;
28
29 import org.apache.maven.api.model.Model;
30
31
32
33
34
35
36 class ReactorModelPool {
37 private final Map<GAKey, Set<Model>> modelsByGa = new ConcurrentHashMap<>();
38
39 private final Map<Path, Model> modelsByPath = new ConcurrentHashMap<>();
40
41
42
43
44
45
46
47
48
49
50 public Model get(String groupId, String artifactId, String version) {
51 return modelsByGa.getOrDefault(new GAKey(groupId, artifactId), Collections.emptySet()).stream()
52 .filter(m -> version == null || version.equals(getVersion(m)))
53 .reduce((a, b) -> {
54 throw new IllegalStateException(
55 "Multiple modules with key " + a.getGroupId() + ':' + a.getArtifactId());
56 })
57 .orElse(null);
58 }
59
60 private String getVersion(Model model) {
61 String version = model.getVersion();
62 if (version == null && model.getParent() != null) {
63 version = model.getParent().getVersion();
64 }
65 return version;
66 }
67
68 void put(Path pomFile, Model model) {
69 if (pomFile != null) {
70 modelsByPath.put(pomFile, model);
71 }
72 modelsByGa
73 .computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<>())
74 .add(model);
75 }
76
77 private static String getGroupId(Model model) {
78 String groupId = model.getGroupId();
79 if (groupId == null && model.getParent() != null) {
80 groupId = model.getParent().getGroupId();
81 }
82 return groupId;
83 }
84
85 private static final class GAKey {
86
87 private final String groupId;
88
89 private final String artifactId;
90
91 private final int hashCode;
92
93 GAKey(String groupId, String artifactId) {
94 this.groupId = (groupId != null) ? groupId : "";
95 this.artifactId = (artifactId != null) ? artifactId : "";
96
97 hashCode = Objects.hash(this.groupId, this.artifactId);
98 }
99
100 @Override
101 public boolean equals(Object obj) {
102 if (this == obj) {
103 return true;
104 }
105 if (!(obj instanceof GAKey)) {
106 return false;
107 }
108 GAKey that = (GAKey) obj;
109 return artifactId.equals(that.artifactId) && groupId.equals(that.groupId);
110 }
111
112 @Override
113 public int hashCode() {
114 return hashCode;
115 }
116
117 @Override
118 public String toString() {
119 return groupId + ':' + artifactId;
120 }
121 }
122 }