org.springframework.util.comparator

## Class NullSafeComparator<T>

- java.lang.Object
- - org.springframework.util.comparator.NullSafeComparator<T>

- Type Parameters:
    `T` - the type of objects that may be compared by this comparator
  All Implemented Interfaces:
    Comparator<T>

      public class NullSafeComparator<T>
      extends Object
      implements Comparator<T>

  A Comparator that will safely compare nulls to be lower or higher than other objects. Can decorate a given Comparator or work on Comparables.
  Since:
    1.2.2
  Author:
    Keith Donald, Juergen Hoeller
  See Also:
    `Comparable`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static NullSafeComparator` | `NULLS_HIGH` A shared default instance of this comparator, treating nulls higher than non-null objects.
    `static NullSafeComparator` | `NULLS_LOW` A shared default instance of this comparator, treating nulls lower than non-null objects.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `NullSafeComparator(Comparator<T> comparator, boolean nullsLow)` Create a NullSafeComparator that sorts `null` based on the provided flag, decorating the given Comparator.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `compare(T o1, T o2)`
    `boolean` | `equals(Object other)`
    `int` | `hashCode()`
    `String` | `toString()`

    - ### Methods inherited from class java.lang.Object

      `clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface java.util.Comparator

      `comparing, comparing, comparingDouble, comparingInt, comparingLong, naturalOrder, nullsFirst, nullsLast, reversed, reverseOrder, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong`

- - ### Field Detail

    - #### NULLS_LOW

          public static final NullSafeComparator NULLS_LOW

      A shared default instance of this comparator, treating nulls lower than non-null objects.
      See Also:
        `Comparators.nullsLow()`

    - #### NULLS_HIGH

          public static final NullSafeComparator NULLS_HIGH

      A shared default instance of this comparator, treating nulls higher than non-null objects.
      See Also:
        `Comparators.nullsHigh()`

  - ### Constructor Detail

    - #### NullSafeComparator

          public NullSafeComparator(Comparator<T> comparator,
                                    boolean nullsLow)

      Create a NullSafeComparator that sorts `null` based on the provided flag, decorating the given Comparator.

      When comparing two non-null objects, the specified Comparator will be used. The given underlying Comparator must be able to handle the elements that this Comparator will be applied to.

      Parameters:
        `comparator` - the comparator to use when comparing two non-null objects
        `nullsLow` - whether to treat nulls lower or higher than non-null objects

  - ### Method Detail

    - #### compare

          public int compare(@Nullable
                             T o1,
                             @Nullable
                             T o2)

      Specified by:
        `compare` in interface `Comparator<T>`

    - #### equals

          public boolean equals(@Nullable
                                Object other)

      Specified by:
        `equals` in interface `Comparator<T>`
      Overrides:
        `equals` in class `Object`

    - #### hashCode

          public int hashCode()

      Overrides:
        `hashCode` in class `Object`

    - #### toString

          public String toString()

      Overrides:
        `toString` in class `Object`