View Javadoc

1   package de.matthias_burbach.deputy.core.project;
2   
3   import java.util.Comparator;
4   
5   
6   /***
7    * Compares objects of type {@link ProjectQualifier}.<br/>
8    * Does not consider the group id but assumes that equal artifact ids imply
9    * equal group ids.
10   *
11   * @author Matthias Burbach
12   */
13  public class ProjectComparator implements Comparator {
14      /***
15       * Used to compare the versions of two projects having the same artifact id.
16       */
17      private VersionComparator versionComparator = new VersionComparator();
18  
19      /*
20       * (non-Javadoc)
21       * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
22       */
23      /***
24       * {@inheritDoc}
25       */
26      public int compare(final Object o1, final Object o2) {
27          ProjectQualifier project1 = (ProjectQualifier) o1;
28          ProjectQualifier project2 = (ProjectQualifier) o2;
29          /*
30           * Compare by artifact id
31           */
32          String artifactId1 = getArtifactId1(project1, project2);
33          String artifactId2 = getArtifactId2(project1, project2);
34          int result = 0;
35          if (artifactId1.startsWith("vrp-")
36                  && !artifactId2.startsWith("vrp-")) {
37              result = -1; //'vrp-'-prefixed names beat others
38          } else if (artifactId2.startsWith("vrp-")
39                      && !artifactId1.startsWith("vrp-")) {
40              result = 1; //'vrp-'-prefixed names beat others
41          } else {
42              result = artifactId1.compareTo(artifactId2);
43          }
44          if (result == 0) {
45              //artifact ids are equal, compare by version
46              result =
47                  versionComparator.compare(
48                      getVersion1(project1, project2),
49                      getVersion2(project1, project2));
50          }
51          return result;
52      }
53  
54      /***
55       * @param pq1 The project qualifier 1 to be compared with pq2.
56       * @param pq2 The project qualifier 2 to be compared with pq1
57       * @return The artifact id 1 to be used in comparisons.
58       */
59      protected String getArtifactId1(
60              final ProjectQualifier pq1, final ProjectQualifier pq2) {
61          return pq1.getArtifactId();
62      }
63  
64      /***
65       * @param pq1 The project qualifier 1 to be compared with pq2.
66       * @param pq2 The project qualifier 2 to be compared with pq1
67       * @return The artifact id 2 to be used in comparisons.
68       */
69      protected String getArtifactId2(
70              final ProjectQualifier pq1, final ProjectQualifier pq2) {
71          return pq2.getArtifactId();
72      }
73  
74      /***
75       * @param pq1 The project qualifier 1 to be compared with pq2.
76       * @param pq2 The project qualifier 2 to be compared with pq1
77       * @return The version 1 to be used in comparisons.
78       */
79      protected String getVersion1(
80              final ProjectQualifier pq1, final ProjectQualifier pq2) {
81          return pq1.getVersion();
82      }
83  
84      /***
85       * @param pq1 The project qualifier 1 to be compared with pq2.
86       * @param pq2 The project qualifier 2 to be compared with pq1
87       * @return The version 2 to be used in comparisons.
88       */
89      protected String getVersion2(
90              final ProjectQualifier pq1, final ProjectQualifier pq2) {
91          return pq2.getVersion();
92      }
93  }