From 5a9b7251f150c2125ef04bfc80d2b0b5fd6f273a Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 5 Jan 2007 21:51:02 +0000 Subject: [PATCH] Added overflow safe integer comparison function. Widened. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2026 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/util/Comparators.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/java/com/samskivert/util/Comparators.java b/src/java/com/samskivert/util/Comparators.java index 5fb89e73..b2c5552d 100644 --- a/src/java/com/samskivert/util/Comparators.java +++ b/src/java/com/samskivert/util/Comparators.java @@ -28,11 +28,9 @@ import java.util.Comparator; public class Comparators { /** - * A comparator that compares the toString() value of all objects - * case insensitively. + * A comparator that compares the toString() value of all objects case insensitively. */ - public static final Comparator LEXICAL_CASE_INSENSITIVE = - new Comparator() { + public static final Comparator LEXICAL_CASE_INSENSITIVE = new Comparator() { public int compare (Object o1, Object o2) { if (o1 == o2) { // catches null == null @@ -51,8 +49,7 @@ public class Comparators /** * A comparator that compares {@link Comparable} instances. */ - public static final Comparator COMPARABLE = - new Comparator() { + public static final Comparator COMPARABLE = new Comparator() { @SuppressWarnings("unchecked") public int compare (Object o1, Object o2) { @@ -66,4 +63,12 @@ public class Comparators return ((Comparable)o1).compareTo(o2); // null-free } }; + + /** + * Compares two integers in an overflow safe manner. + */ + public static int compareTo (int value1, int value2) + { + return (value1 < value2 ? -1 : (value1 == value2 ? 0 : 1)); + } }