View Javadoc

1   package org.apache.maven.model.converter;
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 org.apache.maven.model.Plugin;
23  
24  import java.util.Comparator;
25  
26  /**
27   * A comparator for <code>Plugin</code>s. It compares the plugins' groupIds and
28   * artifactIds.
29   *
30   * @author Dennis Lundberg
31   * @version $Id: PluginComparator.java 661727 2008-05-30 14:21:49Z bentmann $
32   */
33  public class PluginComparator
34      implements Comparator
35  {
36      public int compare( Object o1, Object o2 )
37      {
38          // Check for null objects
39          if ( o1 == null && o2 == null )
40          {
41              return 0;
42          }
43          if ( o1 == null )
44          {
45              return -1;
46          }
47          if ( o2 == null )
48          {
49              return 1;
50          }
51  
52          // Check classes
53          if ( !( o1 instanceof Plugin ) && !( o2 instanceof Plugin ) )
54          {
55              return 0;
56          }
57          if ( !( o1 instanceof Plugin ) )
58          {
59              return -1;
60          }
61          if ( !( o2 instanceof Plugin ) )
62          {
63              return 1;
64          }
65          Plugin plugin1 = (Plugin) o1;
66          Plugin plugin2 = (Plugin) o2;
67  
68          // Check for null values
69          if ( plugin1.getGroupId() == null && plugin2.getGroupId() == null )
70          {
71              return compareArtifactId( plugin1, plugin2 );
72          }
73          if ( plugin1.getGroupId() == null )
74          {
75              return -1;
76          }
77          if ( plugin2.getGroupId() == null )
78          {
79              return 1;
80          }
81  
82          // Compare values
83          int answer;
84          answer = plugin1.getGroupId().compareTo( plugin2.getGroupId() );
85          if ( answer == 0 )
86          {
87              answer = compareArtifactId( plugin1, plugin2 );
88          }
89  
90          return answer;
91      }
92  
93      private int compareArtifactId( Plugin plugin1, Plugin plugin2 )
94      {
95          if ( plugin1.getArtifactId() == null && plugin2.getArtifactId() == null )
96          {
97              return 0;
98          }
99          if ( plugin1.getArtifactId() == null )
100         {
101             return -1;
102         }
103         if ( plugin2.getArtifactId() == null )
104         {
105             return 1;
106         }
107         return plugin1.getArtifactId().compareTo( plugin2.getArtifactId() );
108     }
109 }