package gw.bizrules.util
uses gw.bizrules.diff.RuleMatchableDiffPair
/**
* Utility methods for comparing, matching, and sorting of diff items.
*/
class DiffItemUtils {
/**
* Compares p1 to p2, returning true if and only if p1 is less
* than p2.
*
* The comparison will use the order() value from the left side of the diff pair, if available, and the
* order() value from the right side of the diff pair otherwise (or in order to break a tie).
*
* The final sorted list should therefore be in an order consistent with both the left and right side of the diff
* pair, if possible, or at least with the left side if there is an inconsistency in ordering.
*
* Note: This method assumes the data that will be fed in has a strict ordering, i.e. no two elements are
* considered "equals" with respect to this ordering.
*
* @param p1 First diff item for comparison
* @param p2 Second diff item for comparison
* @param order Block which if given either p1 or p2 returns an integer specifying the order
* @param Type of element being compared in each diff item
* @return true if and only if p1 is less than p2
*/
public static function isBefore(p1: RuleMatchableDiffPair, p2: RuleMatchableDiffPair, order(T) : int) : boolean {
// We rely on the behavior in the diff pair constructor, i.e. left and right cannot both be null
var pos1 = p1.Left != null ? order(p1.Left) : order(p1.Right)
var pos2 = p2.Left != null ? order(p2.Left) : order(p2.Right)
// Try to break ties by favoring right instead
if (pos1 == pos2) {
pos1 = p1.Right != null ? order(p1.Right) : order(p1.Left)
pos2 = p2.Right != null ? order(p2.Right) : order(p2.Left)
// Still failed to break tie; sort "removes" earlier
if (pos1 == pos2) {
return p1.Left != null
}
}
return pos1 < pos2
}
}