1 package org.apache.maven.plugin.changelog;
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.scm.ChangeFile;
23
24 import java.util.Comparator;
25 import java.util.Iterator;
26 import java.util.List;
27
28
29 /**
30 * Object used to sort the file-activity report into descending order.
31 *
32 * @version $Id: FileActivityComparator.java 803814 2009-08-13 09:24:45Z vsiveton $
33 */
34 public class FileActivityComparator
35 implements Comparator
36 {
37 /** {@inheritDoc} */
38 public int compare( Object o1, Object o2 )
39 throws ClassCastException
40 {
41 int returnValue;
42
43 List list1 = (List) o1;
44
45 List list2 = (List) o2;
46
47 returnValue = sortByCommits( list1, list2 );
48
49 if ( returnValue != 0 )
50 {
51 return returnValue;
52 }
53
54 returnValue = sortByRevision( list1, list2 );
55
56 if ( returnValue != 0 )
57 {
58 return returnValue;
59 }
60
61 returnValue = sortByName( list1, list2 );
62
63 return returnValue;
64 }
65
66 /**
67 * compares list1 and list2 by the number of commits
68 *
69 * @param list1 the first object in a compare statement
70 * @param list2 the object to compare list1 against
71 * @return an integer describing the order comparison of list1 and list2
72 */
73 private int sortByCommits( List list1, List list2 )
74 {
75 if ( list1.size() > list2.size() )
76 {
77 return -1;
78 }
79
80 if ( list1.size() < list2.size() )
81 {
82 return 1;
83 }
84
85 return 0;
86 }
87
88 /**
89 * compares list1 and list2 by comparing their revision code
90 *
91 * @param list1 the first object in a compare statement
92 * @param list2 the object to compare list1 against
93 * @return an integer describing the order comparison of list1 and list2
94 */
95 private int sortByRevision( List list1, List list2 )
96 {
97 String revision1 = getLatestRevision( list1 );
98
99 String revision2 = getLatestRevision( list2 );
100
101 return revision1.compareTo( revision2 );
102 }
103
104 /**
105 * retrieves the latest revision from the commits made from the SCM
106 *
107 * @param list The list of revisions from the file
108 * @return the latest revision code
109 */
110 private String getLatestRevision( List list )
111 {
112 String latest = "";
113
114 for ( Iterator i = list.iterator(); i.hasNext(); )
115 {
116 ChangeFile file = (ChangeFile) i.next();
117
118 if ( latest.length() == 0 )
119 {
120 latest = file.getRevision();
121 }
122 else if ( latest.compareTo( file.getRevision() ) < 0 )
123 {
124 latest = file.getRevision();
125 }
126 }
127
128 return latest;
129 }
130
131 /**
132 * compares list1 and list2 by comparing their filenames. Least priority sorting when both number of commits and
133 * and revision are the same
134 *
135 * @param list1 the first object in a compare statement
136 * @param list2 the object to compare list1 against
137 * @return an integer describing the order comparison of list1 and list2
138 */
139 private int sortByName( List list1, List list2 )
140 {
141 ChangeFile file1 = (ChangeFile) list1.get( 0 );
142
143 ChangeFile file2 = (ChangeFile) list2.get( 0 );
144
145 return file1.getName().compareTo( file2.getName() );
146 }
147 }