From 77378ac8805f1b581145156c89ccb9c8be0c86e3 Mon Sep 17 00:00:00 2001 From: ray Date: Wed, 7 May 2008 00:42:18 +0000 Subject: [PATCH] Added static compare() methods for all the primitive types, except where Sun has already done it for us. - We always return 1, 0, or -1. - We guard against overflows for ints and longs. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2300 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Comparators.java | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/java/com/samskivert/util/Comparators.java b/src/java/com/samskivert/util/Comparators.java index 036e8cfe..e08c275a 100644 --- a/src/java/com/samskivert/util/Comparators.java +++ b/src/java/com/samskivert/util/Comparators.java @@ -65,10 +65,60 @@ public class Comparators }; /** - * Compares two integers in an overflow safe manner. + * Compares two bytes, returning 1, 0, or -1. + * TODO: remove when Java finally has this method in Byte. + */ + public static int compare (byte value1, byte value2) + { + return (value1 < value2 ? -1 : (value1 == value2 ? 0 : 1)); + } + + /** + * Compares two chars, returning 1, 0, or -1. + * TODO: remove when Java finally has this method in Character. + */ + public static int compare (char value1, char value2) + { + return (value1 < value2 ? -1 : (value1 == value2 ? 0 : 1)); + } + + /** + * Compares two shorts, returning 1, 0, or -1. + * TODO: remove when Java finally has this method in Character. + */ + public static int compare (short value1, short value2) + { + return (value1 < value2 ? -1 : (value1 == value2 ? 0 : 1)); + } + + /** + * Compares two integers in an overflow safe manner, returning 1, 0, or -1. + * TODO: remove when Java finally has this method in Integer. */ public static int compare (int value1, int value2) { return (value1 < value2 ? -1 : (value1 == value2 ? 0 : 1)); } + + /** + * Compares two longs in an overflow safe manner, returning 1, 0, or -1. + * TODO: remove when Java finally has this method in Long. + */ + public static int compare (long value1, long value2) + { + return (value1 < value2 ? -1 : (value1 == value2 ? 0 : 1)); + } + + // Double.compare() exists + + // Float.compare() exists + + /** + * Compares two booleans, returning 1, 0, or -1. + * TODO: remove when Java finally has this method in Boolean. + */ + public static int compare (boolean value1, boolean value2) + { + return (value1 == value2) ? 0 : (value1 ? 1 : -1); + } }