Top Description Fields Constructors Methods
org.apache.commons.collections4.comparators

public Class NullComparator<E>

extends Object
implements Comparator<E>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, java.util.Comparator
Type Parameters
<E>
the type of objects compared by this comparator
Imports
java.io.Serializable, java.util.Comparator, org.apache.commons.collections4.ComparatorUtils

A Comparator that will compare nulls to be either lower or higher than other objects.
Since
2.0

Field Summary

Modifier and TypeField and Description
private final Comparator<? super E>
nonNullComparator

The comparator to use when comparing two non-null objects.

private final boolean
nullsAreHigh

Specifies whether a null are compared as higher than non-null objects.

private static final long
serialVersionUID

Serialization version.

Constructor Summary

AccessConstructor and Description
public
NullComparator()

Construct an instance that sorts null higher than any non-null object it is compared with.

public
NullComparator(final Comparator<? super E>
the comparator to use when comparing two non-null objects. This argument cannot be null
nonNullComparator
)

Construct an instance that sorts null higher than any non-null object it is compared with.

public
NullComparator(final boolean
a true value indicates that null should be compared as higher than a non-null object. A false value indicates that null should be compared as lower than a non-null object.
nullsAreHigh
)

Construct an instance that sorts null higher or lower than any non-null object it is compared with.

public
NullComparator(final Comparator<? super E>
the comparator to use when comparing two non-null objects. This argument cannot be null
nonNullComparator
,
final boolean
a true value indicates that null should be compared as higher than a non-null object. A false value indicates that null should be compared as lower than a non-null object.
nullsAreHigh
)

Construct an instance that sorts null higher or lower than any non-null object it is compared with.

Method Summary

Modifier and TypeMethod and Description
public int

Returns:

-1 if o1 is "lower" than (less than, before, etc.) o2; 1 if o1 is "higher" than (greater than, after, etc.) o2; or 0 if o1 and o2 are equal.
compare
(final E
the first object to compare
o1
,
final E
the object to compare it to.
o2
)

Implements java.util.Comparator.compare.

Perform a comparison between two objects.
public boolean

Returns:

true if the specified object is a NullComparator with equivalent null comparison behavior (i.e. null high or low) and with equivalent underlying non-null object comparators.
equals
(final Object
the object to compare this comparator with.
obj
)

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Determines whether the specified object represents a comparator that is equal to this comparator.
public int

Returns:

a hash code for this comparator.
hashCode
()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals(Object).
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

nonNullComparatorback to summary
private final Comparator<? super E> nonNullComparator

The comparator to use when comparing two non-null objects.

nullsAreHighback to summary
private final boolean nullsAreHigh

Specifies whether a null are compared as higher than non-null objects.

serialVersionUIDback to summary
private static final long serialVersionUID

Serialization version.

Constructor Detail

NullComparatorback to summary
public NullComparator()

Construct an instance that sorts null higher than any non-null object it is compared with. When comparing two non-null objects, the ComparableComparator is used.

NullComparatorback to summary
public NullComparator(final Comparator<? super E> nonNullComparator)

Construct an instance that sorts null higher than any non-null object it is compared with. When comparing two non-null objects, the specified Comparator is used.

Parameters
nonNullComparator:Comparator<? super E>

the comparator to use when comparing two non-null objects. This argument cannot be null

Exceptions
NullPointerException:
if nonNullComparator is null
NullComparatorback to summary
public NullComparator(final boolean nullsAreHigh)

Construct an instance that sorts null higher or lower than any non-null object it is compared with. When comparing two non-null objects, the ComparableComparator is used.

Parameters
nullsAreHigh:boolean

a true value indicates that null should be compared as higher than a non-null object. A false value indicates that null should be compared as lower than a non-null object.

NullComparatorback to summary
public NullComparator(final Comparator<? super E> nonNullComparator, final boolean nullsAreHigh)

Construct an instance that sorts null higher or lower than any non-null object it is compared with. When comparing two non-null objects, the specified Comparator is used.

Parameters
nonNullComparator:Comparator<? super E>

the comparator to use when comparing two non-null objects. This argument cannot be null

nullsAreHigh:boolean

a true value indicates that null should be compared as higher than a non-null object. A false value indicates that null should be compared as lower than a non-null object.

Exceptions
NullPointerException:
if nonNullComparator is null

Method Detail

compareback to summary
public int compare(final E o1, final E o2)

Implements java.util.Comparator.compare.

Perform a comparison between two objects. If both objects are null, a 0 value is returned. If one object is null and the other is not, the result is determined on whether the Comparator was constructed to have nulls as higher or lower than other objects. If neither object is null, an underlying comparator specified in the constructor (or the default) is used to compare the non-null objects.

Parameters
o1:E

the first object to compare

o2:E

the object to compare it to.

Returns:int

-1 if o1 is "lower" than (less than, before, etc.) o2; 1 if o1 is "higher" than (greater than, after, etc.) o2; or 0 if o1 and o2 are equal.

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

Overrides java.lang.Object.equals.

Implements java.util.Comparator.equals.

Determines whether the specified object represents a comparator that is equal to this comparator.

Parameters
obj:Object

the object to compare this comparator with.

Returns:boolean

true if the specified object is a NullComparator with equivalent null comparison behavior (i.e. null high or low) and with equivalent underlying non-null object comparators.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Implement a hash code for this comparator that is consistent with equals(Object).

Returns:int

a hash code for this comparator.

Annotations
@Override