From 44e6f04ed1539b1f50314b2adf9ed60de549af72 Mon Sep 17 00:00:00 2001 From: samskivert Date: Thu, 5 Feb 2009 23:21:06 +0000 Subject: [PATCH] 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 --- src/java/com/samskivert/util/Comparators.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/java/com/samskivert/util/Comparators.java b/src/java/com/samskivert/util/Comparators.java index 44ca4a47..eb59ddb0 100644 --- a/src/java/com/samskivert/util/Comparators.java +++ b/src/java/com/samskivert/util/Comparators.java @@ -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: + *
+     * return Comparators.compare(name.compareTo(oname), Comparators.compare(price, oprice), ...);
+     * 
+ * 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