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