// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;
import java.util.Comparator;
/**
* Superclass of hit comparators which delegates comparisons of hits which are
* equal according to this comparator, to a secondary comparator.
*
* @author Jon Bratseth
*/
public abstract class ChainableComparator implements Comparator {
private final Comparator secondaryComparator;
/** Creates this comparator, given a secondary comparator, or null if there is no secondary */
public ChainableComparator(Comparator secondaryComparator) {
this.secondaryComparator=secondaryComparator;
}
/** Returns the comparator to use to compare hits which are equal according to this, or null if none */
public Comparator getSecondaryComparator() { return secondaryComparator; }
/**
* Returns the comparison form the secondary comparison, or 0 if the secondary is null.
* When overriding this in the subclass, always return super.compare(first,second)
* at the end of the subclass' implementation.
*/
public int compare(Hit first,Hit second) {
if (secondaryComparator==null) return 0;
return secondaryComparator.compare(first,second);
}
}