View Javadoc
1   package org.apache.maven.repository.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.model.building.ModelCache;
23  import org.eclipse.aether.RepositoryCache;
24  import org.eclipse.aether.RepositorySystemSession;
25  
26  /**
27   * A model builder cache backed by the repository system cache.
28   *
29   * @author Benjamin Bentmann
30   */
31  class DefaultModelCache
32      implements ModelCache
33  {
34  
35      private final RepositorySystemSession session;
36  
37      private final RepositoryCache cache;
38  
39      public static ModelCache newInstance( RepositorySystemSession session )
40      {
41          if ( session.getCache() == null )
42          {
43              return null;
44          }
45          else
46          {
47              return new DefaultModelCache( session );
48          }
49      }
50  
51      private DefaultModelCache( RepositorySystemSession session )
52      {
53          this.session = session;
54          this.cache = session.getCache();
55      }
56  
57      public Object get( String groupId, String artifactId, String version, String tag )
58      {
59          return cache.get( session, new Key( groupId, artifactId, version, tag ) );
60      }
61  
62      public void put( String groupId, String artifactId, String version, String tag, Object data )
63      {
64          cache.put( session, new Key( groupId, artifactId, version, tag ), data );
65      }
66  
67      static class Key
68      {
69  
70          private final String groupId;
71  
72          private final String artifactId;
73  
74          private final String version;
75  
76          private final String tag;
77  
78          private final int hash;
79  
80          Key( String groupId, String artifactId, String version, String tag )
81          {
82              this.groupId = groupId;
83              this.artifactId = artifactId;
84              this.version = version;
85              this.tag = tag;
86  
87              int h = 17;
88              h = h * 31 + this.groupId.hashCode();
89              h = h * 31 + this.artifactId.hashCode();
90              h = h * 31 + this.version.hashCode();
91              h = h * 31 + this.tag.hashCode();
92              hash = h;
93          }
94  
95          @Override
96          public boolean equals( Object obj )
97          {
98              if ( this == obj )
99              {
100                 return true;
101             }
102             if ( null == obj || !getClass().equals( obj.getClass() ) )
103             {
104                 return false;
105             }
106 
107             Key that = (Key) obj;
108             return artifactId.equals( that.artifactId ) && groupId.equals( that.groupId )
109                 && version.equals( that.version ) && tag.equals( that.tag );
110         }
111 
112         @Override
113         public int hashCode()
114         {
115             return hash;
116         }
117 
118     }
119 }