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      @Override
50      public Object get(String groupId, String artifactId, String version, String tag) {
51          return cache.get(session, new Key(groupId, artifactId, version, tag));
52      }
53  
54      @Override
55      public void put(String groupId, String artifactId, String version, String tag, Object data) {
56          cache.put(session, new Key(groupId, artifactId, version, tag), data);
57      }
58  
59      static class Key {
60  
61          private final String groupId;
62  
63          private final String artifactId;
64  
65          private final String version;
66  
67          private final String tag;
68  
69          private final int hash;
70  
71          Key(String groupId, String artifactId, String version, String tag) {
72              this.groupId = groupId;
73              this.artifactId = artifactId;
74              this.version = version;
75              this.tag = tag;
76  
77              int h = 17;
78              h = h * 31 + this.groupId.hashCode();
79              h = h * 31 + this.artifactId.hashCode();
80              h = h * 31 + this.version.hashCode();
81              h = h * 31 + this.tag.hashCode();
82              hash = h;
83          }
84  
85          @Override
86          public boolean equals(Object obj) {
87              if (this == obj) {
88                  return true;
89              }
90              if (null == obj || !getClass().equals(obj.getClass())) {
91                  return false;
92              }
93  
94              Key that = (Key) obj;
95              return artifactId.equals(that.artifactId)
96                      && groupId.equals(that.groupId)
97                      && version.equals(that.version)
98                      && tag.equals(that.tag);
99          }
100 
101         @Override
102         public int hashCode() {
103             return hash;
104         }
105     }
106 }