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

de.malkusch.whoisServerList.publicSuffixList.rule.RuleComparator Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package de.malkusch.whoisServerList.publicSuffixList.rule;

import java.io.Serializable;
import java.util.Comparator;

import javax.annotation.concurrent.Immutable;

/**
 * Orders prevailing rules higher.
 *
 * The rule with the highest {@link Rule#getLabelCount()} is the
 * prevailing rule. An exception rule is always the prevailing rule.
 *
 * @author [email protected]
 *
 * @see Rule#isExceptionRule()
 * @see Rule#getLabelCount()
 * @see Donations
 */
@Immutable
public final class RuleComparator implements Comparator, Serializable {

    /**
     * Version number.
     *
     * @see Serializable
     */
    private static final long serialVersionUID = -3222683638595906734L;

    @Override
    public int compare(final Rule rule1, final Rule rule2) {
        if (rule1.isExceptionRule() && rule2.isExceptionRule()) {
            if (!rule1.equals(rule2)) {
                throw new IllegalArgumentException(
                    "You can't compare two exception rules.");

            }
            return 0;

        }
        if (rule1.isExceptionRule()) {
            return 1;

        }
        if (rule2.isExceptionRule()) {
            return -1;

        }
        return Integer.compare(rule1.getLabelCount(), rule2.getLabelCount());
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy