Class 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>>
      int
      compare​(T comparable1, T comparable2)
      Compares two comparables for order in ascending order.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Comparables

        public Comparables()
    • 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 their Comparable.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 a null comparable to be lower than a non-null comparable.
        This method matches the semantics of Comparator.compare(Object, Object), except that this method allows null arguments.
        Type Parameters:
        T - the type of comparables being compared.
        Parameters:
        comparable1 - The first comparable to be compared, or null if the comparable is not available.
        comparable2 - The second comparable to be compared, or null 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)
      • 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 their Comparable.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 a null 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, or null if the comparable is not available.
        comparable2 - The second comparable to be compared, or null 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 is null.
        java.lang.ClassCastException - if the arguments' types prevent them from being compared.
        See Also:
        Comparable.compareTo(Object)