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.repository.internal;
20  
21  import org.apache.maven.model.building.ModelCache;
22  import org.eclipse.aether.RepositoryCache;
23  import org.eclipse.aether.RepositorySystemSession;
24  
25  /**
26   * A model builder cache backed by the repository system cache.
27   *
28   * @author Benjamin Bentmann
29   */
30  class DefaultModelCache implements ModelCache {
31  
32      private final RepositorySystemSession session;
33  
34      private final RepositoryCache cache;
35  
36      public static ModelCache newInstance(RepositorySystemSession session) {
37          if (session.getCache() == null) {
38              return null;
39          } else {
40              return new DefaultModelCache(session);
41          }
42      }
43  
44      private DefaultModelCache(RepositorySystemSession session) {
45          this.session = session;
46          this.cache = session.getCache();
47      }
48  
49      public Object get(String groupId, String artifactId, String version, String tag) {
50          return cache.get(session, new Key(groupId, artifactId, version, tag));
51      }
52  
53      public void put(String groupId, String artifactId, String version, String tag, Object data) {
54          cache.put(session, new Key(groupId, artifactId, version, tag), data);
55      }
56  
57      static class Key {
58  
59          private final String groupId;
60  
61          private final String artifactId;
62  
63          private final String version;
64  
65          private final String tag;
66  
67          private final int hash;
68  
69          Key(String groupId, String artifactId, String version, String tag) {
70              this.groupId = groupId;
71              this.artifactId = artifactId;
72              this.version = version;
73              this.tag = tag;
74  
75              int h = 17;
76              h = h * 31 + this.groupId.hashCode();
77              h = h * 31 + this.artifactId.hashCode();
78              h = h * 31 + this.version.hashCode();
79              h = h * 31 + this.tag.hashCode();
80              hash = h;
81          }
82  
83          @Override
84          public boolean equals(Object obj) {
85              if (this == obj) {
86                  return true;
87              }
88              if (null == obj || !getClass().equals(obj.getClass())) {
89                  return false;
90              }
91  
92              Key that = (Key) obj;
93              return artifactId.equals(that.artifactId)
94                      && groupId.equals(that.groupId)
95                      && version.equals(that.version)
96                      && tag.equals(that.tag);
97          }
98  
99          @Override
100         public int hashCode() {
101             return hash;
102         }
103     }
104 }