Package com.globalmentor.java
Class Comparables
- java.lang.Object
-
- com.globalmentor.java.Comparables
-
public class Comparables extends java.lang.Object
Utilities for working with comparables.- Author:
- Garret Wilson
- See Also:
Comparable
-
-
Constructor Summary
Constructors Constructor Description Comparables()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T extends java.lang.Comparable<? super T>>
intcompare(T comparable1, T comparable2)
Compares two comparables for order in ascending order.static <T extends java.lang.Comparable<? super T>>
intcompare(T comparable1, T comparable2, SortOrder sortOrder)
Compares two comparables for order using the specified sort order.
-
-
-
Method Detail
-
compare
public static <T extends java.lang.Comparable<? super T>> int compare(T comparable1, T comparable2)
Compares two comparables for order in ascending order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical comparables are always considered equal. This method functions exactly as if the two comparables were compared using theirComparable.compareTo(Object)
argument, except:- Identical comparables are recognized as such without delegating to the actual
Comparable.compareTo(Object)
method. - This method allows
null
arguments, considering anull
comparable to be lower than a non-null
comparable.
Comparator.compare(Object, Object)
, except that this method allowsnull
arguments.- Type Parameters:
T
- the type of comparables being compared.- Parameters:
comparable1
- The first comparable to be compared, ornull
if the comparable is not available.comparable2
- The second comparable to be compared, ornull
if the comparable is not available.- Returns:
- A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
- Throws:
java.lang.ClassCastException
- if the arguments' types prevent them from being compared.- See Also:
Comparable.compareTo(Object)
- Identical comparables are recognized as such without delegating to the actual
-
compare
public static <T extends java.lang.Comparable<? super T>> int compare(T comparable1, T comparable2, SortOrder sortOrder)
Compares two comparables for order using the specified sort order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Identical comparables are always considered equal. This method functions exactly as if the two comparables were compared using theirComparable.compareTo(Object)
argument, except:- Identical comparables are recognized as such without delegating to the actual
Comparable.compareTo(Object)
method. - This method allows
null
arguments, considering anull
comparable to be lower than a non-null
comparable.
- Type Parameters:
T
- the type of comparables being compared.- Parameters:
comparable1
- The first comparable to be compared, ornull
if the comparable is not available.comparable2
- The second comparable to be compared, ornull
if the comparable is not available.sortOrder
- The order in which to perform comparisons.- Returns:
- A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
- Throws:
java.lang.NullPointerException
- if the given sort order isnull
.java.lang.ClassCastException
- if the arguments' types prevent them from being compared.- See Also:
Comparable.compareTo(Object)
- Identical comparables are recognized as such without delegating to the actual
-
-