Added convenience method for combining the output of multiple comparison

conditions.


git-svn-id: https://samskivert.googlecode.com/svn/trunk@2528 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
samskivert
2009-02-05 23:21:06 +00:00
parent 1316f48499
commit 44e6f04ed1
@@ -109,6 +109,24 @@ public class Comparators
return (value1 < value2 ? -1 : (value1 == value2 ? 0 : 1));
}
/**
* Returns the first non-zero value in the supplied list. This is useful for combining
* comparators:
* <pre>
* return Comparators.compare(name.compareTo(oname), Comparators.compare(price, oprice), ...);
* </pre>
* If all values in the array are zero, zero is returned.
*/
public static int combine (int ... values)
{
for (int value : values) {
if (value != 0) {
return value;
}
}
return 0;
}
// Double.compare() exists
// Float.compare() exists