mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-19 20:28:00 +00:00
verifyTransitivity on saComparator
This commit is contained in:
committed by
Michael Kamensky
parent
3ea93ac9b4
commit
fb159a6261
67
forge-core/src/main/java/forge/util/ComparatorUtil.java
Normal file
67
forge-core/src/main/java/forge/util/ComparatorUtil.java
Normal file
@@ -0,0 +1,67 @@
|
||||
package forge.util;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author Gili Tzabari
|
||||
*/
|
||||
public final class ComparatorUtil
|
||||
{
|
||||
/**
|
||||
* Verify that a comparator is transitive.
|
||||
*
|
||||
* @param <T> the type being compared
|
||||
* @param comparator the comparator to test
|
||||
* @param elements the elements to test against
|
||||
* @throws AssertionError if the comparator is not transitive
|
||||
*/
|
||||
public static <T> void verifyTransitivity(Comparator<T> comparator, Collection<T> elements)
|
||||
{
|
||||
for (T first: elements)
|
||||
{
|
||||
for (T second: elements)
|
||||
{
|
||||
int result1 = comparator.compare(first, second);
|
||||
int result2 = comparator.compare(second, first);
|
||||
if (result1 != -result2)
|
||||
{
|
||||
// Uncomment the following line to step through the failed case
|
||||
//comparator.compare(first, second);
|
||||
throw new AssertionError("compare(" + first + ", " + second + ") == " + result1 +
|
||||
" but swapping the parameters returns " + result2);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (T first: elements)
|
||||
{
|
||||
for (T second: elements)
|
||||
{
|
||||
int firstGreaterThanSecond = comparator.compare(first, second);
|
||||
if (firstGreaterThanSecond <= 0)
|
||||
continue;
|
||||
for (T third: elements)
|
||||
{
|
||||
int secondGreaterThanThird = comparator.compare(second, third);
|
||||
if (secondGreaterThanThird <= 0)
|
||||
continue;
|
||||
int firstGreaterThanThird = comparator.compare(first, third);
|
||||
if (firstGreaterThanThird <= 0)
|
||||
{
|
||||
// Uncomment the following line to step through the failed case
|
||||
//comparator.compare(first, third);
|
||||
throw new AssertionError("compare(" + first + ", " + second + ") > 0, " +
|
||||
"compare(" + second + ", " + third + ") > 0, but compare(" + first + ", " + third + ") == " +
|
||||
firstGreaterThanThird);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prevent construction.
|
||||
*/
|
||||
private ComparatorUtil()
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user