Coverage Report - org.apache.maven.changelog.FileActivityComparator
 
Classes in this File Line Coverage Branch Coverage Complexity
FileActivityComparator
100% 
100% 
3,2
 
 1  
 package org.apache.maven.changelog;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2006 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * 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, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.apache.maven.scm.ChangeFile;
 20  
 
 21  
 import java.util.Comparator;
 22  
 import java.util.Iterator;
 23  
 import java.util.List;
 24  
 
 25  
 
 26  
 /**
 27  
  * Object used to sort the file-activity report into descending order
 28  
  */
 29  25
 public class FileActivityComparator
 30  
     implements Comparator
 31  
 {
 32  
     /**
 33  
      * @see java.util.Comparator#compare(Object,Object)
 34  
      */
 35  
     public int compare( Object o1, Object o2 )
 36  
         throws ClassCastException
 37  
     {
 38  
         int returnValue;
 39  
 
 40  40
         List list1 = (List) o1;
 41  
 
 42  40
         List list2 = (List) o2;
 43  
 
 44  40
         returnValue = sortByCommits( list1, list2 );
 45  
 
 46  40
         if ( returnValue != 0 )
 47  
         {
 48  20
             return returnValue;
 49  
         }
 50  
 
 51  20
         returnValue = sortByRevision( list1, list2 );
 52  
 
 53  20
         if ( returnValue != 0 )
 54  
         {
 55  15
             return returnValue;
 56  
         }
 57  
 
 58  5
         returnValue = sortByName( list1, list2 );
 59  
 
 60  5
         return returnValue;
 61  
     }
 62  
 
 63  
     /**
 64  
      * compares list1 and list2 by the number of commits
 65  
      *
 66  
      * @param list1 the first object in a compare statement
 67  
      * @param list2 the object to compare list1 against
 68  
      * @return an integer describing the order comparison of list1 and list2
 69  
      */
 70  
     private int sortByCommits( List list1, List list2 )
 71  
     {
 72  40
         if ( list1.size() > list2.size() )
 73  
         {
 74  15
             return -1;
 75  
         }
 76  
 
 77  25
         if ( list1.size() < list2.size() )
 78  
         {
 79  5
             return 1;
 80  
         }
 81  
 
 82  20
         return 0;
 83  
     }
 84  
 
 85  
     /**
 86  
      * compares list1 and list2 by comparing their revision code
 87  
      *
 88  
      * @param list1 the first object in a compare statement
 89  
      * @param list2 the object to compare list1 against
 90  
      * @return an integer describing the order comparison of list1 and list2
 91  
      */
 92  
     private int sortByRevision( List list1, List list2 )
 93  
     {
 94  20
         String revision1 = getLatestRevision( list1 );
 95  
 
 96  20
         String revision2 = getLatestRevision( list2 );
 97  
 
 98  20
         return revision1.compareTo( revision2 );
 99  
     }
 100  
 
 101  
     /**
 102  
      * retrieves the latest revision from the commits made from the SCM
 103  
      *
 104  
      * @param list The list of revisions from the file
 105  
      * @return the latest revision code
 106  
      */
 107  
     private String getLatestRevision( List list )
 108  
     {
 109  40
         String latest = "";
 110  
 
 111  40
         for ( Iterator i = list.iterator(); i.hasNext(); )
 112  
         {
 113  80
             ChangeFile file = (ChangeFile) i.next();
 114  
 
 115  80
             if ( latest.length() == 0 )
 116  
             {
 117  40
                 latest = file.getRevision();
 118  
             }
 119  40
             else if ( latest.compareTo( file.getRevision() ) < 0 )
 120  
             {
 121  40
                 latest = file.getRevision();
 122  
             }
 123  
         }
 124  
 
 125  40
         return latest;
 126  
     }
 127  
 
 128  
     /**
 129  
      * compares list1 and list2 by comparing their filenames. Least priority sorting when both number of commits and
 130  
      * and revision are the same
 131  
      *
 132  
      * @param list1 the first object in a compare statement
 133  
      * @param list2 the object to compare list1 against
 134  
      * @return an integer describing the order comparison of list1 and list2
 135  
      */
 136  
     private int sortByName( List list1, List list2 )
 137  
     {
 138  5
         ChangeFile file1 = (ChangeFile) list1.get( 0 );
 139  
 
 140  5
         ChangeFile file2 = (ChangeFile) list2.get( 0 );
 141  
 
 142  5
         return file1.getName().compareTo( file2.getName() );
 143  
     }
 144  
 }