Class BaseComparator<T>

java.lang.Object
overit.geocallapp.utilities.core.bl.BaseComparator<T>
Type Parameters:
T - the type of objects that may be compared by this comparator
All Implemented Interfaces:
Comparator<T>
Direct Known Subclasses:
SchedulingCancellingComparator

public abstract class BaseComparator<T> extends Object implements Comparator<T>
Abstract base class for null-safe comparators.
This class provides a foundation for implementing Comparator with built-in null-safety handling. It automatically manages null value comparisons according to a consistent strategy:
  • If both objects are null, they are considered equal (returns 0)
  • If only the first object is null, it is considered greater (returns 1)
  • If only the second object is null, it is considered lesser (returns -1)
  • If both objects are non-null, delegates to the safeCompare(Object, Object) method
This approach ensures that null values are consistently handled across all comparator implementations and eliminates the need for repetitive null checks in subclasses.
Since:
1.0
  • Constructor Details

    • BaseComparator

      public BaseComparator()
  • Method Details

    • compare

      public int compare(T o1, T o2)
      Compares two objects with automatic null-safety handling. This method implements the Comparator.compare(Object, Object) contract with built-in null value handling. The comparison logic is:
      • If both objects are null: returns 0 (equal)
      • If only o1 is null: returns 1 (o1 > o2)
      • If only o2 is null: returns -1 (o1 < o2)
      • If both are non-null: delegates to safeCompare(Object, Object)
      Specified by:
      compare in interface Comparator<T>
      Parameters:
      o1 - the first object to be compared
      o2 - 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
    • safeCompare

      protected abstract int safeCompare(T t1, T t2)
      Compares two non-null objects. This method is called by compare(Object, Object) when both objects are guaranteed to be non-null. Subclasses must implement this method to provide the specific comparison logic for their object type.
      Parameters:
      t1 - the first non-null object to be compared
      t2 - the second non-null 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