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