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.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          modelsByPath.put(pomFile, model);
70          modelsByGa
71                  .computeIfAbsent(new GAKey(getGroupId(model), model.getArtifactId()), k -> new HashSet<>())
72                  .add(model);
73      }
74  
75      private static String getGroupId(Model model) {
76          String groupId = model.getGroupId();
77          if (groupId == null && model.getParent() != null) {
78              groupId = model.getParent().getGroupId();
79          }
80          return groupId;
81      }
82  
83      private static final class GAKey {
84  
85          private final String groupId;
86  
87          private final String artifactId;
88  
89          private final int hashCode;
90  
91          GAKey(String groupId, String artifactId) {
92              this.groupId = (groupId != null) ? groupId : "";
93              this.artifactId = (artifactId != null) ? artifactId : "";
94  
95              hashCode = Objects.hash(this.groupId, this.artifactId);
96          }
97  
98          @Override
99          public boolean equals(Object obj) {
100             if (this == obj) {
101                 return true;
102             }
103             if (!(obj instanceof GAKey)) {
104                 return false;
105             }
106             GAKey that = (GAKey) obj;
107             return artifactId.equals(that.artifactId) && groupId.equals(that.groupId);
108         }
109 
110         @Override
111         public int hashCode() {
112             return hashCode;
113         }
114 
115         @Override
116         public String toString() {
117             return groupId + ':' + artifactId;
118         }
119     }
120 }