View Javadoc
1   package org.apache.maven.plugin;
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.Iterator;
23  import java.util.List;
24  
25  import org.apache.maven.model.Dependency;
26  import org.apache.maven.model.Exclusion;
27  import org.apache.maven.model.Plugin;
28  
29  /**
30   * @author Benjamin Bentmann
31   */
32  class CacheUtils
33  {
34  
35      public static <T> boolean eq( T s1, T s2 )
36      {
37          return s1 != null ? s1.equals( s2 ) : s2 == null;
38      }
39  
40      public static int hash( Object obj )
41      {
42          return obj != null ? obj.hashCode() : 0;
43      }
44  
45      public static int pluginHashCode( Plugin plugin )
46      {
47          int hash = 17;
48  
49          hash = hash * 31 + hash( plugin.getGroupId() );
50          hash = hash * 31 + hash( plugin.getArtifactId() );
51          hash = hash * 31 + hash( plugin.getVersion() );
52  
53          hash = hash * 31 + ( plugin.isExtensions() ? 1 : 0 );
54  
55          for ( Dependency dependency : plugin.getDependencies() )
56          {
57              hash = hash * 31 + hash( dependency.getGroupId() );
58              hash = hash * 31 + hash( dependency.getArtifactId() );
59              hash = hash * 31 + hash( dependency.getVersion() );
60              hash = hash * 31 + hash( dependency.getType() );
61              hash = hash * 31 + hash( dependency.getClassifier() );
62              hash = hash * 31 + hash( dependency.getScope() );
63  
64              for ( Exclusion exclusion : dependency.getExclusions() )
65              {
66                  hash = hash * 31 + hash( exclusion.getGroupId() );
67                  hash = hash * 31 + hash( exclusion.getArtifactId() );
68              }
69          }
70  
71          return hash;
72      }
73  
74      public static boolean pluginEquals( Plugin a, Plugin b )
75      {
76          return eq( a.getArtifactId(), b.getArtifactId() ) //
77              && eq( a.getGroupId(), b.getGroupId() ) //
78              && eq( a.getVersion(), b.getVersion() ) //
79              && a.isExtensions() == b.isExtensions() //
80              && dependenciesEquals( a.getDependencies(), b.getDependencies() );
81      }
82  
83      private static boolean dependenciesEquals( List<Dependency> a, List<Dependency> b )
84      {
85          if ( a.size() != b.size() )
86          {
87              return false;
88          }
89  
90          Iterator<Dependency> aI = a.iterator();
91          Iterator<Dependency> bI = b.iterator();
92  
93          while ( aI.hasNext() )
94          {
95              Dependency aD = aI.next();
96              Dependency bD = bI.next();
97  
98              boolean r = eq( aD.getGroupId(), bD.getGroupId() ) //
99                  && eq( aD.getArtifactId(), bD.getArtifactId() ) //
100                 && eq( aD.getVersion(), bD.getVersion() ) //
101                 && eq( aD.getType(), bD.getType() ) //
102                 && eq( aD.getClassifier(), bD.getClassifier() ) //
103                 && eq( aD.getScope(), bD.getScope() );
104 
105             r &= exclusionsEquals( aD.getExclusions(), bD.getExclusions() );
106 
107             if ( !r )
108             {
109                 return false;
110             }
111         }
112 
113         return true;
114     }
115 
116     private static boolean exclusionsEquals( List<Exclusion> a, List<Exclusion> b )
117     {
118         if ( a.size() != b.size() )
119         {
120             return false;
121         }
122 
123         Iterator<Exclusion> aI = a.iterator();
124         Iterator<Exclusion> bI = b.iterator();
125 
126         while ( aI.hasNext() )
127         {
128             Exclusion aD = aI.next();
129             Exclusion bD = bI.next();
130 
131             boolean r = eq( aD.getGroupId(), bD.getGroupId() ) //
132                 && eq( aD.getArtifactId(), bD.getArtifactId() );
133 
134             if ( !r )
135             {
136                 return false;
137             }
138         }
139 
140         return true;
141     }
142 
143 }