Top Description Fields Constructors Methods
org.springframework.util.comparator

public Class NullSafeComparator<T>

extends Object
implements Comparator<T>
Class Inheritance
All Implemented Interfaces
java.util.Comparator
Annotations
@Deprecated
since:6.1
Type Parameters
<T>
the type of objects that may be compared by this comparator
Imports
java.util.Comparator, org.springframework.lang.Nullable, org.springframework.util.Assert

Deprecated

since 6.1.

as of 6.1 in favor of Comparator#nullsLast and Comparator#nullsFirst

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

Field Summary

Modifier and TypeField and Description
private final Comparator<T>
public static final NullSafeComparator<T>
NULLS_HIGH

A shared default instance of this comparator, treating nulls higher than non-null objects.

public static final NullSafeComparator<T>
NULLS_LOW

A shared default instance of this comparator, treating nulls lower than non-null objects.

private final boolean

Constructor Summary

AccessConstructor and Description
private
NullSafeComparator(boolean
whether to treat nulls lower or higher than non-null objects
nullsLow
)

Create a NullSafeComparator that sorts null based on the provided flag, working on Comparables.

public
NullSafeComparator(Comparator<T>
the comparator to use when comparing two non-null objects
comparator
,
boolean
whether to treat nulls lower or higher than non-null objects
nullsLow
)

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

Method Summary

Modifier and TypeMethod and Description
public int
compare(T
the first object to be compared.
left
,
T
the second object to be compared.
right
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Indicates whether some other object is "equal to" this comparator.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

nonNullComparatorback to summary
private final Comparator<T> nonNullComparator
NULLS_HIGHback to summary
public static final NullSafeComparator<T> NULLS_HIGH

Deprecated

as a consequence of NullSafeComparator being deprecated. See corresponding docs for further information.

A shared default instance of this comparator, treating nulls higher than non-null objects.

Annotations
@SuppressWarnings:rawtypes
See Also
Comparators#nullsHigh()
NULLS_LOWback to summary
public static final NullSafeComparator<T> NULLS_LOW

Deprecated

as a consequence of NullSafeComparator being deprecated. See corresponding docs for further information.

A shared default instance of this comparator, treating nulls lower than non-null objects.

Annotations
@SuppressWarnings:rawtypes
See Also
Comparators#nullsLow()
nullsLowback to summary
private final boolean nullsLow

Constructor Detail

NullSafeComparatorback to summary
private NullSafeComparator(boolean nullsLow)

Create a NullSafeComparator that sorts null based on the provided flag, working on Comparables.

When comparing two non-null objects, their Comparable implementation will be used: this means that non-null elements (that this Comparator will be applied to) need to implement Comparable.

As a convenience, you can use the default shared instances: NullSafeComparator.NULLS_LOW and NullSafeComparator.NULLS_HIGH.

Parameters
nullsLow:boolean

whether to treat nulls lower or higher than non-null objects

See Also
Comparable, NULLS_LOW, NULLS_HIGH
NullSafeComparatorback to summary
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:Comparator<T>

the comparator to use when comparing two non-null objects

nullsLow:boolean

whether to treat nulls lower or higher than non-null objects

Method Detail

compareback to summary
public int compare(T left, T right)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments 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.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
left:T

the first object to be compared.

right:T

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Doc from java.util.Comparator.equals.

Indicates whether some other object is "equal to" this comparator. This method must obey the general contract of Object#equals(Object). Additionally, this method can return true only if the specified object is also a comparator and it imposes the same ordering as this comparator. Thus, comp1.equals(comp2) implies that signum(comp1.compare(o1, o2))==signum(comp2.compare(o1, o2)) for every object reference o1 and o2.

Note that it is always safe not to override Object.equals(Object). However, overriding this method may, in some cases, improve performance by allowing programs to determine that two distinct comparators impose the same order.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true only if the specified object is also a comparator and it imposes the same ordering as this comparator.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override