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