Added overflow safe integer comparison function. Widened.
git-svn-id: https://samskivert.googlecode.com/svn/trunk@2026 6335cc39-0255-0410-8fd6-9bcaacd3b74c
This commit is contained in:
@@ -28,11 +28,9 @@ import java.util.Comparator;
|
|||||||
public class Comparators
|
public class Comparators
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* A comparator that compares the toString() value of all objects
|
* A comparator that compares the toString() value of all objects case insensitively.
|
||||||
* case insensitively.
|
|
||||||
*/
|
*/
|
||||||
public static final Comparator<Object> LEXICAL_CASE_INSENSITIVE =
|
public static final Comparator<Object> LEXICAL_CASE_INSENSITIVE = new Comparator<Object>() {
|
||||||
new Comparator<Object>() {
|
|
||||||
public int compare (Object o1, Object o2)
|
public int compare (Object o1, Object o2)
|
||||||
{
|
{
|
||||||
if (o1 == o2) { // catches null == null
|
if (o1 == o2) { // catches null == null
|
||||||
@@ -51,8 +49,7 @@ public class Comparators
|
|||||||
/**
|
/**
|
||||||
* A comparator that compares {@link Comparable} instances.
|
* A comparator that compares {@link Comparable} instances.
|
||||||
*/
|
*/
|
||||||
public static final Comparator<Object> COMPARABLE =
|
public static final Comparator<Object> COMPARABLE = new Comparator<Object>() {
|
||||||
new Comparator<Object>() {
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public int compare (Object o1, Object o2)
|
public int compare (Object o1, Object o2)
|
||||||
{
|
{
|
||||||
@@ -66,4 +63,12 @@ public class Comparators
|
|||||||
return ((Comparable<Object>)o1).compareTo(o2); // null-free
|
return ((Comparable<Object>)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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user