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