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.util.Map;
22  import java.util.concurrent.ConcurrentHashMap;
23  
24  import org.apache.maven.model.building.ModelCache;
25  
26  /**
27   * A simple model cache used to accelerate model building during a reactor build.
28   *
29   * @author Benjamin Bentmann
30   */
31  class ReactorModelCache implements ModelCache {
32  
33      private final Map<CacheKey, Object> models = new ConcurrentHashMap<>(256);
34  
35      public Object get(String groupId, String artifactId, String version, String tag) {
36          return models.get(new CacheKey(groupId, artifactId, version, tag));
37      }
38  
39      public void put(String groupId, String artifactId, String version, String tag, Object data) {
40          models.put(new CacheKey(groupId, artifactId, version, tag), data);
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 String tag;
52  
53          private final int hashCode;
54  
55          CacheKey(String groupId, String artifactId, String version, String tag) {
56              this.groupId = (groupId != null) ? groupId : "";
57              this.artifactId = (artifactId != null) ? artifactId : "";
58              this.version = (version != null) ? version : "";
59              this.tag = (tag != null) ? tag : "";
60  
61              int hash = 17;
62              hash = hash * 31 + this.groupId.hashCode();
63              hash = hash * 31 + this.artifactId.hashCode();
64              hash = hash * 31 + this.version.hashCode();
65              hash = hash * 31 + this.tag.hashCode();
66              hashCode = hash;
67          }
68  
69          @Override
70          public boolean equals(Object obj) {
71              if (this == obj) {
72                  return true;
73              }
74  
75              if (!(obj instanceof CacheKey)) {
76                  return false;
77              }
78  
79              CacheKey that = (CacheKey) obj;
80  
81              return artifactId.equals(that.artifactId)
82                      && groupId.equals(that.groupId)
83                      && version.equals(that.version)
84                      && tag.equals(that.tag);
85          }
86  
87          @Override
88          public int hashCode() {
89              return hashCode;
90          }
91      }
92  }