View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import java.io.File;
22  import java.util.HashMap;
23  import java.util.Map;
24  
25  /**
26   * Holds all POM files that are known to the reactor. This allows the project builder to resolve imported POMs from the
27   * reactor when building another project's effective model.
28   *
29   * @author Benjamin Bentmann
30   */
31  class ReactorModelPool {
32  
33      private final Map<CacheKey, File> pomFiles = new HashMap<>();
34  
35      public File get(String groupId, String artifactId, String version) {
36          return pomFiles.get(new CacheKey(groupId, artifactId, version));
37      }
38  
39      public void put(String groupId, String artifactId, String version, File pomFile) {
40          pomFiles.put(new CacheKey(groupId, artifactId, version), pomFile);
41      }
42  
43      private static final class CacheKey {
44  
45          private final String groupId;
46  
47          private final String artifactId;
48  
49          private final String version;
50  
51          private final int hashCode;
52  
53          CacheKey(String groupId, String artifactId, String version) {
54              this.groupId = (groupId != null) ? groupId : "";
55              this.artifactId = (artifactId != null) ? artifactId : "";
56              this.version = (version != null) ? version : "";
57  
58              int hash = 17;
59              hash = hash * 31 + this.groupId.hashCode();
60              hash = hash * 31 + this.artifactId.hashCode();
61              hash = hash * 31 + this.version.hashCode();
62              hashCode = hash;
63          }
64  
65          @Override
66          public boolean equals(Object obj) {
67              if (this == obj) {
68                  return true;
69              }
70  
71              if (!(obj instanceof CacheKey)) {
72                  return false;
73              }
74  
75              CacheKey that = (CacheKey) obj;
76  
77              return artifactId.equals(that.artifactId) && groupId.equals(that.groupId) && version.equals(that.version);
78          }
79  
80          @Override
81          public int hashCode() {
82              return hashCode;
83          }
84  
85          @Override
86          public String toString() {
87              StringBuilder buffer = new StringBuilder(128);
88              buffer.append(groupId).append(':').append(artifactId).append(':').append(version);
89              return buffer.toString();
90          }
91      }
92  }