Class SerialDelegateComparator<T>

java.lang.Object
com.globalmentor.collections.comparators.AbstractSortOrderComparator<T>
com.globalmentor.collections.comparators.SerialDelegateComparator<T>
Type Parameters:
T - The type of objects that may be compared by this comparator.
All Implemented Interfaces:
SortOrderComparator<T>, Comparator<T>

public class SerialDelegateComparator<T> extends AbstractSortOrderComparator<T>
A comparator that can perform comparisons based upon a sequence of other comparators. The last comparator in the sequence should unambiguously distinguish non-equal objects.

If this comparator is used in descending order mode, delegate comparators that implement SortOrderComparator should be used in ascending mode, as this comparator will sort in reverse order automatically; otherwise, the descending orders will cancel each other out and each sub-comparison will be performed in ascending order.

Descending order for this comparator does not mean that the delegates will be traversed in reverse order.

Author:
Garret Wilson
  • Constructor Details

    • SerialDelegateComparator

      public SerialDelegateComparator(Comparator<? super T>... comparators)
      Comparators constructor for ascending order sorting. The last comparator in the sequence should unambiguously distinguish non-equal objects.
      Parameters:
      comparators - The delegate comparators.
      Throws:
      NullPointerException - if the given sort order and/or comparators is null.
      IllegalArgumentException - if no comparators are given.
    • SerialDelegateComparator

      public SerialDelegateComparator(SortOrder sortOrder, Comparator<? super T>... comparators)
      Sort order constructor and comparators constructor. The last comparator in the sequence should unambiguously distinguish non-equal objects.
      Parameters:
      sortOrder - The order in which to perform comparisons.
      comparators - The delegate comparators.
      Throws:
      NullPointerException - if the given sort order and/or comparators is null.
      IllegalArgumentException - if no comparators are given.
  • Method Details

    • compareImpl

      public int compareImpl(T object1, T object2)
      Implementation to compare two objects for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. Implementations do not need to perform special checks for identity, as the AbstractSortOrderComparator.compare(Object, Object) implementation will do this.
      Specified by:
      compareImpl in class AbstractSortOrderComparator<T>
      Parameters:
      object1 - The first object to be compared.
      object2 - The second object to be compared.
      Returns:
      A negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.