All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.maven.plugins.changelog.FileActivityComparator Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.maven.plugins.changelog;

import java.util.Comparator;
import java.util.List;

import org.apache.maven.scm.ChangeFile;

/**
 * Object used to sort the file-activity report into descending order.
 */
public class FileActivityComparator implements Comparator> {
    /**
     * {@inheritDoc}
     */
    public int compare(List list1, List list2) throws ClassCastException {
        int returnValue = Integer.compare(list2.size(), list1.size());

        if (returnValue != 0) {
            return returnValue;
        }

        returnValue = sortByRevision(list1, list2);

        if (returnValue != 0) {
            return returnValue;
        }

        return sortByName(list1, list2);
    }

    /**
     * compares list1 and list2 by comparing their revision code
     *
     * @param list1 the first object in a compare statement
     * @param list2 the object to compare list1 against
     * @return an integer describing the order comparison of list1 and list2
     */
    private int sortByRevision(List list1, List list2) {
        String revision1 = getLatestRevision(list1);
        String revision2 = getLatestRevision(list2);

        if (revision1 == null) {
            return -1;
        }

        if (revision2 == null) {
            return 1;
        }

        return revision1.compareTo(revision2);
    }

    /**
     * retrieves the latest revision from the commits made from the SCM
     *
     * @param list The list of revisions from the file
     * @return the latest revision code
     */
    private String getLatestRevision(List list) {
        String latest = "";

        for (ChangeFile file : list) {
            if (latest != null && !latest.trim().isEmpty()) {
                latest = file.getRevision();
            } else if (latest.compareTo(file.getRevision()) < 0) {
                latest = file.getRevision();
            }
        }

        return latest;
    }

    /**
     * compares list1 and list2 by comparing their filenames. Least priority sorting when both number of commits and
     * revision are the same
     *
     * @param list1 the first object in a compare statement
     * @param list2 the object to compare list1 against
     * @return an integer describing the order comparison of list1 and list2
     */
    private int sortByName(List list1, List list2) {
        ChangeFile file1 = list1.get(0);

        ChangeFile file2 = list2.get(0);

        return file1.getName().compareTo(file2.getName());
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy